Hello i have a strange question.
space is a character
so if we have
so now we have in ch = ' '. Am i wrong?
if NO then why this calculator program works if i put as example
4+5= and
4 + 5 =
i mean the spaces when we type 4 + we have a space so the operator has the space as a character and not +. I feel that is something so easy but now i can't unerstand it.Thanks
space is a character
so if we have
Code:
char ch;
scanf("%c", &ch); /*and i press space and enter*/
if NO then why this calculator program works if i put as example
4+5= and
4 + 5 =
i mean the spaces when we type 4 + we have a space so the operator has the space as a character and not +. I feel that is something so easy but now i can't unerstand it.Thanks
Code:
#include <stdio.h>
int main(void) {
int i1, i2, ok;
char operator, equals;
double res;
printf("give an expression like <number> <operator> <number> =\n");
do {
scanf("%d %c %d %c", &i1, &operator, &i2, &equals);
ok = 1;
switch(operator) {
case '+': res = i1 + i2;
break;
case '-': res = i1 - i2;
break;
case '*': res = i1 * i2;
break;
case '/':
if (i2 != 0)
res = (double)i1 / i2;
else {
printf("the divider should not be 0\n");
ok = 0;
}
break;
default:
printf("the operator is not valid\n");
ok = 0;
}
if (equals != '=') {
printf("the expression should have =\n");
ok = 0;
}
} while (!ok);
printf("%d %c %d = %f\n", i1, operator, i2, res);
}
Comment