# A simple calculator

## Title Requirements:

Simulate the operation of a simple arithmetic unit. Assuming that the calculator can only perform addition, subtraction, multiplication and division, and the operands and results are integers, the four operators have the same priority and are calculated from left to right.

Input format:
Input gives a four arithmetic expression in one line, with no spaces and at least one operand. When an equal sign "=" indicates the end of input.

Output format:
Output the operation result of the formula in one line, or if the division denominator is 0 or there is an illegal operator, output the ERROR message "ERROR".

Input sample:

```1+2*10-10/2=
```

Output example:

```10
```

## Idea:

### How do I save the elements of an equation into an array?

Create an int array to hold the input equation elements.
You cannot use getchar() on all elements because numbers can have multiple digits.
Because every time an operator is read (excluding '='), it must be followed by a number,
So you can read and store one operator at a time,
Use scanf ("% d", & equal [i]) to read and store a number.
In this way, all elements of the equation are saved in the equation array.
The code snippet is as follows:

```	int equal = {0};					//Establish equation array
int c;									//Create character variable
int i = 0, j, cnt;						//Operator pointer, right operand pointer, calculation times
int right;								//Create right operand variable
int tot;								//Create cumulant

scanf("%d", &equal[i]);					//First store the first number in the equation array
i++;									//Let the index of the array storing equation elements + 1

while( (c = getchar()) != '=')
{
equal[i] = c;						//Read operation symbol
i++;								//Let the index of the array storing equation elements + 1
scanf("%d", &equal[i++]);			//Read the number and let the index of the array storing equation elements + 1
//(that is, the index should be + 1 no matter whether the number or symbol is read)

switch (c)							//The EORROR operation returns a character that is not read
{
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
default:
printf("ERROR\n");
return 0;
}

if (c == '/' && equal[i-1] == 0)	//If the divisor is 0, ERROR is returned
{
printf("ERROR\n");
return 0;
}
}
```

### How to use the equation array to operate from left to right?

Suppose the input string is 1 + 2 * 10 - 10 / 2=
Through the above method of saving the equation array, the resulting array is:

element 1 + 2 * 10 - 10 / 2
Indexes 0 1 2 3 4 5 6 7 8

p.s. all operators are stored in the form of ASCII code and are of type int. to get the character type, you need to use type conversion, that is, (char) equal[i]

It can be found by observation
The index of the operator is odd (2n +1)
The index of the operator (which I prefer to call operand) is even (2n).
So we can create a cumulative variable (tot), which becomes the left operand of the operation, and every number in the element (except the first) becomes the right operand.
e.g.

tot = 1 i = 1; j = 2;
tot + 2 = 3 --> tot = 3 n = 1;+ ----> equal(1) ; 2 ----> equal(2); i = 2 *n +1 = 3; n = n + 1 = 2; j = 2 * n = 4;
tot *10 = 30 --> tot = 30 n = 2;* ----> equal(3) ; 10 ----> equal(4); i = 2 *n +1 = 5;n = n + 1 = 3; j = 2 * n =6 ;
tot - 10 = 20 --> tot = 20 n = 3; + ----> equal(5) ; 2 ----> equal(6); i = 2 *n +1 = 7; n = n + 1 = 4; j = 2 * n = 8;
tot / 2 = 10 --> tot = 10 n = 4; + ----> equal(7) ; 2 ----> equal(8); i = 2 *n +1 = 9; n = n + 1 = 5; j = 2 * n = 10;

The code snippet is as follows:

```int lene = i;							// lene ---> length of equal
tot = equal;							// Total -- > Total (cumulative value)
right = equal;						// Right --- > right operand (equal[0,2,4,6,8,...])

for (i = 1, j = 2, cnt = 1; i < lene;)	// Calculate the equation (tot + or - or * or / equal[0,2,4,6,8,...])
{
switch ((char)equal[i])
{
case '+':
//printf("hr+\n");
tot += right;
//printf("tot = %d\n",tot);
break;
case '-':
//printf("hr-\n");
tot -= right;
//printf("tot = %d\n",tot);
break;
case '*':
//printf("hr*\n");
tot *= right;
//printf("tot = %d\n",tot);
break;
case '/':
//printf("hr/\n");
tot /= right;
//printf("tot = %d\n",tot);
break;
}

i = 2 * cnt + 1;					// i is the pointer to the operation symbol, i = 1,3,5,7
cnt++;								// Number of calculations
j = 2 * cnt;						// J is the pointer to the right operand, j = 2,4,6,8
right = equal[j];
}
```

Finally, the value of tot is the result of the equation.

## code

```#include <stdio.h>

int main()
{
int equal = {0};					//Establish equation array
int c;									//Create character variable
int i = 0, j, cnt;						//Operator pointer, right operand pointer, calculation times
int right;								//Create right operand variable
int tot;								//Create cumulant

scanf("%d", &equal[i]);					//First store the first number in the equation array
i++;									//Let the index of the array storing equation elements + 1

while( (c = getchar()) != '=')
{
equal[i] = c;						//Read operation symbol
i++;								//Let the index of the array storing equation elements + 1
scanf("%d", &equal[i++]);			//Read numbers and make the index of the array storing equation elements + 1 (that is, make the index + 1 regardless of whether reading numbers or symbols)

switch (c)							//Eoerror is returned if a character that is not an operation symbol is read
{
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
default:
printf("ERROR\n");
return 0;
}

if (c == '/' && equal[i-1] == 0)	//If the divisor is 0, ERROR is returned
{
printf("ERROR\n");
return 0;
}
}

int lene = i;							// lene ---> length of equal
tot = equal;							// Total -- > Total (cumulative value)
right = equal;						// Right --- > right operand (equal[0,2,4,6,8,...])

for (i = 1, j = 2, cnt = 1; i < lene;)	// Calculate the equation (tot + or - or * or / equal[0,2,4,6,8,...])
{
switch ((char)equal[i])
{
case '+':
//printf("hr+\n");
tot += right;
//printf("tot = %d\n",tot);
break;
case '-':
//printf("hr-\n");
tot -= right;
//printf("tot = %d\n",tot);
break;
case '*':
//printf("hr*\n");
tot *= right;
//printf("tot = %d\n",tot);
break;
case '/':
//printf("hr/\n");
tot /= right;
//printf("tot = %d\n",tot);
break;
}

i = 2 * cnt + 1;					// i is the pointer to the operation symbol, i = 1,3,5,7
cnt++;								// Number of calculations
j = 2 * cnt;						// J is the pointer to the right operand, j = 2,4,6,8
right = equal[j];
}

printf("%d\n", tot);					//	Print out the last calculated value
return 0;
}

```

Tags: C Algorithm

Posted by Lessur on Mon, 09 May 2022 02:04:30 +0300