亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語(yǔ)言圖書管理系統(tǒng)實(shí)驗(yàn)

 更新時(shí)間:2022年03月11日 15:09:00   作者:王躍坤  
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言圖書管理系統(tǒng)實(shí)驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)驗(yàn)為大家分享了C語(yǔ)言圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)驗(yàn)要求:

1、圖書信息:包括ISBN、書名、主編、出版社、定價(jià)

2、功能:

(1)插入:若表中不存在新圖書信息(ISBN不同),則插入(表尾)新圖書信息。
(2)刪除:按ISBN刪除
(3)查找:按ISBN查找
(4)修改:按ISBN查找,然后修改各個(gè)屬性
(5)排序:按ISBN排序
(6)計(jì)數(shù):輸出圖書信息的個(gè)數(shù)
(7)導(dǎo)入:從TXT文件中讀取已有圖書信息(不同的屬性之間用tab鍵隔開(kāi))
(8)保存:將表中現(xiàn)有信息保存到txt文件中
(9)打?。涸谄聊簧巷@示所有圖書信息

3、擴(kuò)展功能(選做):

(1)增加按書名、主編、出版社進(jìn)行查找
(2)增加按書名、主編、出版社、定價(jià)排序

4、界面要求:簡(jiǎn)單的人機(jī)交互界面

#include <stdio.h>
#include <stdlib.h>
#include <string.h>?
? ?//頭文件?
? ?
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 50
#define LISTINCREMENT 10
? ?//宏定義,即定義常量?
? ?
typedef int status;
typedef struct {
?? ?char ISBN[15];
?? ?char bookname[30];
?? ?char writername[10];
?? ?char publisher[20];
?? ?float price;
} ElemType;
typedef struct {
?? ?ElemType *elem;
?? ?int length;
?? ?int listsize;?? ?
} SqList;?
? ? //定義結(jié)構(gòu)體類型,即 ?typedef 類型 ?類型名?
? ??
status ?InitList_Sq(SqList &L) ?? ?
{ ? //構(gòu)造一個(gè)空的線性表L。
?? ?L.elem=(ElemType *)?? ?malloc(LIST_INIT_SIZE*sizeof(ElemType));
?? ?//申請(qǐng)初始化長(zhǎng)度的內(nèi)存?
?? ?if(!L.elem){
?? ?//如果L的序列非空?
?? ??? ?exit(OVERFLOW);?? ?
?? ??? ?//說(shuō)明L被重定義了,拋出溢出?
?? ?}
?? ?L.length=0;
?? ?//默認(rèn)L內(nèi)有0個(gè)元素?? ?
?? ?L.listsize=LIST_INIT_SIZE;?
?? ?//默認(rèn)L的容量為初始化長(zhǎng)度?
?? ?return OK;
}?

int LocateElem_Sq(SqList L,char ISBN[])
{ ? ?//在順序表L中遍歷查找第1個(gè)值與e.ISBN相等的元素的位序,若找到,返回其位序,否則返回0;?
?? ?for(int i=0;i<L.length;i++){
?? ??? ?if(strcmp(ISBN,L.elem[i].ISBN)==0){
?? ??? ??? ?return i+1;
?? ??? ?}
?? ?}
?? ?return 0;
}

status ListInsert_Sq(SqList &L,ElemType e)
{//元素e插入表尾
?? ?ElemType *newbase;
?? ?if(LocateElem_Sq(L,e.ISBN)){
?? ??? ?return ERROR;
?? ?}
?? ?if(L.length>=L.listsize) {
?? ??? ?newbase =(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
?? ??? ?if(!newbase){
?? ??? ??? ?exit(OVERFLOW);
?? ??? ?}
?? ??? ?L.elem=newbase;
?? ??? ?L.listsize+=LISTINCREMENT;
?? ?}
?? ?L.elem[L.length]=e;
?? ?L.length++;
?? ?return OK;
}

status ListDelete_Sq(SqList &L,char ISBN[] ,ElemType &e)
{ ? //刪除L中ISBN的元素,并返回刪除元素;?
?? ?int i,j;
?? ?if((i=LocateElem_Sq(L,ISBN))==0){
?? ??? ?return ERROR;
?? ?}
?? ?
?? ?e=L.elem[i-1];
?? ?for(j=i;j<L.length;j++){
?? ??? ?L.elem[j-1]=L.elem[j];
?? ?}
?? ?L.length--;
?? ?return OK;
}

status SetElem_Sq(SqList &L,int i,ElemType e)
{ ? //修改在i的位置的元素?
?? ?if(i<1||i>L.length){
?? ??? ?return ERROR;
?? ?}
?? ?L.elem[i-1]=e;
?? ?return OK;
}

void sort_ISBN(SqList &L)
{?? ?//對(duì)表中所有元素進(jìn)行排序;?
?? ?int i,j,k;
?? ?ElemType e;
?? ?for(i=0;i<L.length-1;i++){
?? ??? ?k=i;
?? ??? ?for(j=i+1;j<L.length;j++){
?? ??? ??? ?if(strcmp(L.elem[k].ISBN,L.elem[j].ISBN)>0){
?? ??? ??? ??? ?k=j;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if(k!=i){
?? ??? ??? ?e=L.elem[i];
?? ??? ??? ?L.elem[i]=L.elem[k];
?? ??? ??? ?L.elem[k]=e;
?? ??? ?}?? ?
?? ?}
}?

void print(SqList L)
{?? ?//打印出所有元素
?? ?int i;
?? ?printf("\n圖書信息為:\n");
?? ?for(i=0;i<L.length;i++){
?? ??? ?printf("%-15s%-30s%-10s%-20s%-6.2f\n",L.elem[i].ISBN,L.elem[i].bookname,L.elem[i].writername,L.elem[i].publisher,L.elem[i].price);?
?? ?}?? ?
}

void import(SqList &L,char *filename)
{?? ?//從文件中導(dǎo)入信息?
?? ?FILE *fp;
?? ?int i;
?? ?char ISBN[15],bookname[30],writername[10],publisher[20];
?? ?float price;
?? ?if((fp=fopen(filename,"r"))==NULL){
?? ??? ?printf("文件不存在!\n");
?? ??? ?return ;
?? ?}
?? ?i=0;
?? ?while(!feof(fp)){
?? ??? ?fscanf(fp,"%s%s%s%s%f",ISBN,bookname,writername,publisher,&price);
?? ??? ?strcpy(L.elem[i].ISBN,ISBN);
?? ??? ?strcpy(L.elem[i].bookname,bookname);
?? ??? ?strcpy(L.elem[i].writername,writername);
?? ??? ?strcpy(L.elem[i].publisher,publisher);
?? ??? ?L.elem[i].price=price;
?? ??? ?i++;
?? ?}
?? ?L.length=i;
?? ?fclose(fp);
}

void save(SqList L,char *filename)
{ ??? ?//把信息保存到文件?
?? ?FILE *fp;
?? ?int i;
?? ?if((fp=fopen(filename,"w"))==NULL){
?? ??? ?printf("文件不存在!\n");
?? ??? ?return ;
?? ?}?
?? ?for(i=0;i<L.length;i++){
?? ??? ?fprintf(fp,"%-15s\t%-30s\t%-10s\t%-20s\t%-6.2f\n",L.elem[i].ISBN,L.elem[i].bookname,L.elem[i].writername,L.elem[i].publisher,L.elem[i].price);
?? ?}
?? ?fclose(fp);
?}?


int main(){
?? ?int ch;
?? ?SqList L;
?? ?char ISBN[15],filename[30];
?? ?int i;
?? ?ElemType e;
?? ?InitList_Sq(L);
?? ?printf("1. 插入 ? 2. 刪除 ? 3. 查找 ? 4. 修改元素 ? 5. 按ISBN排序元素 ? 6. 輸出 ? 7. 導(dǎo)入 ? 8. 保存 ? 9. 退出\n");
?? ?printf("請(qǐng)選擇:");
?? ?scanf("%d",&ch);
?? ?while(ch!=9){
?? ??? ?switch(ch){
?? ??? ?case 1:?? ?printf("請(qǐng)輸入要增加的元素");
?? ??? ??? ??? ?scanf("%s%s%s%s%f",e.ISBN,e.bookname,e.writername,e.publisher,&e.price);?
?? ??? ??? ??? ?if(ListInsert_Sq(L,e)==OK){
?? ??? ??? ??? ??? ?printf("插入成功!當(dāng)前信息為:\n");
?? ??? ??? ??? ??? ?print(L);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else {
?? ??? ??? ??? ??? ?printf("表中已存在該圖書!\n");
?? ??? ??? ??? ?}?
?? ??? ??? ??? ?break;
?? ??? ?case 2:?? ?printf("請(qǐng)輸入刪除的圖書ISBN:");
?? ??? ??? ??? ?scanf("%s",ISBN);
?? ??? ??? ??? ?if(ListDelete_Sq(L,ISBN,e)==OK)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?printf("刪除成功!被刪圖書為%s\t%s\t%s\t%s\t%.2f\n",e.ISBN,e.bookname,e.writername,e.publisher,e.price);
?? ??? ??? ??? ??? ?print(L);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?printf("該圖書不存在!\n");
?? ??? ??? ??? ?break;
?? ??? ?case 3:?? ?printf("請(qǐng)輸入要查找的圖書ISBN:");
?? ??? ??? ??? ?scanf("%s",&ISBN);
?? ??? ??? ??? ?i=LocateElem_Sq(L,ISBN);
?? ??? ??? ??? ?if(i!=0)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?printf("該圖書為%s\t%s\t%s\t%s\t%.2f\n",L.elem[i-1].ISBN,L.elem[i-1].bookname,L.elem[i-1].writername,L.elem[i-1].publisher,L.elem[i-1].price);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ??? ?printf("該圖書不存在!\n");
?? ??? ??? ??? ?break;
?? ??? ?case 4: printf("請(qǐng)輸入要修改的圖書ISBN:");
?? ??? ??? ??? ?scanf("%s",&ISBN);
?? ??? ??? ??? ?i=LocateElem_Sq(L,ISBN);
?? ??? ??? ??? ?if(i==0){
?? ??? ??? ??? ??? ?printf("該圖書不存在!\n");
?? ??? ??? ??? ?}?? ?
?? ??? ??? ??? ?else{
?? ??? ??? ??? ??? ?printf("請(qǐng)輸入圖書的ISBN,書名,主編,出版商和定價(jià)");
?? ??? ??? ??? ??? ?scanf("%s%s%s%s%f",e.ISBN,e.bookname,e.writername,e.publisher,e.price);
?? ??? ??? ??? ??? ?SetElem_Sq(L,i,e);
?? ??? ??? ??? ??? ?printf("修改成功,圖書信息為:");
?? ??? ??? ??? ??? ?print(L); ?? ?
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ?case 5:?? ?sort_ISBN(L);
?? ??? ??? ??? ?printf("按照ISBN排序后\n");
?? ??? ??? ??? ?print(L);
?? ??? ??? ??? ?break;
?? ??? ?case 6: print(L);
?? ??? ??? ??? ?break;
?? ??? ?case 7: printf("請(qǐng)輸入導(dǎo)入文件名:");
?? ??? ??? ??? ?scanf("%s",filename);
?? ??? ??? ??? ?import(L,filename);
?? ??? ??? ??? ?break;
?? ??? ?case 8: printf("請(qǐng)輸入導(dǎo)出文件名:");
?? ??? ??? ??? ?scanf("%s",filename);
?? ??? ??? ??? ?save(L,filename);
?? ??? ??? ??? ?break;?? ?
?? ??? ?default: printf("輸入錯(cuò)誤,請(qǐng)重新選擇\n");
?? ??? ?}
?? ??? ?printf("1. 插入 ? 2. 刪除 ? 3. 查找 ? 4. 修改元素 ? 5. 按ISBN排序元素 ? 6. 輸出 ? 7. 導(dǎo)入 ? 8. 保存 ? 9. 退出\n");
?? ??? ?printf("請(qǐng)選擇:");
?? ??? ?scanf("%d",&ch);
?? ?}
}?

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論