#include<stdio.h>
#include<stdlib.h>
float add (void);
float subtract (void);
float multiply (void);
float divide (void);
char choice (void);
int main()
{int ch;
while ((ch=choice())!='q')
{
switch (ch)
{
case 'a': add();
break;
case 's':subtract();
break;
case 'm':multiply();
break;
case 'd':divide();
break;
default:
break;
}
}
printf("bye\n");
system("pause");
return 0;
}
char choice (void)
{
char ch;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
ch=getchar();
while (getchar()!='\n')
{
continue;
}
return ch;
}
float add(void)
{float a,b;
float e;
char ch;
printf("Enter first number:");
while (scanf("%f",&a)!=1)
{ while ((ch=getchar())!='\n')
{
putchar(ch);
}
printf("is not a number,");
printf("you are wrong,enter again:");
}
printf("Enter second number:");
while (scanf("%f",&b)!=1)
{
printf("you are wrong,enter again:");
}
e=a+b;
printf("%f + %f = %f\n",a,b,e);
return e;
}
float subtract (void)
{float a,b;
float e;
printf("Enter first number:");
while (scanf("%f",&a)!=1)
{
printf("you are wrong,enter again:");
}
printf("Enter second number:");
while (scanf("%f",&b)!=1)
{
printf("you are wrong,enter again:");
}
e=a-b;
printf("%f - %f = %f\n",a,b,e);
return e;
}
float multiply (void)
{float a,b;
float e;
printf("Enter first number:");
while (scanf("%f",&a)!=1)
{
printf("you are wrong,enter again:");
}
printf("Enter second number:");
while (scanf("%f",&b)!=1)
{
printf("you are wrong,enter again:");
}
e=a*b;
printf("%f * %f = %f\n",a,b,e);
return e;
}
float divide (void)
{
float a,b;
float e;
printf("Enter first number:");
while (scanf("%f",&a)!=1)
{
printf("you are wrong,enter again:");
}
printf("Enter second number:");
while (scanf("%f",&b)!=1)
{
printf("you are wrong,enter again:");
}
if (b==0)
{
printf("Enter a number other than 0:");
scanf("%f",&b);
}
e=a/b;
printf("%f / %f = %f\n",a,b,e);
return e;
}