2017-09-01 46 views
2

我是一名新程序員,使用邏輯運算符時遇到了一些問題。程序運行良好但邏輯運算符在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; 
} 
+0

這個值後會更可讀和可調試如果你打破這一關進的功能,編寫代碼這種方式難以遵循,調試移動的subjects的聲明,延伸。 –

+1

如果程序不打印正確的結果,該程序如何「運行良好」? – Gerhardh

+0

您可以省略所有這些檢查:'&&(per Gerhardh

回答

2

你計算grade作爲totalmarks的絕對值,但後來他們用百分比值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) { 
... 

我已經優化了邏輯並刪除了一些括號。注意:A clean語法是成爲一名優秀程序員的一切方法。

+0

非常感謝現在我明白邏輯中存在問題。感謝您的時間。 – Muslim

+0

如果你保持邏輯儘可能短,它會使程序更容易維護。 – yacc

1

你好穆斯林,

你的代碼問題

你的錯誤代碼,當你計算per那段時間計算得到錯誤的。
而在這下面條件的其他錯誤,

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."); 
    } 

代碼其它問題

當用戶輸入超過100馬克(注意:如果試卷每科100馬克)每科明智所以也給等級但它是無效的,所以改善代碼。

建議

當用戶輸入標記每個科目明智以上100(注意:如果試卷每科100馬克),這樣就給消息(即主題標記不超過100),並要求重新標記。

解決方案

#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."); 
    } 

} 
+0

你好穆斯林... –

0

其中您有其他問題,此代碼:

int no_of_subjects = 0; 
int subjects[no_of_subjects]; 

聲明subjects數組的大小爲0。如果以後更改no_of_subjects,一點不改變數組的大小,然後你使用超出範圍的索引。

你需要你的no_of_subjects