简单的计算器和while循环

时间:2013-05-04 18:22:25

标签: c loops

嗨我正在使用while(true)和它的工作来编写一个简单的calulator,除了当循环结束并再次开始时,它重复第一行两次,是否有人有解决方案?提前谢谢......

#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);
    }}}}}}
}}

4 个答案:

答案 0 :(得分:2)

使用%c输入char时,输入流中会留下换行符。因此,它不会要求输入,因为输入流中剩下的换行符将被消耗用于后续迭代。

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

变化:

scanf("%c",&a);

为:

scanf(" %c",&a);

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

答案 1 :(得分:1)

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

为避免这种情况,您可以将代码修改为:

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

答案 2 :(得分:1)

这应归功于scanf。抛出scanf并尝试使用getchar()来接收操作命令。值得一试。

也可以使用switch..case来完成这类任务。包括'default'子句也通知用户..

答案 3 :(得分: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变量。

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