C语言中使用while计算整数四则运算

在C语言中,怎么用while计算整数的四则运算,并且判断是不是四则运算,循环输入数学式子

该回答引用GPT:
可以使用while循环来实现,每次循环中,先从用户输入中获取一个数学式子,然后判断是不是四则运算,如果是,就计算出结果,如果不是,就提示用户输入正确的数学式子。

#include <stdio.h>

int main()
{
    char str[100];
    int result;
    while(1)
    {
        printf("请输入一个数学式子:");
        scanf("%s", str);
        if(str[1] == '+' || str[1] == '-' || str[1] == '*' || str[1] == '/')
        {
            if(str[1] == '+')
            {
                result = str[0] - '0' + str[2] - '0';
            }
            else if(str[1] == '-')
            {
                result = str[0] - '0' - str[2] - '0';
            }
            else if(str[1] == '*')
            {
                result = (str[0] - '0') * (str[2] - '0');
            }
            else
            {
                result = (str[0] - '0') / (str[2] - '0');
            }
            printf("结果是:%d\n", result);
        }
        else
        {
            printf("输入的不是四则运算,请重新输入!\n");
        }
    }
    return 0;
}

如还有疑问,可留言帮助解决。