我是一名新程序員,使用邏輯運算符時遇到了一些問題。程序運行良好但邏輯運算符在C中不能正常工作
我編制了一個程序,從用戶那裏得到以下數據,包括考試科目總分數,個別科目的分數,而不是添加所有這些分數並獲得百分比。然後我使用邏輯運算符根據獲得的百分比打印一些消息。程序正在運行,但未顯示基於百分比的正確消息。
爲什麼它運行不正常?
#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;
}
這個值後會更可讀和可調試如果你打破這一關進的功能,編寫代碼這種方式難以遵循,調試移動的
subjects
的聲明,延伸。 –如果程序不打印正確的結果,該程序如何「運行良好」? – Gerhardh
您可以省略所有這些檢查:'&&(per
Gerhardh