本文介绍了你需要写一个菜单驱动的程序..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您需要编写一个菜单驱动的程序。

该程序允许用户输入五个数字,然后要求用户从菜单中选择一个选项。菜单应提供以下选项 -

1.显示输入的最小数字

2.显示输入的最大数字

3.显示输入的五个数字的总和

4.显示输入的五个数字的平均值。

5.退出

你必须使用开关 代码中的语句,用于确定要采取的操作。如果输入的选项无效,则提供错误消息。

运行程序五次,每个选项运行一次,使用无效选项运行一次。每次运行都使用以下数据集(但您的程序需要使用用户输入的任意五个数字):

18,21,17,44,9。



我的尝试:



You need to write a menu driven program.
The program allows a user to enter five numbers and then asks the user to select a choice from a menu. The menu should offer the following options –
1. Display the smallest number entered
2. Display the largest number entered
3. Display the sum of the five numbers entered
4. Display the average of the five numbers entered.
5. Exit
You must use a "switch" statement in your code to determine what action to take. Provide an error message if an invalid choice is entered.
Run the program five times, once with each option and once with an invalid option. Each run is to use the following set of data (but your program needs to work with any five numbers entered by the user):
18, 21, 17, 44, 9.

What I have tried:

/*
This is a menu driven program that prompts a user for either the smallest, largest, sum, or average for a group of 5 integers
Written by: James Raghubir
Date: July 13, 2018
*/


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


// Function Declarations
int option;
int menu(void);
int small(int u, int w, int x, int y, int z);
int large(int u, int w, int x, int y, int z);
int sum(int u, int w, int x, int y, int z);
int avg(int u, int w, int x, int y, int z);


int a;
int b;


int one;
int two;
int three;
int four;
int five;


int main(void)
{


	// Statements
	option = menu();
	printf("\n Please enter 5 integers:");
	scanf_s("%d %d %d %d %d", &one, &two, &three, &four, &five);

	switch (option)
	{
	case 1: small(one, two, three, four, five);
		break;
	case 2: large(one, two, three, four, five);
		break;
	case 3: sum(one, two, three, four, five);
		break;
	case 4: avg(one, two, three, four, five);
	}
	return 0;
}   // main


	// Functions
int menu(void)
{
	// Local Declarations
	int option;
	// Statements
	printf("\t***********************");
	printf("\n\t*        MENU          ");
	printf("\n\t*                     *");
	printf("\n\t*   1. SMALLEST       *");
	printf("\n\t*   2. LARGEST        *");
	printf("\n\t*   3. SUM            *");
	printf("\n\t*   4. AVERAGE        *");
	printf("\n\t)**********************");
	printf("\n Please type your choice: ");
	scanf_s("%d", &option);
	if (option>4)
	{
		printf("INVALID CHOICE");
		exit(0);
	}
	return option;
}


int small(int u, int w, int x, int y, int z)
{
	// Statements
	if (one < two && one < three && one < four && one < five)
		printf("%i", one);


	else if (two<one && two<three && two<four && two<five)
		printf("%i", two);


	else if (three<one && three<two && three<four && three<five)
		printf("%i", three);


	else if (four<one && four<two && four<three && four<five)
		printf("%i", four);


	else
		printf("%i", five);
	int  small = 0;
	return 0;
}


int large(int u, int w, int x, int y, int z)
{
	//Statements
	if (one > two && one > three && one > four && one > five)
		printf("%i", one);


	else if (two > one && two > three && two > four && two > five)
		printf("%i", two);


	else if (three > one && three > two && three > four && three > five)
		printf("%i", three);


	else if (four > one && four > two && four > three && four > five)
		printf("%i", four);


	else
		printf("%i", five);
	return 0;
}


int sum(int u, int w, int x, int y, int z)
{
	// Statements
	int a = one + two + three + four + five;
	printf("%i", a);
	return 0;
}


int avg(int u, int w, int x, int y, int z)
{
	// Statements
	int b = (one + two + three + four + five) /  5;
	printf("%i", b);
	return(0);
	system("pause");
}

推荐答案

option = menu();

并处理该选项:

And process that option:

switch (option)
    {
    ...
    case 5: return (0);
    }

(对于更好的应用,不要直接使用数字,使用 #define 为它们定义名称:

(For a better app, don't use numbers directly, use #define to define names for them:

#define  OPT_SMALLEST 0
#DEFINE  OPT_LARGEST  1
...
#DEFINE  OPT_EXIT     5

并在切换代码 - 它使代码更具可读性!



然后将代码放入<$ c $循环中c> main 函数,并进行测试!

And use those names in your switch code instead - it makes the code a lot more readable!

Then put the code into a loop in the main function, and get testing!



这篇关于你需要写一个菜单驱动的程序..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:02