我使用代码块16.01当我调试此代码时它向我显示正确的输出,但是当我运行它时显示错误的输出!怎么解决这个?
int main()
{
char ch[100],var[100],val[100],tempVa[100];
int i = 0,j=0,count=0;
while (1)
{
puts("Enter the expression (or (end) to exit):");
gets(ch);
if (strcmp(ch, "end") == 0 || strcmp(ch, "END") == 0)
exit(-1);
else if(2 == sscanf(ch,"%s = %s", var, val))
{ i = 0;
printf("Variable is : %s\t Value Before evaluating : %s\n",var, val);
while (i<=strlen(val))
{
while (val[i]!='-'&&val[i]!='%'&&val[i]!='/'&&val[i]!='*'&&val[i]!='+'&&i<strlen(val))
tempVa[j++]=val[i++];
i++;
for (count=0; count<strlen(tempVa); count++)
printf("%c", tempVa[count]);
for (count=strlen(tempVa); count>=0; count--)
tempVa[count]='\0';
j=0;
}
}
else
printf("Invalid!");
}
return 0;
}
Smaple输入:Hassan = Merna + Mohamed + Ahmed
那些垃圾从哪里来?!
答案 0 :(得分:1)
我测试了你的代码并且它有效 编辑:
代码:
#include <stdio.h>
#include <string.h> // edit
int main()
{
char ch[100], var[100], val[100], tempVa[100];
int i = 0, j = 0, count = 0;
while (1)
{
puts("\nEnter the expression (or (end) to exit):");
gets(ch);
if (strcmp(ch, "end") == 0 || strcmp(ch, "END") == 0)
return -1; // edit
else if(2 == sscanf(ch, "%s = %s", var, val))
{
i = 0;
printf("Variable is : %s\t Value Before evaluating : %s\n", var, val);
while (i <= strlen(val))
{
while (val[i] != '-' && val[i] != '%' && val[i] != '/' && val[i] != '*' && val[i] != '+' && i < strlen(val))
tempVa[j++] = val[i++];
i++;
for (count = 0; count < strlen(tempVa); count++)
printf("%c", tempVa[count]);
for (count = strlen(tempVa); count >= 0; count--)
tempVa[count] = '\0';
j = 0;
}
}
else
printf("Invalid!");
}
return 0;
}
示例运行:
输入表达式(或(结束)退出):Hassan = Merna + Mohamed + Ahmed
变量是:哈桑价值在评估之前:Merna + Mohamed + Ahmed MernaMohamedAhmed
输入表达式(或(结束)退出):