RSS Forums RSS

Solution for Big Integer used Linked - List.

Please support our C++ advertiser: Programming Forums
Reply
Posts: 14
Reputation: vincent551987vn is an unknown quantity at this point 
Solved Threads: 1
vincent551987vn's Avatar
vincent551987vn vincent551987vn is offline Offline
Newbie Poster

News Solution for Big Integer used Linked - List.

  #1  
Oct 11th, 2007
main.cpp:

#include <stdio.h>

#include "VeryLongInt.h"

int InitializeFromFile(char* fileName, VeryLongInt* &x, VeryLongInt* &y);
//void ShowScreen();

int main(int argc, char* argv[]){
	char* inputFile;
	char* outputFile;
	if (argc < 2){
		printf("Usage: LongIntDataType.exe [InputFile] [OutputFile]\n");
		printf("Using default configuration: LongIntDataType.exe input.txt output.txt\n\n");
		inputFile = "input.txt";
		outputFile = "output.txt"; // "output/output.txt"
	} else {
		inputFile = argv[1];
		if (argc == 2)
			outputFile = "output.txt";
		else
			outputFile = argv[2];
	}

	VeryLongInt *x, *y, *z;
	int succ = InitializeFromFile(inputFile, x, y);
	if (!succ) return 0;

	z = VeryLongInt::Add(x, y);
	z->SaveToFile(outputFile, 0);
	delete z;

	z = VeryLongInt::Subtract(x, y);
	z->SaveToFile(outputFile, 1);
	delete z;

	z = VeryLongInt::Multiply(x, y);
	z->SaveToFile(outputFile, 1);
	delete z;

	z = VeryLongInt::Div(x, y);
	if (z != NULL){
		z->SaveToFile(outputFile, 1);
		delete z;
	} // else FW: write NULL to file for checking students' result

	z = VeryLongInt::Mod(x, y);
	if (z != NULL) {
		z->SaveToFile(outputFile, 1);
		delete z;
	} // else FW: write NULL to file for checking students' result

	// Calculate x^10
	z = VeryLongInt::Power(x, 10);
	z->SaveToFile(outputFile, 1);
	delete z;

	delete x;
	delete y;

//	ShowScreen();

	return 1;
}

// Note: I have not optimized this function yet. Anyways, it works
// It's easier to use fgets function but it's not efficient for memory.
int InitializeFromFile(char* fileName, VeryLongInt* &x, VeryLongInt* &y){
	FILE *fptr;
	char* buffer;
	char c;
	fopen_s(&fptr, fileName, "r");
	if (fptr == NULL){
		printf("Can't open file [%s]", fileName);
		return 0;
	}
	// Read the first number
	int count = 0;
	int ok = 1;
	while (!feof(fptr) && ok){
		c = fgetc(fptr);
		if (c != '\n')
			count++;
		else ok = 0;
	}
	fseek(fptr,0, 0);
	buffer = new char[count+1];// +1 for NULL character
	buffer[count] = '\0';
	count = 0;
	ok = 1;
	while (!feof(fptr) && ok){
		c = fgetc(fptr);
		if (c != '\n'){
			buffer[count] = c;
			count++;
		} else ok = 0;
	}
	x = VeryLongInt::Parse(buffer);
	delete buffer;

	// Read the second number
	int secondCount = 0;
	ok = 1;
	while (!feof(fptr) && ok){
		c = fgetc(fptr);
		if (c != '\n'){
			secondCount++;
		} else ok = 0;
	}
	buffer = new char[secondCount+1];// +1 for NULL character
	buffer[secondCount] = '\0';

	fseek(fptr, count+2, 0);
	secondCount = 0;
	ok = 1;
	while (!feof(fptr) && ok){
		c = fgetc(fptr);
		if (c != '\n'){
			buffer[secondCount] = c;
			secondCount++;
		} else ok = 0;
	}
	y = VeryLongInt::Parse(buffer);
	delete buffer;
	fclose(fptr);
	return 1;
}


void ShowScreen(){
	VeryLongInt *x = new VeryLongInt(998);
	VeryLongInt *y = new VeryLongInt(9);
	VeryLongInt *z;

	printf("Simulation of operators between 2 very long integers\n");
	printf("Sum:\t\t");
	z = VeryLongInt::Add(x, y);
	x->Print(); printf(" + "); y->Print();
	printf(" = "); z->Print(); printf("\n");
	delete z;

	printf("Subtract:\t");
	z = VeryLongInt::Subtract(x, y);
	x->Print(); printf(" - "); y->Print();
	printf(" = "); z->Print(); printf("\n");
	delete z;

	printf("Multiply:\t");
	x->Print(); printf(" * "); y->Print();
	z = VeryLongInt::Multiply(x, y);
	printf(" = "); z->Print(); printf("\n");
	delete z;

	printf("DIV:\t\t");
	x->Print(); printf(" / "); y->Print();
	z = VeryLongInt::Div(x, y);
	printf(" = "); z->Print(); printf("\n");
	delete z;

	printf("MOD:\t\t");
	z = VeryLongInt::Mod(x, y);
	x->Print(); printf(" %% "); y->Print();
	printf(" = "); z->Print(); printf("\n");
	delete z;

	delete x;
	delete y;

	x = VeryLongInt::Parse("-23234");
	y = VeryLongInt::Parse("256");

	if (VeryLongInt::Compare(x, y) == -1){
		printf("First number is less than second number.\n");
	} else 
		printf("First number isnot less than second number.\n");

	z = VeryLongInt::Add(x, y);
	printf("Sum:\t\t");
	x->Print(); printf(" + "); y->Print(); printf(" = "); z->Print();
	printf("\n");
	delete z;
	printf("Subtract:\t");
	z = VeryLongInt::Subtract(x, y);
	x->Print(); printf(" - "); y->Print(); printf(" = "); z->Print();
	printf("\n");
	delete z;

	x->Print(); printf("^10 = ");
	z = VeryLongInt::Power(x, 10);
	char *temp = z->ToString();
	printf("%s\n", temp);
	delete temp;

	delete z;

	delete x;
	delete y;
}

and the VeryLongInt.h

class VeryLongInt{
public:
	VeryLongInt();
	VeryLongInt(int val); // convert an integer into VeryLongInt object
	~VeryLongInt(); 

	VeryLongInt* Clone(); // return another copy of current object (replicate the current object)
	int GetSign(); // return sign of current number
	void ToggleSign(); // toggle sign of current number
	char* ToString(); // convert current object into string
	void Print(); // output current number to console by using printf() function
	int SaveToFile(char* fileName, int appendFlag);// output to file. appendFlag: +1: append, 0: overwrite

	static VeryLongInt* Add(VeryLongInt *x, VeryLongInt *y); // x + y
	static VeryLongInt* Subtract(VeryLongInt *x, VeryLongInt *y); // x - y
	static VeryLongInt* Multiply(VeryLongInt *x, VeryLongInt *y); // x * y
	static VeryLongInt* Div(VeryLongInt *x, VeryLongInt *y); // x div y
	static VeryLongInt* Mod(VeryLongInt *x, VeryLongInt *y); // x % y
	static VeryLongInt* Power(VeryLongInt *x, int n); // x^n
	static int EqualTo(VeryLongInt *x, VeryLongInt *y); // x == y?
	static int Compare(VeryLongInt *x, VeryLongInt *y); // return +1 if x > y; 0 if x = y; -1 if x < y
	static VeryLongInt* Parse(char *str); // convert a string into a very long integer
	// Add your neccessary methods here ...
};


Many things i dont know up to now, but i seem so usual.
Say It Like It Is.
Có sao nói vậy.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 5,068
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 355
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Solution for Big Integer used Linked - List.

  #2  
Oct 11th, 2007
So is that actually your program or did you copy it of someone.
*Voted best profile in the world*
Reply With Quote  
Posts: 7,460
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 676
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Solution for Big Integer used Linked - List.

  #3  
Oct 11th, 2007
>So is that actually your program or did you copy it of someone.
I'd wager that since he went from nothing (knowing nothing) to that in two hours, he copied it from somewhere.
Last edited by Narue : Oct 11th, 2007 at 4:16 pm.
I'm here to prove you wrong.
Reply With Quote  
Posts: 5,068
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 355
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Solution for Big Integer used Linked - List.

  #4  
Oct 11th, 2007
That's what I though from looking at his previous threads. He he. So I guess that means we aren't going to point out all of his mistakes. Ha ha.
Last edited by iamthwee : Oct 11th, 2007 at 4:30 pm.
*Voted best profile in the world*
Reply With Quote  
Posts: 14
Reputation: vincent551987vn is an unknown quantity at this point 
Solved Threads: 1
vincent551987vn's Avatar
vincent551987vn vincent551987vn is offline Offline
Newbie Poster

Re: Solution for Big Integer used Linked - List.

  #5  
Oct 11th, 2007
He he. Maybe, but nowhere i can copy that, and this my doing's Big Brother ^^. I only want to take ideas from around ^^.

===========
To open ur eyes to see more and more intensively.
Last edited by vincent551987vn : Oct 11th, 2007 at 11:01 pm.
Say It Like It Is.
Có sao nói vậy.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 2702 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:25 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC