2013-05-04 98 views
1

嗨我正在wrinting一个简单的calulator使用while(true)和它的工作,除了当循环结束并重新开始,它重复第一行两次,有没有人有这个解决方案?在此先感谢...简单的计算器和while循环

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

int main() 
{ 
char a,ch; 
int x,y,i,n,e,s; 
while(1) { 
    printf("\nEnter the operation:"); 
    scanf("%c",&a); 
    e=a; 
    if (e=='+') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x+y; 
     printf("\nThe result of %d+%d is:%d\n",x,y,s); 
    } else { 
     if (e=='-') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x-y; 
     printf("\nThe result of %d-%d is:%d\n",x,y,s); 
    } else { 
     if (e=='*') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x*y; 
     printf("\nThe result of %dx%d is:%d\n",x,y,s); 
    } else { 
     if (e=='/') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x/y; 
     printf("\nThe result of %d/%d is:%d\n",x,y,s); 
    } else { 
     if (e=='%') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x%y; 
     printf("\nThe result of %d%%d is:%d\n",x,y,s); 
    } else { 
     if (e=='^') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=pow(x,y); 
     printf("\nThe result of %d^%d is:%d\n",x,y,s); 
    }}}}}} 
}} 

回答

2

当你输入字符使用%c,还有留在输入流中的换行字符。因此,它不会要求输入,因为输入流中留下的换行符会被用于后续迭代。

在格式字符串中添加一个空格,以便scanf()忽略换行符(任何whitesapce)剩下的字符。

变化:

scanf("%c",&a); 

到:

scanf(" %c",&a); 

这样的scanf()会忽略所有的空格。

+0

我爱你!谢谢 ! – 2013-05-04 19:11:07

+0

哈哈......不客气! – 2013-05-04 19:15:58

1

当您使用scanf()读取键盘输入时,输入被按下后会被读取,但由enter键生成的换行符不会被scanf()调用消耗。这意味着下一次您从标准输入中读取时,将会有一个换行符等待您(这将使下一个scanf()调用立即返回,而无数据)。

为了避免这种情况,你可以修改你的代码是这样的:

while(1) { 
    printf("\nEnter the operation:"); 
    scanf("%c%*c", &a); 
0

目前,如果你提供什么,你是不是期待的循环,它只会再次执行,打印初始提示多倍。下面是用最少的改变你的程序的解决方案,它只会在成功完成你处理计算的一个转载的初始提示:

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

int main() 
{ 
char a,ch; 
int x,y,i,n,e,s; 
int new_operation = 1; 
while(1) { 
    if (new_operation) { 
     printf("\nEnter the operation:"); 
     new_operation = 0; 
    } 
    scanf("%c",&a); 
    e=a; 
    if (e=='+') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x+y; 
     printf("\nThe result of %d+%d is:%d\n",x,y,s); 
     new_operation = 1; 
    } else { 
     if (e=='-') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x-y; 
     printf("\nThe result of %d-%d is:%d\n",x,y,s); 
     new_operation = 1; 
    } else { 
     if (e=='*') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x*y; 
     printf("\nThe result of %dx%d is:%d\n",x,y,s); 
     new_operation = 1; 
    } else { 
     if (e=='/') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x/y; 
     printf("\nThe result of %d/%d is:%d\n",x,y,s); 
     new_operation = 1; 
    } else { 
     if (e=='%') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=x%y; 
     printf("\nThe result of %d%%d is:%d\n",x,y,s); 
     new_operation = 1; 
    } else { 
     if (e=='^') { 
     printf("\nEnter the first integer:"); 
     scanf("%d",&x); 
     printf("\nEnter the second integer:"); 
     scanf("%d",&y); 
     s=pow(x,y); 
     printf("\nThe result of %d^%d is:%d\n",x,y,s); 
     new_operation = 1; 
    }}}}}} 
}} 

注意的变化包括一个新的变量声明,if语句周围的初始提示,并在每次操作完成后重置new_operation变量。

这将处理无关的换行符以及对初始提示的任何其他未处理的响应。

1

这应该是由于scanf。离开scanf并尝试使用getchar()来接收操作命令。值得一试。

还可以使用switch..case来处理这类任务。包含'默认'条款也通知用户..

+0

我试过这个开关,但问题是我不能在while循环中使用它 – 2013-05-04 19:16:04