Write a program that acts as a simple “printing” calculator. The program should allow the user to type in expressions of the form:
Number operator
The following operators should be recognized by the program:
+ - * / S E
The S operator tells the program to set the “accumulator” to the typed in number. The E operator tells the program that execution is to end. The arithmetic operators are performed on the contents of the accumulator with the number that was keyed in acting as the second operand.
Make sure that the program detects division by 0 and also checks for unknown operators. Here's a sample:
Begin Calculations
10 S (Set Accumulator to 10)
= 10.000000 (Contents of Accumulator)
2 / (Divide by 2)
= 5.000000 (Contents of Accumulator)
55 – (Subtract 55)
-50.000000
100.25 S (Set Accumulator to 100.25)
= 100.250000
4 * (Multiply by 4)
= 401.000000
0 E (End of program)
= 401.00000
End of Calculations
Please if you dont know, dont answer
Need help on C Programming. I can't seem to figure out the answer. Here are the details:?
%26gt;Please if you dont know, dont answer
Since I've been writting SW for years, I know how to do it in about 2 mins in a couple dozen languages. So I guess I can answer.
We'll help you if you get stuck.
We won't do your homework.
Reply:#include %26lt;stdio.h%26gt;
int main (int argc, char **argv)
{
int i;
int value;
int total;
char operator;
total=atoi(argv[1]);
for(i=2; argc%26gt;i+1; i=i+2)
{
operator=argv[i][0];
value=atoi(argv[i+1]);
if(operator=='+')
{
total=total+value;
}
else if(operator=='-')
{
total=total-value;
}
else if(operator=='*')
{
total=total*value;
}
else if(operator=='/')
{
total=total/value;
}
}
fprintf(stdout, "%d\n", total);
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment