C primer plus 第六版 第八章 第八题 编程练习答案

版权声明:转载请注明来源~ https://blog.csdn.net/Lth_1571138383/article/details/80524443

Github 地址:这里这里φ(>ω<*)

/*
    本程序应题目而建立。
  题目要求: 编写一个程序,显示一个提供 加法、减法、乘法、除法的菜单。 
                获得用户输入选项后,提示用户输入两个数字,然后执行用户刚才选择的功能。
    该程序只接受菜单提供的选项。  程序用 float 类型的变量储存用户输入数字。
   如果用户输入失败,则允许再次输入。如果用户输入 0 作为第二个数字。则提示用户重新输入一个正确的值。
*/


#include<stdio.h>


#define true 1


char Menu( void );     // 菜单 部分。
void Count( char c );     // 功能服务 部分。


int main( void )
{
char n = '\n';      // 换行符。
char get_Menu = 0;  // 接受 菜单部分 return 的 choices 。


get_Menu = Menu();


while ( get_Menu != 'q' )
{
Count( get_Menu );
putchar(n);
putchar(n);

get_Menu = Menu();
}


if ( Menu() == 'q' )
{
printf("The program is close now !\n");
}
else
{
// 空语句。
;
}


printf("Bye !\n");


return 0;
 }


char Menu( void )
{
// 菜单部分。


int i = 0;         // 循环用。
char n = '\n';        // 换行符。
char choices = 0;  // 接受用户选项

while ( i<40 )
{
printf("*");
i++;
}
putchar(n);


printf("---------------Menu---------------\n");
printf(" a. add                b. subtract\n");
printf(" c. multiply           d. divide  \n");
printf(" q. quit");

putchar(n);
putchar(n);


for ( i = 0; i < 40; i++ )
{
printf("*");
}


putchar(n);
putchar(n);


printf("Enter the operation of your choices :");
choices = getchar();


return choices;
}


void Count( char c )
{
char input = c;       // 用户输入的 choices。
char n = '\n';        // 换行符。


float value_1 = 0;    // 接受用户输入的第一个值。
float value_2 = 0;    // 接受用户输入的第二个值。  
float value = 0;      // 保存计算结果并输出。


switch ( input ) 
{
case 'a':  // 接受输入用户输入。  加法。
printf("The part of add is running now !  \n");

printf("Please input the first value  :");

while ( true )
{
// 这个循环负责第一个 value 的输入。

if ( scanf_s("%f",&value_1) == 1)
{
// 正确输入,即退出循环。
putchar(n);
break;
}
else
{
// 输入错误,则迭代,继续输入。
putchar(n);
printf("The value is wroing ! Please input again  :");
continue;
}
}
       
printf("Please input  the second value  :");

while ( true )
{
//这个循环负责第二个 value 的输入。


if ( scanf_s("%f",&value_2) == 1)
{
// 正确输入,即退出循环。
putchar(n);
break;
}
else
{
// 输入错误,则迭代,继续输入。
putchar(n);
printf("The value is wroing ! Please input again  :");
continue;
}
}


// 计算并输出结果。
value = value_1 + value_2;
printf(" %f + %f = %f  \n", value_1, value_2, value);
break;


case 'b':  // 接受输入用户输入。  减法。
printf("The part of subtract is running now !  \n");


printf("Please input the first value  :");


while (true)
{
// 这个循环负责第一个 value 的输入。


if (scanf_s("%f", &value_1) == 1)
{
// 正确输入,即退出循环。
putchar(n);
break;
}
else
{
// 输入错误,则迭代,继续输入。
putchar(n);
printf("The value is wroing ! Please input again  :");
continue;
}
}


printf("Please input  the second value  :");


while (true)
{
//这个循环负责第二个 value 的输入。


if (scanf_s("%f", &value_2) == 1)
{
// 正确输入,即退出循环。
putchar(n);
break;
}
else
{
// 输入错误,则迭代,继续输入。
putchar(n);
printf("The value is wroing ! Please input again  :");
continue;
}
}


// 计算并输出结果。
value = value_1 - value_2;
printf(" %f - %f = %f  \n", value_1, value_2, value);
break;


case 'c':  // 接受输入用户输入。  乘法。
printf("The part of multiply is running now !  \n");


printf("Please input the first value  :");


while (true)
{
// 这个循环负责第一个 value 的输入。


if (scanf_s("%f", &value_1) == 1)
{
// 正确输入,即退出循环。
putchar(n);
break;
}
else
{
// 输入错误,则迭代,继续输入。
putchar(n);
printf("The value is wroing ! Please input again  :");
continue;
}
}


printf("Please input  the second value  :");


while (true)
{
//这个循环负责第二个 value 的输入。


if (scanf_s("%f", &value_2) == 1)
{
// 正确输入,即退出循环。
putchar(n);
break;
}
else
{
// 输入错误,则迭代,继续输入。
putchar(n);
printf("The value is wroing ! Please input again :");
continue;
}
}


// 计算并输出结果。
value = value_1 * value_2;
printf(" %f * %f = %f  \n", value_1, value_2, value);
break;


case 'd':  // 接受输入用户输入。  除法。
printf("The part of add is running now !  \n");


printf("Please input the first value  :");


while (true)
{
// 这个循环负责第一个 value 的输入。


if (scanf_s("%f", &value_1) == 1)
{
// 正确输入,即退出循环。
putchar(n);
break;
}
else
{
// 输入错误,则迭代,继续输入。
putchar(n);
printf("The value is wroing ! Please input again  :");
continue;
}
}


printf("Please input  the second value ( :");


while (true)
{
//这个循环负责第二个 value 的输入。


if (scanf_s("%f", &value_2) == 1)
{
// 正确输入,且不为0的情况下,退出循环。 否则重新输入。
putchar(n);

if ( value == 0 )
{
printf("The second value can't be '0'. Please input again :");
continue;
}
else
{
// 空语句。
;
}


break;
}
else
{
// 输入错误,则迭代,继续输入。
putchar(n);
printf("The value is wroing ! Please input again  :");
continue;
}
}


// 计算并输出结果。
value = value_1 / value_2;
printf(" %f // %f = %f  \n", value_1, value_2, value);
break;


default:  // 莫名情况。退出程序。
printf("WARNING ! The program will close now !\n");
break;
}


// Program is Over !....

return 0;
}

猜你喜欢

转载自blog.csdn.net/Lth_1571138383/article/details/80524443
今日推荐