我是一名新程序员,使用逻辑运算符时遇到了一些问题。
我制作了一个程序,该程序从用户那里获得以下数据,包括考试的主题总分不是单个科目的标记,而是添加所有这些标记并获得百分比。然后我使用逻辑运算符根据获得的百分比打印一些消息。程序正在运行,但它没有显示基于百分比的正确消息。
为什么它无法正常运行?
#include<stdio.h>
#include<stdlib.h>
int main(){
int total_marks = 0;
int no_of_subjects = 0;
int subjects[no_of_subjects];
int sum=0;
float per=0;
float gradeAplus=0;
float gradeA=0;
float gradeB=0;
float gradeC=0;
float gradeD=0;
float gradeE=0;
printf("What is the total number of subjects : ");
scanf("%d",&no_of_subjects);
printf("What is total number of marks in your exam : ");
scanf("%d",&total_marks);
gradeAplus = .9*total_marks;
gradeA = .8*total_marks;
gradeB = .7*total_marks;
gradeC = .6*total_marks;
gradeD = .5*total_marks;
gradeE = .4*total_marks;
for(int c=0;c<no_of_subjects;c++){
printf("Enter the marks of your %d subject : ",c+1);
scanf("%d",&subjects[c]);
sum+=subjects[c];
}
per = ((float)sum/(float)total_marks)*100.0;
if((per == gradeAplus)||(per > gradeAplus)){
printf("\nCongratulation you have achieve A plus grade.You are excellent.");
}
else if((per == gradeA)||((per > gradeA) && (per < gradeAplus))){
printf("\nCongratulation you have achieve A grade.You are very good.");
}
else if((per==gradeB)||((per > gradeB) && (per < gradeA))){
printf("\nWell you have achieved grade B.You are good.");
}
else if((per==gradeC)||((per > gradeC) && (per < gradeB))){
printf("\nYou have achieve grade C.You have to do some hard work.");
}
else if((per==gradeD)||((per > gradeD) && (per < gradeC))){
printf("\nYou have obtained grade D.You must work hard in order to pass the exam.");
}
else if((per==gradeE)||((per > gradeE) && (per < gradeE))){
printf("\nYou have obtained grade E.You put a really bad performance.If you do not work hard you will be fail.");
}
else if(per<gradeE){
printf("\nSorry you are fail.Try next time.\n");
}
else{
printf("You have entered wrong data.");
}
return EXIT_SUCCESS;
}
答案 0 :(得分:2)
您将grade
计算为总标记的绝对值,但稍后将其与百分比值per
进行比较。
gradeAplus = 90;
gradeA = 80;
gradeB = 70;
gradeC = 60;
gradeD = 50;
gradeE = 40;
...
per = sum * 100.0 / total_marks;
if (per >= gradeAplus) {
printf("\nCongratulation you have achieve A plus grade.You are excellent.");
}
else if (per >= gradeA) {
printf("\nCongratulation you have achieve A grade.You are very good.");
}
else if (per >= gradeB) {
...
我对逻辑进行了一些优化并删掉了一些括号。注意: clean 语法是成为优秀程序员的最佳方法。
答案 1 :(得分:1)
Hello Muslim,
当您计算else if((per==gradeE)||((per > gradeE) && (per < gradeE))){
printf("\nYou have obtained grade E.You put a really bad performance.If you do not work hard you will be fail.");
}
时间计算出错时,您在代码中的错误
和以下情况中的其他错误,
#include<stdio.h>
#include<stdlib.h>
void main(){
int total_marks = 0;
int no_of_subjects = 0;
int subjects[no_of_subjects];
int sum=0;
float per=0.0;
float gradeAplus=0;
float gradeA=0;
float gradeB=0;
float gradeC=0;
float gradeD=0;
float gradeE=0;
printf("What is the total number of subjects : ");
scanf("%d",&no_of_subjects);
printf("What is total number of marks in your exam : ");
scanf("%d",&total_marks);
gradeAplus = .9*total_marks;
gradeA = .8*total_marks;
gradeB = .7*total_marks;
gradeC = .6*total_marks;
gradeD = .5*total_marks;
gradeE = .4*total_marks;
for(int c=0;c<no_of_subjects;c++)
{
printf("Enter the marks of your %d subject : ",c+1);
scanf("%d",&subjects[c]);
sum+=subjects[c];
}
per = ((float)(sum*100.00)/(float)(total_marks*no_of_subjects));
if(per >= gradeAplus)
{
printf("\nCongratulation you have achieve A plus grade.You are excellent.");
}
else if((per >= gradeA) && (per < gradeAplus)){
printf("\nCongratulation you have achieve A grade.You are very good.");
}
else if((per >= gradeB) && (per < gradeA)){
printf("\nWell you have achieved grade B.You are good.");
}
else if((per >= gradeC) && (per < gradeB)){
printf("\nYou have achieve grade C.You have to do some hard work.");
}
else if((per >= gradeD) && (per < gradeC)){
printf("\nYou have obtained grade D.You must work hard in order to pass the exam.");
}
else if((per >= gradeE) && (per < gradeD)){
printf("\nYou have obtained grade E.You put a really bad performance.If you do not work hard you will be fail.");
}
else if(per<gradeE){
printf("\nSorry you are fail.Try next time.\n");
}
else{
printf("You have entered wrong data.");
}
}
当用户在每个主题方面输入100以上的标记(注意:如果每个科目有100个考试成绩。)时,也要给出成绩,但它无效,所以改进代码。
当用户在每个主题上方输入100以上的标记时(注意:如果每个主题的试卷100分。),请给出信息(即主题标记不在100以上)并再次要求标记。< / p>
{{1}}
答案 2 :(得分:0)
您遇到的其他问题包括:
int no_of_subjects = 0;
int subjects[no_of_subjects];
声明subjects
数组的大小为0.当您稍后更改no_of_subjects
时,这不会更改数组的大小,因此您可以使用超出范围的索引。
在获得subjects
no_of_subjects
的声明