Esempio n. 1
0
struct student *readfromefile(struct student *phead,char *file)
{
    int num=0;
    int n=0;
    struct student *pstu=NULL;
    FILE *fp=fopen(file,"r");
    if(NULL==fp)
    {
        return NULL;
    }
    while(1)
    {
        pstu=malloc(sizeof(struct student));
        n=fread(pstu,sizeof(struct student),1,fp);
        if(0 == n)
        {
            free(pstu);
            pstu=NULL;
            break;
        }
        pstu->next=NULL;
        pstu->pre=NULL;
        addstu(&phead,pstu);
        num++;
    }
    fclose(fp);
    return phead;
}
Esempio n. 2
0
int FileInit() {
    printf("Read From File\n");
    freopen("Data.txt","r",stdin);
    freopen("Temp.txt","w",stdout);
    while (~scanf("%d",&Order)) {
        switch (Order) {
            case 1 : addstu(); break;
            case 2 : addact(); break;
            case 3 : qstu(); break;
            case 4 : qact(); break;
            case 5 : qc(); break;
            case 0 : break;
            default : break;
        }
    }
    freopen("/dev/tty","r",stdin);
    freopen("/dev/tty","w",stdout);
    printf("Read Finish\n");
}
Esempio n. 3
0
int main() {
    Init();
    FileInit();
    Order = -1;

    //work
    while (Order != 0) {
        ReadInit();
        switch (Order) {
            case 1 : addstu(); break;
            case 2 : addact(); break;
            case 3 : qstu(); break;
            case 4 : qact(); break;
            case 5 : qc(); break;
            case 0 : break;
            default : printf("Unknown Order\n");break;
        }
    }

    // free the memory
    EndFree();
    return 0;
}
Esempio n. 4
0
int main(int argc,char *argv[])
{
    int a=0;
    char buf[1024];
    int cmdno;
    int quit=0;
    char name[20];
    char id[20];
    int rt;
    int age;
    char sex;
    struct student *pstu;
    struct student stu;
    struct listhead phead;
    phead.next = phead.pre = &phead;
   // readfromfile(&phead,STUFILE);
    //struct allstudent allstu;
    //rt=init_allstu(&allstu);
    printf("****************************************************************\n");
    printf("please input cmd:");
    if(rt==-1)
    {
        return -1;
    }
    while(0==quit)
    {
        fgets(buf,1024,stdin);
        buf[strlen(buf)-1] = '\0';
        cmdno=getCmdno(buf);
        switch(cmdno)
        {
            case HELP:
                 help();

                break;
            case ADD:
                printf("add\n");
                printf("please input student info\n");
                printf("please input student id\n");
                scanf("%s",id);
                
               
                
                
                 printf("please input student name\n");
                scanf("%s",name);
                printf("please input student age\n");
                scanf("%d",&age);
                printf("please input student sex\n");
                fgets(buf,1024,stdin);
                scanf("%c",&sex);
               // strcpy(stu.id,id);
               // strcpy(stu.name,name);
               // stu.age=(char)age;
               // stu.sex = sex;
               // addbyorder(&allstu,&stu);
                pstu = malloc(sizeof(struct student));
                if(NULL==pstu)
                {
                    printf("no\n");
                    break;
                }
                strcpy(pstu->id,id);
                strcpy(pstu->name,name);
                pstu->age=(char)age;
                pstu->sex=sex;
               
                addstu(&phead,pstu);
                
                break;
            case DEL:
                printf("please input student id\n");
                scanf("%s",id);
  //              pstu=findbyid(&phead,id);
                if(NULL==pstu)
                {
                    printf("%s can not exit\n",id);
                    break;
                }
                
                    delstu(pstu);
                    free(pstu);
                    pstu=NULL;
                
                
                break;
    /*        case FIND:
                printf("please input student id\n");
                scanf("%s",id);
                pstu=findbyid(&phead,id);
                if(NULL==pstu)
                {
                    printf("no this student\n");
                    break;
                }
                showone(pstu);
                 
                break;
            case FNAME:
                printf("please input name:");
                scanf("%s",name);
                pstu=NULL;
                do
                {
                    pstu=findbyname(&phead,name,pstu);
                    if(NULL!=pstu)
                    {
                        showone(pstu);
                    }
                }while(NULL!=pstu);
                break;*/
            case SHOW:
                showall(&phead);
                printf("please input cmd:");
                break;
    /*        case RSHOW:
                rshowall(&phead);
                printf("please input cmd:");
                break;
            case REVER:

               reverse(&phead);
               printf("please input cmd:");
                break;*/
            case QUIT:


                quit=1;
                break;
         /*   case SAVE:
                savefile(&phead,STUFILE);
                printf("please input cmd:");
               break;
            case MODIFY:
              
               break;
*/


            default:
                printf("please input cmd:");
                break;
                
        }
    }
    destroyall(&phead);

    return 0;
}
Esempio n. 5
0
int main()
{
    struct idhash allstu;
    struct student *p;
    struct student tempstu;
    struct student *stu;
    int tempage;
    char cmd[1024];
    int cmdnum;
    int quit = 0;
    int select;
    FILE *studata;

    studata = fopen("studata","r+");
    if(NULL == studata)
    {
        studata = fopen("studata","w+");
        if(NULL == studata)
        {
            printf("cannot open file !\n");
            return -1;
        }
    }
    
    initallidhash(&allstu);
    readfromfile(&allstu,studata);
    printf("|-------------------------------------------|\n");
    printf("|----Welcome to Student Database System-----|\n");
    printf("|-------------------------------------------|\n\n");
    printf("%d student have!\n",count(&allstu));

    while(0 == quit)
    {
        printf("|-------------------------------------------|\n");
        printf("|-------Please Input \"help\" for Help--------|\n");
        printf("|-------------------------------------------|\n");
        
        fgets(cmd,1024,stdin);
        cmd[strlen(cmd)-1] = 0;
        cmdnum = getcmd(cmd);
        switch(cmdnum)
        {
            case CMDSHOW:
                showallstu(&allstu);
                break;
            case CMDQUIT:
                printf("quit successful!\n");
                quit = 1;
                break;
            case CMDADD:
                printf("please input a new student:\nstudent's ID:");
                scanf("%s",tempstu.id);
                printf("please input student's Name:");
                scanf("%s",tempstu.name);
                printf("please input student's age:");
                scanf("%d",&tempage);
                tempstu.age = (char)tempage;
                fgets(cmd,1024,stdin);
                printf("please input student's sex:");
                scanf("%c",&tempstu.sex);
                fgets(cmd,1024,stdin);
                tempstu.next = NULL;
                p = findstu(&allstu,tempstu.id);
                if(NULL != p)
                {
                    printf("This id is already exist!\n");
                    break;
                }
                stu = newstu(&tempstu);
                addstu(&allstu,stu);
                break;
            case CMDDEL:
                printf("please input the id you wante to delete:");
                scanf("%s",tempstu.id);
                fgets(cmd,1024,stdin);
                p = findstu(&allstu,tempstu.id);
                if(NULL == p)
                {
                    printf("not found the id!\n");
                    break;
                }
                delstu(&allstu,p);
                break;
            case CMDFIND:
                printf("please input 1 for id,2 for name:");
                scanf("%d",&select);
                fgets(cmd,1024,stdin);
                if(1 == select)
                {
                    scanf("%s",tempstu.id);
                    fgets(cmd,1024,stdin);
                    p = findstu(&allstu,tempstu.id);
                    if(NULL == p)
                    {
                        printf("not found the id!\n");
                        break;
                    }
                    showstu(p);
                }
                /*
                else if(2 == select)
                {
                    printf("please input the name you want to find:");
                    scanf("%s",tempstu.name);
                    fgets(cmd,1024,stdin);
                    p = phead;
                    do
                    {
                        p = findnamenext(p,tempstu.name);
                        if(NULL != p)
                        {
                            showstu(p);
                            p = p->next;
                        }
                    }while(NULL != p);
                    break;
                }
                */
            case CMDSAVE:
                save(&allstu,studata);
                break;
            case CMDREVERSE:
                //phead = reverse(phead);
                break;
            case CMDHELP:
                showhelp();
                break;
            default:
                printf("not this cmd\n");
                break;
        }
    }
    fclose(studata);
}