[英]C Calculator With getchar/putchar
第一次有C编程课。 我必须使用putchar / getchar创建一个C计算器。 下面的代码,如果我有远,并循环,要求用户输入。 我遇到的问题是如何在输入之前考虑空格/多个空格,数字和操作数之间的空格/多个空格,操作数和数字之间的空格以及用户输入后的空格。
现在代码将使用数字和操作数之间的一个空格。
含义,10 + 5作品。 但是,例如10 + 5无效,____ 5 + 10(其中__ =空格),10 + 5 ______或10 _________ + 10无效。
任何有关如何在数字和操作数之间以及在任何用户输入之前考虑多个空格的建议或帮助都非常受欢迎。
非常感谢您对当前代码的任何和所有帮助。 真的很感谢你的帮助和时间!
#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);
char cont(void);
int main()
{
int answer = 0;
int ch = 0;
int input1 = 0;
char operand = 0;
int input2 = 0;
int function = 0;
char flag;
do {
input1 = 0, input2 = 0, operand = 0;
printf("\nPlease enter a calculation to be made.\n");
while (((ch = getchar()) != ' ') && (ch != EOF) && (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 != EOF) && (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);
}
flag = cont();
}
while (flag == 'y' || flag == 'Y');
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;
}
char cont()
{
char flag;
printf("\nDo you want to process another calculation (y/n)? ");
scanf("%c%*c", &flag);
return flag;
}
好! 这个问题的解决方案涉及了解getchar()
工作原理。 它还包括标准输入[ stdin
]的输入如何工作的概念。
因此,每当使用scanf()/ getchar()等对变量进行输入时,都需要执行任一操作,以提醒编译器已确认变量的输入。 一个是新行\\n
(按Enter键),另一个是white-space
。 他们将警告输入已完成。
这可以通过输入10 + 5 [Press Enter]
。 第一个空格确保ch的值为10
,因此input1
的值为10。然后+
通过,空格之后, operand
获得+
的值。 稍后,在写入5
之后按Enter。 这将在input2
保存值5
。 其余操作会完美进行。
在我们有这么多空格____5 + 10
10+5______
/ 10_________+ 10
其他情况下,我们必须看看代码将如何遵循这些输入。
_____5+10
第一个空格将错过第一个while
循环,因为空格是退出条件。 然后,控制转到第二个while
循环,其中第二个空格也将是该循环的退出条件。 第三个while
循环也是如此,它也会退出。 因此,答案将是input1 operand input2
并且所有这些都具有默认值,即0,0(当按比例缩小为字符时为空白)和0。 10+5_____
这里第一个空格出现在5
之后,因此, getchar()
将为input1
提供值10+5
而其他两个变量甚至不会被赋值。 这说明了为什么无法为变量分配我们期望的值。 此问题的解决方案可以通过额外的处理来解决,但要检查空格的显示方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.