鱼C论坛

 找回密码
 立即注册
查看: 2161|回复: 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
第一题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct patient{
        char id[256];
        char name[256];
        char symptom[256];
        struct patient *next;
};

void getinput(struct patient *head){
        printf("请输入病历号:");
        scanf("%s",head->id);
        printf("请输入姓名:");
        scanf("%s",head->name);
        printf("请输入症状:");
        scanf("%s",head->symptom) ;
}

void readin(struct patient **head){
        struct patient *new,*temp;
        new=(struct patient*)malloc(sizeof(struct patient));
        if(new==NULL){
                printf("内存分配失败!\n");
                exit(1);
        }
        getinput(new);
        if(*head!=NULL){
                temp=*head;
                *head=new;
                new->next=temp;
        }
        else{
                *head=new;
                new->next=NULL;
        }
        
}
struct patient *search(struct patient *head,char *x){
        struct patient *current;
        current=head;
        while(current!=NULL){
                if(!strcmp(current->name,x))
                {
                        break;
                }
                current=current->next;
        }
        return current;
}



void release(struct patient **head){
        struct patient *temp;
        while(*head!=NULL){
                temp=*head;
                *head=(*head)->next;
                free(temp);
        }
}

void print(struct patient* head)
{
        struct patient *p=head;
        
                printf("%s\n",p->id);
                printf("%s\n",p->name);
                printf("%s\n",p->symptom);
}
int main()
{
        struct patient *head=NULL,*p;
        int n=5;
        char name[256];
        while(n--){
                readin(&head);
                }
        printf("请输入要查找病人的姓名:"); 
        scanf("%s",name);
        p=search(head,name);
        if(p)print(p);
        else printf("查无此人\n");
        return 0;
}
请输入病历号:123
请输入姓名:小张
请输入症状:发烧
请输入病历号:456
请输入姓名:小李
请输入症状:头痛
请输入病历号:789
请输入姓名:小王
请输入症状:胃痛
请输入病历号:321
请输入姓名:小任
请输入症状:咽痛
请输入病历号:654
请输入姓名:小白
请输入症状:牙痛
请输入要查找病人的姓名:小白
654
小白
牙痛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-7 21:44:43 From FishC Mobile | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2023-3-8 08:57:20 | 显示全部楼层    本楼为最佳答案   
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2023-3-8 10:21:27 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2023-3-8 12:36:28 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-7 17:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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