gpt4 book ai didi

计算平均值等的C程序

转载 作者:行者123 更新时间:2023-11-30 19:24:04 25 4
gpt4 key购买 nike

我今天在老师的帮助下在类里面编写了这段代码,但我现在回家了,需要指导,我不知道接下来我应该做什么才能让它至少编译

目标是:

创建菜单输入一个数字(选项A)显示平均值(选项 B)显示最大和最小数字(选项C和D)显示输入的所有数字的总数(选项 E)显示输入的数字总数(选项F)然后退出(选项 G)

这是我到目前为止所拥有的,如果它很困惑,我很抱歉

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

//int getNumber (aNumber) {
// printf("Enter an integer between 0 and 1000.\n");
// scanf("%i", &aNumber);
// int result;
// }
char getMenuLetter();
int getNumber();
//declare variables

int aNumber = 0;
float avg = 0.0;
int high = -1;
int low = 1001;
int total = 0;
int count = 0;
char getChoice = 'x';

int main() {

//proptotype functions



do {
getChoice = getMenuLetter();

switch (getChoice)
case 'A':
aNumber = getNumber();
count++;
total += aNumber;
low = testLow(aNumber, low)
high = testHigh(aNumber, high);
break;
case 'B';
avg = (double) total/count; //display avg
printf("The average is %.2f", avg);
break;
case 'C':
high = getHigh();
printf("The highest value of all the numbers entered is %i.\n", high); //display highest number
break;
case 'D':
low = getLow;
printf("The lowest value of all the numbers entered is %i.\n", low); //displayer lowest value
break;
case 'E':
printf("The total of all the numbers entered is %i.\n", total);
break;
case 'F':
printf("The amount of numbers entered so far is %i.\n", count);
case 'G';
break: //end switch


} while (userChoice != 'G');

}

int testLow(int n) {
int result;

if (n < low)
result = n;
else

return 0;

} //End of main

char getMenuLetter() {
char result;
system("cls") //clear the screen.

printf("*************************************************\n");
printf("A) Enter a number between 0 and 1,000\n");
printf("B) Display the average\n");
printf("C) Display the highest value entered\n");
printf("D) Display the lowest value entered\n");
printf("E) Display the sum of all numbers\n");
printf("F) Display the count of all numbers entered\n");
printf("G) Quit the program\n");
printf("*************************************************\n");
scanf("%c", &result);
result =toupper(result);
///print f %c
//system pause

if (result != 'A' || result != 'B' || result !='C' || result !='D' || result !='E' || result != 'F' || result !='G'){
printf("You must enter A - G only! \n)");
system("pause");
} //end if

} while(result != 'A' || result != 'B' || result !='C' || result !='D' || result !='E' || result != 'F' || result !='G');
return result;
//end of GetMenuLetter

最佳答案

这是我的建议:

  1. 首先编译您的程序。您的编译器将返回大部分错误(至少是重要的错误)。
  2. 注意 curl 基数的使用。在 C(以及许多其他语言)中,编译器将线性处理其他行后面的行。大括号导致多维解释。作为编程初学者,您应该尽可能练习使用大括号,这样您就可以养成分隔指令的习惯。另外,您应该密切注意左花括号与右花括号的匹配。有关更多信息,您应该查看C Standard, 6.8: Statements and Blocks .
  3. 您的 switch() block 应以 default: 值结尾,以防您做出意外选择。
  4. 我不建议将函数原型(prototype)放入 main() 过程中。它与范围有关。请查看标准的第 6.2.1 节

2 For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces. There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)

我不知道还能告诉你什么。按顺序尝试我建议的方法。但请务必阅读该标准。最后的建议是:尝试以更有序的方式进行编程。如果您继续编码的目的是希望在完成时能够阅读到一些内容,那么您的代码就不会看起来那么草率。

祝你好运。

关于计算平均值等的C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7666444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com