C语言图书管理系统(不是文件)
作者:互联网
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
#define MAXSIZE 20
typedef struct
{
char No_book[100];
char Name[100];
float price;
}Elemtype;
typedef struct
{
Elemtype *data;
int listsize;
int length;
}Sqlist;
int initSqlist (Sqlist *L,int maxsize);
int insertSqlist (Sqlist *L,int i,Elemtype x);
int deleteSqlist (Sqlist *L,int i,Elemtype* x);
int getSqlist (Sqlist L,int i,Elemtype* x);
int locationSqlist (Sqlist L,char *NewName);
int disSqlist (Sqlist L);
int updateSqlist (Sqlist *L,int i,float x);
int menu();
main()
{
int n,maxsize;
maxsize=MAXSIZE;
int m,i,num;
float jg;
char name[10];
Sqlist L;
Elemtype x;
while (1)
{
n=menu();
switch (n)
{
case 1:
printf("【创建】\n");
initSqlist(&L,maxsize);
printf("请输入最初总书籍数目:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("请输入第%d本书的序号,书名,价格,用空格隔开:",i+1);
scanf("%s%s%f",x.No_book,x.Name,&x.price);
insertSqlist(&L,i+1,x);
}
getch();
break;
case 2:
printf("【插入】\n");
printf("请输入插入的位置(只能比已知位置小):");
scanf("%d",&m);
{printf("请输入需要插入的书本序号,名字,价格,用空格隔开:\n");
scanf("%s%s%f",x.No_book,x.Name,&x.price);
insertSqlist (&L,m,x);}
getch();
break;
case 3:
printf("【删除】\n");
printf("请输入需要删除的书籍名:");
scanf("%s",name);
getchar();
i=locationSqlist(L,name);
deleteSqlist (&L,i,&x);
printf("删除书籍数据为:");
printf("%10s%10s%7.2f\n",x.No_book,x.Name,x.price);
getch();
break;
case 4:
printf("【按书名查询】\n");
printf("请输入需要查找的书籍名:");
scanf("%s",name);
getchar();
i=locationSqlist(L,name);
getSqlist (L,i,&x);
printf("查找书籍数据为:");
printf("%10s%10s%7.2f\n",L.data[i-1].No_book,L.data[i-1].Name,L.data[i-1].price);
getch();
break;
case 5:
printf("【按顺序查询】\n");
printf("请输入需要查找的第几本书籍:");
scanf("%d",&i);
getSqlist (L,i,&x);
printf("查找书籍数据为:");
printf("%10s%10s%7.2f\n",x.No_book,x.Name,x.price);
getch();
break;
case 6:
printf("【修改价格】\n");
printf("输入需要修改的书籍名:");
scanf("%s",name);
printf("输入需要修改的信息:");
scanf("%f",&jg);
updateSqlist (&L,i, jg);
printf("修改书籍数据为:");
printf("%10s%10s%7.2f\n",L.data[i-1].No_book,L.data[i-1].Name,L.data[i-1].price);
getch();
break;
case 7:
printf("【显示全部数据】\n");
disSqlist ( L);
getch();
break;
case 0:
printf("【退出】\n");
getch();exit (0);
break;
default :
printf("【error】\n");
getch();
break;
}
}
}
int menu()
{
int n;
while(1)
{system("cls");
printf("****************************************\n");
printf("\t1.创建\t2.插入\n");
printf("\t3.删除\t4.按书名查询\n");
printf("\t5.按顺序查询\t6.修改价格\n");
printf("\t7.显示全部数据\t0.退出\n");
printf("****************************************\n");
printf("请选择(0~7):\n");
scanf("%d",&n);
if(n<0||n>7)
{
printf("输入有误");
getch();
}
else return n;
}
}
int initSqlist (Sqlist *L,int maxsize)
{
L->data=(Elemtype*)malloc(maxsize*sizeof(Elemtype));
if(L->data==NULL)
{
printf("初始化空间申请失败!");
exit (0);
}
L->listsize=maxsize;
L->length=0;
return 1;
}
int insertSqlist (Sqlist *L,int i,Elemtype x)//i是插入位置,对应下标i-1
{
int k;
if(i<1||i>L->length+1){printf("插入位置异常!\n");return 0;}
if(L->length>=L->listsize){printf("容量不够!\n");return 0;}
//将i-1,L->length-1内元素后移
for(k=L->length-1;k>=i-1;k--)
L->data[k+1]=L->data[k];
L->data[i-1]=x;
L->length=L->length+1;
return 1;
}
int deleteSqlist (Sqlist *L,int i,Elemtype *x)
{
int k;
if(L->length==0)
{
printf("没有数据,不能删除!\n");
return 0;
}
if(i<=0||i>L->length)
{
printf("位置异常!\n");
return 0;
}
*x=L->data[i-1];
for (k=i;k<L->length;k++)
L->data[k-1]=L->data[k];
L->length=L->length-1;
return 1;
}
int getSqlist (Sqlist L,int i,Elemtype *x)
{
if(L.length==0)
{
printf("没有数据!");return 0;
}
if(i<1||i>L.length)
{
printf("位置异常,不能查找!\n");return 0;
}
*x=L.data[i-1];
}
int locationSqlist (Sqlist L,char *NewName)
{
int i;
if(L.length==0)
{
printf("没有数据!");return 0;
}
for(i=0;i<L.length;i++)
if(strcmp(L.data[i].Name,NewName)==0)
{
return i+1;
}
return 0;
}
int disSqlist (Sqlist L)
{
int i;
if (L.length==0)
{
printf("没有数据!\n");return 0;
}
for (i=0;i<L.length;i++)
printf("%10s%10s%7.2f\n",L.data[i].No_book,L.data[i].Name,L.data[i].price);
return 1;
}
int updateSqlist (Sqlist *L,int i,float x)
{
if(L->length==0)
{
printf("没有数据,不能更新!\n");return 0;
}
if(i<1||i>L->length)
{
printf("位置不合理!\n");return 0;
}
L->data[i-1].price=x;
return 1;
}
标签:return,管理系统,int,C语言,length,Sqlist,printf,data,图书 来源: https://blog.csdn.net/wmx941/article/details/123645814