数据结构必做题参考:实验一T1-20,实验2 T1
#include <bits/stdc++.h>
using namespace std;
const int N=;
struct Book
{
string isbn;
string name;
double price;
}b[N],r[N];
class book
{
private:
string isbn;
string name;
double price;
int length;
book *next;
static book *head;
static book *tail;
public:
book():next(NULL)
{
head=tail=this;
}
book(string a,string b,double c):isbn(a),name(b),price(c),next(NULL){}
void Input()
{
head->next=new book("","",-);
tail=head->next;
string a,b;
double c;
/*length=0;//T1-3
while(cin>>a>>b>>c)
{
if(a=="0"&&b=="0"&&c==0)return;
tail->next=new book(a,b,c);
tail=tail->next;
length++;
}*/
/*cin>>length;//T4-9
for(int i=0;i<length;++i)
{
cin>>a>>b>>c;
tail->next=new book(a,b,c);
tail=tail->next;
}*/
}
void Output()
{
cout<<length<<endl;//T1,10
book *now=head->next;
while(now->next)
{
now=now->next;
cout<<now->isbn<<" "<<now->name<<" "<<now->price<<endl;
}
}
void BubbleSort()
{
book *now;
book *tmp1;
book *tmp2;
double temp1;
string temp2,temp3;
for(int i=;i<length;++i)
{
now=head->next->next;
for(int j=;j<length-i;++j)
{
tmp1=now;
tmp2=now->next;
if(tmp1->price<tmp2->price)
{
temp1=tmp1->price;
tmp1->price=tmp2->price;
tmp2->price=temp1;
temp2=tmp1->isbn;
tmp1->isbn=tmp2->isbn;
tmp2->isbn=temp2;
temp3=tmp1->name;
tmp1->name=tmp2->name;
tmp2->name=temp3;
}
now=now->next;
}
}
}
void Sort(int s,int t)
{
if(s==t)return;
int m=s+(t-s)/;
Sort(s,m);
Sort(m+,t);
int i=s,j=m+,k=s;
while(i<=m&&j<=t)r[k++]=b[i].price>=b[j].price?b[i++]:b[j++];
while(i<=m)r[k++]=b[i++];
while(j<=t)r[k++]=b[j++];
for(int ii=s;ii<=t;++ii)b[ii]=r[ii];
}
void MergeSort()
{
int i=;
book *now=head->next;
while(now->next)
{
now=now->next;
b[i].isbn=now->isbn;
b[i].name=now->name;
b[i++].price=now->price;
}
Sort(,length-);
now=head->next;
i=;
while(now->next)
{
now=now->next;
now->isbn=b[i].isbn;
now->name=b[i].name;
now->price=b[i++].price;
}
}
void Update()
{
double sum=;
book *now=head->next;
while(now->next)
{
now=now->next;
sum+=now->price;
if(now->next==NULL)break;
}
sum/=length;
cout<<sum<<endl;
now=head->next;
while(now->next)
{
now=now->next;
now->price*=now->price<sum?1.2:1.1;
if(now->next==NULL)break;
}
}
void Inverse()
{
book *p=head->next;//头结点
book *q=p->next;//首元节点
book *t;
while(q!=NULL)
{//依次调整链表指针指向
t=q->next;
q->next=p;
p=q;
q=t;
}
head->next->next->next=NULL;//调整链表尾和头
head->next->next=p;//
}
void inverse()
{
book *pre=NULL;
book *cur=head->next->next;
book*curnext=cur->next;
while(cur!=NULL)
{//依次调整链表指针指向
cur->next=pre;
pre=cur;
cur=curnext;
if(curnext!=NULL)curnext=curnext->next;
}
head->next->next=pre;//调整头结点
}
void QueryMax()
{
book *now=head->next;
double mmax=;
int num=;
while(now->next)
{
now=now->next;
if((now->price)>mmax)
{
mmax=now->price;
num=;
}
else if(now->price==mmax)
{
num++;
}
}
now=head;
cout<<num<<endl;
while(now->next)
{
now=now->next;
if(now->price==mmax)
{
cout<<now->isbn<<" "<<now->name<<" "<<now->price<<endl;
}
}
}
void findfond()
{
int m;
cin>>m;
for(int i=;i<m;++i)
{
string name;
cin>>name;
book *now=head->next;
int num=;
while(now->next)
{
now=now->next;
if(now->name==name)num++;
}
if(num==)
{
cout<<"Sorry,there is no your favourite!"<<endl;
}
else
{
cout<<num<<endl;
now=head->next;
while(now->next)
{
now=now->next;
if(now->name==name)
{
cout<<now->isbn<<" "<<now->name<<" "<<now->price<<endl;
}
}
}
}
}
void Query()
{
int m;
cin>>m;
for(int i=;i<m;++i)
{
int num;
cin>>num;
if(num<||num>length)
{
cout<<"Sorry,the book on the best position doesn't exist!"<<endl;
}
else
{
book *now=head->next;
while(num--)
{
now=now->next;
}
cout<<now->isbn<<" "<<now->name<<" "<<now->price<<endl;
}
}
}
void Insert()
{
int pos;
string a,b;
double c;
cin>>pos>>a>>b>>c;
if(pos<||pos>length+)
{
cout<<"Sorry,the position to be inserted is invalid!"<<endl;
}
else
{
book *now=head->next;
while(--pos)now=now->next;
book *t=new book(a,b,c);
t->next=now->next;
now->next=t;
length++;
Output();
}
}
void Delete()
{
int pos;
cin>>pos;
if(pos<||pos>length)
{
cout<<"Sorry,the position to be deleted is invalid!"<<endl;
}
else
{
book *now=head->next;
while(--pos)now=now->next;
book *t=now->next->next;
delete now->next;
now->next=t;
length--;
Output();
}
}
void Unique()
{
head->next=new book("","",-);
tail=head->next;
string a,b;
double c;
cin>>length;
int num=;
for(int i=;i<length;++i)
{
cin>>a>>b>>c;
int flag=;
book *now=head->next;
while(now->next)
{
now=now->next;
if(now->isbn==a)
{
flag++;
num++;
break;
}
}
if(flag)continue;
tail->next=new book(a,b,c);
tail=tail->next;
}
length-=num;
Output();
}
}List;
book *book::head;
book *book::tail;
int main()
{
cout<<fixed<<setprecision();
//List.Input();//T1-9
//List.BubbleSort();//T2
//List.MergeSort();//T2
//List.Update();//T3
//List.Inverse();//T4
//List.inverse();//T4
//List.Output();//T1-4
//List.QueryMax();//T5
//List.findfond();//T6
//List.Query();//T7
//List.Insert();//T8
//List.Delete();//T9
//List.Unique();//T10
return ;
}
实验一T11-20
#include <bits/stdc++.h>
using namespace std;
struct Stack1
{
double a[];
int top;
void init(){top=;}
void push(double x){a[++top]=x;}
void pop(){top--;}
int Size(){return top;}
double query(){return a[top];}
}opnd;
struct Stack2
{
char a[];
int top;
void init(){top=;}
void push(char x){a[++top]=x;}
void pop(){top--;}
int Size(){return top;}
char query(){return a[top];}
}optr;
inline bool isnum(char a)
{
if((a>=''&&a<='')||a=='.')return true;
else return false;
}
inline double cal(char a,double b,double c)
{
if(a=='+')return b+c;
else if(a=='-')return b-c;
else if(a=='*')return b*c;
else return b/c;
}
inline void opt()//从符号栈弹出一个数,数栈弹出两个数进行计算,将得到的数重新压回数栈
{
char a=optr.query();
optr.pop();
double c=opnd.query();
opnd.pop();
double b=opnd.query();
opnd.pop();
opnd.push(cal(a,b,c));
}
char s[];
int main()
{
cout<<fixed<<setprecision();
while(~scanf("%s",s))//将整个字符串读入方便处理
{
optr.init();//栈初始化
opnd.init();
if(s[]=='=')break;
int len=strlen(s);
for(int i=;i<len;++i)
{
if(isnum(s[i]))//如果读到数字,将其拼成一个double类型的数,如果是一位数可直接入栈
{
double tmp1=,tmp2=,t=;
int flag=;
for(int j=i;j<len;++j)
{
if(isnum(s[j]))
{
if(s[j]=='.')
{
flag=;
continue;
}
if(flag==)
{
tmp1*=;
tmp1+=(s[j]-'');
}
else
{
t*=0.1;
tmp2+=(s[j]-'')*t;
}
}
else
{
i=j-;
break;
}
}
opnd.push(tmp1+tmp2);
}
else if(s[i]=='+'||s[i]=='-')//读入的符号为+-
{
if(optr.Size())//如果符号栈不为空才能从数栈弹出2个数进行运算
{
char a=optr.query();
while(a=='+'||a=='-'||a=='*'||a=='/')//将将高于*/优先级的符号(即先压入符号栈的+-*/)全部弹出进行计算
{
opt();
if(optr.Size())a=optr.query();
else break;//符号栈为空跳出
}
}
optr.push(s[i]);
}
else if(s[i]=='*'||s[i]=='/')//读入的符号为*/
{
if(optr.Size())//如果符号栈不为空才能从数栈弹出2个数进行运算
{
char a=optr.query();//将高于*/优先级的符号(即先压入符号栈的*/)全部弹出进行计算
while(a=='*'||a=='/')
{
opt();
if(optr.Size())a=optr.query();
else break;//符号栈为空跳出
}
}
optr.push(s[i]);
}
else if(s[i]=='(')optr.push(s[i]);//左括号直接压入栈中
else if(s[i]==')')//右括号不入栈,一直弹出运算直到弹出左括号
{
while(optr.query()!='(')opt();
optr.pop();//弹出左括号
}
}
while(optr.Size())opt();//将符号栈全部弹出,此时数栈应只剩一个数
cout<<opnd.query()<<endl;//输出中缀算术表达式的结果
}
return ;
}//程序默认输入的是正确的中缀算术表达式,如果要判断是否错误,可以通过检查栈是否越界来实现
实验二T1
上述代码已全部在OJ上编译通过。有不足或者不懂的地方可以留言交流。
数据结构必做题参考:实验一T1-20,实验2 T1的更多相关文章
- Reverse Linked List I&&II——数据结构课上的一道题(经典必做题)
Reverse Linked List I Question Solution Reverse a singly linked list. Reverse Linked List I 设置三个指针即可 ...
- CODEVS 必做题:3149、2821、1531、3369、1230
3149 爱改名的小融 2 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description Wikioi上有个人叫小融,他喜 ...
- Machine learning 吴恩达第二周coding作业(必做题)
1.warmUpExercise: function A = warmUpExercise() %WARMUPEXERCISE Example function in octave % A = WAR ...
- 最小割 总结&&做题记录
模型要点: 1.一般适用于二取一问题或者01规划. 2.利用最小割=最大流,转化为最大流求之. 建议阅读胡伯涛的论文 <<最小割模型在信息学竞赛的应用>>,有精彩有序的证明和各 ...
- CodeM美团点评编程大赛复赛 做题感悟&题解
[T1] [简要题意] 长度为N的括号序列,随机确定括号的方向:对于一个已确定的序列,每次消除相邻的左右括号(右左不行),消除后可以进一步合并和消除直到不能消为止.求剩下的括号的期望.\(N \l ...
- 20175221 MyCP(课下作业,必做)
MyCP(课下作业,必做) 任务详情 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: - java MyCP -tx XXX1.txt XXX2 ...
- 20172302《程序设计与数据结构》实验四Android程序设计实验报告
课程:<程序设计与数据结构> 班级: 1723 姓名: 侯泽洋 学号:20172302 实验教师:王志强老师 实验日期:2018年5月30日 必修/选修: 必修 1.实验内容 (1)And ...
- [NOIP补坑计划]NOIP2012 题解&做题心得
场上预计得分:100+90+70+100+100+3060=490520(省一分数线245) 题解: D1T1 Vigenère 密码 题面 水题送温暖~~ #include<iostream& ...
- C语言程序设计做题笔记之C语言基础知识(下)
C 语言是一种功能强大.简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定的任务.我们可以利用C语言创建程序(即一组指令),并让计算机依指令行 事.并且C是相当灵活的,用于执行计算机程序能完成的 ...
随机推荐
- SpringMVC 转发、重定向
转发.重定向到其它业务方法 @org.springframework.stereotype.Controller @RequestMapping("/userController" ...
- 「牛客CSP-S2019赛前集训营1」仓鼠的石子游戏
传送门 NowCoder 解题思路 考虑这样一件事:在任何的同一个石圈,后手肯定会输. 证明很简单,手玩一下就可以大致意会. 但是有一种特殊情况,就是大小为1的圈,这种圈就是起到一次交换先后手的作用, ...
- Python数据分析在互联网寒冬下,数据分析师还吃香吗?
伴随着移动互联网的飞速发展,越来越多用户被互联网连接在一起,用户所积累下来的数据越来越多,市场对数据方面人才的需求也越来越大,由此也带火了如数据分析.数据挖掘.算法等职业,而作为其中入门门槛相对较低. ...
- leetcode841 Keys and Rooms
""" There are N rooms and you start in room 0. Each room has a distinct number in 0, ...
- 三 Road
3—5年程序员的发展和出路在哪里? 是继续做技术人,还是向管理者发力?是继续留在大公司,还是转投潜力小公司?如果没有核心竞争力,入行一两年的新程序员朋友是可以替代你大部分工作的,而且薪资还低,要怎么办 ...
- python-python基础4
本章内容: 装饰器 生成器 迭代器 json & pickle 模块 软件目录结构规范 一.装饰器 装饰器 在不改动函数代码的基础上无限制扩展函数功能的一种机制,本质上讲,装饰器是一个返回函数 ...
- python2学习------基础语法5(常用容器以及相关操作)
1.list(列表) #生成数据list a=[x for x in range(10)]; #print a; #遍历list for i in a: pass; #print i; #追加元素 a ...
- word2010文档如何隐藏右侧灰色空白不可编辑区域
word2010文档如何隐藏右侧灰色空白不可编辑区域, (word2007也是差不多的操作) 两种方法: 1.点击图中的“最终状态”按钮: 2.点击图中的”以嵌入方式显示所有修订“的按钮:
- docker学习笔记-04:docker容器数据卷
一.容器数据卷是什么 1.为了保存docker容器运行时产生的数据,做数据的持久化,我们需要用到容器数据卷.因为如果不通过docker commit 生成新的镜像,那么当容器被删除时,数据自然就没有了 ...
- R 《回归分析与线性统计模型》page120,4.3
#P120习题4.3 rm(list = ls()) A = read.xlsx("xiti_4.xlsx",sheet = 3) names(A) = c("ord&q ...