2017-03-15 54 views
0

我正在處理一個初學C的練習,我已經完成了第一部分,我覺得非常正確(根本不算優雅);如果插入分數,則輸出正確的標記。 但我真的不能在練習的最後部分成功(加上或減去),我不明白爲什麼。 我真的很想明白爲什麼line[1]不能按預期工作。初學C練習[sscanf - fgets - if else]

的練習題的文本: 一位教授生成使用下表字母等級:

0–60 -> F 
61–70 -> D 
71–80 -> C 
81–90 -> B 
91–100 -> A 

給出一個數字等級,打印的信。 現在,根據樂譜的最後一位數字,修改上一個節目,在信件成績之後打印+或 - 。 最後位改性劑

1–3 -> "–" 
4–7 -> <blank> 
8–0 -> "+" 

例如,81 = B-,94 = A,和68 = d +。注意:F只是F.沒有F +或F-。

我所做的:

#include <stdio.h> 

char line[20];    //prepare the input from keyboard 
int score; 
char plusminus; 

int main() { 
    printf("insert your score: ");  // ask for the score 
    fgets(line, sizeof(line), stdin); 
    sscanf(line, "%d", &score); 

// check for conditions 
    if (score <= 60) { 
     printf("F"); 
    } 
    if (score <= 70 && score >60) { 
     printf("D"); 
    } 
    if (score <= 80 && score >70) { 
     printf("C"); 
    } 
    if (score <= 90 && score >80) { 
     printf("B"); 
    } 
    if (score <= 100 && score >90) { 
     printf("A"); 
    } 

// plus and minus to the mark, but didn't succed :(
    plusminus = line[1]; 
    if (plusminus < "3") { 
     printf("-"); 
    } 
    if (plusminus > "8") { 
     printf("+"); 
    } 
} 

謝謝大家!

+0

雙引號 - 「 」3「」 - 將創建一個包含'3 \ 0'的字符串('\ 0'是一個nul終止符 - C如何知道字符串的末尾在哪裏)。單引號 - 「3」 - 將字符的值存儲在內存中 - 51. – Attie

+1

這裏只是一個普遍的建議:將邏輯與輸入/輸出分開。怎麼做?創建新的功能,例如「translateScoreToGrade」,它具有整數作爲輸入,並將結構等級作爲輸出(char等級,字符修飾符)。它更加優雅,並且測試更加清潔,並且您的教授會給您更高的分數:) –

+0

您也可以將這些ifs轉換爲切換 - >(100分)/ 10,情況0 - >案例1-> B ... default-> F –

回答

0

您正在比較字符串與字符。字符串文字是指向內存中實際字符串數組的指針。一個字符是一個小整數。

您應該使用字符字面代替,使用單個像例如, '3'

0
plusminus = line[1]; 
if (plusminus < '3'&&plusminus!='0') { 
    printf("-"); 
} 
if (plusminus > '8'||plusminus=='0') { 
    printf("+"); 
} 

你還需要檢查「0」,因爲它是< 3,但輸出應該是「+」根據規則。

1

由於您已將得分作爲int,因此可以使用模數運算符%來獲得得分的餘數除以10,這與最後一位數字相同。

例如:

if((score % 10) <= 3)&&((score % 10) >= 1)) 
    { 
    printf("-"); 
    } 
0

如果你想獲得一個基地10號的最後一位數字,使用i % 10。然後你可以使用整數而不是字符來比較這個數字,這就是你正在做的事情。

1

可以簡化IFS的第一鏈:

// check for conditions 
if (score <= 60) { 
    printf("F"); 
} else if (score <= 70) { 
    printf("D"); 
} else if (score <= 80) { 
    printf("C"); 
} else if (score <= 90) { 
    printf("B"); 
} else { 
    printf("A"); 
} 

和處理+/-這樣的:

if ((score - 1) % 10 < 2) { 
    printf("-"); 
} else if ((score - 1) % 10 > 7) { 
    printf("+"); 
} 
0
#include <stdio.h> 

int main(){ 
    int grade=0; 
    printf("Enter your grade\n"); 
    scanf("%d",&grade); 

    if (grade<=60) { 
     printf("F\n"); 
    } 
    if (grade>=61^grade>=70) { 
     printf("D\n"); 
    } 
    if (grade>=71^grade>=80) { 
     printf("C\n"); 
    } 
    if (grade>=81^grade>=90) { 
     printf("B\n"); 
    } 
    if (grade>=91^grade>=100) { 
     printf("A\n"); 
    } 
    return 0; 
} 
0
#include <stdlib.h> 

char * scoreToGrade(int); 

int main(){ 
    int score; 
    printf("Enter your score:"); 
    scanf("%d",&score); 

    char * grade = scoreToGrade(score); 

    printf("Grade: %s\n", grade); 
} 

char * scoreToGrade(int score){ 
    /* 
    * 1 character for the letter 
    * 1 character for the +/- (if it's there, otherwise a terminating 
    * character, and 1 last character if there was a +/- 
    */ 
    char * result = malloc(sizeof(char) * 3); 

    if(score <= 60){ 
     result[0] = 'F'; 
    } else if(score <= 70){ 
     result[0] = 'D'; 
    } else if(score <= 80){ 
     result[0] = 'C'; 
    } else if(score <= 90){ 
     result[0] = 'B'; 
    } else { 
     result[0] = 'A'; 
    } 

    /* Handle plusses and minuses if the grade is not an F */ 
    if(result[0] != 'F'){ 
     int subscore = (score - 1) % 10; 
     if(subscore >= 7){ 
      result[1] = '+'; 
     } else if(subscore <= 2){ 
      result[1] = '-'; 
     } else { 
      result[1] = '\0'; /* Add a string terminator, making the string one character long */ 
     } 
    } else { 
     result[1] = '\0'; /* Add a string terminator, making the string one character long. This is separate because the 'F' forces a one-character grade */ 
    } 
    /* This is stuck on so that if there is a '+' or '-', the string does not go un-terminated. */ 
    result[2] = '\0'; 

    return result; 
}