鱼C论坛

 找回密码
 立即注册
查看: 2593|回复: 4

[已解决]C语言函数及结构题问题

[复制链接]
发表于 2023-3-7 09:08:43 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 zzs2963584895 于 2023-3-9 21:27 编辑

1.实现病历查询功能。具体要求如下:
定义一个结构体描述病人病历信息(病历号,姓名,症状);完成功能如下:
1) 输入功能:输入5个病人的信息;
2) 查询功能:输入姓名,在5个病历中进行查找,如果找到则显示该人的信息,如果没有找到,则显示“查无此人”。
    假设病历类型名为patient,要求使用指针,并使用以下两个函数(函数的实现自行完成):
void readin(patient *p);//用来输入病人信息。
void search(patient *p,char *x);//根据姓名查询病人病历信息,如果成功则打印病人信息,如果不成功显示“查无此人”。
提示:请注意输入函数的用法。

2.设计一个函数,计算S=1-2+3-4+5-6+……+/-N的值,并计算你所设计的函数的时间复杂度。

3. 歌德巴赫猜想:任一大于2的偶数,都可以表示成两个素数之和。请编写自定义函数验证:2000以内大于2的偶数都能够分解为两个素数之和。
最佳答案
2023-3-8 08:57:20
第一题
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. struct patient{
  5.         char id[256];
  6.         char name[256];
  7.         char symptom[256];
  8.         struct patient *next;
  9. };

  10. void getinput(struct patient *head){
  11.         printf("请输入病历号:");
  12.         scanf("%s",head->id);
  13.         printf("请输入姓名:");
  14.         scanf("%s",head->name);
  15.         printf("请输入症状:");
  16.         scanf("%s",head->symptom) ;
  17. }

  18. void readin(struct patient **head){
  19.         struct patient *new,*temp;
  20.         new=(struct patient*)malloc(sizeof(struct patient));
  21.         if(new==NULL){
  22.                 printf("内存分配失败!\n");
  23.                 exit(1);
  24.         }
  25.         getinput(new);
  26.         if(*head!=NULL){
  27.                 temp=*head;
  28.                 *head=new;
  29.                 new->next=temp;
  30.         }
  31.         else{
  32.                 *head=new;
  33.                 new->next=NULL;
  34.         }
  35.        
  36. }
  37. struct patient *search(struct patient *head,char *x){
  38.         struct patient *current;
  39.         current=head;
  40.         while(current!=NULL){
  41.                 if(!strcmp(current->name,x))
  42.                 {
  43.                         break;
  44.                 }
  45.                 current=current->next;
  46.         }
  47.         return current;
  48. }



  49. void release(struct patient **head){
  50.         struct patient *temp;
  51.         while(*head!=NULL){
  52.                 temp=*head;
  53.                 *head=(*head)->next;
  54.                 free(temp);
  55.         }
  56. }

  57. void print(struct patient* head)
  58. {
  59.         struct patient *p=head;
  60.        
  61.                 printf("%s\n",p->id);
  62.                 printf("%s\n",p->name);
  63.                 printf("%s\n",p->symptom);
  64. }
  65. int main()
  66. {
  67.         struct patient *head=NULL,*p;
  68.         int n=5;
  69.         char name[256];
  70.         while(n--){
  71.                 readin(&head);
  72.                 }
  73.         printf("请输入要查找病人的姓名:");
  74.         scanf("%s",name);
  75.         p=search(head,name);
  76.         if(p)print(p);
  77.         else printf("查无此人\n");
  78.         return 0;
  79. }
复制代码

  1. 请输入病历号:123
  2. 请输入姓名:小张
  3. 请输入症状:发烧
  4. 请输入病历号:456
  5. 请输入姓名:小李
  6. 请输入症状:头痛
  7. 请输入病历号:789
  8. 请输入姓名:小王
  9. 请输入症状:胃痛
  10. 请输入病历号:321
  11. 请输入姓名:小任
  12. 请输入症状:咽痛
  13. 请输入病历号:654
  14. 请输入姓名:小白
  15. 请输入症状:牙痛
  16. 请输入要查找病人的姓名:小白
  17. 654
  18. 小白
  19. 牙痛
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-7 21:44:43 From FishC Mobile | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2023-3-8 08:57:20 | 显示全部楼层    本楼为最佳答案   
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2023-3-8 10:21:27 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2023-3-8 12:36:28 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 21:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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