图书馆管理系统使用c

时间:2017-10-15 13:57:37

标签: c

我必须建立一个图书馆管理系统,只是为了练习而我(主要是)。每当我重新执行程序时,都会根据新的运行覆盖给予书籍ID,名称等的值。如何防止这种情况并将数据存储在最后一次运行中?

#include <stdio.h>
#include <stdlib.h>

typedef struct Book
{
 char *bname[40];
 int ssnid;
 int location;
 int issuedate;
 int issuemonth;
 int returndate;
 int returnmonth;
}book;
typedef struct Student
{
char *sname[30];
int sid;
int count;
}student;
void addbook(book lbook[10],int num)
{
int i;
FILE *fptr;
printf("enter the book name \t");
fflush(stdin);
gets(lbook[num].bname);
lbook[num].location=0;
lbook[num].issuedate=0;
lbook[num].issuemonth=0;
lbook[num].returndate=0;
lbook[num].returnmonth=0;
label:
printf("\nenter the SSN ID :\t");
scanf("%d",&lbook[num].ssnid);
for(i=0;i<num;i++)
{
    if(lbook[i].ssnid==lbook[num].ssnid)
     {
        printf("\nthis id has already been used\n");
        goto label;
     }
}
fptr=fopen("book.txt","a");
fprintf(fptr,"\nbook number %d \nbook name :\t",num+1);
fputs(lbook[num].bname,fptr);
fprintf(fptr,"\n book SSN ID %d",lbook[num].ssnid);
fclose(fptr);
}
  void addstudent(student lstudent[10],int snum)
 {
 int i;
 FILE *fptr;
 printf("enter the student name \t");
 fflush(stdin);
 gets(lstudent[snum].sname);
 lstudent[snum].count=0;
 label2:
 printf("\nenter the ID :\t");
 scanf("%d",&lstudent[snum].sid);
 for(i=0;i<snum;i++)
{
    if(lstudent[i].sid==lstudent[snum].sid)
    {
        printf("\nthis id has already been used\n");
        goto label2;
    }
}
fptr=fopen("student.txt","a");
fprintf(fptr,"\nstudent number %d \nstudent name :\t",snum+1);
fputs(lstudent[snum].sname,fptr);
fprintf(fptr,"\n student ID %d",lstudent[snum].sid);
fclose(fptr);
 }
  /*void delbook(book lbook[10],int num,int bnum)
 {
FILE *fptr;
int i,n;
for(n=num;n<bnum+1;n++)
{
    strcpy(lbook[10].bname,lbook[n].bname);
    lbook[10].ssnid=lbook[n].ssnid;
    strcpy(lbook[n].bname,lbook[n+1].bname);
    lbook[n].ssnid=lbook[n+1].ssnid;
    strcpy(lbook[n+1].bname,lbook[10].bname);
    lbook[n+1].ssnid=lbook[10].ssnid;

  }
  lbook[bnum].ssnid=0;
  fptr=fopen("book.txt","w");
for(i=0;i<bnum;i++)
{fprintf(fptr,"\nbook number %d \nbook name :\t",i+1);
fputs(lbook[i].bname,fptr);
fprintf(fptr,"\n book SSN ID %d",lbook[i].ssnid);
}
fclose(fptr);
}*/
 void issuebook(book lbook[10],student lstudent[10],int bnum,int snum)
{
int nb,i,ns,j;
int date,month;
FILE *cptr;
printf("\nenter book id to be issued\t");
scanf("%d",&nb);
for(i=0;i<bnum;i++)
{
    if(lbook[i].ssnid==nb)
    {
        if(lbook[i].location==0)
        {
            printf("\nenter the student id: \t");
        scanf("%d",&ns);
        for(j=0;j<snum;j++)
        {
            if(lstudent[j].sid==ns)
            {
                if(lstudent[j].count==0||lstudent[j].count==1)
                {
                    printf("\nenter date :\t");
                    scanf("%d",&lbook[i].issuedate);
                    printf("\nenter month :\t");
                    scanf("%d",&lbook[i].issuemonth);
                    lbook[i].location=lstudent[j].sid;
                lstudent[j].count++;
                cptr=fopen("log.txt","a");
                fprintf(cptr,"\nbook id %d issued by student id %d date on 
               %d month %d",lbook[i].ssnid,lstudent[j].sid,lbook[i].issuedate,lbook[i].issuemonth);
                fclose(cptr);
                }
            else{printf("\nlimit reached i.e. 2 books already issued\n");}
        }
    }
        }
    else{printf("book already issued\n");}
    }
   /* else
    {
        printf("\nbook not available\n");
    }*/
}
}

 void returnbook(book lbook[10],student lstudent[10],int bnum,int snum)
{
int nb,i,ns,j;
int date,month,dcount;
FILE *cptr;printf("\nenter book id to be returned\t");
scanf("%d",&nb);
for(i=0;i<bnum;i++)
{
    if(lbook[i].ssnid==nb)
    {
        if(lbook[i].location!=0)
        {
            printf("\nenter the student id: \t");
        scanf("%d",&ns);
        for(j=0;j<snum;j++)
        {
            if(lstudent[j].sid==ns)
            {
                //if(lstudent[j].count==0||lstudent[j].count==1)
                //{
                    printf("\nenter return date :\t");
                    scanf("%d",&lbook[i].returndate);
                    printf("\nenter return month :\t");
                    scanf("%d",&lbook[i].returnmonth);
                    printf("\nissue date \t%d",lbook[i].issuedate);
                    printf("\nissue month%d",lbook[i].issuemonth);
                    printf("\nenter days between issue and return\t");
                    scanf("%d",&dcount);
                    if(dcount>7)
                    {printf("fine :%d \t",(dcount-7)*20);}
                    lbook[i].location=0;
                lstudent[j].count--;
                cptr=fopen("log.txt","a");
  fprintf(cptr,"\nbook id %d returned by student id %d on date %d month 

 %d",lbook[i].ssnid,lstudent[j].sid,lbook[i].returndate,lbook[i].returnmonth
 );
if(dcount>7)
{fprintf(cptr,"\n fine payed of rs. %d ",(dcount-7)*20);}
                fclose(cptr);
                //}
             //else{printf("\nlimit reached i.e. 2 books already issued\n");}
        }
    }
        }
    else{printf("book already in library\n");}
    }
 }
 }
  void displaybook()
  {
  FILE *fptr;
  char ch;
  fptr=fopen("book.txt","r");
  ch=fgetc(fptr);
  while(ch!=EOF)
 {
    putchar(ch);
    ch=fgetc(fptr);
  }
  fclose(fptr);
  } 
  void displaystudent()
 {
FILE *fptr;
char ch;
fptr=fopen("student.txt","r");
ch=fgetc(fptr);
while(ch!=EOF)
{
    putchar(ch);
    ch=fgetc(fptr);
}
fclose(fptr);
}
int main()
{
book lbook[10];
student lstudent[10];
int bnum=0;
int snum=0;
char ch;
int i;
int delid;
int choice;
FILE *fptr;
fptr=fopen("book.txt","r");
for(i=0;i<10;i++)
{
    fscanf(fptr,"%d",lbook[i].ssnid);
    fscanf(fptr,"%d",lbook[i].issuedate);
    fscanf(fptr,"%d",lbook[i].issuemonth);
    fscanf(fptr,"%d",lbook[i].returndate);
    fscanf(fptr,"%d",lbook[i].returnmonth);
    fscanf(fptr,"%d",lbook[i].location);
    fgets(lbook[i].bname,1,fptr);
  }
  fclose(fptr);
  fptr=fopen("student.txt","r");
  for(i=0;i<10;i++)
 {
    fscanf(fptr,"%d",lstudent[i].sid);
    fscanf(fptr,"%d",lstudent[i].count);
    fgets(lstudent[i].sname,1,fptr);
 }
 fclose(fptr);
 printf("\t \t \tLIBRARY MANAGEMENT SYSTEM\n");
 do{
 printf("enter 1.for adding a book\tenter 2.for adding a student\n3.for 
deleting a book\t4.for issuing a book\n5.for returning book\n");
 printf("enter 6 display books\tenter 7 for display students\n");
 scanf("%d",&choice);
 switch(choice)
 {
 case 1:if(bnum<10)
{
    addbook(lbook,bnum);
    bnum++;
}
else{printf("\nbooks complete\n");}
    break;
case 2:if(snum<10)
{
    addstudent(lstudent,snum);
    snum++;
}
else{printf("\nstudents complete\n");}
    break;
  /* case 3: printf("\nenter id to delete\t");
    scanf("%d",&delid);
  delbook(lbook,delid,bnum);
  bnum--;
  break;*/
  case 4:issuebook(lbook,lstudent,bnum,snum);
    break;
    case 5:returnbook(lbook,lstudent,bnum,snum);
    break;
    case 6:displaybook();
        break;
    case 7:displaystudent();
        break;
    case 8:
        break;
}
printf("enter y to continue\t");
scanf("%s",&ch);
}while(ch=='y');
}

2 个答案:

答案 0 :(得分:0)

首先,我只是说你的答案需要Minimal, Complete, and Verifiable部分。

但接下来,我没有阅读你的所有代码,但这是我从你的问题中得到的:

我很确定你在存储记录方面遇到了麻烦。

一种选择是将其存储在像SQLite这样的数据库中(它作为文件存储在本地,因此不需要服务器)

答案 1 :(得分:0)

一个问题是用addbook(3个项目)写的文件与main(7个项目)中试图读取的文件不匹配。
通常不要混用scanffgets。使用fgets进行所有输入,并根据需要使用sscanf进行解析。不要使用gets

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Book
{
    char bname[40];
    int ssnid;
    int location;
    int issuedate;
    int issuemonth;
    int returndate;
    int returnmonth;
}book;
typedef struct Student
{
    char sname[30];
    int sid;
    int count;
}student;

void addbook(book lbook[10],int num)
{
    char line[200] = "";
    int i;
    int result = 0;
    FILE *fptr;

    printf("enter the book name \t");
    fflush(stdout);
    fgets(lbook[num].bname, sizeof lbook[num].bname, stdin);
    lbook[num].bname[strcspn ( lbook[num].bname, "\n")] = '\0';//remove newline
    lbook[num].location=0;
    lbook[num].issuedate=0;
    lbook[num].issuemonth=0;
    lbook[num].returndate=0;
    lbook[num].returnmonth=0;

    while ( 1) {
        do {
            printf("\nenter the SSN ID :\t");
            fgets ( line, sizeof line, stdin);
            result = sscanf(line, "%d",&lbook[num].ssnid);
        } while ( result != 1);
        for(i=0;i<num;i++)
        {
            if(lbook[i].ssnid==lbook[num].ssnid)
            {
                printf("\nthis id has already been used\n");
                continue;
            }
        }
        break;
    }
    fptr=fopen("book.txt","a");
    fprintf(fptr,"%d ",lbook[i].ssnid);
    fprintf(fptr,"%d ",lbook[i].issuedate);
    fprintf(fptr,"%d ",lbook[i].issuemonth);
    fprintf(fptr,"%d ",lbook[i].returndate);
    fprintf(fptr,"%d ",lbook[i].returnmonth);
    fprintf(fptr,"%d ",lbook[i].location);
    fprintf(fptr,"%s\n",lbook[num].bname);
    fclose(fptr);
}

void addstudent(student lstudent[10],int snum)
{
    char line[200] = "";
    int i;
    int result = 0;
    FILE *fptr;

    printf("enter the student name \t");
    fflush(stdout);
    fgets(lstudent[snum].sname, sizeof lstudent[snum].sname, stdin);
    lstudent[snum].sname[strcspn ( lstudent[snum].sname, "\n")] = '\0';//remove newline
    lstudent[snum].count=0;
    while ( 1) {
        do {
            printf("\nenter the ID :\t");
            fgets ( line, sizeof line, stdin);
            result = sscanf(line, "%d",&lstudent[snum].sid);
        } while ( result != 1);
        for(i=0;i<snum;i++)
        {
            if(lstudent[i].sid==lstudent[snum].sid)
            {
                printf("\nthis id has already been used\n");
                continue;
            }
        }
        break;
    }
    fptr=fopen("student.txt","a");
    fprintf(fptr,"%d %d %s\n",lstudent[snum].sid, lstudent[snum].count, lstudent[snum].sname);
    fclose(fptr);
}

void issuebook(book lbook[10],student lstudent[10],int bnum,int snum)
{
    char line[200] = "";
    int nb,i,ns,j;
    int result = 0;
    FILE *cptr;
    do {
        printf("\nenter book id to be issued\t");
        fgets ( line, sizeof line, stdin);
        result = sscanf( line, "%d",&nb);
    } while ( result != 1);
    for(i=0;i<bnum;i++)
    {
        if(lbook[i].ssnid==nb)
        {
            if(lbook[i].location==0)
            {
                do {
                    printf("\nenter the student id: \t");
                    fgets ( line, sizeof line, stdin);
                    result = sscanf(line, "%d",&ns);
                } while ( result != 1);
                for(j=0;j<snum;j++)
                {
                    if(lstudent[j].sid==ns)
                    {
                        if(lstudent[j].count==0||lstudent[j].count==1)
                        {
                            do {
                                printf("\nenter date :\t");
                                fgets ( line, sizeof line, stdin);
                                result = sscanf(line, "%d",&lbook[i].issuedate);
                            } while ( result != 1);
                            do {
                                printf("\nenter month :\t");
                                fgets ( line, sizeof line, stdin);
                                result = sscanf(line, "%d",&lbook[i].issuemonth);
                            } while ( result != 1);
                            lbook[i].location=lstudent[j].sid;
                            lstudent[j].count++;
                            cptr=fopen("log.txt","a");
                            fprintf(cptr,"\nbook id %d issued by student id %d date on %d month %d"
                            ,lbook[i].ssnid,lstudent[j].sid,lbook[i].issuedate,lbook[i].issuemonth);
                            fclose(cptr);
                        }
                        else{
                            printf("\nlimit reached i.e. 2 books already issued\n");
                        }
                    }
                }
            }
            else{printf("book already issued\n");}
        }
    }
}

void returnbook(book lbook[10],student lstudent[10],int bnum,int snum)
{
    int nb,i,ns,j;
    int dcount;
    int result = 0;
    char line[200] = "";
    FILE *cptr;
    do {
        printf("\nenter book id to be returned\t");
        fgets ( line, sizeof line, stdin);
        result = sscanf(line, "%d",&nb);
    } while ( result != 1);
    for(i=0;i<bnum;i++)
    {
        if(lbook[i].ssnid==nb)
        {
            if(lbook[i].location!=0)
            {
                do {
                    printf("\nenter the student id: \t");
                    fgets ( line, sizeof line, stdin);
                    result = sscanf(line, "%d",&ns);
                } while ( result != 1);
                for(j=0;j<snum;j++)
                {
                    if(lstudent[j].sid==ns)
                    {
                        do {
                            printf("\nenter return date :\t");
                            fgets ( line, sizeof line, stdin);
                            result = sscanf(line, "%d",&lbook[i].returndate);
                        } while ( result != 1);
                        do {
                            printf("\nenter return month :\t");
                            fgets ( line, sizeof line, stdin);
                            result = sscanf(line, "%d",&lbook[i].returnmonth);
                        } while ( result != 1);
                        printf("\nissue date \t%d",lbook[i].issuedate);
                        printf("\nissue month%d",lbook[i].issuemonth);
                        do {
                            printf("\nenter days between issue and return\t");
                            fgets ( line, sizeof line, stdin);
                            result = sscanf(line, "%d",&dcount);
                        } while ( result != 1);
                        if(dcount>7)
                        {
                            printf("fine :%d \t",(dcount-7)*20);
                        }
                        lbook[i].location=0;
                        lstudent[j].count--;
                        cptr=fopen("log.txt","a");
                        fprintf(cptr,"\nbook id %d returned by student id %d on date %d month %d"
                        ,lbook[i].ssnid,lstudent[j].sid,lbook[i].returndate,lbook[i].returnmonth
                        );
                        if(dcount>7)
                        {
                            fprintf(cptr,"\n fine payed of rs. %d ",(dcount-7)*20);
                        }
                        fclose(cptr);
                    }
                }
            }
            else{printf("book already in library\n");}
        }
    }
}

void displaybook()
{
    FILE *fptr;
    char line[200] = "";
    if ( NULL != ( fptr=fopen("book.txt","r"))) {
        while(fgets ( line, sizeof line, fptr))
        {
            printf ( "%s",line);
        }
        fclose(fptr);
    }
    else {
        printf ( "no books to show\n");
    }
}

void displaystudent()
{
    FILE *fptr;
    char line[200] = "";
    if ( NULL != ( fptr=fopen("student.txt","r"))) {
        while(fgets ( line, sizeof line, fptr))
        {
            printf ( "%s",line);
        }
        fclose(fptr);
    }
    else {
        printf ( "no students to show\n");
    }
}

int main()
{
    book lbook[10];
    student lstudent[10];
    int bnum=0;
    int snum=0;
    char line[200] = "";
    int i;
    FILE *fptr;
    if ( NULL != ( fptr=fopen("book.txt","r"))) {
        i = 0;
        while ( i < 10 && fgets ( line, sizeof line, fptr)) {
            if ( 7 == sscanf(line,"%d %d %d %d %d %d %39[^\n]"
            , &lbook[i].ssnid
            , &lbook[i].issuedate
            , &lbook[i].issuemonth
            , &lbook[i].returndate
            , &lbook[i].returnmonth
            , &lbook[i].location
            , lbook[i].bname)) {
                i++;
            }
        }
        fclose(fptr);
    }
    if ( NULL != ( fptr=fopen("student.txt","r"))) {
        i = 0;
        while ( i < 10 && fgets ( line, sizeof line, fptr)) {
            if ( 3 == sscanf(line,"%d %d %29[^\n]"
            , &lstudent[i].sid
            , &lstudent[i].count
            , lstudent[i].sname)) {
                i++;
            }
        }
        fclose(fptr);
    }
    do{
        printf("\n\n\t\t\tLIBRARY MANAGEMENT SYSTEM\n");
        printf ( "\t\t1 (for adding a book)\n\t\t2 (for adding a student)\n");
        printf ( "\t\t3 (for deleting a book)\n\t\t4 (for issuing a book)\n\t\t5 (for returning book)\n");
        printf ( "\t\t6 (display books)\n\t\t7 (for display students)\n\t\t8 ( quit)\n");
        printf("\tchoice: ");
        fflush ( stdout);
        fgets ( line, sizeof line, stdin);
        switch( line[0])
        {
            case '1':
                if(bnum<10)
                {
                    addbook(lbook,bnum);
                    bnum++;
                }
                else{printf("\nbooks complete\n");}
                break;
            case '2':
                if(snum<10)
                {
                    addstudent(lstudent,snum);
                    snum++;
                }
                else{printf("\nstudents complete\n");}
                break;
            case '4':
                issuebook(lbook,lstudent,bnum,snum);
                break;
            case '5':
                returnbook(lbook,lstudent,bnum,snum);
                break;
            case '6':
                displaybook();
                break;
            case '7':
                displaystudent();
                break;
            case '8':
                return 0;
            default:
                printf ( "\n\tnot a valid choice\n\n");
                break;
        }
        printf("enter y to continue\t");
        fgets ( line, sizeof line, stdin);
    }while(line[0]=='y');
}