鱼C论坛

 找回密码
 立即注册
查看: 951|回复: 9

[已解决]结构体函数调用问题

[复制链接]
发表于 2022-1-4 18:17:58 | 显示全部楼层 |阅读模式

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

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

x
为什么性别那里会把邮箱的信息写进去
代码放在一楼
最佳答案
2022-1-4 20:17:04
本帖最后由 jhq999 于 2022-1-4 20:20 编辑
  1. typedef struct student {////////第一个问题
  2.         int num;
  3.         int age;
  4.         char natuve_place[50];
  5.         char gender[3];////////////输入时必须是1个汉字,占两个字节,最后的字节是给字符串结束标志‘\0’留的
  6.         char email[20];
  7.         char QQ[15];
  8.         char name[20];
  9.         char academy[30];
  10.         int grade;
  11.         struct student* next;
  12.         struct student* prior;
  13. } Student;
复制代码
uTools_1641291416753.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-1-4 18:18:32 | 显示全部楼层
本帖最后由 焦糖橙子 于 2022-1-4 18:20 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct student {
  4.         int num;
  5.         int age;
  6.         char natuve_place[50];
  7.         char gender[2];
  8.         char email[20];
  9.         char QQ[15];
  10.         char name[20];
  11.         char academy[30];
  12.         int grade;
  13.         struct student* next;
  14.         struct student* prior;
  15. } Student;

  16. void DeletData(Student*, int);//删除数据
  17. void CrateData(Student*);//增加数据
  18. Student* ChcekData(Student*, int);//查看数据
  19. void ReviseData(Student*, int);//修改数据
  20. void PrintfData(Student*, int);//打印数据

  21. int main() {
  22.         int input, num, flag = 1;
  23.         Student* temp = (Student*)malloc(sizeof(Student));
  24.         Student* pStu, * PStu;
  25.         pStu = (Student*)malloc(sizeof(Student));
  26.         pStu->next = NULL;
  27.         pStu->prior = NULL;
  28.         PStu = (Student*)malloc(sizeof(Student));
  29.         pStu = PStu;
  30.         while (flag) {
  31.                 printf("\n****************************************\n");
  32.                 printf("1.删除学生信息\n");
  33.                 printf("2.新增学生信息\n");
  34.                 printf("3.查询学生信息\n");
  35.                 printf("4.修改学生信息\n");
  36.                 printf("5.打印学生信息\n");
  37.                 printf("0.结束程序\n");
  38.                 printf("请输入操作指令:");
  39.                 scanf("%d", &input);
  40.                 if (input != 0) {
  41.                         printf("请输入学生学号:");
  42.                         scanf("%d", &num);
  43.                 }
  44.                 else
  45.                         flag = 0;
  46.                 putchar('\n');
  47.                 switch (input) {
  48.                 case 1:
  49.                         DeletData(pStu, num);
  50.                         break;
  51.                 case 2:
  52.                         CrateData(pStu);
  53.                         break;
  54.                 case 3:
  55.                         temp = ChcekData(pStu, num);
  56.                         printf("\n学生的姓名:%s", temp->name);
  57.                         printf("\n学生的学号:%d", temp->num);
  58.                         printf("\n学生的年龄:%d", temp->age);
  59.                         printf("\n学生的性别:%s", temp->gender);
  60.                         printf("\n学生的籍贯:%s", temp->natuve_place);
  61.                         printf("\n学生的邮箱:%s", temp->email);
  62.                         printf("\n学生的QQ:%s", temp->QQ);
  63.                         printf("\n学生的学院:%s", temp->academy);
  64.                         printf("\n学生的年级:%d", temp->grade);
  65.                         break;
  66.                 case 4:
  67.                         ReviseData(pStu, num);
  68.                         break;
  69.                 case 5:
  70.                         PrintfData(pStu, num);
  71.                         break;
  72.                 case 0:
  73.                         flag = 0;
  74.                         break;
  75.                 default:
  76.                         printf("请输入正确的数字!\n");
  77.                         break;
  78.                 }
  79.         }

  80.         return 0;
  81. }

  82. void DeletData(Student* pStu, int num) {

  83.         Student* temp = (Student*)malloc(sizeof(Student));
  84.         temp = ChcekData(pStu, num);
  85.         temp->prior->next = temp->next;
  86.         temp->next->prior = temp->prior;

  87. }

  88. Student* ChcekData(Student* p, int num) {
  89.         while (p) {

  90.                 if (p->num == num)
  91.                         return p;
  92.                 else
  93.                         p = p->next;
  94.         }
  95.         printf("查无此人\n");
  96. }

  97. void CrateData(Student* pStu) { //pStu是头指针



  98.         Student* n_student = (Student*)malloc(sizeof(Student));

  99.         printf("请输入学生的姓名:");
  100.         scanf(" %s", &n_student->name);

  101.         printf("请输入学生的学号:");
  102.         scanf(" %d", &n_student->num);

  103.         printf("请输入学生的性别:");
  104.         scanf(" %s", &n_student->gender);

  105.         printf("请输入学生的年龄:");
  106.         scanf(" %d", &n_student->age);
  107.        
  108.         printf("请输入学生的籍贯:");
  109.         scanf(" %s", &n_student->natuve_place);

  110.         printf("请输入学生的邮箱:");
  111.         scanf(" %s", &n_student->email);

  112.         printf("请输入学生的QQ:");
  113.         scanf(" %s", &n_student->QQ);

  114.         printf("请输入学生的学院:");
  115.         scanf("%s", &n_student->academy);

  116.         printf("请输入学生的年级:");
  117.         scanf(" %d", &n_student->grade);

  118.         if (!pStu->prior) {
  119.                 n_student->prior = pStu;
  120.                 pStu->prior = n_student;
  121.                 n_student->next = pStu;
  122.                 pStu->next = n_student;
  123.         }
  124.         else {
  125.                 n_student->prior = pStu;
  126.                 pStu->prior = n_student;
  127.                 n_student->next = pStu;
  128.                 pStu->next = n_student;
  129.         }
  130.         printf("\n成功创建!\n");

  131. }

  132. void ReviseData(Student* pStu, int num) {

  133.         Student* temp = (Student*)malloc(sizeof(Student));
  134.         temp = ChcekData(pStu, num);

  135.         printf("当前学生信息如下:\n");
  136.         printf("\n学生的姓名:%s", temp->name);
  137.         printf("\n学生的学号:%d", temp->num);
  138.         printf("\n学生的年龄:%d", temp->age);
  139.         printf("\n学生的性别:%s", temp->gender);
  140.         printf("\n学生的籍贯:%s", temp->natuve_place);
  141.         printf("\n学生的邮箱:%s", temp->email);
  142.         printf("\n学生的QQ:%s", temp->QQ);
  143.         printf("\n学生的学院:%s", temp->academy);
  144.         printf("\n学生的年级:%d", temp->grade);

  145.         char ch;
  146.         printf("是否确认修改?(Y/N)");
  147.         scanf("%c", &ch);
  148.         if (ch == 'Y')
  149.         {

  150.                 printf("请输入学生的姓名:");
  151.                 scanf(" %s", &temp->name);

  152.                 printf("请输入学生的学号:");
  153.                 scanf(" %d", &temp->num);

  154.                 printf("请输入学生的性别:");
  155.                 scanf(" %s", &temp->gender);

  156.                 printf("请输入学生的年龄:");
  157.                 scanf(" %d", &temp->age);
  158.                
  159.                 printf("请输入学生的籍贯:");
  160.                 scanf(" %s", &temp->natuve_place);

  161.                 printf("请输入学生的邮箱:");
  162.                 scanf(" %s", &temp->email);

  163.                 printf("请输入学生的QQ:");
  164.                 scanf(" %s", &temp->QQ);

  165.                 printf("请输入学生的学院:");
  166.                 scanf(" %s", &temp->academy);

  167.                 printf("请输入学生的年级:");
  168.                 scanf(" %d", &temp->grade);

  169.                 printf("修改成功,修改后结果如下:\n");
  170.                 printf("\n学生的姓名:%s", temp->name);
  171.                 printf("\n学生的学号:%d", temp->num);
  172.                 printf("\n学生的年龄:%d", temp->age);
  173.                 printf("\n学生的性别:%s", temp->gender);
  174.                 printf("\n学生的籍贯:%s", temp->natuve_place);
  175.                 printf("\n学生的邮箱:%s", temp->email);
  176.                 printf("\n学生的QQ:%s", temp->QQ);
  177.                 printf("\n学生的学院:%s", temp->academy);
  178.                 printf("\n学生的年级:%d", temp->grade);
  179.         }
  180. }

  181. void PrintfData(Student* pStu, int num) {
  182.         Student* temp = (Student*)malloc(sizeof(Student));
  183.         temp = ChcekData(pStu, num);
  184.         printf("学生姓名:%s  学生学号:%d\n", temp->name, temp->num);
  185.         printf("学生年龄:%d  学生性别:%s\n", temp->age, temp->gender);
  186.         printf("学生籍贯:%s  学生邮箱:%d\n", temp->natuve_place, temp->email);
  187.         printf("学生QQ:%s  学生学院:%d  学生年级:%d\n", temp->academy, temp->grade);


  188. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-4 18:35:46 | 显示全部楼层
ReviseData函数也有问题176行中的scanf,我还没输入就自动跳下一行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-4 20:17:04 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-1-4 20:20 编辑
  1. typedef struct student {////////第一个问题
  2.         int num;
  3.         int age;
  4.         char natuve_place[50];
  5.         char gender[3];////////////输入时必须是1个汉字,占两个字节,最后的字节是给字符串结束标志‘\0’留的
  6.         char email[20];
  7.         char QQ[15];
  8.         char name[20];
  9.         char academy[30];
  10.         int grade;
  11.         struct student* next;
  12.         struct student* prior;
  13. } Student;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-4 20:20:17 | 显示全部楼层
第2个问题在调用scanf函数前加
  1. fflush(stdin)//清除输入缓存
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-5 09:51:31 | 显示全部楼层
本帖最后由 焦糖橙子 于 2022-1-5 09:53 编辑
jhq999 发表于 2022-1-4 20:20
第2个问题在调用scanf函数前加


还是会跳过scanf函数

Screenshot 2022-01-05 095241.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-5 09:55:33 | 显示全部楼层
jhq999 发表于 2022-1-4 20:20
第2个问题在调用scanf函数前加

我用DEV-c跑出来了,但是vs里面跑不出来。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-5 11:47:29 | 显示全部楼层
焦糖橙子 发表于 2022-1-5 09:55
我用DEV-c跑出来了,但是vs里面跑不出来。。。

VS在这里 if (ch == 'Y')设个断点,看看ch的值
或者在它前面加上
printf("%c",ch);
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-5 21:07:47 | 显示全部楼层
jhq999 发表于 2022-1-5 11:47
VS在这里 if (ch == 'Y')设个断点,看看ch的值
或者在它前面加上
printf("%c",ch);
  1. char ch;
  2.         printf("\n是否确认修改?(Y/N)");
  3.         fflush(stdin);//清除输入缓存
  4.         scanf("%c", &ch);
  5.         printf("%c",ch);
复制代码


改成这样子,但是在DEV-C++上键盘到这一步就不能输入了
屏幕截图 2022-01-05 210504.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-5 21:10:29 | 显示全部楼层
jhq999 发表于 2022-1-5 11:47
VS在这里 if (ch == 'Y')设个断点,看看ch的值
或者在它前面加上
printf("%c",ch);

恩?不知道为什么突然正常了,我之前在笔记本上跑,现在用台式。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 00:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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