Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct integer{
  5. int digit;
  6. struct integer *next;
  7. };
  8.  
  9. struct integer* convert_integer(char* stringInt);
  10. void print(struct integer *p);
  11. struct integer* add(struct integer *p, struct integer *q);
  12. struct integer* subtract(struct integer *p, struct integer *q);
  13. int compare(struct integer *p, struct integer *q);
  14. struct integer* removeZeros(struct integer* p);
  15. void delete(struct integer* p);
  16.  
  17. int main()
  18. {
  19. FILE *ifp;
  20. ifp=fopen("bigint.txt","r");
  21. if(ifp==NULL)
  22. return 0;
  23. int numLines;
  24. fscanf(ifp,"%d",&numLines);//the first number in the file is the number of equations we have to solve
  25. while(numLines>0)
  26. {
  27. int addOrSubtract;
  28. fscanf(ifp,"%d",&addOrSubtract);//1 if add and 2 if subtract
  29. char num1[201];
  30. fscanf(ifp,"%s",num1);//read in first number
  31. char num2[201];
  32. fscanf(ifp,"%s",num2);//read in seconnd number
  33. struct integer* list1=convert_integer(num1);//convert to linked list
  34. struct integer* list2=convert_integer(num2);//convert to linked list
  35. struct integer* end;//what will be the end result of the equation
  36. if(addOrSubtract==1)
  37. {
  38. print(list1);
  39. printf(" + ");
  40. print(list2);
  41. printf(" = ");
  42. end=add(list1,list2);
  43. print(end);
  44. }
  45. else
  46. {
  47. if(compare(list1,list2)>0)//compare will return 1 if list 1 is greater, 0 if they are =, and -1 if list 2 is greater
  48. {
  49. print(list1);
  50. printf(" - ");
  51. print(list2);
  52. printf(" = ");
  53. end=subtract(list1,list2);
  54. print(end);
  55. }
  56. else if(compare(list1,list2)==0)//if they are =
  57. {
  58. print(list1);
  59. printf(" - ");
  60. print(list2);
  61. printf(" = ");
  62. printf("0");
  63. }
  64. else//if 2 is greater
  65. {
  66. print(list2);
  67. printf(" - ");
  68. print(list1);
  69. printf(" = ");
  70. end=subtract(list2,list1);
  71. print(end);
  72. }
  73. }
  74. printf("\n");
  75. numLines--;
  76. delete(list1);
  77. delete(list2);
  78. delete(end);
  79. }
  80. fclose(ifp);
  81. system("pause");
  82. return 0;
  83. }
  84.  
  85. struct integer* convert_integer(char* stringInt)
  86. {
  87. struct integer* reverse=NULL;
  88. int i;
  89. for(i=0;i<strlen(stringInt);i++)
  90. {
  91. struct integer* temp=NULL;//need temp to keep track of place
  92. temp=(struct integer*)(malloc(sizeof(struct integer)));//allocate memory
  93. temp->digit=stringInt[i]-'0';
  94. temp->next=reverse;//add to front
  95. reverse=temp;//make reverse = temp so when it adds to front again it will actually add to front
  96. }
  97. return reverse;
  98. }
  99.  
  100. void print(struct integer *p)
  101. {
  102. if(p!=NULL)
  103. {
  104. print(p->next);
  105. printf("%d",p->digit);
  106. }
  107. }
  108.  
  109. struct integer* add(struct integer *p, struct integer *q)
  110. {
  111. struct integer* end=(struct integer*)(malloc(sizeof(struct integer)));
  112. struct integer* front = end;
  113. struct integer* temp=NULL;//need temp to keep track of place
  114. int carry=0;
  115. while(p!=NULL&&q!=NULL)
  116. {
  117. int sum=p->digit+q->digit;
  118. //printf("sum %d",sum);
  119. if(carry==1)sum+=1; //if you needed to carry over then add one to the sum
  120. carry=0;
  121. if(sum>9)
  122. {
  123. sum-=10;
  124. carry=1;
  125. }
  126. temp=(struct integer*)(malloc(sizeof(struct integer)));//allocate memory
  127. temp->digit=sum;
  128. end->next=temp;
  129. end=end->next;
  130. p=p->next;
  131. q=q->next;
  132. }
  133. while(p!=NULL&&q==NULL)//make test case for this
  134. {
  135. if(carry==0)
  136. end->next=p;
  137. if(carry==1&&p->next!=NULL)//if it needs to be carried over
  138. {
  139. int sum=p->digit;
  140. sum+=1;//if you needed to carry over then add one to the sum
  141. carry=0;
  142. if(sum>10)
  143. {
  144. sum-=10;
  145. carry=1;
  146. }
  147. end->digit=sum;
  148. end=end->next;
  149. }
  150. if(carry==1)
  151. {
  152. end->next->digit=1;
  153. }
  154. p=p->next;
  155. }
  156. while(q!=NULL&&p==NULL)//make test case for this
  157. {
  158. if(carry==0)
  159. end->next=p;
  160. if(carry==1&&q->next!=NULL)
  161. {
  162. int sum=q->digit;
  163. sum+=1;//if you needed to carry over then add one to the sum
  164. carry=0;
  165. if(sum>10)
  166. {
  167. sum-=10;
  168. carry=1;
  169. }
  170. end->digit=sum;
  171. end=end->next;
  172. }
  173. if(carry==1)
  174. {
  175. end->next->digit=1;
  176. }
  177. q=q->next;
  178. }
  179. return front;
  180. }
  181. struct integer* subtract(struct integer *p, struct integer *q)
  182. {
  183. struct integer* end=(struct integer*)(malloc(sizeof(struct integer)));
  184. struct integer* front = end;
  185. struct integer* temp=NULL;//need temp to keep track of place
  186. end->next=NULL;
  187. int carry=0;
  188. while(p!=NULL&&q!=NULL)
  189. {
  190. int difference;
  191. difference=p->digit-q->digit;
  192. if(carry==1)difference-=1;//if you needed to carry over then add one to the sum
  193. carry=0;
  194. if(difference<0)
  195. {
  196. difference+=10;
  197. carry=1;
  198. }
  199. end->digit=difference;
  200. if(p->next!=NULL)
  201. {
  202. temp = (struct integer*)malloc(sizeof(struct integer));
  203. temp->next=NULL;
  204. end->next=temp;
  205. end=end->next;
  206. }
  207. p=p->next;
  208. q=q->next;
  209. }
  210. while(p!=NULL)
  211. {
  212. int difference;
  213. difference=p->digit;
  214. if(carry==1)difference-=1;//if you needed to carry over then add one to the sum
  215. carry=0;
  216. if(difference<0)
  217. {
  218. difference+=10;
  219. carry=1;
  220. }
  221. end->digit=difference;
  222. if(p->next!=NULL)
  223. {
  224. temp = (struct integer*)malloc(sizeof(struct integer));
  225. temp->next=NULL;
  226. end->next=temp;
  227. end=end->next;
  228. }
  229. p=p->next;
  230. }
  231. front=removeZeros(front);
  232. return front;
  233. }
  234.  
  235. int compare(struct integer *p, struct integer *q)
  236. {
  237. if(p==NULL&&q==NULL)
  238. return 0;
  239. if(p==NULL)
  240. return -1;
  241. if(q==NULL)
  242. return 1;
  243. int check = compare(p->next,q->next);
  244. if(check!=0)
  245. return check;
  246. if(p->digit>q->digit)
  247. return 1;
  248. if(p->digit<q->digit)
  249. return -1;
  250. if(p->digit==q->digit)
  251. return 0;
  252. }
  253.  
  254. struct integer* removeZeros(struct integer* p)
  255. {
  256. struct integer* firstZero=p;
  257. struct integer* temp=p;
  258. while(temp!=NULL)
  259. {
  260. if(temp->digit!=0)
  261. firstZero=temp;
  262. temp=temp->next;
  263. }
  264. delete(firstZero->next);
  265. firstZero->next=NULL;
  266. return p;
  267. }
  268.  
  269. void delete(struct integer* p)
  270. {
  271. if(p!=NULL)
  272. {
  273. delete(p->next);
  274. free(p);
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement