C语言作业,一个数据库程序的实现

/// 2019_12_19

Requirements (least):
1. For given herb name (in Cn or En), display all compound(s) together with target(s) regulated.
2. For given compound (in En), display all herb and target(s) and target(s).
3. For given target, display all target(s) together with herb(s)


4. Insert/Update/Delete(herb, compound)
5. Insert/Update/Delete(compound, target)
6. PPT(Gains, Pains, Envision)
Further:
Additional functions interest you.

有两个匹配的文本:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
#include <errno.h>
struct Herb
{
    char cHerb_CN[20];
    char cCompound_cn[30];
    char cCompound[30];
    char cChemical_Formula[15];
    char cLiterture[150];
    char cPublishingdate[30];
    char cAuthor[15];
    char cCurator[10];
    char IsDelete[5];
    char Annotation[50];
    struct Herb * next;
}a[5000];
struct Compound
{
    char cCompound2[30];
    char cAction[10];
    char cTarget[100];
    char cEvidence[30];
    char cReserch_interests[100];
    char cAuthor[30];
    char cValidator[10];
    char cAnnotation[30];
    struct Compound * next;
}b[2000];
void menu()
{
    system("cls");
    printf("\n");
    printf("\t|-----------------------------------|\n");
    printf("\t0.exit\n");
    printf("\t1.herb name-->compound(s) and target(s)\n");
    printf("\t2.compound-->herb and target(s)\n");
    printf("\t3.target-->target(s) and herb(s)\n");
    printf("\t4.insert(herb, compound)\n");
    printf("\t5.update(herb, compound)\n");
    printf("\t6.delete(herb, compound)\n");
    printf("\t7.insert(compound, target)\n");
    printf("\t8.update(compound, target)\n");
    printf("\t9.delete(compound, target)\n");
    printf("\t|-----------------------------------|\n");
    printf("\tPlease choose(0~9):");
}
void print_C(struct Herb * p)
{
    int i=0;
    while(p != NULL)
    {
        fprintf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
               i++, p->cHerb_CN, p->cCompound_cn,p->cCompound,p->cChemical_Formula,p->cLiterture,
               p->cPublishingdate,p->cAuthor,p->cCurator,p->IsDelete,p->Annotation );
        p = p->next;
    }
    printf("\n");
}
void function1()
{
    struct Herb * p=(struct Herb *)malloc(sizeof(struct Herb));
    FILE *HerbCompound= fopen(".\\Herb_Compound.txt","r+");
	if((HerbCompound=fopen(".\\Herb_Compound.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char herb_name[30],herbname[300];
	int n,i=0;
    printf("please input the herb name :");
	scanf("%s",&herb_name);
	n=strlen(herb_name);
	while(fgets(herbname,300,HerbCompound)!=NULL&&!feof(HerbCompound))
        {
            if(strncmp(herb_name,herbname,n)==0)
            {
                printf("NO %d: %s",i,herbname);
                p=strtok(herbname,"\t");
                if(p)
                p=strtok(NULL,"\t");
                if(p)
                p=strtok(NULL,"\t");
                if(p)
                i++;
                function10(p);
            }
        }
    fclose(HerbCompound);
}
void function10(char compound_name[30])
{
    struct Compound * p=(struct Compound *)malloc(sizeof(struct Compound));
    FILE *CompundTarget= fopen(".\\CompundTarget.txt","r+");
	if((CompundTarget=fopen(".\\CompundTarget.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char compoundname[300];
	int n,i=0;
	n=strlen(compound_name);
	while(fgets(compoundname,300,CompundTarget)!=NULL&&!feof(CompundTarget))
        {
            if(strncmp(compound_name,compoundname,n)==0)
            {
                printf("\tNO %d: %s",i,compoundname);
                i=i+1;
            }
        }
        if(i==0)
        printf("\t\tNone!\n");
        fclose(CompundTarget);
}
void function2()
{
    struct Compound * p=(struct Compound *)malloc(sizeof(struct Compound));
    FILE *CompundTarget= fopen(".\\CompundTarget.txt","r+");
	if((CompundTarget=fopen(".\\CompundTarget.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char compound_name[30],compoundname[300];
	int n,i=0;
    printf("please input the compound name :");
	scanf("%s",&compound_name);
	n=strlen(compound_name);
	while(fgets(compoundname,300,CompundTarget)!=NULL&&!feof(CompundTarget))
        {
            if(strncmp(compound_name,compoundname,n)==0)
            {
                printf("NO %d: %s",i,compoundname);
                p=strtok(compoundname,"\t");
                if(p)
                p=strtok(NULL,"\t");
                if(p)
                p=strtok(NULL,"\t");
                if(p)
                i++;
                function20(p);
            }
        }
	fclose(CompundTarget);
}
void function20(char compound_name[30])
{
    struct Herb * p=(struct Herb *)malloc(sizeof(struct Herb));
    FILE *HerbCompound= fopen(".\\Herb_Compound.txt","r+");
	if((HerbCompound=fopen(".\\Herb_Compound.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char herbname[300];
	int i=0;
	while(fgets(herbname,300,HerbCompound)!=NULL&&!feof(HerbCompound))
        {
            if(strstr(herbname,compound_name)!=NULL)
            {
                printf("\tNO %d: %s",i,herbname);
                i=i+1;
            }
        }
    if(i==0)
    printf("\t\tNone!\n");
    fclose(HerbCompound);
}
void function3()
{
    struct Compound * p=(struct Compound *)malloc(sizeof(struct Compound));
    FILE *CompundTarget= fopen(".\\CompundTarget.txt","r+");
	if((CompundTarget=fopen(".\\CompundTarget.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char target[50],compoundname[300];
    printf("please input the target you want to find:");
	scanf("%s",&target);
	int i=0;
	while(fgets(compoundname,300,CompundTarget)!=NULL&&!feof(CompundTarget))
        {
            if(strstr(compoundname,target)!=NULL)
            {
                printf("NO %d: %s",i,compoundname);
                p=strtok(compoundname,"\t");
                if(p)
                p=strtok(NULL,"\t");
                if(p)
                p=strtok(NULL,"\t");
                if(p)
                i++;
                function30(p);
            }
        }
	fclose(CompundTarget);
}
void function30(char compound_name[30])
{
    struct Herb * p=(struct Herb *)malloc(sizeof(struct Herb));
    FILE *HerbCompound= fopen(".\\Herb_Compound.txt","r+");
	if((HerbCompound=fopen(".\\Herb_Compound.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char herbname[300];
	int i=0;
	while(fgets(herbname,300,HerbCompound)!=NULL&&!feof(HerbCompound))
        {
            if(strstr(herbname,compound_name)!=NULL)
            {
                printf("\tNO %d: %s",i,herbname);
                i=i+1;
            }
        }
        if(i==0)
            printf("\t\tNone!\n");
        fclose(HerbCompound);
}
struct Herb * insert1(struct Herb * h)
{
    struct Herb * p=(struct Herb *)malloc(sizeof(struct Herb));
    FILE *HerbCompound;
	if((HerbCompound=fopen(".\\Herb_Compound.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char herb_name[30],herbname[300];
	int n,i=0,m=0,k=0;
    printf("Insert: please insert Herb, Compound and so on: ");
        scanf("%s%s%s%s%s%s%s%s%s%s", &a[i].cHerb_CN,&a[i].cCompound_cn,&a[i].cCompound,&a[i].cChemical_Formula,&a[i].cLiterture,
              &a[i].cPublishingdate,&a[i].cAuthor,&a[i].cCurator,&a[i].IsDelete,&a[i].Annotation);
              strcpy(herb_name,a[i].cHerb_CN);
              i=i+1;
	n=strlen(herb_name);
		while(fgets(herbname,300,HerbCompound)!=NULL&&!feof(HerbCompound))
        {
            if(strncmp(herb_name,herbname,n)==0)
            {
                p=strtok(herbname,"\t");
                if(p)
                strcpy(a[i].cHerb_CN,p);
                p=strtok(NULL,"\t");
                if(p)
                p=strtok(NULL,"\t");
                if(p)
                strcpy(a[i].cCompound,p);
                i++;
                if(p->cHerb_CN)
                {
                    p->next = NULL;
                    h = p;
                }
            }
        }
        struct Herb * p1, * p2;
    struct Herb * p3=(struct Herb *)malloc(sizeof(struct Herb));
        p1 = p2 = h;
        p3->next = NULL;
            while(p3->cHerb_CN == p1->cHerb_CN && p1->next != NULL)
            {
                p2 = p1;
                p1 = p1->next;
            }
            if(p3->cHerb_CN == p1->cHerb_CN)
            {
                p1->next = p3;
                p3->next = NULL;
                printf("insert: end.\n");
            }
        printf("insert: end.\n");
        fprintf(HerbCompound,&a[0].cHerb_CN);
        fprintf(HerbCompound,"\t");
        fprintf(HerbCompound,&a[0].cCompound_cn);
        fprintf(HerbCompound,"\t");
        fprintf(HerbCompound,&a[0].cCompound);
        fprintf(HerbCompound,"\t");
        fprintf(HerbCompound,&a[0].cChemical_Formula);
        fprintf(HerbCompound,"\t");
        fprintf(HerbCompound,&a[0].cLiterture);
        fprintf(HerbCompound,"\t");
        fprintf(HerbCompound,&a[0].cPublishingdate);
        fprintf(HerbCompound,"\t");
        fprintf(HerbCompound,&a[0].cAuthor);
        fprintf(HerbCompound,"\t");
        fprintf(HerbCompound,&a[0].cCurator);
        fprintf(HerbCompound,"\t");
        fprintf(HerbCompound,&a[0].IsDelete);
        fprintf(HerbCompound,"\t");
        fprintf(HerbCompound,&a[0].Annotation);
        fprintf(HerbCompound,"\n");
	fclose(HerbCompound);
}
struct Compound * insert2(struct Compound * h)
{
    struct Compound * p=(struct Compound *)malloc(sizeof(struct Compound));
    FILE *CompundTarget;
	if((CompundTarget=fopen(".\\CompundTarget.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char compound_name[30],compoundname[300];
	int n,i=0;
    printf("Insert: please insert Compound ,Action ,Target and so on: ");
        scanf("%s%s%s%s%s%s%s%s", &b[i].cCompound2,&b[i].cAction,&b[i].cTarget,&b[i].cEvidence,&b[i].cReserch_interests,
                    &b[i].cAuthor,&b[i].cValidator,&b[i].cAnnotation);
    strcpy(compound_name,b[i].cCompound2);
	n=strlen(compound_name);
	while(fgets(compoundname,300,CompundTarget)!=NULL&&!feof(CompundTarget))
        {
            if(strncmp(compound_name,compoundname,n)==0)
            {
                p=strtok(compoundname,"\t");
                if(p)
                strcpy(b[i+1].cCompound2,p);
                p=strtok(NULL,"\t");
                if(p)
                strcpy(b[i+1].cAction,p);
                p=strtok(NULL,"\t");
                if(p)
                strcpy(b[i+1].cTarget,p);
                i++;
                 if(p->cCompound2)
                {
                    p->next = NULL;
                    h = p;
                }
            }
        }
        struct Compound * p1, * p2;
        struct Compound * p3=(struct Compound *)malloc(sizeof(struct Compound));
        if(h == NULL)
        h = insert2(h);
        p1 = p2 = h;
        p3->next = NULL;
            while(p3->cCompound2 == p1->cCompound2 && p1->next != NULL)
            {
                p2 = p1;
                p1 = p1->next;
            }
            if(p3->cCompound2 == p1->cCompound2)
            {
                p1->next = p3;
                p3->next = NULL;
                printf("insert: end.\n");
            }
        printf("insert: end.\n");
        fprintf(CompundTarget,&b[0].cCompound2);
        fprintf(CompundTarget,"\t");
        fprintf(CompundTarget,&b[0].cAction);
        fprintf(CompundTarget,"\t");
        fprintf(CompundTarget,&b[0].cTarget);
        fprintf(CompundTarget,"\t");
        fprintf(CompundTarget,&b[0].cEvidence);
        fprintf(CompundTarget,"\t");
        fprintf(CompundTarget,&b[0].cReserch_interests);
        fprintf(CompundTarget,"\t");
        fprintf(CompundTarget,&b[0].cAuthor);
        fprintf(CompundTarget,"\t");
        fprintf(CompundTarget,&b[0].cValidator);
        fprintf(CompundTarget,"\t");
        fprintf(CompundTarget,&b[0].cAnnotation);
        fprintf(CompundTarget,"\n");
	fclose(CompundTarget);
}
struct Herb * del1()
{
    FILE *HerbCompound;
    FILE *beiyong;
	if((HerbCompound=fopen(".\\Herb_Compound.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	if((beiyong=fopen(".\\beiyong.txt","w"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char herb_name[30],herbname[300],compound[50];
	int n,k=0,i=0;
    printf("please input the herb name and compound you want to delete:");
	scanf("%s %s",&herb_name,&compound);
	n=strlen(herb_name);
    while(fgets(herbname,300,HerbCompound)!=NULL&&!feof(HerbCompound))
    {
       if((strncmp(herb_name,herbname,n)==0))
       {
           if(strstr(herbname,compound)!=NULL)
            continue;
       }
       fprintf(beiyong,herbname);
    }
    fclose(beiyong);
    fclose(HerbCompound);
    remove("Herb_Compound.txt");
    if(rename("beiyong.txt","Herb_Compound.txt")<0)
    printf("error!\n");
    else
    printf("ok!\n");
};
struct Herb * del3()
{
    FILE *HerbCompound;
    FILE *beiyong;
	if((HerbCompound=fopen(".\\Herb_Compound.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	if((beiyong=fopen(".\\beiyong.txt","w"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char herb_name[30],herbname[300],compound[50];
	int n,k=0,i=0;
    printf("please input the information you want to update(请输入文件中有的物质和化学成分即可):");
	scanf("%s %s",&herb_name,&compound);
	n=strlen(herb_name);
    while(fgets(herbname,300,HerbCompound)!=NULL&&!feof(HerbCompound))
    {
       if((strncmp(herb_name,herbname,n)==0))
       {
           if(strstr(herbname,compound)!=NULL)
            continue;
       }
       fprintf(beiyong,herbname);
    }
    fclose(beiyong);
    fclose(HerbCompound);
    remove("Herb_Compound.txt");
    if(rename("beiyong.txt","Herb_Compound.txt")<0)
    printf("error!\n");
    else
    printf("ok!\n");
};
struct Herb * del2()
{
    FILE *CompundTarget;
    FILE *beiyong;
	if((CompundTarget=fopen(".\\CompundTarget.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	if((beiyong=fopen(".\\beiyong.txt","w"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char compound_name[30],compoundname[300],target[50],action[10];
	int n,k=0,i=0;
    printf("please input the compound ,action and target you want to delete:");
	scanf("%s %s %s",&compound_name,&action,&target);
	n=strlen(compound_name);
    while(fgets(compoundname,300,CompundTarget)!=NULL&&!feof(CompundTarget))
    {
       if((strncmp(compound_name,compoundname,n)==0))
       {
           if(strstr(compoundname,target)!=NULL&&strstr(compoundname,action)!=NULL)
            continue;
       }
       fprintf(beiyong,compoundname);
    }
    fclose(beiyong);
    fclose(CompundTarget);
    remove("CompundTarget.txt");
    if(rename("beiyong.txt","CompundTarget.txt")<0)
    printf("error!\n");
    else
    printf("ok!\n");
};
struct Herb * del4()
{
    FILE *CompundTarget;
    FILE *beiyong;
	if((CompundTarget=fopen(".\\CompundTarget.txt","r+"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	if((beiyong=fopen(".\\beiyong.txt","w"))==NULL)
	{
		printf("can not open!\n");
		exit(0);
	}
	char compound_name[30],compoundname[300],target[50],action[10];
	int n,k=0,i=0;
    printf("please input the informationb you want to update(请输入文件中的化学成分活性以及对应蛋白质或基因):");
	scanf("%s %s %s",&compound_name,&action,&target);
	n=strlen(compound_name);
    while(fgets(compoundname,300,CompundTarget)!=NULL&&!feof(CompundTarget))
    {
       if((strncmp(compound_name,compoundname,n)==0))
       {
           if(strstr(compoundname,target)!=NULL&&strstr(compoundname,action)!=NULL)
            continue;
       }
       fprintf(beiyong,compoundname);
    }
    fclose(beiyong);
    fclose(CompundTarget);
    remove("CompundTarget.txt");
    if(rename("beiyong.txt","CompundTarget.txt")<0)
    printf("error!\n");
    else
    printf("ok!\n");
};
int main()
{
    struct Herb * head = NULL;
    int e;
    menu();
    scanf("%d",&e);
    while(e)
    {
        switch(e)
        {
        case 1:
            function1();
            break;
        case 2:
            function2();
            break;
        case 3:
            function3();
            break;
        case 4:
            head= insert1(head);
           break;
        case 5:
            head=del3();
            head= insert1(head);
            break;
        case 6:
            head=del1();
            break;
        case 7:
            head=insert2(head);
            break;
        case 8:
            head=del4();
            head= insert2(head);
            break;
        case 9:
            head=del2();
            break;
        }
        getch();
        menu();
        scanf("%d",&e);
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值