基于顺序存储结构的图书信息表的排序
#include<iostream>
#include<sstream>
#include<stdlib.h>
#include<string.h>#define MAXSIZE 1000 //顺序表所能容纳的最大长度
#define OK 0
typedef int status;using namespace std;typedef struct{char no[20];char name[50];float price;
}Book;typedef struct{Book* elem;int len;
}SqList; status InitList(SqList*L){L->elem=(Book*)malloc(MAXSIZE*sizeof(Book));if(!L->elem)exit(-1);L->len=0;return OK;
}status CreateList(SqList*L,char* no,char* name,float price){Book b;strcpy(b.no,no);strcpy(b.name,name);b.price=price;L->elem[L->len]=b;L->len++;return OK;
}status PrintList(SqList*L){//cout<<L->len<<endl;for(int i=0;i<L->len;i++){printf("%s %s %.2f\n", L->elem[i].no, L->elem[i].name, L->elem[i].price);}return OK;
}status SortList(SqList* L){for(int i=0;i<L->len-1;i++){for(int j=0;j<L->len-1-i;j++){if(L->elem[j].price<L->elem[j+1].price){Book book;book=L->elem[j];L->elem[j]=L->elem[j+1];L->elem[j+1]=book;}}}return OK;
}int main(){char no[20];char name[50];float price;SqList L;InitList(&L);while (1){cin>>no>>name>>price;if(!strcmp(no,"0")&&!strcmp(name,"0")&&price==0.0)break;CreateList(&L,no,name,price);}SortList(&L);PrintList(&L);return 0;
}
知识点:冒泡排序
基于顺序存储结构的图书信息表的修改
#include<iostream>
#include<sstream>
#include<stdlib.h>
#include<string.h>#define MAXSIZE 1000 //顺序表所能容纳的最大长度
#define OK 0
typedef int status;using namespace std;typedef struct{char no[20];char name[50];float price;
}Book;typedef struct{Book* elem;int len;
}SqList; status InitList(SqList*L){L->elem=(Book*)malloc(MAXSIZE*sizeof(Book));if(!L->elem)exit(-1);L->len=0;return OK;
}status CreateList(SqList*L,char* no,char* name,float price){Book b;strcpy(b.no,no);strcpy(b.name,name);b.price=price;L->elem[L->len]=b;L->len++;return OK;
}status PrintList(SqList*L){//cout<<L->len<<endl;for(int i=0;i<L->len;i++){printf("%s %s %.2f\n", L->elem[i].no, L->elem[i].name, L->elem[i].price);}return OK;
}/*status SortList(SqList* L){for(int i=0;i<L->len-1;i++){for(int j=0;j<L->len-1-i;j++){if(L->elem[j].price<L->elem[j+1].price){Book book;book=L->elem[j];L->elem[j]=L->elem[j+1];L->elem[j+1]=book;}}}return OK;
}*///图书价格处理
status PriceChange(SqList* L,float Aprice){for(int i=0;i<L->len;i++){if(L->elem[i].price<Aprice)L->elem[i].price*=1.2;elseL->elem[i].price*=1.1;}
} int main(){char no[20];char name[50];float price;float sum=0.0;SqList L;InitList(&L);while (1){cin>>no>>name>>price;if(!strcmp(no,"0")&&!strcmp(name,"0")&&price==0.0)break;CreateList(&L,no,name,price);sum+=price;}float ans=sum/(L.len);printf("%.2f\n",ans);PriceChange(&L,ans);PrintList(&L);return 0;
}
基于顺序存储结构的图书信息表的逆序存储
#include<iostream>
#include<sstream>
#include<stdlib.h>
#include<string.h>#define MAXSIZE 1000 //顺序表所能容纳的最大长度
#define OK 0
typedef int status;using namespace std;typedef struct{char no[20];char name[50];float price;
}Book;typedef struct{Book* elem;int len;
}SqList; status InitList(SqList*L,int sum){L->elem=(Book*)malloc(sum*sizeof(Book));if(!L->elem)exit(-1);L->len=sum;return OK;
}status CreateList(SqList*L,char* no,char* name,float price){Book b;strcpy(b.no,no);strcpy(b.name,name);b.price=price;L->elem[L->len-1]=b;L->len--;return OK;
}status PrintList(SqList*L,int sum){//cout<<L->len<<endl;for(int i=0;i<sum;i++){printf("%s %s %.2f\n", L->elem[i].no, L->elem[i].name, L->elem[i].price);}return OK;
}//排序
/*status SortList(SqList* L){for(int i=0;i<L->len-1;i++){for(int j=0;j<L->len-1-i;j++){if(L->elem[j].price<L->elem[j+1].price){Book book;book=L->elem[j];L->elem[j]=L->elem[j+1];L->elem[j+1]=book;}}}return OK;
}*///图书价格处理
/*status PriceChange(SqList* L,float Aprice){for(int i=0;i<L->len;i++){if(L->elem[i].price<Aprice)L->elem[i].price*=1.2;elseL->elem[i].price*=1.1;}
} */int main(){char no[20];char name[50];float price;SqList L;int sum; //图书总数目 cin>>sum;InitList(&L,sum);int cnt=0;while (cnt<sum){cnt++;cin>>no>>name>>price;CreateList(&L,no,name,price);}PrintList(&L,sum);return 0;
}
基于顺序存储结构的图书信息表的最贵图书的查找
#include<iostream>
#include<sstream>
#include<stdlib.h>
#include<string.h>#define MAXSIZE 1000 //顺序表所能容纳的最大长度
#define OK 0
typedef int status;using namespace std;typedef struct{char no[20];char name[50];float price;
}Book;typedef struct{Book* elem;int len;
}SqList; status InitList(SqList*L){L->elem=(Book*)malloc(MAXSIZE*sizeof(Book));if(!L->elem)exit(-1);L->len=0;return OK;
}status CreateList(SqList*L,char* no,char* name,float price){Book b;strcpy(b.no,no);strcpy(b.name,name);b.price=price;L->elem[L->len]=b;L->len++;return OK;
}status ExpensiveList(SqList*L1,SqList*L2,float max){for(int i=0;i<L1->len;i++){if(L1->elem[i].price==max){L2->elem[L2->len]=L1->elem[i];L2->len++;}}return OK;
}status PrintExpensive(SqList*L){//cout<<L->len<<endl;cout<<L->len<<endl;for(int i=0;i<L->len;i++){printf("%s %s %.2f\n", L->elem[i].no, L->elem[i].name, L->elem[i].price);}return OK;
}int main(){char no[20];char name[50];float price;SqList L; //总表 InitList(&L);int n;cin>>n;float max=0.0; //维护最高价 int cnt=0; while (cnt<n){cnt++; cin>>no>>name>>price;if(!strcmp(no,"0")&&!strcmp(name,"0")&&price==0.0)break;CreateList(&L,no,name,price);if(price>max)max=price;}SqList newL;InitList(&newL);ExpensiveList(&L,&newL,max);PrintExpensive(&newL);return 0;
}
基于顺序存储结构的图书信息表的最爱图书的查找(这个用了纯C++风格字符串)
这样就很容易用数组来存储待查喜爱书名表了(真的搞不清char* char[] string[]之间的赋值和比较TAT)
#include <iostream>
#include<iomanip>
using namespace std;
#define MAXSIZE 10000typedef struct
{string no;string name;double price;
}Book;typedef struct
{Book *elem;int num;
}SqList;void Input(SqList &L)
{int n;int i=0;cin>>n;string a,b;double c;while(i<n){ cin>>a>>b>>c;if(i==n) break;L.elem[L.num].no=a;L.elem[L.num].name=b;L.elem[L.num].price=c;L.num++;i++;}return ;
}void Output(SqList &L)
{int i;for(i=0;i<L.num-1;i++)cout<<L.elem[i].no<<" "<<L.elem[i].name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<L.elem[i].price<<endl;cout<<L.elem[L.num-1].no<<" "<<L.elem[L.num-1].name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<L.elem[L.num-1].price<<endl;return ;
}void InitList(SqList &L)
{L.elem=new Book[MAXSIZE];if(!L.elem) return;L.num=0;return;
}void favourite(SqList &L)//查找同名的多个目标
{int n;cin>>n;//多个查找目标 的数量string nam[n]; //用数组存储多个查找目标的名称 int loc[MAXSIZE]; //不同于flag数组,用loc数组配合计数器k从0存储多个目标的下标 int i;for(i=0;i<n;i++){cin>>nam[i];//输入多个查找目标 }int k=0;//计数:查找到的目标的个数 int j;for(j=0;j<n;j++)//正在查找第j个目标 {for(i=0;i<L.num;i++)//遍历顺序表,对目标j 进行查找 {if(nam[j]==L.elem[i].name){loc[k]=i;//找到目标,用k计数并依次存储目标的下标 k++;}}if(k==0)cout<<"Sorry,there is no your favourite!"<<endl;else{cout<<k<<endl;for(i=0;i<k;i++)cout<<L.elem[loc[i]].no<<" "<<L.elem[loc[i]].name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<L.elem[loc[i]].price<<endl;}k=0; }}int main()
{SqList L;InitList(L);Input(L); favourite(L); return 0;
}
基于顺序存储结构的图书信息表的最佳位置图书的查找
这和上一道类似,这两个都是用一个数组存储待查表,然后遍历,找到的就输出
很简单,注意函数的封装,让主函数整洁一点
#include<iostream>
#include<string>
#include<stdio.h>
#include<iomanip>
#define MAXSIZE 1000
#define OK 1
#define OVERFLOW -2
using namespace std;typedef struct
{string isbn; string name;double price;
}Book;typedef struct
{ Book *elem; int length;
}SqList; int InitList(SqList &L)
{ L.elem=new Book[MAXSIZE]; if(!L.elem)exit(OVERFLOW); L.length=0; return OK;
}int Input(SqList &L)
{int i,number,count;cin>>number;for( i = 0 ; i<number; i++){ cin>>L.elem[i].isbn>>L.elem[i].name>>L.elem[i].price;L.length++;}return OK;
}
int Locate(SqList L){int loc[MAXSIZE];int count,i;cin>>count;for ( i = 0 ;i<count ; i++)cin>>loc[i];for( i = 0 ; i<count ; i++){if(loc[i]<1||loc[i]>L.length) cout<<"Sorry,the book on the best position doesn't exist!"<<endl;elsecout<<L.elem[loc[i]-1].isbn <<" "<< L.elem[loc[i]-1].name <<" "<< fixed<<setprecision(2)<<L.elem[loc[i]-1].price<<endl;}return OK;
}int main()
{SqList L; InitList(L); Input(L); Locate(L); return 0;
}
其中整体查找遍历的思想大同小异,复健ing...