Writing in C language Lannisters of Justice You are an entry level hero and your
ID: 3594314 • Letter: W
Question
Writing in C language
Lannisters of Justice
You are an entry level hero and your first task from the Lannisters of Justice is to break into the throne room of the Mad Mathemagician. The only issue is that his throne is guarded by a numeric password that is solved by following a specific ordering of operations on an expression.
No one before you was successful, but that was because they didn't figure out that the Mathemagician followed his own Mad order of operations; he ordered it by the amount of strokes it took his iron pen to write the operators. The ordering you have deduced (from first to last) is: subtraction( - ), division( / ), addition( + ), multiplication( * )
Input Specifications
You will be given a single line of character input of no more than 100 characters that is a valid math expression using integers and math operations. Example could be something as simple as 3+4 or something a little more complex, such as 3*4+12/6. Your input will only contain the 4 operators and numbers, no other characters.
Output Specifications
You will need to output the solution to the equation when applying the Mathemagician's order of operations. All output will be a single integer value.
Sample Input/Output
INPUT
OUTPUT
EXPLANATION
Subtraction happens before multiplication, so the calculation is 16-4 = 12, then 12 * 2 = 24, which is the solution.
INPUT
OUTPUT
EXPLANATION
First you take 6-2 = 4. Then, 24/4 = 6 and 6/3 = 2, so now the expression is 2+3*1. Addition is next, so 2+3=5, so you get 5*2 = 10
Explanation / Answer
Below is your code:-
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double evaluate (char []);
int precedence(char);
double calculate(char , double , double );
char checknumber(char);
int main(void)
{
char expression[100];
double result;
scanf("%[^ ]", expression);
result = evaluate(expression);
printf("%d ", (int)result);
return 0;
}
double evaluate(char expr[])
{
double numbers[5]; int nsi = 0;
char operators[5]; int osi = 0;
char numbuf[16]; int nbi = 0;
char ch; int i = 0;
while ((ch = expr[i]) != 0) {
if (checknumber(ch))
{
numbuf[nbi++] = ch;
if (!checknumber(expr[i + 1]))
{
numbuf[nbi] = 0; nbi = 0;
sscanf(numbuf, "%lf", &numbers[nsi++]);
}
}
else
{
while ((osi > 0) && (precedence(ch) <= precedence(operators[osi - 1])))
{
numbers[nsi - 2] = calculate(operators[osi - 1], numbers[nsi - 2], numbers[nsi - 1]);
osi--; nsi--;
}
operators[osi++] = ch;
}
i++;
}
while (osi > 0) {
numbers[nsi - 2] = calculate(operators[osi - 1], numbers[nsi - 2], numbers[nsi - 1]);
osi--; nsi--;
}
return numbers[0];
}
char checknumber(char ch)
{
if ((ch >= '0' && ch <= '9') || ch == '.') return 1; else return 0;
}
int precedence(char ch)
{
int precedence;
switch (ch)
{
case '*':
precedence = 0;
break;
case '+':
precedence = 1;
break;
case '/':
precedence = 2;
break;
case '-':
precedence = 3;
}
return precedence;
}
double calculate(char moperator, double num1, double num2)
{
double result;
switch (moperator)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
case '^':
result = pow(num1, num2);
break;
default:
printf("Invalid Operator ");
exit(-1);
}
return result;
}
Output
2+3*24/6-2/3
10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.