北林OJ204、211、212、214
原创
©著作权归作者所有:来自51CTO博客作者liangliang凉凉的原创作品,谢绝转载,否则将追究法律责任
204
#include<iostream>
#include <string>
#include<iomanip>
#define Maxsize 100
using namespace std;
struct Book
{
string no;
string name;
double price;
};
struct sqlist
{
Book elem[Maxsize];
int length;
};
//初始化
void Init(sqlist & L)
{
L.length=0;
}
//输入
void input(sqlist & L)
{
while(1)
{
cin>>L.elem[L.length].no>>L.elem[L.length].name>>L.elem[L.length].price;
if(L.elem[L.length].no=="0"&&L.elem[L.length].name=="0"&&L.elem[L.length].price==0)
break;
L.length++;
}
}
//输出
void output(const sqlist &L)
{
cout<<L.length<<endl;
for(int i=0;i<L.length;i++)
{
cout<<L.elem[i].no<<" "<<L.elem[i].name<<" "<<fixed<<setprecision(2)<<L.elem[i].price<<endl;
}
}
int main()
{
sqlist L1;
Init(L1);
input( L1);
output(L1);
return 0;
}
211
#include <iostream>
#include <string>
#include <cassert>
#include<iomanip>
const int Maxsize = 1000;
using namespace std;
//图书
struct Book
{
string no;
string name;
double price;
};
//顺序表
struct sqlist
{
Book* pbook;
int length;
};
//初始化
int Initsqlist(sqlist& L)
{
//L.pbook = (Book *)malloc(sizeof(Book)*Maxsize);//开辟空间
L.pbook = new Book[Maxsize];
/*if (!L.pbook)
return 0;*/
assert(L.pbook);
L.length = 0;
return 1;
}
//插入
int Insertsqlist(sqlist& L, int pos, const Book &e)
{
//判断是否合理
assert(L.pbook);
if (pos<1 || pos>=L.length+1)
return 0;
if (L.length == Maxsize)
return 0;
//尾插
else if (pos == L.length)
{
L.pbook[L.length] = e;
L.length++;
return 1;
}
else
{
//移动:从最后一个元素向后移动
for (int i = L.length - 1; i >= pos - 1; i--)
{
L.pbook[i + 1] = L.pbook[i];
}
//插入
L.pbook[pos - 1] = e;
L.length++;
return 1;
}
}
void Printsqlist(const sqlist& L)
{
assert(L.pbook);
for (int i = 0; i <= L.length - 1; i++)
cout << L.pbook[i].no << " " << L.pbook[i].name << " " <<
fixed << setprecision(2) << L.pbook[i].price<< endl;
}
int main()
{
int n=0;
Book e;
sqlist L;
//初始化
Initsqlist(L);
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> e.no >> e.name >> e.price;
L.pbook[i] = e;
L.length++;
/*Insertsqlist(L, i + 1, e);*/
}
int pos = 0;
cin >> pos;
//输入新书的信息
cin >> e.no >> e.name >> e.price ;
int flag=Insertsqlist(L, pos, e);
if (0 == flag)
cout << "Sorry,the position to be inserted is invalid!" << endl;
else
Printsqlist(L);
return 0;
}
212
#include <iostream>
#include <string>
#include <cassert>
#include<iomanip>
const int Maxsize = 1000;
using namespace std;
//图书
struct Book
{
string no;
string name;
double price;
};
//顺序表
struct sqlist
{
Book* pbook;
int length;
};
//初始化
int Initsqlist(sqlist& L)
{
//L.pbook = (Book *)malloc(sizeof(Book)*Maxsize);//开辟空间
L.pbook = new Book[Maxsize];
/*if (!L.pbook)
return 0;*/
assert(L.pbook);
L.length = 0;
return 1;
}
//插入
int Insertsqlist(sqlist& L, int pos, const Book &e)
{
//判断是否合理
assert(L.pbook);
if (pos<1 || pos>=L.length+1)
return 0;
if (L.length == Maxsize)
return 0;
//尾插
else if (pos == L.length)
{
L.pbook[L.length] = e;
L.length++;
return 1;
}
else
{
//移动:从最后一个元素向后移动
for (int i = L.length - 1; i >= pos - 1; i--)
{
L.pbook[i + 1] = L.pbook[i];
}
//插入
L.pbook[pos - 1] = e;
L.length++;
return 1;
}
}
void Printsqlist(const sqlist& L)
{
assert(L.pbook);
for (int i = 0; i <= L.length - 1; i++)
cout << L.pbook[i].no << " " << L.pbook[i].name << " " <<
fixed << setprecision(2) << L.pbook[i].price<< endl;
}
int deletesqlist(sqlist& L, int pos)
{
/*assert(L.pbook);*/
if (!L.pbook)
return 0;
//判断位置是否合理
if (pos<1 || pos>L.length)
return 0;
//移动元素
for (int i = pos; i <L.length; i++)
L.pbook[i - 1] = L.pbook[i];
L.length--;
return 1;
}
int main()
{
int n=0;
Book e;
sqlist L;
//初始化
Initsqlist(L);
//图书总数
cin >> n;
//图书入库
for (int i = 0; i < n; i++)
{
cin >> e.no >> e.name >> e.price;
L.pbook[i] = e;
L.length++;
/*Insertsqlist(L, i + 1, e);*/
}
int pos = 0;
cin >> pos;
//删除
int flag=deletesqlist(L, pos);
////输入新书的信息
//cin >> e.no >> e.name >> e.price ;
//int flag=Insertsqlist(L, pos, e);
if (0 == flag)
cout << "Sorry,the position to be deleted is invalid!" << endl;
else
Printsqlist(L);
return 0;
}
214
#include <iostream>
#include<cassert>
#include <iostream>
#include <string>
#include <cassert>
#include<iomanip>
using namespace std;
//图书
typedef struct Book
{
string no;
string name;
double price;
}ElemType;
//链表
struct LNode
{
//数据域
ElemType data;
LNode* next;
};
//初始化
void InitList(LNode *& L)
{
//创建头节点
L= new LNode;
L->next = NULL;//头结点的指针域置空
}
//尾插
/*int PushbackList(LNode &L,ElemType e)
{
//尾指针
LNode * tail=L
while(1)
{
//新节点
LNode *r=new LNode;
cin>>e.no>>e.name>>e.price;
r->data=e;
}
}*/
//遍历--计算长度
int TraversalList(LNode *& L)
{
LNode* tmp = L->next;
int count = 0;
while (tmp)
{
tmp = tmp->next;
count++;
}
return count;
}
//尾插
int PushbackList(LNode*& L, ElemType e)
{
LNode* tmp = L;//error
int count = 0;
while (tmp->next)//tmp指向最后一个节点
{
tmp = tmp->next;
}
LNode *pnode=new LNode;
pnode->data = e;
pnode->next = NULL;
//插入
tmp->next = pnode;
return 1;
}
//打印链表
void PrintList(LNode*& L)
{
LNode* tmp = L->next;
while (tmp)
{
//打印
cout << tmp->data.no <<" " << tmp->data.name <<" " << fixed << setprecision(2) << tmp->data.price << endl;
tmp = tmp->next;
}
}
int main()
{
//链表
LNode* L;
//初始化
InitList(L);
ElemType e;
//读入图书信息,创建链表
while (1)
{
cin >> e.no >> e.name >> e.price;
if (e.no == "0" && e.name == "0" && e.price == 0)
break;
PushbackList(L, e);
}
//统计图书个数
int length = TraversalList(L);
//打印个数及图书信息
cout << length << endl;
PrintList(L);
return 0;
}