943,958 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4182
  • C++ RSS
Oct 11th, 2007
0

Solution for Big Integer used Linked - List.

Expand Post »
main.cpp:

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vincent551987vn is offline Offline
14 posts
since Oct 2007
Oct 11th, 2007
0

Re: Solution for Big Integer used Linked - List.

So is that actually your program or did you copy it of someone.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 11th, 2007
0

Re: Solution for Big Integer used Linked - List.

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 11th, 2007
0

Re: Solution for Big Integer used Linked - List.

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 12th, 2007
0

Re: Solution for Big Integer used Linked - List.

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vincent551987vn is offline Offline
14 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Compilation process
Next Thread in C++ Forum Timeline: c++ functions to compare numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC