C代码

/*
 ============================================================================
 Name        : staff_info_system.c
 Author      : CodeSun
 Compiler    : GCC4.7
 System      : Fedora17
 Version     : 0.0.1
 Description : Staff Information System
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//----------------------------Data Block---------------------------------------
typedef struct Staff
{
	char id[10],name[20],sex[5],birth[10],edu[20],job[20];
	int pay;
	struct Staff* next;
} Node;

typedef struct linkedlist_t {
	struct Staff* head;
	struct Staff* tail;
} LinkedList;
//------------------------------------------------------------------------------


void menu();
void mode(LinkedList* list);
void input(Node* rhs);
void output(Node* rhs);
void search_edu(LinkedList* list);
void search_pay(LinkedList* list);
void initlinkedlist(LinkedList* list);
void readlinkedlist(LinkedList* list);
void writelinkedlist(LinkedList* list);
void insert(LinkedList* list);
void scan(LinkedList* list);
void delete(LinkedList* list);
void update(LinkedList* list);
_Bool empty(LinkedList* list);

//-----------------------------------------------------------------------------
int main(void) 
{
    char flag='z';
    LinkedList list_m;
    initlinkedlist(&list_m);
    readlinkedlist(&list_m);
    mode(&list_m);
    printf("Are you sure to save the change?(y/n)");
    scanf(" %c",&flag);
    if(flag=='y')
        writelinkedlist(&list_m);
   	return 0;
}

//---------------------------------------------------------------------------------------
void menu()
{
	printf("|----------------|\n");
	printf("|      Menu      |\n");
	printf("|----------------|\n");
	printf("|    1.Insert    |\n");
	printf("|    2.Scan      |\n");
	printf("|    3.Search    |\n");
	printf("|    4.Delete    |\n");
	printf("|    5.Update    |\n");
	printf("|    0.Exit      |\n");
	printf("|----------------|\n");

}
void mode(LinkedList* list)
{
    int mode;
    int mmode;
    while(1)
    {
        menu();
	    printf("Please enter the mode: ");
        scanf("%d",&mode);
        switch(mode)
        {
            case 0:
                return;
            case 1:
                insert(list);
                break;
            case 2:
                scan(list);
                break;
            case 3:
                printf("Search by\n1.payment\n2.edubackground\n");
                scanf("%d",&mmode);
                if(mmode==1)
                    search_pay(list);
                else
                    search_edu(list);
                break;
            case 4:
                delete(list);
                break;
            case 5:
                update(list);
                break;
            default:
                printf("Error:wrong mode!\n");
        }
    }
}
//-------------------------------------------------------------------------------------------





//---------------------------------------IO---------------------------------

void input(Node* rhs)
{
	printf("ID: ");
	scanf("%s",rhs->id);
	printf("Name: ");
	scanf("%s",rhs->name);
	printf("Sex: ");
	scanf("%s",rhs->sex);
	printf("Birthday: ");
	scanf("%s",rhs->birth);
	printf("EducationBackground: ");
	scanf("%s",rhs->edu);
	printf("Position: ");
	scanf("%s",rhs->job);
	printf("Payment: ");
	scanf("%d",&(rhs->pay));
    rhs->next=0;
}
void output(Node* rhs)
{
    printf("ID: %s\n",rhs->id);
    printf("Name: %s\n",rhs->name);
    printf("Sex: %s\n",rhs->sex);
    printf("Birthday: %s\n",rhs->birth);
    printf("Education: %s\n",rhs->edu);
    printf("Position: %s\n",rhs->job);
    printf("Payment: %d\n",rhs->pay);

}
//----------------------------------------------------------------------------


//------------------------------Function Search-------------------------------
void search_edu(LinkedList* list)
{
    char edu_m[20];
    printf("Enter the education background: ");
    scanf("%s",edu_m);

    for(Node* p=list->head;p!=0;p=p->next)
    {
        if(strcmp(edu_m,p->edu)==0)
            output(p);
    }

}
void search_pay(LinkedList* list)
{
    int pay_m;
    printf("Enter the payment: ");
    scanf("%d",&pay_m);
    for(Node* p=list->head;p!=0;p=p->next)
    {
        if(pay_m==p->pay)
            output(p);
    }

}
//------------------------------------------------------------------------------
_Bool empty(LinkedList* list)
{
	return list->head==0;
}
void insert(LinkedList* list)
{
	if(!empty(list))
	{
        Node* temp=(Node*)malloc(sizeof(Node));
        input(temp);
		list->tail->next=temp;
        list->tail=temp;
	}
	else
	{
		list->head=list->tail=(Node*)malloc(sizeof(Node));
        input(list->head);
	}	
}
void delete(LinkedList* list)
{
    
    if(empty(list))
    {
        printf("Error: no person in the list!\n");
        return;
    }
    char id_m[10];
    printf("Enter the id whose you want to delete: ");
    scanf("%s",id_m);
    Node* p=list->head;
    if(strcmp(p->id,id_m)==0)
    {
        list->head=p->next;
        free(p);
        //If the queue is empty, the tail should be NULL(0)
        if(empty(list))
            list->tail=0;
        return;
    }
    for(;p!=0;p=p->next)
    {
        //Take care, don't ignore the tail of the queue!
        if(strcmp(p->next->id,id_m)==0)
        {
            if(p->next->next==0)
                list->tail=p;
            Node* temp=p->next;
            p->next=temp->next;
            free(temp);
            break;
        }
    }
}
void scan(LinkedList* list)
{
    if(empty(list))
    {
        printf("Error: no person in the list!\n");
        return;
    }
    for(Node* p=list->head;p!=0;p=p->next)
        output(p);
}
void update(LinkedList* list)
{
    if(empty(list))
    {
        printf("Error: no person in the list!\n");
        return;
    }
    char id_m[10];
    printf("Enter the ID you want to update: ");
    scanf("%s",id_m);
    for(Node* p=list->head;p!=0;p=p->next)
    {
        if(strcmp(p->id,id_m)==0)
        {
            char flag='z';
            printf("Update Name?(y/n)");
            scanf(" %c",&flag);
            if(flag=='y')
            {
                printf("Name: ");
                scanf("%s",p->name);
            }
            flag='z';
            printf("Update Sex?(y/n)");
            scanf(" %c",&flag);
            if(flag=='y')
            {
                printf("Sex: ");
                scanf("%s",p->sex);
            }
            flag='z';
            printf("Update Birthday?(y/n)");
            scanf(" %c",&flag);
            if(flag=='y')
            {
                printf("Birthday: ");
                scanf("%s",p->birth);
            }
            flag='z';
            printf("Update Education?(y/n)");
            scanf(" %c",&flag);
            if(flag=='y')
            {
                printf("Education: ");
                scanf("%s",p->edu);
            }
            flag='z';
            printf("Update Position?(y/n)");
            scanf(" %c",&flag);
            if(flag=='y')
            {
                printf("Position: ");
                scanf("%s",p->job);
            }
            flag='z';
            printf("Update Payment?(y/n)");
            scanf(" %c",&flag);
            if(flag=='y')
            {
                printf("Payment: ");
                scanf("%d",&(p->pay));
            }
            break;
        }
    }

}
//-------------------Get ready for LinkedList---------------------
//Initlize the queue
void initlinkedlist(LinkedList* list)
{
    list->head=list->tail=0;
}
//Read the data in the file
void readlinkedlist(LinkedList*  list)
{
    FILE* fp;
    Node* p=list->head=(Node*)malloc(sizeof(Node));
    p->next=0;
    if((fp=fopen("data.dat","r"))!=NULL)
    {
        while(fscanf(fp,"%s %s %s %s %s %s %d",p->id,p->name,p->sex,p->birth,p->edu,p->job,&(p->pay))!=EOF)
        {
            list->tail=p;
            p->next=(Node*)malloc(sizeof(Node));
            p=p->next;
            p->next=0;
        }
    }
    fclose(fp);
    free(p);
    if(list->head==p)
        list->head=list->tail=0;
    else
        list->tail->next=0;
}
//Write the data to the file
void writelinkedlist(LinkedList* list)
{
    FILE* fp;
    if((fp=fopen("data.dat","w"))!=NULL)
    {
        for(Node* p=list->head;p!=0;p=p->next)
            fprintf(fp,"%s %s %s %s %s %s %d\n",p->id,p->name,p->sex,p->birth,p->edu,p->job,p->pay);
    }
    fclose(fp);
}
//----------------------------------------------------------------

昨儿,刚看完《C primer plus》,写段代码玩玩,欢迎评论

你可能感兴趣的:(c,代码)