【问题标题】:How can I shorten this code? - C Programming如何缩短此代码? - C编程
【发布时间】:2022-01-19 13:23:39
【问题描述】:

目标是提示用户输入不同科目的成绩。它将显示输入等级、数字等级和备注。之后,我使用了 do-while 循环,这样用户就可以重用该程序。我知道当您可以编写更短的代码时编写冗长的代码是愚蠢的,但问题是,我不知道该使用什么。我应该只在函数中使用if / else 语句吗?

代码运行良好,但我想寻找另一个“方法”

#include <stdio.h>

int it, prog, arts, comm, math, fil, pe;

int main()
{
  char n;

  do
  {
    printf("ACADEMIC RECORD\n");
    printf("********************************\n");
    printf("Intro to Computing: ");
    scanf("%d", &it);
    printf("Computer Programming 1: ");
    scanf("%d", &prog);
    printf("Arts Appreciation: ");
    scanf("%d", &arts);
    printf("Purposive Communication: ");
    scanf("%d", &comm);
    printf("Mathematics in the Modern World: ");
    scanf("%d", &math);
    printf("Filipino: ");
    scanf("%d", &fil);
    printf("Fundamentals of Physical Fitness: ");
    scanf("%d", &pe);

    printf("\nSubject\t\t\t\t\t\tGrade\t  Numerical Rating\tRemarks\t\n");
    if(it >=75 && it <=100)
    {
      if(it >= 75 && it <= 76)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 77 && it <= 79)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 80 && it <= 82)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 83 && it <= 85)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 86 && it <= 88)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 89 && it <= 91)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 92 && it <= 94)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 95 && it <= 97)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(it >= 98 && it <= 100)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(it <75 && it >=0)
      {
        printf("Intro to Computing\t\t\t\t %d\t\t", it);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(it >100 || it <0)
      {
        printf("Please input valid grade!\n");
      }

    if(prog >=75 && prog <=100)
    {
      if(prog >= 75 && prog <= 76){
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 77 && prog <= 79)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 80 && prog <= 82)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 83 && prog <= 85)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 86 && prog <= 88)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 89 && prog <= 91)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 92 && prog <= 94)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 95 && prog <= 97)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(prog >= 98 && prog <= 100)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(prog <75 && prog >=0)
      {
        printf("Computer Programming 1\t\t\t\t %d\t\t", prog);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(prog >100 || prog <0)
      {
        printf("Please input valid grade!\n");
      }

    if(arts >=75 && arts <=100)
    {
      if(arts >= 75 && arts <= 76){
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 77 && arts <= 79)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 80 && arts <= 82)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 83 && arts <= 85)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 86 && arts <= 88)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 89 && arts <= 91)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 92 && arts <= 94)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 95 && arts <= 97)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(arts >= 98 && arts <= 100)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(arts <75 && arts >=0)
      {
        printf("Arts Appreciation\t\t\t\t %d\t\t", arts);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(arts >100 || arts <0)
      {
        printf("Please input valid grade!\n");
      }

    if(comm >=75 && comm <=100)
    {
      if(comm >= 75 && comm <= 76){
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 77 && comm <= 79)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 80 && comm <= 82)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 83 && comm <= 85)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 86 && comm <= 88)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 89 && comm <= 91)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 92 && comm <= 94)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 95 && comm <= 97)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(comm >= 98 && comm <= 100)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(comm <75 && comm >=0)
      {
        printf("Purposive Communication\t\t\t\t %d\t\t", comm);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(comm >100 || comm <0)
      {
        printf("Please input valid grade!\n");
      }

    if(math >=75 && math <=100)
    {
      if(math >= 75 && math <= 76){
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 77 && math <= 79)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 80 && math <= 82)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 83 && math <= 85)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 86 && math <= 88)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 89 && math <= 91)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 92 && math <= 94)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 95 && math <= 97)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(math >= 98 && math <= 100)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(math <75 && math >=0)
      {
        printf("Mathematics in the Modern World\t\t\t %d\t\t", math);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(math >100 || math <0)
      {
        printf("Please input valid grade!\n");
      }

    if(fil >=75 && fil <=100)
    {
      if(fil >= 75 && fil <= 76){
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 77 && fil <= 79)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 80 && fil <= 82)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 83 && fil <= 85)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 86 && fil <= 88)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 89 && fil <= 91)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 92 && fil <= 94)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 95 && fil <= 97)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(fil >= 98 && fil <= 100)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(fil <75 && fil >=0)
      {
        printf("Kontekstwalisadong Komunikasyon sa Filipino\t %d\t\t", fil);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(fil >100 || fil <0)
      {
        printf("Please input valid grade!\n");
      }

    if(pe >=75 && pe <=100)
    {
      if(pe >= 75 && pe <= 76){
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("3.00\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 77 && pe <= 79)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("2.75\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 80 && pe <= 82)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("2.50\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 83 && pe <= 85)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("2.25\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 86 && pe <= 88)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("2.00\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 89 && pe <= 91)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("1.75\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 92 && pe <= 94)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("1.50\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 95 && pe <= 97)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", math);
        printf("1.25\t\t");
        printf("Passed\t\n");
      }
      else if(pe >= 98 && pe <= 100)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("1.00\t\t");
        printf("Passed\t\n");
      }
      }
      else if(pe <75 && pe >=0)
      {
        printf("Fundamentals of Physical Fitness\t\t %d\t\t", pe);
        printf("5.00\t\t");
        printf("Failed\t\n");
      }
      else if(pe >100 || pe <0)
      {
        printf("Please input valid grade!\n");
      }

    printf("\nDo you like to try again? ");
    printf("Press 'y' for yes, 'n' for no\n-->");
    scanf("%s", &n);
  }while(n == 'y');
  printf("OK\n");
}

【问题讨论】:

  • 如果代码有效,您应该在codereview.stackexchange.com 询问 cmets。但是,我可以在您的代码末尾看到一个明显的错误:scanf("%s", &amp;n); 将接受任意长度的字符串,这会弄乱您的堆栈并导致未定义的行为。
  • 通过使用将区间映射到数字的函数,例如 [75,76] -> 3, [77, 79] -> 2.75 等。或者可能使用公式,或两者的组合。
  • 我会尝试在所有这些范围检查中找到一个模式。也许您可以使用一个数学公式来代替?
  • 您可以考虑使用主题、乐队和分数的数组,并制定一个通用的解决方案。
  • 您可以通过删除重复的范围检查使其更具可读性。例如,对于if(prog &lt;= 76) {...} else if(prog &gt;= 77 &amp;&amp; prog &lt;= 79) {...},您已经知道 prog &gt;= 77 必须为真。这也是一种糟糕的风格,因为您使用两个 不同 '幻数' 7677 来引用 same 断点。

标签: c if-statement input conditional-statements output


【解决方案1】:

您正在寻找的是因式分解。您应该使用一个函数来计算数字等级并打印给定主题和等级的输出行,您可以将其作为参数传递。

另请注意,可以使用 及格 范围内成绩的简单公式计算数字评分:rating = 1.00 + (100 - grade) / 3 * 0.25

这是修改后的版本:

#include <stdio.h>

void print_grade(const char *subject, int grade) {
    if (grade < 0 || grade > 100) {
        printf("Invalid grade for %s: %d\n", subject, grade);
    } else {
        double rating;
        const char *pass;

        if (grade >= 75) {
            rating = 1.00 + (100 - grade) / 3 * 0.25;
            pass = "Passed";
        } else {
            rating = 5.00;
            pass = "Failed";
        }
        printf("%-45s %6d %16.2f %s\n", subject, grade, rating, pass);
    }
}

int main() {
    do {
        char n = 'n';
        int it = -1, prog = -1, arts = -1, comm = -1, math = -1, fil = -1, pe = -1;

        printf("ACADEMIC RECORD\n");
        printf("********************************\n");
        printf("Intro to Computing: ");
        scanf("%d", &it);
        printf("Computer Programming 1: ");
        scanf("%d", &prog);
        printf("Arts Appreciation: ");
        scanf("%d", &arts);
        printf("Purposive Communication: ");
        scanf("%d", &comm);
        printf("Mathematics in the Modern World: ");
        scanf("%d", &math);
        printf("Kontekstwalisadong Komunikasyon sa Filipino: ");
        scanf("%d", &fil);
        printf("Fundamentals of Physical Fitness: ");
        scanf("%d", &pe);

        printf("\n%-45s %6s %16s %s\n",
               "Subject", "Grade", "Numerical Rating", "Remarks");

        print_grade("Intro to Computing", it);
        print_grade("Computer Programming 1", prog);
        print_grade("Arts Appreciation", arts);
        print_grade("Purposive Communication", comm);
        print_grade("Mathematics in the Modern World", math);
        print_grade("Kontekstwalisadong Komunikasyon sa Filipino", fil);
        print_grade("Fundamentals of Physical Fitness", pe);

        printf("\nDo you like to try again? ");
        printf("Press 'y' for yes, 'n' for no\n-->");
        scanf(" %c", &n);
    } while (n == 'y');

    printf("OK\n");
    return 0;
}

可以使用一系列科目和成绩进一步减少代码:

int main() {
#define NCOURSES  7
    const char *subject[NCOURSES] = {
        "Intro to Computing",
        "Computer Programming 1",
        "Arts Appreciation",
        "Purposive Communication",
        "Mathematics in the Modern World",
        "Kontekstwalisadong Komunikasyon sa Filipino",
        "Fundamentals of Physical Fitness",
    };
    int grade[NCOURSES];

    do {
        char n = 'n';
        int i;

        printf("ACADEMIC RECORD\n");
        printf("********************************\n");
        for (i = 0; i < NCOURSES; i++) {
            printf("%s: ", subject[i]);
            if (scanf("%d", &grade[i]) != 1) {
                printf("input error\n");
                break;
            }
        }
        if (i < NCOURSES)
            break;
        printf("\n%-45s %6s %16s %s\n",
               "Subject", "Grade", "Numerical Rating", "Remarks");
        for (i = 0; i < NCOURSES; i++) {
            print_grade(subject[i], grade[i]);
        }

        printf("\nDo you like to try again? ");
        printf("Press 'y' for yes, 'n' for no\n-->");
        scanf(" %c", &n);
    } while (n == 'y');

    printf("OK\n");
    return 0;
}

【讨论】:

    【解决方案2】:
    #include<stdio.h>
    
      
    void calculateGrades(int marks, char* subject);
    int main()
    {
      char n;
      int marks[7];
      char subjects[7][50] = {
                             "Intro to Computing",
                             "Computer Programming",
                             "Arts Appreciation",
                             "Purposive Communication",
                             "Mathematics in the Modern World",
                             "Kontekstwalisadong Komunikasyon sa Filipino",
                             "Fundamentals of Physical Fitness"
                         };
    
      do
      {
        printf("ACADEMIC RECORD\n");
        printf("********************************\n");
        for(int i=0;i<2;i++){
            printf("%s : ",&subjects[i]);
            scanf("%d", &marks[i]);
        
        }
        
        printf("\nSubject\t\t\t\t\t\tGrade\t  Numerical Rating\tRemarks\t\n");
        
        for(int i=0;i<2;i++){
            calculateGrades(marks[i],subjects[i]);
        }
        
        printf("\nDo you like to try again? ");
        printf("Press 'y' for yes, 'n' for no\n-->");
        scanf("%s", &n);
      }while(n == 'y');
      printf("OK\n");
      }
      
      
    void calculateGrades(int mark, char* subject){
        
        printf("%s\t\t\t\t %d\t\t",subject,mark);
        if(mark >= 98 && mark <= 100)
        {
            printf("1.00\t\t");
            printf("Passed\t\n");
        }
        else if (mark >= 95 ){
            printf("1.25\t\t");
            printf("Passed\t\n");
        }
        else if (mark >= 92 ){
            printf("1.50\t\t");
            printf("Passed\t\n");
        }
        else if (mark >= 89 ){
            printf("1.75\t\t");
            printf("Passed\t\n");
        }
        else if (mark >= 86 ){
            printf("2.00\t\t");
            printf("Passed\t\n");
        }
        else if (mark >= 83 ){
            printf("2.25\t\t");
            printf("Passed\t\n");
        }
        else if (mark >= 80 ){
            printf("2.50\t\t");
            printf("Passed\t\n");
        }
        else if (mark >= 77 ){
            printf("2.75\t\t");
            printf("Passed\t\n");
        }
        else if (mark >= 75 ){
            printf("3.00\t\t");
            printf("Passed\t\n");
        }
        else if (mark < 75){
            printf("5.00\t\t");
            printf("Failed\t\n");
        }
        else{
            printf("Please input valid grade!\n");
        }
        
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多