首页 > 代码库 > 题8.11.8答案和解析

题8.11.8答案和解析

题:编写一个程序,现实一个菜单,为您提供加法、减法、乘法或除法的选项。获得您的选择后该程序请求两个数,然后执行您选择的操作。该程序应该只接受它所提供的菜单选项。它应该使用float类型的数,并且如果用户为能输入数字允许其重新输入。在除法的情况中,如果用户输入0作为第二个数,该程序应该提示用户输入一个新的值。一个典型的程序运行应该如下所示:

Enter the operation of you choice:
a.add    s.subtract
m.multiply    d.divide
q.quit
a
Enter first number : 22.4
Enter second number :one
one is not an number.
Please enter a number . such as 2.5,-1.78E8, or 3: 1
22.4+1=23.4
Enter the operation of you choice:
a.add    s.subtract
m.multiply    d.divide
q.quit
d
Enter first number : 18.4
Enter second number: 0
Enter a number other than 0:0.2
18.4/0.2=92
Enter the operation of you choice:
a.add    s.subtract
m.multiply    d.divide
q
Bye.

我的答案如下:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

void menu();
bool menu_switch(char input);
char menu_input();
float get_num();
void add();
void subtract();
void multiply();
void divide();
void clear_buffer();        //为了以防万一而设置

int main(void){
        bool _exit;
    for(_exit=false;menu(),(_exit=menu_switch(menu_input()))!=true;)       /*获取选择项的返回值赋予_exit 进行判断是否退出*/
    continue; 
    printf("Bye.\n");
    return 0;
}

void menu(){
    printf("a.add\t\ts.subtract\n");
    printf("m.mulyiply\td.divide\n");
    printf("q.quit\n");
    printf(">>>");
}

bool menu_switch(char input){
    switch(input){
        case ‘a‘:
            add();
            clear_buffer();        //为保证缓冲区绝对干净,每个计算函数结束后都进行一次清除,以此来清除选择选项或者在输入数值时缓冲区内的残余数据
            return false;

        case ‘s‘:
            subtract();
            clear_buffer();
            return false;

        case ‘m‘:
            multiply();
            clear_buffer();
            return false;

        case ‘d‘:
            divide();
            clear_buffer();
            return false;

        case ‘q‘:
            return true;

        default:
            printf("[Please Enter true option]\n");
            return false;
    }
}

char menu_input(){
    int ch;
    char output;
    while((ch=getchar())!=‘\n‘){
        output=(char)ch;            //获取到储存在ch里的第一个字符的ASCII数值强制转换成char类型储存在output里面
        clear_buffer();                //清除缓冲区内的剩余内容
        return output;                //返回output值给menu_switch进行选择判定
    }
}

float get_num(){
    float input;
    int ch;
    while(scanf("%f",&input)!=1){            //判断是否输入的值是float数值,否则进入循环提示用户输入数值
        while((ch=getchar())!=‘\n‘)
            putchar(ch);
        printf(" : Please enter true number (such as 3,-13,2E6) : ");
    }
    return input;
}

void add(){
    float Fnum,Snum;
    printf("Please enter first number >>> ");
    Fnum = get_num();
    printf("Please enter second number >>> ");
    Snum = get_num();
    printf("%.2f + %.2f = %.2f\n",Fnum,Snum,Fnum + Snum);
}

void subtract(){
    float Fnum,Snum;
    printf("Please enter first number >>> ");
    Fnum = get_num();
    printf("Please enter second number >>> ");
    Snum = get_num();
    printf("%.2f - %.2f = %.2f\n",Fnum,Snum,Fnum - Snum);
}

void multiply(){
    float Fnum,Snum;
    printf("Please enter first number >>> ");
    Fnum = get_num();
    printf("Please enter second number >>> ");
    Snum = get_num();
    printf("%.2f * %.2f = %.2f\n",Fnum,Snum,Fnum * Snum);
}

void divide(){
    float Fnum,Snum;
    printf("Please enter first number >>> ");
    while((Fnum = get_num())==0){                    //判断是否为0,若为0则提示用户重新输入
        printf("Enter the number than 0 : ");       
        continue;
    }
    printf("Please enter second number >>> ");
    while((Snum = get_num())==0){
        printf("Enter the number than 0 : ");
        continue;
    }
    printf("%.2f / %.2f = %.2f\n",Fnum,Snum,Fnum / Snum);
}

void clear_buffer(){
    while(getchar()!=‘\n‘)
        continue;
}

函数说明:

  • main() 顺序控制
  • menu(); 显示菜单内容
  • menu_switch(); 选择项选择
  • menu_input(); 选择项输入
  • get_num(); 数值输入
  • add(); 加法
  • subtract(); 减法
  • multiply(); 乘法
  • divide(); 除法
  • clear_buffer(); 强制读出缓冲区内的内容

流程图如下

流程图

代码公开