Первый раз C класс программирования. Я должен создать калькулятор C, используя putchar/getchar. Код ниже, если у меня есть далеко и петли, чтобы попросить пользователя ввести. Проблема, с которой я сталкиваюсь, заключается в том, как учитывать пробел/несколько пробелов перед входом, пробелом/несколькими пробелами между цифрами и операндом, без пробелов между операндом и цифрами и пробелами после ввода пользователем.C Калькулятор с getchar/putchar
В настоящий момент код будет работать с одним пробелом между цифрами и операндом.
Смысл, 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;
}
В цикле 'while()' с 'case '+'' код должен выйти из цикла после '+'. Не нужно искать больше операндов. – chux
Вы должны думать, чтобы создать 2 функции для вас. Первый принимает числа, а второй принимает операнды. Оба должны позаботиться о том, если несимметричные символы, например пробелы. Ваш код будет проще, и вы сможете отлаживать его лучше. – LPs