I wrote this "printing" calculator as I am doing C nowdays. Here's the source code:
#include <stdio.h>
int main(){
int num, num1, result;
char ch;
printf("Begin Calculations\n");
printf("------ --------------\n");
scanf("%d %c", &num, &ch);
if(ch != 'S' && ch != 's'){
printf("Set Accumulator first!\n");
}
else {
printf("Accumulator set to %d.\n", num);
while(ch != 'E' && ch != 'e'){
scanf("%d %c", &num1, &ch);
switch(ch){
case '+':
printf("Add %d.\n", num1);
result = num + num1;
printf("=%d\tContents of Acumulator\n", result);
break;
case '-':
result = num - num1;
printf("Subtract %d.\n", num1);
printf("=%d\tContents of Acumulator\n", result);
break;
case '*':
result = num * num1;
printf("Multiply by %d.\n", num1);
printf("=%d\tContents of Acumulator\n", result);
break;
case '/':
if(num1 == 0){
printf("Cannot divide by zero.\n");
}
else{
printf("Divide by %d.\n", num1);
result = num / num1;
printf("=%d\tContents of Acumulator\n", result);
}
break;
case 's':
num = num1;
printf("Accumulator set to %d.\n", num);
break;
case 'S':
num = num1;
printf("Accumulator set to %d.\n", num);
break;
case 'e':
printf("End of program.\n");
break;
case 'E':
printf("End of program.\n");
break;
default:
printf("Unknown operator!\n");
break;
}
}
}
return 0;
}
Sample run:
Begin Calculations
------ --------------
15 s
Accumulator set to 15.
12 +
Add 12.
=27 Contents of Acumulator
15 -
Subtract 15.
=0 Contents of Acumulator
10 e
End of program.