Code:
#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");
}
elseif(!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");
}
elseif(!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);
}
elseif(operand =='-'){
answer = subtract(input1, operand, input2);
printf("%d", answer);
}
elseif(operand =='%'){
answer = mod(input1, operand, input2);
printf("%d", answer);
}
elseif(operand =='*'){
answer = multiply(input1, operand, input2);
printf("%d", answer);
}
elseif(operand =='/'){
answer = divide(input1, operand, input2);
printf("%d", answer);
}
flag = cont();
}
while(flag =='y'|| flag =='Y');
return0;
}
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;
}