zhuwei9999 发表于 2020-1-4 14:54:51

switch报错

下面这段代码计算某一天是一年中的第几天。switch的第七个case开始报错。删掉case 7 ~ case 12则可以运行。
报错:||=== Build: Debug in project (compiler: GNU GCC Compiler) ===|
D:\myCODE\c\project\main.c||In function 'Days':|
D:\myCODE\c\project\main.c|19|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|19|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|20|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|20|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|21|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|21|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|22|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|22|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|23|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|23|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c|24|error: lvalue required as increment operand|
D:\myCODE\c\project\main.c|24|error: expected ';' before numeric constant|
D:\myCODE\c\project\main.c||In function 'main':|
D:\myCODE\c\project\main.c|42|error: expected expression before '=' token|
||=== Build failed: 13 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>

int Days(int *a, int *b);
int Days(int *a, int *b){
    unsigned int x = *a;
    int y = *b;
    int days;
    switch(x){
      case 1: days = y;break;
      case 2: days = 31+y;break;
      case 3: days = 31+28+y;break;
      case 4: days = 31+28+31+y;break;
      case 5: days = 31+28+31+30+y;break;
      case 6: days = 31+28+31+30+31+y;break;
      case 7: days = 31+28+31+30++31+30+y;break; //从这里开始会报错
      case 8: days = 31+28+31+30++31+30+31+y;break;
      case 9: days = 31+28+31+30++31+30+31+31+y;break;
      case 10: days = 31+28+31+30++31+30+31+31+30+y;break;
      case 11: days = 31+28+31+30++31+30+31+31+30+31+y;break;
      case 12: days = 31+28+31+30++31+30+31+31+30+31+30+y;break;
      default: break;
    }
    return days;
}

int main(void)
{
    int year, month, day;
    int DAY;
    printf("please enter year-month-day.eg: 1995-5-3");
    scanf("%d-%d-%d",&year, &month, &day);
    if(year%4 ==0 || year%400 == 0){
      DAY = Days(&month, &day) + 1;
      printf("this is the %d th day of %d", DAY, year);
    }
    else
      printf("this is the %d th day of %d", Days(&month, &day), year);
    return = 0;
}


zltzlt 发表于 2020-1-4 14:57:42

代码有问题,试试这样:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>

int Days(int *a, int *b);
int Days(int *a, int *b){
    unsigned int x = *a;
    int y = *b;
    int days;
    switch(x){
      case 1: days = y;break;
      case 2: days = 31+y;break;
      case 3: days = 31+28+y;break;
      case 4: days = 31+28+31+y;break;
      case 5: days = 31+28+31+30+y;break;
      case 6: days = 31+28+31+30+31+y;break;
      case 7: days = 31+28+31+30+31+30+y;break;
      case 8: days = 31+28+31+30+31+30+31+y;break;
      case 9: days = 31+28+31+30+31+30+31+31+y;break;
      case 10: days = 31+28+31+30+31+30+31+31+30+y;break;
      case 11: days = 31+28+31+30+31+30+31+31+30+31+y;break;
      case 12: days = 31+28+31+30+31+30+31+31+30+31+30+y;break;
      default: break;
    }
    return days;
}

int main(void)
{
    int year, month, day;
    int DAY;
    printf("please enter year-month-day.eg: 1995-5-3");
    scanf("%d-%d-%d",&year, &month, &day);
    if(year%4 ==0 || year%400 == 0){
      DAY = Days(&month, &day) + 1;
      printf("this is the %d th day of %d", DAY, year);
    }
    else
      printf("this is the %d th day of %d", Days(&month, &day), year);
    return 0;
}

zhuwei9999 发表于 2020-1-4 15:03:36

zltzlt 发表于 2020-1-4 14:57
代码有问题,试试这样:

谢谢,你这样是正确运行的。请问改动了哪里?

zltzlt 发表于 2020-1-4 15:05:57

zhuwei9999 发表于 2020-1-4 15:03
谢谢,你这样是正确运行的。请问改动了哪里?

      case 7: days = 31+28+31+30++31+30+y;break;
      case 8: days = 31+28+31+30++31+30+31+y;break;
      case 9: days = 31+28+31+30++31+30+31+31+y;break;
      case 10: days = 31+28+31+30++31+30+31+31+30+y;break;
      case 11: days = 31+28+31+30++31+30+31+31+30+31+y;break;
      case 12: days = 31+28+31+30++31+30+31+31+30+31+30+y;break;

还有程序最后:

return = 0;

zhuwei9999 发表于 2020-1-4 15:07:04

zltzlt 发表于 2020-1-4 15:05
case 7: days = 31+28+31+30++31+30+y;break;
      case 8: days = 31+28+31+30++31+30+31 ...

谢谢,我太粗心了
页: [1]
查看完整版本: switch报错