So, I decided to take an independent study Computer science course at my school and my "teacher" assigned me some projects to do (4), which I promised to get them done over Christmas break. I'm currently lost on (project 2) how to make this:
The following would be a typical input for this program
7+22-15*23/100^2
For this assignment parenthesis will not be allowed and will not be tested for. Any illegal character should be ignored and not cause a program error. Decimal points are not allowed in this application, only integers. However, displayed results may be in decimal form (four digits significance).
I know how to do the basic stuff like have a user input a number, ask if they want to add/sub/div/mult/etc.., then get second number and display results. But I don't know how I would go about getting all the inputs on the same line and have the computer read them (for example, if the user inputs: 7+22-15*23/100^2).
This is what I have come up with so far, however it does not work:
Code:#include <stdio.h>
#include <math.h>
int option;
char q;
int number;
int currentsum;
int value;
void calc()
{
printf("\t\t\t\t\tBegin\n"); // says begin in top middle of screen
option = 0; number = 0; currentsum = 0; value = 0;
while(1){
value = getchar(); // gets user input and sets equal to value
if (value >= '0' && value <= '9')
{
number = number*10 + (value - '0'); /* a way of reading the "values" left to right. If someone puts in 729 as their first number then it will go across
and do number = 0*10 (because number was set equal to 0) + 7. then it goes through again. number is now 7 so it goes through and does 7*10 + 2 thus making
72. Number is now 72 which means it goes 72*10 + 9 making number 729. It will follow this process if the input is a number 1-9*/
}
if (value == '+' || value == '-' || value == '*' || value == '/' || value == '^' || value == '=' || value == '\n')
{
if(value == '+') currentsum = currentsum + number; // if I come across one of these symbols then it performs the operation described
if(value == '-') currentsum = currentsum - number;
if(value == '*') currentsum = currentsum * number;
if(value == '/') currentsum = currentsum / number;
if(value == '^') currentsum = pow(currentsum, number);
if(value == '=') printf(currentsum); // when it reads an equal sign or a new line it displays the result of the equation.
if(value == '\n') printf(currentsum);
}
else break; // come across any other symbols or characters break the loop
}
}
int main()
{
system("title Calculator"); // titles windo "Calculator
system("color 1A"); // makes background blue
calc(); // goes to void calc
system("pause");
scanf("%c",&q);
if( q == 'q')exit(0);
}