C程序到计算平均值等
今天在老师的帮助下,我在课堂上写了这段代码,但我现在在家,需要指导,我不确定下一步该怎么做才能让它至少编译出来 目标是: 创建菜单 输入一个数字(选项a) 显示平均值(选项B) 显示最高和最低数字(选项C和D) 显示输入的所有数字的总数(选项E) 显示输入的数字总数(选项F) 并退出(选项G) 这是我到目前为止所拥有的,如果它很混乱,我道歉C程序到计算平均值等,c,C,今天在老师的帮助下,我在课堂上写了这段代码,但我现在在家,需要指导,我不确定下一步该怎么做才能让它至少编译出来 目标是: 创建菜单 输入一个数字(选项a) 显示平均值(选项B) 显示最高和最低数字(选项C和D) 显示输入的所有数字的总数(选项E) 显示输入的数字总数(选项F) 并退出(选项G) 这是我到目前为止所拥有的,如果它很混乱,我道歉 #include <stdio.h> #include <stdlib.h> #include <ctype.h> /
#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
#包括
#包括
#包括
//int getNumber(一个数){
//printf(“输入一个介于0和1000之间的整数。\n”);
//scanf(“%i”和&aNumber);
//int结果;
// }
char getMenuLetter();
int getNumber();
//声明变量
整数个数=0;
浮动平均值=0.0;
int高=-1;
int低=1001;
int-total=0;
整数计数=0;
char getChoice='x';
int main(){
//固有型函数
做{
getChoice=getMenuLetter();
开关(getChoice)
案例“A”:
aNumber=getNumber();
计数++;
总数+=一个数量;
低=测试低(数量,低)
高=测试高(数量,高);
打破
案例“B”;
平均值=(双精度)总计/计数;//显示平均值
printf(“平均值为%.2f”,平均值);
打破
案例“C”:
high=getHigh();
printf(“输入的所有数字的最大值为%i.\n”,高);//显示最大数字
打破
案例“D”:
low=getLow;
printf(“输入的所有数字的最低值为%i.\n”,低);//显示器最低值
打破
案例“E”:
printf(“输入的所有数字的总数为%i.\n”,总计);
打破
案例“F”:
printf(“到目前为止输入的数字数量为%i.\n”,计数);
案例“G”;
断开/结束开关
}while(userChoice!=“G”);
}
int testLow(int n){
int结果;
如果(n<低)
结果=n;
其他的
返回0;
}//主管道末端
char getMenuLetter(){
字符结果;
系统(“cls”)//清除屏幕。
printf(“*********************************************************************\n”);
printf(“A)输入一个介于0和1000之间的数字\n”);
printf(“B)显示平均值\n”);
printf(“C)显示输入的最高值\n”);
printf(“D)显示输入的最低值\n”);
printf(“E)显示所有数字的总和\n”);
printf(“F)显示输入的所有数字的计数\n”);
printf(“G)退出程序\n”);
printf(“*********************************************************************\n”);
scanf(“%c”、&result);
结果=toupper(结果);
///打印f%c
//系统暂停
如果(结果!=“A”|结果!=“B”|结果!=“C”|结果!=“D”|结果!=“E”|结果!=“F”|结果!=“G”){
printf(“您必须只输入A-G!\n)”;
系统(“暂停”);
}//如果结束,则结束
}while(result!=“A”| result!=“B”| result!=“C”| result!=“D”| result!=“E”| result!=“F”| result!=“G”);
返回结果;
//GetMenuleter的结尾
一些提示:
- 检查从第一个开始的编译器错误和警告
- 打开编译器的其他警告(例如gcc的参数
)李>-W-Wall
- C中的“
”和“;
”之间存在显著差异:
语句的主体必须用大括号括起来switch
- 首先编译程序。编译器将返回大部分错误(至少是重要错误)
- 注意使用卷曲基。在C语言(以及许多其他语言)中,编译器将线性处理紧跟其他行的行。大括号会导致多维解释。作为编程初学者,您应该尽可能地练习使用花括号,这样您就可以养成分隔指令的习惯。此外,您还应密切注意将开放式花括号与闭合式花括号进行匹配。有关更多信息,请参阅
- 您的
块应以switch()
值,以防出现意外的选择李>默认值结尾:
- 我不建议将函数原型放在
过程中。这与作用域有关。请参见本标准第6.2.1节main()
2对于识别人指定的每个不同实体,识别人
仅在程序文本区域内可见(即可以使用)
称其为范围。同一识别人指定的不同实体
或者具有不同的作用域,或者位于不同的名称空间中。那里
有四种作用域:函数、文件、块和函数
原型。(函数原型是一个函数的声明
声明其参数的类型。)
我不知道还能告诉你什么。按顺序试试我的建议。但一定要阅读标准。最后一个建议是:尝试以更有序的方式编程。如果您一直在编写代码,希望在完成时能够读懂一些东西,那么您的代码就不会显得如此草率
- 以下是我的建议:
祝你好运。你试过编译这个吗?如果是这样,你会发现你的问题在哪里。有很多。我会从这里开始,尝试找出尽可能多的错误,然后在你陷入困境的地方寻求帮助;否则返回低水平;如果要在其中使用全局变量low,那么可以删除参数L。首先我看到的是。好的,谢谢你的提示,我修复了testLow函数。我一直在思考如何为testLow编写函数,有什么建议吗?请阅读下面的答案。我给了你这么多