有人可以帮忙吗? [英] Can someone help with this?

查看:56
本文介绍了有人可以帮忙吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题描述

编写一个菜单驱动程序,允许用户输入五个数字,然后在找到最小,最大,总和或平均值之间进行选择。使用switch语句确定要采取的操作。如果输入了无效数据,请提供

错误消息。如果输入了无效数据,则主函数应返回整数

值1.否则,返回整数值0.

示例交互

$ ./a.out

输入五个数字,用空格分隔:18 21 7 54 Z

对不起。你输入的东西我不明白。

$ echo $?

1

$。/ a.out

输入五个数字,用空格分隔:18 21 7 54 9

a。最小的价值

b。最大价值

c。价值总和

d。平均值

输入选择? 1

对不起。你输入的东西我不明白。

$ echo $?

1

$。/ a.out

输入五个数字,用空格分隔:18 21 7 54 9

a。最小的价值

b。最大价值

c。价值总和

d。平均值

输入选择? A

最小值为7.

$ echo $?

0



我尝试过:



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

使用 命名空间标准;

// 函数声明
int 选项;
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 one;
int 两个;
int 三;
int 四;
int 五;

int main( void
{
// 本地声明
printf( \ n输入五个数字,用空格分隔:);
scanf( %d%d%d%d%d,& one ,& two,& three,& four,& five);

// 语句
printf( \ n \ a a。最小值);
printf( \ n \ t b。最大值);
printf( \ n\t c。值的总和);
printf( \ n \ d d。值的平均值);

printf( \ n输入选项?);
scanf( %d,& option);

// 返回选项;
switch (option)
{
case ' A':cout<< 最小的值是<< small(一,二,三,四,五);
break ;
case ' B' :cout<< 最大值是<< large(一,二,三) , 四五);
break ;
case ' C' :cout<< 总和值为<< sum(一,二,三) , 四五);
break ;
case ' D' :cout<< avergae值为<< avg(一,二,三) , 四五);
break ;
默认:cout<< \\对不起。你输入的东西我不明白。;
}

return 0 ;
}

int small( int u, int w, int x, int y, int z)
{
if (一个< two&& one< three &&&< 4&& one< 5)
printf( %d,one);
else if (两个< 1&& two< three&& two< ;&&&<<<<"< 5>
printf( %d,两个);
else if (三个< 1&& three< two&& three< ;四&&&三<五<五<五)
printf( %d,三);
else if (四个< 1&& 4< two&& 4< ;&&& 4< 5< 5< 5>
printf( %d,四);
else
printf( %d,五);

return 0 ;
}

int large( int u, int w, int x, int y, int z)
{
if (一个>两个&&一个>三个&&>一个>四&&一个>五个)
printf( %d,one);
else if (两个>一&& two>三&&>> ; 4&& 2> 5)
printf( %d,两个);
其他 如果(三个>一个&&三>二&&三> ;四&&三>五)
printf( %d,三);
其他 如果(四个>一个&& 4>两个&&四> ; 3&& 4> 5)
printf( %d,四);
else
printf( %d,五);

return 0 ;
}

int sum( int u, int w, int x, int y, int z)
{
return (u + w + x + y + z) ;
}

int avg( int u, int w, int x, int y, int z)
{
return ((u + w + x + y + z )/ 5);
}

解决方案

./ a.out

输入五个数字,用空格分隔: 18 21 7 54 Z

对不起。你输入了一些我不理解的东西。


echo




1

Problem description
Write a menu-driven program that allows a user to enter five numbers and then choose between finding the
smallest, largest, sum, or average. Use a switch statement to determine what action to take. Provide an
error message if invalid data is entered. If invalid data is entered, your main function should return the integer
value 1. Otherwise, return the integer value 0.
Sample interaction
$ ./a.out
Enter five numbers, separated with spaces: 18 21 7 54 Z
Sorry. You entered something I don’t understand.
$ echo $?
1
$ ./a.out
Enter five numbers, separated with spaces: 18 21 7 54 9
a. Smallest value
b. Largest value
c. Sum of values
d. Average of values
Enter choice? 1
Sorry. You entered something I don’t understand.
$ echo $?
1
$ ./a.out
Enter five numbers, separated with spaces: 18 21 7 54 9
a. Smallest value
b. Largest value
c. Sum of values
d. Average of values
Enter choice? A
The smallest value is 7.
$ echo $?
0

What I have tried:

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

using namespace std;
 
//Function Declarations
int option;
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 one;
int two;
int three;
int four;
int five;

int main(void)
{
  //Local Declarations
  printf("\n Enter five numbers, separated with spaces:");
  scanf ("%d %d %d %d %d", &one, &two, &three, &four, &five);
 
  //Statements
  printf("\n\t   a. Smallest value       ");
  printf("\n\t   b. Largest value        ");
  printf("\n\t   c. Sum of values        ");
  printf("\n\t   d. Average of values    ");

  printf("\n Enter choice? ");
  scanf ("%d", &option);

  // return option;
  switch(option)
  {
    case 'A': cout<<"The smallest value is " <<small(one, two, three, four, five);
            break;
    case 'B': cout<<"The largest value is " <<large(one, two, three, four, five);
            break;
    case 'C': cout<<"The sum value is " <<sum(one, two, three, four, five);
            break;
    case 'D': cout<<"The avergae value is " <<avg(one, two, three, four, five);
            break;
    default : cout<<"\n Sorry. You entered something I don’t understand.";
  }
  
  return 0;
} 
 
int small(int u, int w, int x, int y, int z)
{
  if (one<two && one<three && one<four && one<five)
    printf("%d", one);
  else if (two<one && two<three && two<four && two<five)
    printf("%d", two);
  else if (three<one && three<two && three<four && three<five)
    printf("%d", three);
  else if (four<one && four<two && four<three && four<five)
    printf("%d", four);
  else
    printf("%d", five);

  return 0;
}
 
int large(int u, int w, int x, int y, int z)
{
  if (one>two && one>three && one>four && one>five)
    printf("%d", one);
  else if (two>one && two>three && two>four && two>five)
    printf("%d", two);
  else if (three>one && three>two && three>four && three>five)
    printf("%d", three);
  else if (four>one && four>two && four>three && four>five)
    printf("%d", four);
  else
    printf("%d", five);

  return 0;
}
 
int sum(int u, int w, int x, int y, int z)
{
  return(u+w+x+y+z);
}
 
int avg(int u, int w, int x, int y, int z)
{
  return((u+w+x+y+z)/5);
}

解决方案

./a.out
Enter five numbers, separated with spaces: 18 21 7 54 Z
Sorry. You entered something I don’t understand.


echo


?
1


这篇关于有人可以帮忙吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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