• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
8%
SHARE
TWEET

# Untitled

a guest Jul 17th, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.
2. #include <stdio.h>
3.
4. struct integer {
5.        int* digits;
6.        int size;
7. };
8.
9. //Preconditions: the first parameter is string that stores
10. // only contains digits, doesn't start with
11. // 0, and is 200 or fewer characters long.
12. //Postconditions: The function will read the digits of the
13. // large integer character by character,
14. // convert them into integers and return a
15. // pointer to the appropriate struct integer.
16. struct integer* convert_integer(char* stringInt);
17.
18. //Preconditions: p is a pointer to a big integer.
19. //Postconditions: The big integer pointed to by p is
20. // printed out.
21. void print(struct integer *p);
22.
23. //Preconditions: p and q are pointers to struct integers.
24. //Postconditions: A new struct integer is created that
25. // stores the sum of the integers pointed to
26. // by p and q and a pointer to it is
27. // returned.
28. struct integer* add(struct integer *p, struct integer *q);
29.
30. //Preconditions: p and q are pointers to struct integers.
31. //Postconditions: A new struct integer is created that
32. // stores the absolute value of the
33. // difference between the two and a pointer
34. // to this is returned.
35. struct integer* subtract(struct integer *p, struct integer *q);
36.
37. //Preconditions: Both parameters of the function are
38. // pointers to struct integer.
39. //Postconditions: The function compares the digits of two
40. // numbers and returns:
41. // -1 if the first number is smaller than the second,
42. // 0 if the first number is equal to the second number,
43. // 1 if the first number is greater than the second.
44. int compare(struct integer *p, struct integer *q);
45.
46. FILE*out;
47.
48. main() {
49.
50.        int numOperations = 0;
51.        int addOrSub = 0;
52.        char firstNum[201];
53.        char secondNum[201];
54.        int counter;
55.        int comparison;
56.        struct integer* operandOne;
57.        struct integer* operandTwo;
58.        struct integer* equationTotal;
59.
60.        FILE*bigint;
61.        out = fopen ("out.txt", "w");//write
62.
63.
64.        bigint = fopen ("bigint.txt", "r");//read
65.        if (bigint == NULL) { printf("Error opening file"); }
66.
67.
68.
69.        //read in the number of operations
70.        fscanf(bigint, "%d", &numOperations);
71.
72.        for (counter=0; counter<numOperations; counter++){
73.
74.          //read in first integer, then the rest of the numbers as chars
75.          fscanf(bigint, "%d %s %s", &addOrSub, firstNum, secondNum);
76.
77.
78.          //debug
79.          //printf("%s, %s\n", firstNum, secondNum);
80.
81.          //debug
82.          //printf("%d, %s, %s\n", addOrSub, firstNum, operandTwo);
83.
84.             operandOne = convert_integer(firstNum);
85.             operandTwo = convert_integer(secondNum);
86.
87.             //debug, comparison works
88.             //int comparison = compare(operandOne, operandTwo);
89.             //printf("%d \n", comparison);
90.
91.
92.             //debug
93.             //print(operandOne);
94.             //print(operandTwo);
95.
96.
97.
98.          if(addOrSub == 1){
99.
100.                      print(operandOne);
101.                      fprintf(out, " + ");
102.                      print(operandTwo);
103.                      fprintf(out, " = ");
104.                      equationTotal = add(operandOne, operandTwo);
105.                      print(equationTotal);
106.                      fprintf(out, "\n");
107.
108.
109.
110.                      //debug
111.                      //print(equationTotal);
112.
113.
114.                      //printf("%s + %s = %d \n", firstNum, secondNum, equationTotal);
115.          }
116.
117.
118.          if(addOrSub == 2){
119.
120.                      print(operandOne);
121.                      fprintf(out, " - ");
122.                      print(operandTwo);
123.                      fprintf(out, " = ");
124.                      equationTotal = subtract(operandOne, operandTwo);
125.                      print(equationTotal);
126.                      fprintf(out, "\n");
127.
128.                      //debug
129.                      //print(equationTotal);
130.
131.
132.                      //printf("%s + %s = %d", firstNum, secondNum, equationTotal);
133.
134.          }
135.
136.
137.
138.
139.        }
140.
141.
142.        system("PAUSE");
143.        free(operandOne->digits);
144.        free(operandTwo->digits);
145.        free(equationTotal->digits);
146.        free(operandOne);
147.        free(operandTwo);
148.        free(equationTotal);
149.        fclose(out);
150.        fclose(bigint);
151.        return 0;
152. }
153.
154.
155. //Preconditions: the first parameter is string that stores
156. // only contains digits, doesn't start with
157. // 0, and is 200 or fewer characters long.
158. //Postconditions: The function will read the digits of the
159. // large integer character by character,
160. // convert them into integers and return a
161. // pointer to the appropriate struct integer.
162. struct integer* convert_integer(char* stringInt){
163.        int counter = 0;
164.        int counter2 = 0;
165.        int stringLength = 0;
166.        struct integer *currentNumber;
167.
168.        //THIS METHOD WORKS, DO NOT USE STRLEN
169.        while (stringInt[counter] != '\0'){
170.        stringLength++;
171.        counter++;
172.        }
173.
174.        //debug
175.        //************
176.        //printf("%d \n", stringLength);
177.
178.        //THIS DOES NOT WORK AND I DO NOT KNOW WHY, ONLY RETURNS GARBAGE.
179.        //stringLength = strlen(stringInt);
180.
181.        //debug
182.        //printf("%d", stringLength);
183.
184.        currentNumber = malloc(sizeof(struct integer));
185.        currentNumber->digits = malloc(sizeof(int)*stringLength + 1);
186.        currentNumber->size = stringLength;
187.
188.        for (counter = stringLength - 1; counter >= 0; counter--){
189.
190.
191.            currentNumber->digits[counter2] = stringInt[counter] - '0';
192.
193.            counter2++;
194.
195.        }
196.
197.        return currentNumber;
198.
199. }
200.
201. //Preconditions: p is a pointer to a big integer.
202. //Postconditions: The big integer pointed to by p is
203. // printed out.
204. void print(struct integer *p){
205.
206.      int counter;
207.
208.
209.      //backwards to make it readable
210.      for(counter = p->size - 1; counter >= 0; counter--){
211.
212.       fprintf(out, "%d", p->digits[counter]);
213.
214.      }
215.
216.
217.      return;
218.
219. }
220.
221. //Preconditions: p and q are pointers to struct integers.
222. //Postconditions: A new struct integer is created that
223. // stores the sum of the integers pointed to
224. // by p and q and a pointer to it is
225. // returned.
226. struct integer* add(struct integer *p, struct integer *q){
227.
228.        struct integer* sum;
229.        int carry = 0;
230.        int counter;
231.
232.        sum = malloc(sizeof(struct integer));
233.
234.        //old
235.        //sum->digits = malloc(sizeof(int)*p->size + q->size);
236.
237.
238.
239.
240.        if(q->size > p->size){
241.
242.                   sum->digits = malloc(sizeof(int)*p->size);
243.                   sum->size = q->size+1;
244.
245.                   for(counter=0; counter < sum->size; counter++){
246.                                  sum->digits[counter] = 0;
247.                   }
248.
249.                   for(counter=0; counter < q->size; counter++){
250.
251.
252.                   //debug
253.                   //sum->digits[counter] = (q->digits[counter] + p->digits[counter]);
254.
255.
256.
257.                     if(p->digits[counter] + q->digits[counter] >= 10){
258.                                carry = 1;
259.                                sum->digits[counter] = (p->digits[counter] + q->digits[counter] - 10);
260.                     }
261.
262.                     else if(p->digits[counter] + q->digits[counter] < 10){
263.                                sum->digits[counter] = (p->digits[counter] + q->digits[counter]);
264.                     }
265.
266.                     //debug, not neccesary, program adds correctly.
267.                     //printf("%d + %d = %d \n", p->digits[counter], q->digits[counter], sum->digits[counter]);
268.                     //printf("carry = %d \n", carry);
269.
270.                     if(carry == 1){
271.                              sum->digits[counter+1] += 1;
272.                     }
273.
274.
275.                   }
276.
277.
278.
279.
280.                    for(counter = 0; counter < sum->size; counter++){
281.
282.                               if(sum->digits[counter] == 0){
283.                                sum->size -= 1;
284.                               }
285.
286.
287.
288.
289.                   }
290.
291.                   //print(sum);
292.        }
293.
294.        if(p->size >= q->size){
295.
296.                   sum->digits = malloc(sizeof(int)*p->size);
297.                   sum->size = p->size+1;
298.
299.                   for(counter=0; counter < sum->size; counter++){
300.                                  sum->digits[counter] = 0;
301.                   }
302.
303.                   for(counter=0; counter < p->size; counter++){
304.
305.
306.                   //debug
307.                   //sum->digits[counter] = (p->digits[counter] + q->digits[counter]);
308.
309.
310.
311.                     if(p->digits[counter] + q->digits[counter] >= 10){
312.                                carry = 1;
313.                                sum->digits[counter] = (p->digits[counter] + q->digits[counter] - 10);
314.                     }
315.
316.                     else if(p->digits[counter] + q->digits[counter] < 10){
317.                                sum->digits[counter] = (p->digits[counter] + q->digits[counter]);
318.                     }
319.
320.                     //debug, not neccesary, program adds correctly.
321.                     //printf("%d + %d = %d \n", p->digits[counter], q->digits[counter], sum->digits[counter]);
322.                     //printf("carry = %d \n", carry);
323.
324.                     if(carry == 1){
325.                              sum->digits[counter+1] += 1;
326.                     }
327.
328.
329.                   }
330.
331.
332.
333.
334.                   for(counter = 0; counter < sum->size; counter++){
335.
336.                               if(sum->digits[counter] == 0){
337.                                sum->size -= 1;
338.                               }
339.
340.
341.
342.
343.                   }
344.
345.                   //print(sum);
346.
347.
348.        }
349.
350.
351.
352.
353.        return sum;
354. }
355.
356. //Preconditions: p and q are pointers to struct integers.
357. //Postconditions: A new struct integer is created that
358. // stores the absolute value of the
359. // difference between the two and a pointer
360. // to this is returned.
361. struct integer* subtract(struct integer *p, struct integer *q){
362.
363.        struct integer* difference;
364.        struct integer* numberSwap;
365.        int comparison;
366.        int counter;
367.
368.        difference = malloc(sizeof(struct integer));
369.        numberSwap = malloc(sizeof(struct integer));
370.
371.        comparison = compare(p, q);
372.
373.        if(comparison == -1 || 0){
374.                      numberSwap = p;
375.                      p = q;
376.                      q = numberSwap;
377.                      comparison = 1;
378.        }
379.
380.        if(comparison == 1){
381.                      difference->digits = malloc(sizeof(int)*p->size);
382.                      difference->size = p->size+1;
383.
384.                      for(counter=0; counter < difference->size; counter++){
385.                                  difference->digits[counter] = 0;
386.                      }
387.
388.
389.                      for(counter=0; counter < p->size; counter++){
390.                                  difference->digits[counter] = p->digits[counter] - q->digits[counter];
391.                      }
392.
393.                      for(counter = 0; counter < difference->size; counter++){
394.
395.                               if(difference->digits[counter] == 0){
396.                                difference->size -= 1;
397.                               }
398.
399.
400.
401.
402.                      }
403.
404.        }
405.
406.        //print(difference);
407.
408.
409.        return difference;
410. }
411.
412. //Preconditions: Both parameters of the function are
413. // pointers to struct integer.
414. //Postconditions: The function compares the digits of two
415. // numbers and returns:
416. // -1 if the first number is smaller than the second,
417. // 0 if the first number is equal to the second number,
418. // 1 if the first number is greater than the second.
419. int compare(struct integer *p, struct integer *q){
420.
421.      int counter;
422.
423.      if(p->size > q->size){
424.                 return 1;
425.      }
426.
427.      if(q->size > p->size){
428.                 return -1;
429.      }
430.
431.
432.      if(p->size == q->size){
433.       for(counter = p->size - 1; counter >= 0; counter--){
434.                   if(p->digits[counter] > q->digits[counter]){
435.                                         return 1;
436.                   }
437.                   if(q->digits[counter] > p->digits[counter]){
438.                                         return -1;
439.                   }
440.                   if(counter==0 && q->digits[counter] == p->digits[counter]){
441.                                         return 0;
442.                   }
443.
444.       }
445.      }
446. }
RAW Paste Data
Top