Hi, I'm new in c programming and I'm doing this problem where I basically have to program a calculator in c , the problem is it has to be able to handle large numbers in addition, subtraction, multiplication and division
I already know I can't use int or long variables so I have to store numbers in arrays and then I have to deal with digits in arrays (adding individual digits of an array) and I would have to deal with carries as well.
I wrote a code for a calculator but it doesn't handle large numbers so I didn't know should I actually post it here as it is unnecessary I think. (I can delete it if it's against the rules)
I know the code for the large number calculator has to be completely different but if you could just give me a couple of hints on arrays and how to add digits from two arrays, how to handle carries and stuff like that, it would be greatly appreciated.
And this is like only for addition so I'm afraid I still have a lot of work and questions to go....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
char line[80];
while (fgets(line, 80, stdin)) {
int n;
char* t = strtok(line, " \n");
char operator = t[0];
t = strtok(0, " \n");
n = atoi(t);
t = strtok(0, " \n");
while (t) {
int z = atoi(t);
switch (operator) {
case '+': n += z; break;
case '*': n *= z; break;
case '-': n -= z; break;
case '/': n /= z; break;
}
t = strtok(0, " \n");
}
fprintf(stdout, "%d\n", n);
}
return 0;
}