Solution for Big Integer used Linked - List.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 14
Reputation: vincent551987vn is an unknown quantity at this point 
Solved Threads: 1
vincent551987vn's Avatar
vincent551987vn vincent551987vn is offline Offline
Newbie Poster

Solution for Big Integer used Linked - List.

 
0
  #1
Oct 11th, 2007
main.cpp:

  1. #include <stdio.h>
  2.  
  3. #include "VeryLongInt.h"
  4.  
  5. int InitializeFromFile(char* fileName, VeryLongInt* &x, VeryLongInt* &y);
  6. //void ShowScreen();
  7.  
  8. int main(int argc, char* argv[]){
  9. char* inputFile;
  10. char* outputFile;
  11. if (argc < 2){
  12. printf("Usage: LongIntDataType.exe [InputFile] [OutputFile]\n");
  13. printf("Using default configuration: LongIntDataType.exe input.txt output.txt\n\n");
  14. inputFile = "input.txt";
  15. outputFile = "output.txt"; // "output/output.txt"
  16. } else {
  17. inputFile = argv[1];
  18. if (argc == 2)
  19. outputFile = "output.txt";
  20. else
  21. outputFile = argv[2];
  22. }
  23.  
  24. VeryLongInt *x, *y, *z;
  25. int succ = InitializeFromFile(inputFile, x, y);
  26. if (!succ) return 0;
  27.  
  28. z = VeryLongInt::Add(x, y);
  29. z->SaveToFile(outputFile, 0);
  30. delete z;
  31.  
  32. z = VeryLongInt::Subtract(x, y);
  33. z->SaveToFile(outputFile, 1);
  34. delete z;
  35.  
  36. z = VeryLongInt::Multiply(x, y);
  37. z->SaveToFile(outputFile, 1);
  38. delete z;
  39.  
  40. z = VeryLongInt::Div(x, y);
  41. if (z != NULL){
  42. z->SaveToFile(outputFile, 1);
  43. delete z;
  44. } // else FW: write NULL to file for checking students' result
  45.  
  46. z = VeryLongInt::Mod(x, y);
  47. if (z != NULL) {
  48. z->SaveToFile(outputFile, 1);
  49. delete z;
  50. } // else FW: write NULL to file for checking students' result
  51.  
  52. // Calculate x^10
  53. z = VeryLongInt::Power(x, 10);
  54. z->SaveToFile(outputFile, 1);
  55. delete z;
  56.  
  57. delete x;
  58. delete y;
  59.  
  60. // ShowScreen();
  61.  
  62. return 1;
  63. }
  64.  
  65. // Note: I have not optimized this function yet. Anyways, it works
  66. // It's easier to use fgets function but it's not efficient for memory.
  67. int InitializeFromFile(char* fileName, VeryLongInt* &x, VeryLongInt* &y){
  68. FILE *fptr;
  69. char* buffer;
  70. char c;
  71. fopen_s(&fptr, fileName, "r");
  72. if (fptr == NULL){
  73. printf("Can't open file [%s]", fileName);
  74. return 0;
  75. }
  76. // Read the first number
  77. int count = 0;
  78. int ok = 1;
  79. while (!feof(fptr) && ok){
  80. c = fgetc(fptr);
  81. if (c != '\n')
  82. count++;
  83. else ok = 0;
  84. }
  85. fseek(fptr,0, 0);
  86. buffer = new char[count+1];// +1 for NULL character
  87. buffer[count] = '\0';
  88. count = 0;
  89. ok = 1;
  90. while (!feof(fptr) && ok){
  91. c = fgetc(fptr);
  92. if (c != '\n'){
  93. buffer[count] = c;
  94. count++;
  95. } else ok = 0;
  96. }
  97. x = VeryLongInt::Parse(buffer);
  98. delete buffer;
  99.  
  100. // Read the second number
  101. int secondCount = 0;
  102. ok = 1;
  103. while (!feof(fptr) && ok){
  104. c = fgetc(fptr);
  105. if (c != '\n'){
  106. secondCount++;
  107. } else ok = 0;
  108. }
  109. buffer = new char[secondCount+1];// +1 for NULL character
  110. buffer[secondCount] = '\0';
  111.  
  112. fseek(fptr, count+2, 0);
  113. secondCount = 0;
  114. ok = 1;
  115. while (!feof(fptr) && ok){
  116. c = fgetc(fptr);
  117. if (c != '\n'){
  118. buffer[secondCount] = c;
  119. secondCount++;
  120. } else ok = 0;
  121. }
  122. y = VeryLongInt::Parse(buffer);
  123. delete buffer;
  124. fclose(fptr);
  125. return 1;
  126. }
  127.  
  128.  
  129. void ShowScreen(){
  130. VeryLongInt *x = new VeryLongInt(998);
  131. VeryLongInt *y = new VeryLongInt(9);
  132. VeryLongInt *z;
  133.  
  134. printf("Simulation of operators between 2 very long integers\n");
  135. printf("Sum:\t\t");
  136. z = VeryLongInt::Add(x, y);
  137. x->Print(); printf(" + "); y->Print();
  138. printf(" = "); z->Print(); printf("\n");
  139. delete z;
  140.  
  141. printf("Subtract:\t");
  142. z = VeryLongInt::Subtract(x, y);
  143. x->Print(); printf(" - "); y->Print();
  144. printf(" = "); z->Print(); printf("\n");
  145. delete z;
  146.  
  147. printf("Multiply:\t");
  148. x->Print(); printf(" * "); y->Print();
  149. z = VeryLongInt::Multiply(x, y);
  150. printf(" = "); z->Print(); printf("\n");
  151. delete z;
  152.  
  153. printf("DIV:\t\t");
  154. x->Print(); printf(" / "); y->Print();
  155. z = VeryLongInt::Div(x, y);
  156. printf(" = "); z->Print(); printf("\n");
  157. delete z;
  158.  
  159. printf("MOD:\t\t");
  160. z = VeryLongInt::Mod(x, y);
  161. x->Print(); printf(" %% "); y->Print();
  162. printf(" = "); z->Print(); printf("\n");
  163. delete z;
  164.  
  165. delete x;
  166. delete y;
  167.  
  168. x = VeryLongInt::Parse("-23234");
  169. y = VeryLongInt::Parse("256");
  170.  
  171. if (VeryLongInt::Compare(x, y) == -1){
  172. printf("First number is less than second number.\n");
  173. } else
  174. printf("First number isnot less than second number.\n");
  175.  
  176. z = VeryLongInt::Add(x, y);
  177. printf("Sum:\t\t");
  178. x->Print(); printf(" + "); y->Print(); printf(" = "); z->Print();
  179. printf("\n");
  180. delete z;
  181. printf("Subtract:\t");
  182. z = VeryLongInt::Subtract(x, y);
  183. x->Print(); printf(" - "); y->Print(); printf(" = "); z->Print();
  184. printf("\n");
  185. delete z;
  186.  
  187. x->Print(); printf("^10 = ");
  188. z = VeryLongInt::Power(x, 10);
  189. char *temp = z->ToString();
  190. printf("%s\n", temp);
  191. delete temp;
  192.  
  193. delete z;
  194.  
  195. delete x;
  196. delete y;
  197. }

and the VeryLongInt.h

  1. class VeryLongInt{
  2. public:
  3. VeryLongInt();
  4. VeryLongInt(int val); // convert an integer into VeryLongInt object
  5. ~VeryLongInt();
  6.  
  7. VeryLongInt* Clone(); // return another copy of current object (replicate the current object)
  8. int GetSign(); // return sign of current number
  9. void ToggleSign(); // toggle sign of current number
  10. char* ToString(); // convert current object into string
  11. void Print(); // output current number to console by using printf() function
  12. int SaveToFile(char* fileName, int appendFlag);// output to file. appendFlag: +1: append, 0: overwrite
  13.  
  14. static VeryLongInt* Add(VeryLongInt *x, VeryLongInt *y); // x + y
  15. static VeryLongInt* Subtract(VeryLongInt *x, VeryLongInt *y); // x - y
  16. static VeryLongInt* Multiply(VeryLongInt *x, VeryLongInt *y); // x * y
  17. static VeryLongInt* Div(VeryLongInt *x, VeryLongInt *y); // x div y
  18. static VeryLongInt* Mod(VeryLongInt *x, VeryLongInt *y); // x % y
  19. static VeryLongInt* Power(VeryLongInt *x, int n); // x^n
  20. static int EqualTo(VeryLongInt *x, VeryLongInt *y); // x == y?
  21. static int Compare(VeryLongInt *x, VeryLongInt *y); // return +1 if x > y; 0 if x = y; -1 if x < y
  22. static VeryLongInt* Parse(char *str); // convert a string into a very long integer
  23. // Add your neccessary methods here ...
  24. };


Many things i dont know up to now, but i seem so usual.
Say It Like It Is.
Có sao nói vậy.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
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 iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Solution for Big Integer used Linked - List.

 
0
  #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 Quick reply to this message  
Join Date: Sep 2004
Posts: 7,691
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: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Solution for Big Integer used Linked - List.

 
0
  #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 5:16 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
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 iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Solution for Big Integer used Linked - List.

 
0
  #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 5:30 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
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.

 
0
  #5
Oct 12th, 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 12th, 2007 at 12:01 am.
Say It Like It Is.
Có sao nói vậy.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC