题目要求:编写一个程序,显示一个菜单,为您提供加法、减法、乘法或除法的选项,获得您的选择后,该程序请求两个数,然后执行您选择的操作,该程序应该只接受它所提供的菜单选项,它应该使用float类型的数,并且如果用户未能输入数字应允许其重新输入,在除法的情况中,如果用户输入0作为第二个数,该程序应该提示用户输入一个新的值。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
float add(float a, float b);
float subtract(float a, float b);
float multiply(float a, float b);
float divide(float a, float b);
void operation_menu(void);
char get_first(void);
int main(void)
{
float num1, num2;
int ch;
char str[40];
printf_s("Enter the operation of your choice:\n");
operation_menu();
ch = get_first();
while (ch!='q')
{
switch (ch)
{
case 'a':
printf_s("Enter first number:");
while (scanf_s("%f", &num1) != 1)
{
gets_s(str);
printf_s("%s is not an number.\n", str);
printf_s("Please enter a number,such as 2.5,-1.78E8,or 3:");
}
printf_s("Enter second number:");
while (scanf_s("%f", &num2) != 1)
{
gets_s(str);
printf_s("%s is not an number.\n", str);
printf_s("Please enter a number,such as 2.5,-1.78E8,or 3:");
}
printf_s("%.2f+%.2f=%.2f\n",num1,num2,add(num1,num2));
break;
case 's':
printf_s("Enter first number:");
while (scanf_s("%f", &num1) != 1)
{
gets_s(str);
printf_s("%s is not an number.\n", str);
printf_s("Please enter a number,such as 2.5,-1.78E8,or 3:");
}
printf_s("Enter second number:");
while (scanf_s("%f", &num2) != 1)
{
gets_s(str);
printf_s("%s is not an number.\n", str);
printf_s("Please enter a number,such as 2.5,-1.78E8,or 3:");
}
printf_s("%.2f-%.2f=%.2f\n", num1, num2, subtract(num1, num2));
break;
case 'm':
printf_s("Enter first number:");
while (scanf_s("%f", &num1) != 1)
{
gets_s(str);
printf_s("%s is not an number.\n", str);
printf_s("Please enter a number,such as 2.5,-1.78E8,or 3:");
}
printf_s("Enter second number:");
while (scanf_s("%f", &num2) != 1)
{
gets_s(str);
printf_s("%s is not an number.\n", str);
printf_s("Please enter a number,such as 2.5,-1.78E8,or 3:");
}
printf_s("%.2f*%.2f=%.2f\n", num1, num2, multiply(num1, num2));
break;
case 'd':
printf_s("Enter first number:");
while (scanf_s("%f", &num1) != 1)
{
gets_s(str);
printf_s("%s is not an number.\n", str);
printf_s("Please enter a number,such as 2.5,-1.78E8,or 3:");
}
printf_s("Enter second number:");
while (scanf_s("%f", &num2) != 1)
{
gets_s(str);
printf_s("%s is not an number.\n", str);
printf_s("Please enter a number,such as 2.5,-1.78E8,or 3:");
}
if (num2==0)
{
printf_s("!Please enter another number that does't include 0.\n");
scanf_s("%f", &num2);
}
printf_s("%.2f/%.2f=%.2f\n", num1, num2, divide(num1, num2));
break;
default:
printf_s("Please enter the characters of a、s、m、d or q\n");
ch = get_first();
continue;
}
printf_s("Enter the operation of your choice:\n");
operation_menu();
ch = get_first();
}
printf_s("Bye.\n");
system("pause");
return 0;
}
char get_first(void)
{
int ch;
ch = getchar();
while (isspace(ch))
{
ch = getchar();
}
while (getchar()!='\n')
{
continue;
}
return ch;
}
void operation_menu(void)
{
printf_s("a.add s.subtract\n");
printf_s("m.multiply d.divide\n");
printf_s("q.quit\n");
}
float add(float a,float b)
{
float add_result;
add_result = a + b;
return add_result;
}
float subtract(float a, float b)
{
float sub_result;
sub_result = a - b;
return sub_result;
}
float multiply(float a, float b)
{
float mult_result;
mult_result = a*b;
return mult_result;
}
float divide(float a, float b)
{
float div_result;
div_result = a / b;
return div_result;
}
程序执行可以,请问是否可以修改程序使其更加简洁,但需要完成所有内容,并且更加用户友好。谢谢,欢迎大家讨论
2015/5/20
今日探索,代码正式修改为下面,欢迎指正
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void operation_menu();
char get_first();
float get_float();
char get_choice();
int main(void)
{
float num1, num2;
char ch;
printf_s("Enter the operation of your choice:\n");
operation_menu();
while ((ch=get_choice())!='q')
{
printf_s("Enter first number:");
num1 = get_float();
printf_s("Enter second number:");
num2 = get_float();
while (ch == 'd'&&num2 == 0)
{
printf_s("!Please enter another number that does't include 0.\n");
num2 = get_float();
}
switch (ch)
{
case 'a':
printf_s("%.2f+%.2f=%.2f\n", num1, num2, (num1 + num2));
break;
case 's':
printf_s("%.2f-%.2f=%.2f\n", num1, num2, (num1 - num2));
break;
case 'm':
printf_s("%.2f*%.2f=%.2f\n", num1, num2, (num1*num2));
break;
case 'd':
printf_s("%.2f/%.2f=%.2f\n", num1, num2, (num1 / num2));
break;
default:
break;
}
printf_s("Enter the operation of your choice:\n");
operation_menu();
}
printf_s("Bye.\n");
system("pause");
return 0;
}
//获取第一个字符
char get_first()
{
int ch;
ch = getchar();
while (isspace(ch))
{
ch = getchar();
}
while (getchar()!='\n')
{
continue;
}
return ch;
}
//打印菜单选项
void operation_menu()
{
printf_s("a.add s.subtract\n");
printf_s("m.multiply d.divide\n");
printf_s("q.quit\n");
}
//获取数字
float get_float()
{
float num;
char str[40];
while (scanf_s("%f",&num)!=1)
{
gets_s(str);
printf_s("%s is not a number.\n",str);
printf_s("Please enter a number,such as 2.5,-1.78E8,or 3:");
}
while (getchar()!='\n')
{
;
}
return num;
}
//选择函数
char get_choice()
{
char choice;
choice = get_first();
while (choice!='a'&&choice!='s'&&choice!='m'&&choice!='d'&&choice!='q')
{
printf_s("Please response with a,s,m,d or q.\n");
choice = get_first();
}
return choice;
}
你可以用C++或者C的图形库做个计算器,这样你对布局,消息机制,MVC概念都会有进一步的认识,控制台程序主要是用来验证算法的,没必要这样。