【问题标题】:Beginner C exercise [sscanf - fgets - if else]初学者 C 练习 [sscanf - fgets - if else]
【发布时间】:2017-08-06 02:08:15
【问题描述】:

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

练习的文本: 一位教授使用下表生成字母等级:

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" - 将创建一个包含 3\0 的字符串(\0 是一个空终止符 - C 如何知道字符串的结尾在哪里)。单引号 - '3' - 会将字符的值放入内存中 - 51。
  • 这里只是一个一般性建议:将逻辑与输入/输出分开。怎么做?创建新的功能,例如“translateScoreToGrade”,它有整数作为输入,结构等级作为输出(字符等级,字符修饰符)。它更优雅,测试更干净,你的教授会给你更高的分数:)
  • 您也可以将这些 if 转换为 switch -> (100 - score)/10, case 0 ->A case 1->B ... default->F
  • if (plusminus &lt; "3") { 不会生成编译器警告吗?如果没有,请启用所有编译器警告——这样可以节省时间。

标签: c if-statement char fgets scanf


【解决方案1】:
#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;
}

【讨论】:

    【解决方案2】:
    #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;
    }
    

    【讨论】:

      【解决方案3】:

      您可以简化第一个 if 链:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          由于您已经获得了int 的分数,因此您可以使用模数运算符% 将分数的余数除以10,这与最后一位数字相同。

          例如:

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

          【讨论】:

            【解决方案6】:
               plusminus = line[1];   
            if (plusminus < '3'&&plusminus!='0') {
                printf("-");
            }
            if (plusminus > '8'||plusminus=='0') {
                printf("+");
            }
            

            您还需要检查'0',因为它是

            【讨论】:

              【解决方案7】:

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-02-19
                • 1970-01-01
                • 2013-01-08
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多