C 如何克服windows停止工作错误消息?

C 如何克服windows停止工作错误消息?,c,C,我正在为一个简单的计算器编写一个程序,它接受一个包含两个运算符和一个操作数的字符串(不带空格),并输出结果。我的程序编译得很好,但在运行期间,我收到了windows停止工作的错误消息。原因可能是什么?我认为这与我的程序逻辑有关。这是我的密码: #include <stdio.h> #include <math.h> int main() { int operand_1 = 0, operand_2 = 0, i = 0, result = 0; char

我正在为一个简单的计算器编写一个程序,它接受一个包含两个运算符和一个操作数的字符串(不带空格),并输出结果。我的程序编译得很好,但在运行期间,我收到了windows停止工作的错误消息。原因可能是什么?我认为这与我的程序逻辑有关。这是我的密码:

#include <stdio.h>
#include <math.h>
int main()
{
    int operand_1 = 0, operand_2 = 0, i = 0, result = 0;
    char string[10], *ptr, operation;
    ptr = string;
    printf("Enter the expression:\n");
    gets(string);
    //puts(string);
    while(*ptr==0||*ptr==1||*ptr==2||*ptr==3||*ptr==4||*ptr==5||*ptr==6||*ptr==7||*ptr==8||*ptr==9)
    {
        operand_1 = operand_1 + ((int)*ptr)*pow(10,i);
        i++;
        ptr++;
    }
    operation = *ptr;
    ptr++;
    i=0;
    while(ptr!=NULL)
    {
        operand_2 = operand_2 + ((int)*ptr)*pow(10,i); 
        i++;
        ptr++;
    }
    switch(operation)
    {
        case '+':
            result = operand_1 + operand_2;
        case '-':
            result = operand_1 + operand_2;
        case '*':
            result = operand_1 * operand_2;
        case '/':
            result = operand_1 / operand_2;
        case '%':
            result = operand_1 % operand_2;
    }
    printf("%d",result);
}
#包括
#包括
int main()
{
int操作数_1=0,操作数_2=0,i=0,结果=0;
字符字符串[10],*ptr,操作;
ptr=字符串;
printf(“输入表达式:\n”);
获取(字符串);
//放置(字符串);
而(*ptr==0 | |*ptr==1 | |*ptr==2 | |*ptr==3 | |*ptr==4 | |*ptr==5 | |*ptr==6 | |*ptr==7 |*ptr==8 |*ptr==9)
{
操作数_1=操作数_1+((int)*ptr)*pow(10,i);
i++;
ptr++;
}
操作=*ptr;
ptr++;
i=0;
while(ptr!=NULL)
{
操作数_2=操作数_2+((int)*ptr)*pow(10,i);
i++;
ptr++;
}
开关(操作)
{
格“+”:
结果=操作数_1+操作数_2;
案例'-':
结果=操作数_1+操作数_2;
案例“*”:
结果=操作数_1*操作数_2;
案例“/”:
结果=操作数_1/操作数_2;
案例“%”:
结果=操作数_1%操作数_2;
}
printf(“%d”,结果);
}

我已经解决了这个问题,还改进了程序的逻辑,使其工作正常

#include <stdio.h>
#include <math.h>
int main()
{
while(1)
{
int operand_1 = 0, operand_2 = 0, i = 0, result = 0;
char string[10], *ptr, operation;
ptr = string;
printf("\nEnter the expression:");
scanf("%s",string);
if (*ptr=='q' || *ptr=='Q' && *(ptr+1)=='u' || *(ptr+1)=='U' && *(ptr+2)=='i' || *(ptr+2)=='I' && *(ptr+3)=='t' || *(ptr+3)=='T')
{
printf("\nBye");
break;
}
while(*ptr>='0' && *ptr<='9')
{
operand_1 = operand_1*10 + (*ptr - 48);
i++;
ptr++;
}
operation = *ptr;
ptr++;
i=0;
while(*ptr>='0' && *ptr<='9')
{
operand_2 = operand_2*10 + (*ptr-48);
i++;
ptr++;
}
if(operand_2 == 0)
{
printf("\nDivision by zero");
continue;
}
switch(operation)
{
case '+':
result = operand_1 + operand_2;
break;
case '-':
result = operand_1 - operand_2;
break;
case '*':
result = operand_1 * operand_2;
break;
case '/':
result = operand_1 / operand_2;
break;
case '%':
result = operand_1 % operand_2;
break;
}
printf("\nResult = %d",result);
}
}
#包括
#包括
int main()
{
而(1)
{
int操作数_1=0,操作数_2=0,i=0,结果=0;
字符字符串[10],*ptr,操作;
ptr=字符串;
printf(“\n输入表达式:”);
scanf(“%s”,字符串);
如果(*ptr=='q'| |*ptr=='q'|&*(ptr+1)='u'|&*(ptr+1)='u'&*(ptr+2)='i'|&*(ptr+2)='i'&*(ptr+3)='t'|*(ptr+3)='t'|
{
printf(“\n字节”);
打破
}

虽然(*ptr>='0'&&&*ptr='0'&&&*ptrffirst-stop-use
get
。此函数很危险,不再是C的一部分,
ptr
是指向
字符串的指针,但您可以将其与数字进行比较。如果您有
while(ptr!=NULL)
您的意思是
while(*ptr!=0)
?“停止工作”通常意味着它被塞进了一个无限循环。使用调试器找出你被卡住的地方。这是一项基本技能。记住,“编译得很好”并不意味着你的代码有多正确。请注意,
case
语句“失败”,因此,您必须在每个操作之间
断开
,否则最终结果是您将针对顶部的
+
之类的操作运行每个操作。谢谢!简要说明问题所在,并修复更新代码的缩进可能会有所帮助。