我必须在C中编写一个简单的计算器,它只使用getchar / putchar。我需要回显用户输入,然后执行数学运算(只需要使用+,/,%,*和 - )。现在,我只关心我做错了什么,只要将值存储到我的输入变量中。该程序的其余部分应该很容易自己做。
在我的while循环中,我试图小心嵌套if / else' s(主要用于错误检查)仅在我的"否则如果"#34;。我希望我的循环忽略空格,然后分别将数字,数学运算符和其他数字分配给input1,input2和input3。现在,如果我输入类似" 55 * 66"的内容,我会收到类似" * 0"。
的内容。感谢您的光临。
更新(2014年3月22日): 我越来越近了。我现在的问题是,只有在每个数字和操作数之后输入一个空格时,程序才会起作用(即" 2 + 4"有效,但任何没有空格或有多个空格的东西都没有) 。我也没有很好地得到putchar数字输出它们。我使用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;
}
答案 0 :(得分:1)
最简单的方法是按顺序解析每个表达式:
int main(int argc, char **argv)
{
int ch;
int a;
char op;
int b;
int negate;
// start with a character of lookahead
ch = getchar();
// skip leading spaces
while (ch != EOF && isspace(ch) && ch != '\n') ch = getchar();
// read A operand
a = 0;
if ((negative = (ch == '-'))) ch = getchar();
if (ch == EOF || !isdigit(ch)) {
// error, expecting a digit or '-' here
}
do {
// convert digit from ASCII, and "shift" into accumulator
a = (a * 10) + (ch - '0');
ch = getchar();
}
while (ch != EOF && isdigit(ch));
if (negative) a = -a;
// skip spaces
while (ch != EOF && isspace(ch) && ch != '\n') ch = getchar();
// read operator
if (ch == EOF || (ch != '+' && ch != '-' && ...)) {
// error, expecting an operator
}
op = ch;
ch = getchar();
依旧......