初级C练习【sscanf - fgets - if else】

Beginner C exercise [sscanf - fgets - if else]

我正在处理初学者 C 练习,我认为我已经完成了第一部分非常正确(一点也不优雅);如果插入分数,则输出正确的分数。 但是我真的无法在练习的最后部分(加正或减)成功,我也想不通为什么。 我真的很想明白为什么 line[1] 没有按预期工作。

练习文本: 教授使用以下 table 生成字母等级:

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

感谢大家!

您正在将 字符串 与一个字符进行比较。字符串文字是指向内存中实际字符串数组的指针。一个字符是一个小整数。

您应该改用 字符 文字,使用 引号,例如'3'.

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

你还需要检查 '0' 因为它 < 3 但根据规则输出应该是 '+' 。

因为你已经得到了 int 的分数,你可以使用取模运算符 % 将分数的余数除以 10,这与上次相同数字。

例如:

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

如果要获取以 10 为基数的最后一位数字,请使用 i % 10。然后你可以使用整数而不是字符来比较这个数字,这就是你正在做的。

您可以简化第一个 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("+");
}
#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;
}
#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] = '[=10=]'; /* Add a string terminator, making the string one character long */
        }
    } else {
        result[1] = '[=10=]'; /* 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] = '[=10=]';

    return result;
}