69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
int a;
// TEST11.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
void input();
void add();
void subtract();
void multiply();
void divide();
float a[2]; //必须定义为全局变量
int main()
{
char ch;
while(1)
{
printf("Enter the operation of your choice:\n");
printf("a.add s.subtract\n");
printf("b.multiply d.divide \nq.quit\n");
fflush(stdin);
ch=getchar();
if(ch == 'q')
break;
input();
switch(ch)
{
case 'a':add();
break;
case 's':subtract();
break;
case 'b':multiply();
break;
case 'd':divide();
break; //这少了个Break
default:
printf("input error,please input 'a' 's' 'b' 'd' 'q '\n");
}
}
printf("bye.\n");
return 1;
}
void input()
{
int m,n;
printf("Enter first number:");
do
{
m=scanf("%f",&a[0]);
if(m==0)
{
printf("input errror!please enter an number such as 2.5,-2 or -1.788e2\n");
continue;
}
else
{
printf("Enter second number:");
n=scanf("%f",&a[1]);
if(n==0)
{
printf("input errror!please enter an number such as 2.5,-2 or -1.788e2\n");
continue;
}
}
}while(m*n==0);
}
void add()
{
printf("%f + %f =%f \n",a[0],a[1],a[0]+a[1]);
}
void subtract()
{
printf("%f - %f =%f \n",a[0],a[1],a[0]-a[1]);
}
void multiply()
{
printf("%f * %f =%f \n",a[0],a[1],a[1]*a[0]);
}
void divide()
{
while(a[1]==0)
{
printf("enter a number other than 0:");
scanf("%f",&a[1]);
}
printf("%f / %f =%f \n",a[0],a[1],a[1]/a[0]);
}
while(ch!='q');