鱼C论坛

 找回密码
 立即注册
查看: 1566|回复: 3

[已解决]我也不知道这是什么错误

[复制链接]
发表于 2019-7-24 16:17:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 df3379 于 2019-7-24 16:17 编辑
  1. /*
  2. 要求:设计一个程序,
  3. */

  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<string.h>

  7. struct NODE/*定义结构体类型,
  8.   这只是一个类型,不是结构体变量
  9.   */
  10. {
  11.     char name[20];
  12.         int age;
  13.         char sex;
  14.         char num[20];
  15.         struct NODE *next;//用于保存下一个结构体变量的地址,指向下一个结构体变量
  16. };

  17. struct NODE *CreateLink(void);//函数声明,创建链表
  18. void Init(struct NODE *);//函数声明,链表结点初始化
  19. void OutputLink(struct NODE *);//函数声明,输出链表
  20. void InsertNode(struct NODE *);//函数声明,插入结点

  21. int main(void)
  22. {
  23.     char ch='\0';//用于判断是否执行相关程序
  24.         struct NODE *head=NULL;//定义指向空的struct NODE型结构体变量的头指针

  25.         //以下创建链表
  26.         printf("是否创建当前链表?(Y/N)");
  27.         while(1)
  28.         {
  29.             scanf("%c",&ch);
  30.                 getchar();//吸收回车,
  31.                 if(('Y'==ch)--('y'==ch))//提示: error C2105: '--' needs l-value
  32.                 {
  33.                     head=CreateLink();
  34.                         Init(head);
  35.                         OutputLink(head);
  36.                         break;//执行完毕退出创建链表
  37.                 }
  38.                 else if(('N'==ch)--('n'==ch))
  39.                 {
  40.                     return 0;
  41.                 }
  42.                 else
  43.                 {
  44.                     printf("请重新输入(Y/N):");
  45.                 }
  46.         }

  47.         //以下插入结点
  48.         printf("是否要插入结点?(Y/N)");
  49.         ch='\0';
  50.         while(1)
  51.         {
  52.             scanf("%c",&ch);
  53.                 getchar();
  54.                 if(('Y'==ch)--('y'==ch))
  55.                 {
  56.                     InsertNode(head);
  57.                         OutputLink(head);
  58.                         break;
  59.                 }
  60.                 else if(('N'==ch)--('n'==ch))
  61.                 {
  62.                     break;
  63.                 }
  64.                 else
  65.                 {
  66.                     printf("请重新输入(Y/N):");
  67.                 }
  68.         }
  69.         return 0;
  70. }

  71. //以下为创建链表函数
  72. struct NODE *CreateLink(void)
  73. {
  74.     int i=0;//循环变量
  75.         int cnt=0;//学生的数量
  76.         struct NODE *head=malloc(sizeof*head);//定义头指针,并初始化指向头结点
  77.         struct NODE *move;
  78.         if(NULL==head)
  79.         {
  80.             printf("内存分配失败,程序终止!");
  81.                 else(-1);
  82.         }
  83.         move=head;
  84.         move->next=NULL;
  85.         printf("请输入学生的数量:");
  86.         scanf("%d",&cnt);
  87.         getchar();
  88.         for(i=0;i<cnt;++i)
  89.         {
  90.             struct NODE *fresh=malloc(sizeof*fresh);//
  91.                 if(NULL==fresh)
  92.                 {
  93.                     printf("内存分配失败,程序终止!");
  94.                         else(-1);
  95.                 }

  96.                 //结点连接三部曲
  97.                 move->next=fresh;//将新的结点连接到后面
  98.                 fresh->next=NULL;//新连上的结点初始化为指向NULL
  99.                 move=fresh;//指针变量move向后移动,指向当前新建的结点
  100.         }
  101.         return head;
  102. }

  103. //以下为链表结点初始化
  104. void Init(struct NODE *head)
  105. {
  106.     int i=1;
  107.         struct NODE *move=head->next;//初始化为首结点
  108.         while(NULL!=move)
  109.         {
  110.             printf("请输入第%d个学生的信息",i);
  111.                 scanf("%s%d %c %s",move->name,&move->age,&move->sex,&move->num);
  112.                 getchar();
  113.                 move=move->next;
  114.                 ++i;
  115.         }
  116.         return;
  117. }

  118. //以下为链表输出函数
  119. void OutputLink(struct NODE *head)
  120. {
  121.     struct NODE *move=head;
  122.         if(NULL==move)
  123.         {
  124.             printf("未创建链表\n");
  125.                 return;
  126.         }
  127.         if(NULL==move->next)
  128.         {
  129.             printf("链表为空\n");
  130.         }
  131.         while(NULL!=move->next)
  132.         {
  133.         printf("[姓名:%s,年龄:%d,性别:%c,学号:%s]->",move->name,&move->age,&move->sex,&move->num);
  134.         move=move->next;
  135.         }
  136.         printf("[^]\n");
  137. }

  138. //以下为插入结点函数
  139. void InsertNode(struct NODE *head)
  140. {
  141.     char num[20]="\0";//输入一个学号,在这个学号的后面插入一个学生的信息
  142.         struct NODE *fresh=malloc(sizeof*fresh);//存储要插入的学生的信息
  143.         printf("请输入你想在那个学号的学生后面再插入一个学生信息:");
  144.         while(1)//解决输入无该学号学生的BUG
  145.         {
  146.             struct NODE *move=head->next;//
  147.                 scanf("%s",num);
  148.                 getchar();
  149.                 while(NULL!=move)
  150.                 {
  151.                     if(0==strcmp(num,move->num))
  152.                         {
  153.                             printf("请输入插入学生的信息,分别是:姓名、年龄、性别、学号:");
  154.                                 scanf("%s%d %c %s",fresh->name,&fresh->age,&fresh->sex,&fresh->num);
  155.                                 getchar();
  156.                                 //插入结点
  157.                                 fresh->next=move->next;
  158.                                 move->next=fresh;
  159.                                 return;
  160.                         }
  161.                         move=move->next;
  162.                 }
  163.                 printf("无该结点,请重新输入:");
  164.         }
  165. }
复制代码


报错如下:

1.第36、43、60、66行报错: error C2105: '--' needs l-value   error C2064: term does not evaluate to a function

2.第88、101行报错:error C2181: illegal else without matching if

请问这是为什么?该如何改?
最佳答案
2019-7-24 16:23:29
  1. if(('Y'==ch)--('y'==ch))
复制代码


这是什么操作?谁教你的?

  1. if(('Y' == ch) || ('y' == ch))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-24 16:23:29 | 显示全部楼层    本楼为最佳答案   
  1. if(('Y'==ch)--('y'==ch))
复制代码


这是什么操作?谁教你的?

  1. if(('Y' == ch) || ('y' == ch))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-24 16:24:56 | 显示全部楼层
  1. if(NULL==fresh)
  2.                 {
  3.                     printf("内存分配失败,程序终止!");
  4.                         else(-1);
  5.                 }
复制代码

  1. if(NULL==fresh)
  2.                 {
  3.                     printf("内存分配失败,程序终止!");
  4.                         exit(-1);
  5.                 }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-24 16:31:35 | 显示全部楼层
人造人 发表于 2019-7-24 16:23
这是什么操作?谁教你的?

按照书上敲的。我没查看运算符表,我都知道应该是逻辑或的意思。结果照着书本就跟着错了。

另外那两个是我自己敲错了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 09:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表