Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, can also write in C language. thanks Hi, help to provided clearly written on

ID: 3349102 • Letter: H

Question

Hi, can also write in C language.
thanks

Hi, help to provided clearly written on this programming. Help to ensure no error inside this programing and also please provide fully answer. thanks 4. Write a program to take in a simple mathematical operation in the form of strings. For example, "1+". When the user presses the enter key, the program should convert the string into respective numbers and mathematical operation, then, display the output. Start with the arithmetic operation between 2 numbers. When it's working, expand the program in order to be able to perform arithmetic operation between 5 numbers. An example of the output interface is: Enter a simple mathematical operation: 1+1 Press Enter The result of 1+1 is 2 Do you want to try again? (Y/N) Y Press Enter Enter a simple mathematical operation: 1 +1x2 Press Enter The result of 1+1x2 is 3 Do you want to try again? (Y/N) Y Press Enter Enter a simple mathematical operation: 2+22+1 +1x2 Press Enter Sorry, the maximum is 5 numbers. Do you want to try again? (Y/N) N Press Enter Bye.. [25 marks]

Explanation / Answer

#include <stdio.h>
#include<stdlib.h>
#include <math.h>
#include<string.h>

double evaluate (char []);
int precedence(char);
double calculate(char , double , double );
char checknumber(char);

int main()
{
int count=0;
char expression[100000];
char c;
double result;
  
loop:

printf("Enter the Expression: ");
scanf("%s",&expression);
int l=strlen(expression);
for(int i=0;i<l;i++)
{
if(expression[i]=='+' || expression[i]=='-' || expression[i]=='*' || expression[i]=='/' || expression[i]=='^')
count++;
}
if(count<=4)
{
result = evaluate(expression);
printf("The result of %s is %lf ", expression,result);
  
}
else
{
printf("Sorry, the maximum is 5 numbers ");
}
printf("Do you want to try again?(Y/N) ");
scanf("%s",&c);
if(c=='Y' || c=='y')
goto loop;
else
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 '+':
case '-':
precedence = 0;
break;
case '*':
case '/':
precedence = 1;
break;
case '^':
precedence = 2;
}
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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote