2013-05-04 117 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