一个简单的计算C语言四则运算
#include<stdio.h>
#include<string.h>
int main(void)
{
char string[100];
int index = 1;
int sum = 0;
int temval = 1;
char ope, c,inope;
memset(string, 0, 100);
scanf("%s",string);
sum += (string[0] - 48);
while(string[index] != '\0')
{
c = string[index];
switch(c)
{
case '+':ope = '+';index++;
break;
case '-':ope = '-';index++;
break;
case '*':ope = '*';index++;
break;
case '/':ope = '/';index++;
break;
default:temval = c - 48;index++;
while(string[index] != '+'&&string[index] != '-'&&string[index] != '\0')
{
inope = string[index];
if(inope == '*')
{
temval *= (string[++index] - 48);
index++;
}
if(inope == '/')
{
temval /= (string[++index] - 48);
index++;
}
}
if(ope =='+')
{
sum +=temval;
}
if(ope == '-')
{
sum -= temval;
}
}
}
printf("\n表达式%s的值为:%d\n",string,sum);
return 0;
}
计算类似1+2-3*3/3的表达式,如果输入的格式不正确,因为没有加入判断表达式是否正确,会出错的。

浙公网安备 33010602011771号