2016-08-03 109 views
0

要打印例如 等級:4/5 80% 程序問用戶他們想要多少數學問題來解決,並打印出「冤屈數/數權利「和他們的等級。我覺得我沒有我的數學就在代碼,最終導致其打印出來,例如: 等級:4/5 -7446528%的C程序:簡單的數學程序

+++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++

# include <stdio.h> 

int main() 
{ 
    int NumberOfTimes, AddAns, SubAns, AddCorrect=0, SubCorrect=0, CorrectAnsAdd, CorrectAnsSub, TotalCorrect, TotalWrong, Add; 
    int i,a,b,c,d,e,f,g; 
    float percent; 

    printf("\n"); 
    printf("-------------------MATH QUIZ------------------------\n"); 
    printf("Enter the number of Math problems you want to solve:"); //enters the # of problems the program produces 
    scanf("%d", &NumberOfTimes); 
    printf("\n"); 
    srand(time(NULL)); 
    //Random number generator 
    for (i=0;i<NumberOfTimes;++i) 
    { 
     b = rand() %3 + 1; 
     c = rand() %3 + 1; 
     a = rand() %2 + 1; 

     //Random addition problems 
     if (a == 1) 
     { 
      printf("%d + %d = ", b,c); 
      scanf("%d", &AddAns); 
      d = b + c; 
      if (AddAns == d) 
      { 
       printf(" +Correct\n"); 
       AddCorrect = AddCorrect + 1; 
      } 
      //Random subtraction problems 
      else 
      { 
       printf(" +Wrong, it was %d\n", d); 
       AddIncorrect = AddIncorrect + 1; 
      } 
     } 
     if (a == 2) 
     { 
      printf("%d - %d = ", b,c); 
      scanf("%d", &SubAns); 
      g = b - c; 
      //Produces right or wrong answers 
      if (SubAns == g) 
      { 
       printf(" +Correct\n"); 
       SubCorrect = SubCorrect + 1; 
      } 
      else 
      { 
       printf(" +Wrong, it was %d\n", g); 
       SubIncorrect = SubIncorrect + 1; 
      } 
     } 
    } 
    //Producing the output to wrong/right numbers and grade percentage 
    TotalCorrect = AddCorrect + SubCorrect; 
    printf("\n"); 
    printf("Grade: %d/%d\n",TotalCorrect,NumberOfTimes); 
    printf("\n"); 
    percent=NumberOfTimes/TotalCorrect; 
    printf("%d percent \n", percent); 
    return 0; 
} 
+0

使用'%f'打印浮點值。即'printf(「%f%\ n」,百分比);'。 – Marian

+0

你對百分比的計算是錯誤的......應該是數字正確除以問題的數量,而不是相反......並且它需要乘以100,並且其中的某些內容需要「浮動」以便它贏得不能用整數除法計算。你也需要'%f'格式說明符來打印它,而不是'%d',它是整數。 – Dmitri

+0

目前還不清楚你在問什麼。問題通常以'?'結尾。閱讀[問]。 – xenteros

回答

3

有幾件事情正在進行。首先,要用printf打印float,請使用%f%d適用於int s。

其次,當你計算percent,你意外地使用整數除法。由於NumberOfTimesTotalCorrect都是整數,因此NumberOfTimes/TotalCorrect執行整數除法併產生int。它僅在之後被轉換爲float,評估整個初始化表達式。使用這個來代替:

percent = (float)TotalCorrect/NumberOfTimes; 
// OR, if you want an actual percent: 
percent = 100.0f*TotalCorrect/NumberOfTimes; 

然後,使用%f

printf("%f percent\n", percent); // "80.000000 percent" 

注意,這將顯示出百分比很多位小數;如果你想不帶小數點清潔顯示器,你可以只計算百分比爲int

// multiply before dividing to avoid integer division problems 
int percent = 100*TotalCorrect/NumberOfTimes; 
printf("%d percent\n", percent); // "80 percent" 

希望這有助於!

1

在C中,劃分兩個整數將不會給你一個浮點數,即使賦值給一個浮點數。這就是語言的工作原理。您必須將NumberOfTimes和TotalCorrect作爲花車。

因此,與

percent=(float)TotalCorrect/(float)NumberOfTimes * 100; 

更換 percent=NumberOfTimes/TotalCorrect;此外,你想打印一個浮點數作爲一個整數線

printf("%d percent \n", percent); 

這是給你一個靠不住的結果。相反,請嘗試:

printf("%d percent \n", (int)percent); 
+0

施放其中一個會做...另一個會自動轉換。或者將乘法移到前面並使用「float」文本('100f')而不是「int」。 – Dmitri