我试图制作一个简单的计算器程序,但是我的打印和扫描语句在第一次循环后仍然打印两次。任何帮助将不胜感激。到目前为止,这是我的代码。
#include <stdio.h>
int main ()
{
char(input) = 0;
int(num1) = 0;
int(num2) =0;
int i;
for(i=1; i!=0; i++)
{
printf("This program will perform the following math \n");
printf("'+' :addtion of two integers\n");
printf("'-': subtraction of two integers\n");
printf("'*': multiplication of two integers\n");
printf("'/' : division of two integers\n");
printf("'%%' : modulus of two integers\n");
printf("'q' or 'Q' : Quit the program\n");
scanf("%c", &input);
switch (input)
{
case '+':
printf("Enter two integers:\n");
scanf ("%d%d", &num1, &num2);
printf("%d + %d = %d\n", num1, num2, num1 + num2);
break;
case '-':
printf("Enter two integers:\n");
scanf ("%d%d", &num1, &num2);
printf("%d - %d = %d\n", num1, num2, num1 - num2);
break;
case '*':
printf("Enter two integers:\n");
scanf ("%d%d", &num1, &num2);
printf("%d * %d = %d\n", num1, num2, num1*num2);
break;
case '/':
printf("Enter two integers:\n");
scanf ("%d%d", &num1, &num2);
if (num2 !=0)
{
printf("%d / %d = %d\n", num1, num2, num1/num2);
break;
}
else
{
printf ("invalid input, denominator can not be 0");
break;
}
case '%':
printf("Enter two integers:\n");
scanf ("%d%d", &num1, &num2);
if (num1 >= 0)
{
printf("%d mod %d = %d\n", num1, num2, num1%num2);
break;
}
else if (num2 >=0)
{
printf("%d mod %d = %d\n", num1, num2, num1%num2);
break;
}
else
{
printf ("invalid input, integers have to be positive");
break;
}
case 'q':
printf("Goodbye\n");
i = -1;
break;
case 'Q':
printf("Goodbye\n");
i = -1;
break;
}
}
return 0;
}
谢谢你们