【发布时间】:2014-03-21 21:54:15
【问题描述】:
我必须用 C 语言编写一个简单的计算器,它只使用 getchar/putchar。我需要回显用户输入,然后执行数学运算(只需要使用 +、/、%、* 和 -)。现在,我只关心我做错了什么,只要将值存储到我的输入变量中。程序的其余部分应该很容易自己完成。
在我的 while 循环中,我尝试小心地将 if/else 嵌套(主要用于错误检查)仅在我的“else if”中。我希望我的循环忽略空格,然后将数字、数学运算符和其他数字分别分配给 input1、input2 和 input3。现在,如果我输入“55 * 66”之类的内容,我会返回“*0”之类的内容。
感谢收看。
更新(2014 年 3 月 22 日): 我越来越近了。我现在的问题是,只有当我在每个数字和操作数之后输入一个空格时,程序才能工作(即“2 + 4”有效,但任何没有空格或多个空格的东西都不能)。我也不太明白如何输入数字以输出它们。我使用了 printf,因此我至少可以同时拥有一个工作程序。 谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int add(int input1,char operand, int input2);
int subtract(int input1,char operand, int input2);
int mod(int input1,char operand, int input2);
int multiply(int input1,char operand, int input2);
int divide(int input1,char operand, int input2);
int main()
{
int answer = 0;
int ch = 0;
int input1 = 0;
char operand = 0;
int input2 = 0;
int function = 0;
printf("\nPlease enter a calculation to be made.\n");
while (((ch = getchar()) != ' ') && (ch != '\n')){
if (ch == '-') {
printf("\nError: no negatives allowed.\n");
}
else if (!isdigit(ch)){
printf("\nError: number not inputted (first number).\n");
}
else {
input1 = (input1 * 10) + (ch - '0');
}
}
while (((ch = getchar()) != ' ') && (ch != '\n')){
switch(ch){
case '+':
operand = '+';
break;
case '-':
operand = '-';
break;
case '%':
operand = '%';
break;
case '*':
operand = '*';
break;
case '/':
operand = '/';
break;
default:
printf("Error: input is not one of the allowed operands.");
break;
}
}
while (((ch = getchar()) != ' ') && (ch != '\n')){
if (ch == '-') {
printf("\nError: no negatives allowed.\n");
}
else if (!isdigit(ch)){
printf("\nError: number not inputted (second number).\n");
}
else {
input2 = (input2 * 10) + (ch - '0');
}
}
printf("%d", input1);
putchar(' ');
printf("%c", operand);
putchar(' ');
printf("%d", input2);
putchar(' ');
putchar('=');
putchar(' ');
if (operand == '+'){
answer = add(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '-'){
answer = subtract(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '%'){
answer = mod(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '*'){
answer = multiply(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '/'){
answer = divide(input1, operand, input2);
printf("%d", answer);
}
return 0;
}
int add(int input1,char operand, int input2){
return input1 + input2;
}
int subtract(int input1,char operand, int input2){
return input1 - input2;
}
int mod(int input1,char operand, int input2){
return input1 % input2;
}
int multiply(int input1,char operand, int input2){
return input1 * input2;
}
int divide(int input1,char operand, int input2){
return input1/input2;
}
【问题讨论】:
-
您有两个案例测试
isdigit(ch)。第一个不做任何事情,并且将始终支持将ch存储在input3中的第二个。 -
您可以使用
isspace来检查空格。另外,在确定ch是一个数字之后,您希望通过比较ch >= 0来实现什么?不要忘记ch是数字字符的 ASCII 值,而不是数字本身的数值。 -
ch >= 0不会测试数字是否大于或等于 0。它将测试输入的字符的编码是否 >= 0。不是你想要的。也许你的意思是ch >= '0',但这也没什么用。只需使用isdigit()。请记住,如果用户输入负数,您将首先读取-。 -
感谢您的回复 - 关于检查 ch >= 0,此代码的一部分是我要检查并排除负数。我应该说清楚的。我还添加了“input1 = ch;”在第一个 else if 中,我不确定为什么它不存在。不过,我仍然遇到问题。
-
你手上的是单个数字字符的 ASCII 值,而不是带符号的多位整数。您将不得不编写一个更复杂的解析器来读取可选的 '-' 后跟多个数字字符,将每个数字从 ASCII 转换为实际数值,并将它们合并为某个数基中的单个整数(通常,我们使用基数 10!)。