【问题标题】:Expected expression before <= token<= 标记之前的预期表达式
【发布时间】:2014-04-17 04:39:53
【问题描述】:

当我在 linux 中编译时出现错误:

project9v2.c: In function `main`:
project9v2.c:34:33: error: expected expression before `<=` token
project9v2.c:38:33: error: expected expression before `<=` token
project9v2.c:42:33: error: expected expression before `<=` token
project9v2.c:46:33: error: expected expression before `<=` token

它扫描文件a.txt 并应该输出到b.txta.txt的内容是:

97 85 70 84 33 100 283 53 81 69 89 73 65 86 77 556 -1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *inFile, *outFile;
    int current;
    int sum = 0, b, i;
    int A=0, B=0, C=0, D=0, F=0;
    int theGrades[100];


    inFile = fopen("a.txt", "r");
    outFile = fopen("b.txt", "w");

    if (inFile != NULL) 
    {
        b = fscanf(inFile, "%d", &current); 
    }

    while(b != -1)
    {
        theGrades[sum] = current;
        sum++;
        b = fscanf(inFile, "%d", &current);
    }

    for(i=0; i<sum; i++)
    {
        if (theGrades[i] <0)
        {
            break;
        }
        else if (theGrades[i] >=90 && <=100)
        {
            A++;
        }
        else if (theGrades[i] >=80 && <=89)
        {
            B++;
        }
        else if (theGrades[i] >=70 && <=79)
        {
            C++;
        }
        else if (theGrades[i] >=60 && <=69)
        {
            D++;
        }
        else  
        {
            F++;
        }
    }
    fprintf(outFile, "The total number of grades is:" , sum);
    fprintf(outFile, "Number of A’s = %d\n" ,A);
    fprintf(outFile, "Number of B’s = %d\n" ,B);
    fprintf(outFile, "Number of C’s = %d\n" ,C);
    fprintf(outFile, "Number of D’s = %d\n" ,D);
    fprintf(outFile, "Number of F’s = %d\n" ,F);

    fclose(inFile);
    fclose(outFile);
    }

【问题讨论】:

  • linux 不是 C 编译器

标签: c arrays linux compiler-construction


【解决方案1】:

按照 c 语法,你应该像下面这样修改它。 :) 比较运算符应该用于两个变量或数字之间。

    else if (theGrades[i] >=90 && theGrades[i]<=100)
    {
        A++;
    }
    else if (theGrades[i] >=80 && theGrades[i]<=89)
    {
        B++;
    }
    else if (theGrades[i] >=70 && theGrades[i]<=79)
    {
        C++;
    }
    else if (theGrades[i] >=60 && theGrades[i]<=69)

【讨论】:

    【解决方案2】:
    if ( theGrades[i] < 0 )
        break;
    
    switch( theGrades[i] / 10 )
    {
        case 10: 
        case 9:  ++A; break;
        case 8:  ++B; break;
        case 7:  ++C; break;
        case 6:  ++D; break;
        default: ++F; 
    }
    

    【讨论】:

    • @Human:很惊讶你没有在9 等之前放置一个空格,以便这些列对齐;)
    • 哈哈哈,拍摄我应该这样做。 :) 不错的答案,顺便说一句!
    • 直到他们将等级从 55-70 改为 60-70...if..else if 塔可能更好 TBH,当然没有重复测试
    【解决方案3】:

    你需要这样写你的陈述

        else if ((theGrades[i] >=90) && (theGrades[i] <=100))
    

    为了便于阅读,您应该添加额外的括号

    【讨论】:

      【解决方案4】:

      你可能会为你的 for() 循环考虑这个逻辑:

      for(i=0; i<sum; i++)
      {
          if (theGrades[i] > 100)
             break;
      
          if (theGrades[i] >=90)
          {
              A++;
              continue;
          }
      
          if (theGrades[i] >=80)
          {
              B++;
              continue;
          }
      
          if (theGrades[i] >=70)
          {
              C++;
              continue;
          }
      
          if (theGrades[i] >=60)
          {
              D++;
              continue;
          }
          if (theGrades[i] >= 0)  
          {
              F++;
              continue;
          }
      
          break;
      }
      

      【讨论】:

      • if...else if{ continue } 更具可读性
      【解决方案5】:

      更新下一行

          else if (theGrades[i] >=90 && theGrades[i] <=100)
          {
              A++;
          }
          else if (theGrades[i] >=80 && theGrades[i] <=89)
          {
              B++;
          }
          else if (theGrades[i] >=70 && theGrades[i] <=79)
          {
              C++;
          }
          else if (theGrades[i] >=60 && theGrades[i] <=69)
      

      【讨论】:

        猜你喜欢
        • 2012-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-29
        相关资源
        最近更新 更多