你需要写一个菜单驱动的程序.. [英] You need to write a menu driven program..

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

问题描述

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

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

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");
}

推荐答案

从逻辑上看你的代码和事情是做什么的:将它与它的实际效果进行比较!

这个问题要求你从用户那里得到五个数字,然后进入一个菜单系统 - 你不这样做,它要求选择菜单,然后获取值。另外,5是问题中的一个值选择,但不在代码中!



首先从用户获取值,并使用 menu 函数返回有效选项1到5而不是使用全局变量。如果用户输入了无效值,那么 menu 会告诉他,并且直到他这样做才会返回。

然后在你的主函数中调用它:

Look at your code and thing logically about what it is doing: compare that to what it actually does!
The question asks you to get five numbers from the user, then enter a menu system - your's doesn't do that, it asks for a menu choice, then gets the values. In addition, "5" is a value choice in the question, but not in the code!

Start by getting the values from the user, and having the menu function return the valid option 1 to 5 instead of using a global variable. If the user enters an invalid value, then menu tells him so, and does not return until he does.
Then call that in your main function:
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!


一个不错的开始,但接下来你需要使用调试器使其运行更顺畅并找到有问题的代码。以下是调试器教程
A nice start but at next you need to use the debugger to make it smoother running and find the problematic code. Here is some debugger tutorial.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆