实验一T1-10
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=;
  4. struct Book
  5. {
  6. string isbn;
  7. string name;
  8. double price;
  9. }b[N],r[N];
  10. class book
  11. {
  12. private:
  13. string isbn;
  14. string name;
  15. double price;
  16. int length;
  17. book *next;
  18. static book *head;
  19. static book *tail;
  20. public:
  21. book():next(NULL)
  22. {
  23. head=tail=this;
  24. }
  25. book(string a,string b,double c):isbn(a),name(b),price(c),next(NULL){}
  26. void Input()
  27. {
  28. head->next=new book("","",-);
  29. tail=head->next;
  30. string a,b;
  31. double c;
  32. /*length=0;//T1-3
  33. while(cin>>a>>b>>c)
  34. {
  35. if(a=="0"&&b=="0"&&c==0)return;
  36. tail->next=new book(a,b,c);
  37. tail=tail->next;
  38. length++;
  39. }*/
  40. /*cin>>length;//T4-9
  41. for(int i=0;i<length;++i)
  42. {
  43. cin>>a>>b>>c;
  44. tail->next=new book(a,b,c);
  45. tail=tail->next;
  46. }*/
  47. }
  48. void Output()
  49. {
  50. cout<<length<<endl;//T1,10
  51. book *now=head->next;
  52. while(now->next)
  53. {
  54. now=now->next;
  55. cout<<now->isbn<<" "<<now->name<<" "<<now->price<<endl;
  56. }
  57. }
  58. void BubbleSort()
  59. {
  60. book *now;
  61. book *tmp1;
  62. book *tmp2;
  63. double temp1;
  64. string temp2,temp3;
  65. for(int i=;i<length;++i)
  66. {
  67. now=head->next->next;
  68. for(int j=;j<length-i;++j)
  69. {
  70. tmp1=now;
  71. tmp2=now->next;
  72. if(tmp1->price<tmp2->price)
  73. {
  74. temp1=tmp1->price;
  75. tmp1->price=tmp2->price;
  76. tmp2->price=temp1;
  77. temp2=tmp1->isbn;
  78. tmp1->isbn=tmp2->isbn;
  79. tmp2->isbn=temp2;
  80. temp3=tmp1->name;
  81. tmp1->name=tmp2->name;
  82. tmp2->name=temp3;
  83. }
  84. now=now->next;
  85. }
  86. }
  87. }
  88. void Sort(int s,int t)
  89. {
  90. if(s==t)return;
  91. int m=s+(t-s)/;
  92. Sort(s,m);
  93. Sort(m+,t);
  94. int i=s,j=m+,k=s;
  95. while(i<=m&&j<=t)r[k++]=b[i].price>=b[j].price?b[i++]:b[j++];
  96. while(i<=m)r[k++]=b[i++];
  97. while(j<=t)r[k++]=b[j++];
  98. for(int ii=s;ii<=t;++ii)b[ii]=r[ii];
  99. }
  100. void MergeSort()
  101. {
  102. int i=;
  103. book *now=head->next;
  104. while(now->next)
  105. {
  106. now=now->next;
  107. b[i].isbn=now->isbn;
  108. b[i].name=now->name;
  109. b[i++].price=now->price;
  110. }
  111. Sort(,length-);
  112. now=head->next;
  113. i=;
  114. while(now->next)
  115. {
  116. now=now->next;
  117. now->isbn=b[i].isbn;
  118. now->name=b[i].name;
  119. now->price=b[i++].price;
  120. }
  121. }
  122. void Update()
  123. {
  124. double sum=;
  125. book *now=head->next;
  126. while(now->next)
  127. {
  128. now=now->next;
  129. sum+=now->price;
  130. if(now->next==NULL)break;
  131. }
  132. sum/=length;
  133. cout<<sum<<endl;
  134. now=head->next;
  135. while(now->next)
  136. {
  137. now=now->next;
  138. now->price*=now->price<sum?1.2:1.1;
  139. if(now->next==NULL)break;
  140. }
  141. }
  142. void Inverse()
  143. {
  144. book *p=head->next;//头结点
  145. book *q=p->next;//首元节点
  146. book *t;
  147. while(q!=NULL)
  148. {//依次调整链表指针指向
  149. t=q->next;
  150. q->next=p;
  151. p=q;
  152. q=t;
  153. }
  154. head->next->next->next=NULL;//调整链表尾和头
  155. head->next->next=p;//
  156. }
  157. void inverse()
  158. {
  159. book *pre=NULL;
  160. book *cur=head->next->next;
  161. book*curnext=cur->next;
  162. while(cur!=NULL)
  163. {//依次调整链表指针指向
  164. cur->next=pre;
  165. pre=cur;
  166. cur=curnext;
  167. if(curnext!=NULL)curnext=curnext->next;
  168. }
  169. head->next->next=pre;//调整头结点
  170. }
  171. void QueryMax()
  172. {
  173. book *now=head->next;
  174. double mmax=;
  175. int num=;
  176. while(now->next)
  177. {
  178. now=now->next;
  179. if((now->price)>mmax)
  180. {
  181. mmax=now->price;
  182. num=;
  183. }
  184. else if(now->price==mmax)
  185. {
  186. num++;
  187. }
  188. }
  189. now=head;
  190. cout<<num<<endl;
  191. while(now->next)
  192. {
  193. now=now->next;
  194. if(now->price==mmax)
  195. {
  196. cout<<now->isbn<<" "<<now->name<<" "<<now->price<<endl;
  197. }
  198. }
  199. }
  200. void findfond()
  201. {
  202. int m;
  203. cin>>m;
  204. for(int i=;i<m;++i)
  205. {
  206. string name;
  207. cin>>name;
  208. book *now=head->next;
  209. int num=;
  210. while(now->next)
  211. {
  212. now=now->next;
  213. if(now->name==name)num++;
  214. }
  215. if(num==)
  216. {
  217. cout<<"Sorry,there is no your favourite!"<<endl;
  218. }
  219. else
  220. {
  221. cout<<num<<endl;
  222. now=head->next;
  223. while(now->next)
  224. {
  225. now=now->next;
  226. if(now->name==name)
  227. {
  228. cout<<now->isbn<<" "<<now->name<<" "<<now->price<<endl;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. void Query()
  235. {
  236. int m;
  237. cin>>m;
  238. for(int i=;i<m;++i)
  239. {
  240. int num;
  241. cin>>num;
  242. if(num<||num>length)
  243. {
  244. cout<<"Sorry,the book on the best position doesn't exist!"<<endl;
  245. }
  246. else
  247. {
  248. book *now=head->next;
  249. while(num--)
  250. {
  251. now=now->next;
  252. }
  253. cout<<now->isbn<<" "<<now->name<<" "<<now->price<<endl;
  254. }
  255. }
  256. }
  257. void Insert()
  258. {
  259. int pos;
  260. string a,b;
  261. double c;
  262. cin>>pos>>a>>b>>c;
  263. if(pos<||pos>length+)
  264. {
  265. cout<<"Sorry,the position to be inserted is invalid!"<<endl;
  266. }
  267. else
  268. {
  269. book *now=head->next;
  270. while(--pos)now=now->next;
  271. book *t=new book(a,b,c);
  272. t->next=now->next;
  273. now->next=t;
  274. length++;
  275. Output();
  276. }
  277. }
  278. void Delete()
  279. {
  280. int pos;
  281. cin>>pos;
  282. if(pos<||pos>length)
  283. {
  284. cout<<"Sorry,the position to be deleted is invalid!"<<endl;
  285. }
  286. else
  287. {
  288. book *now=head->next;
  289. while(--pos)now=now->next;
  290. book *t=now->next->next;
  291. delete now->next;
  292. now->next=t;
  293. length--;
  294. Output();
  295. }
  296. }
  297. void Unique()
  298. {
  299. head->next=new book("","",-);
  300. tail=head->next;
  301. string a,b;
  302. double c;
  303. cin>>length;
  304. int num=;
  305. for(int i=;i<length;++i)
  306. {
  307. cin>>a>>b>>c;
  308. int flag=;
  309. book *now=head->next;
  310. while(now->next)
  311. {
  312. now=now->next;
  313. if(now->isbn==a)
  314. {
  315. flag++;
  316. num++;
  317. break;
  318. }
  319. }
  320. if(flag)continue;
  321. tail->next=new book(a,b,c);
  322. tail=tail->next;
  323. }
  324. length-=num;
  325. Output();
  326. }
  327. }List;
  328. book *book::head;
  329. book *book::tail;
  330. int main()
  331. {
  332. cout<<fixed<<setprecision();
  333. //List.Input();//T1-9
  334. //List.BubbleSort();//T2
  335. //List.MergeSort();//T2
  336. //List.Update();//T3
  337. //List.Inverse();//T4
  338. //List.inverse();//T4
  339. //List.Output();//T1-4
  340. //List.QueryMax();//T5
  341. //List.findfond();//T6
  342. //List.Query();//T7
  343. //List.Insert();//T8
  344. //List.Delete();//T9
  345. //List.Unique();//T10
  346. return ;
  347. }

实验一T11-20

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct Stack1
  4. {
  5. double a[];
  6. int top;
  7. void init(){top=;}
  8. void push(double x){a[++top]=x;}
  9. void pop(){top--;}
  10. int Size(){return top;}
  11. double query(){return a[top];}
  12. }opnd;
  13. struct Stack2
  14. {
  15. char a[];
  16. int top;
  17. void init(){top=;}
  18. void push(char x){a[++top]=x;}
  19. void pop(){top--;}
  20. int Size(){return top;}
  21. char query(){return a[top];}
  22. }optr;
  23. inline bool isnum(char a)
  24. {
  25. if((a>=''&&a<='')||a=='.')return true;
  26. else return false;
  27. }
  28. inline double cal(char a,double b,double c)
  29. {
  30. if(a=='+')return b+c;
  31. else if(a=='-')return b-c;
  32. else if(a=='*')return b*c;
  33. else return b/c;
  34. }
  35. inline void opt()//从符号栈弹出一个数,数栈弹出两个数进行计算,将得到的数重新压回数栈
  36. {
  37. char a=optr.query();
  38. optr.pop();
  39. double c=opnd.query();
  40. opnd.pop();
  41. double b=opnd.query();
  42. opnd.pop();
  43. opnd.push(cal(a,b,c));
  44. }
  45. char s[];
  46. int main()
  47. {
  48. cout<<fixed<<setprecision();
  49. while(~scanf("%s",s))//将整个字符串读入方便处理
  50. {
  51. optr.init();//栈初始化
  52. opnd.init();
  53. if(s[]=='=')break;
  54. int len=strlen(s);
  55. for(int i=;i<len;++i)
  56. {
  57. if(isnum(s[i]))//如果读到数字,将其拼成一个double类型的数,如果是一位数可直接入栈
  58. {
  59. double tmp1=,tmp2=,t=;
  60. int flag=;
  61. for(int j=i;j<len;++j)
  62. {
  63. if(isnum(s[j]))
  64. {
  65. if(s[j]=='.')
  66. {
  67. flag=;
  68. continue;
  69. }
  70. if(flag==)
  71. {
  72. tmp1*=;
  73. tmp1+=(s[j]-'');
  74. }
  75. else
  76. {
  77. t*=0.1;
  78. tmp2+=(s[j]-'')*t;
  79. }
  80. }
  81. else
  82. {
  83. i=j-;
  84. break;
  85. }
  86. }
  87. opnd.push(tmp1+tmp2);
  88. }
  89. else if(s[i]=='+'||s[i]=='-')//读入的符号为+-
  90. {
  91. if(optr.Size())//如果符号栈不为空才能从数栈弹出2个数进行运算
  92. {
  93. char a=optr.query();
  94. while(a=='+'||a=='-'||a=='*'||a=='/')//将将高于*/优先级的符号(即先压入符号栈的+-*/)全部弹出进行计算
  95. {
  96. opt();
  97. if(optr.Size())a=optr.query();
  98. else break;//符号栈为空跳出
  99. }
  100. }
  101. optr.push(s[i]);
  102. }
  103. else if(s[i]=='*'||s[i]=='/')//读入的符号为*/
  104. {
  105. if(optr.Size())//如果符号栈不为空才能从数栈弹出2个数进行运算
  106. {
  107. char a=optr.query();//将高于*/优先级的符号(即先压入符号栈的*/)全部弹出进行计算
  108. while(a=='*'||a=='/')
  109. {
  110. opt();
  111. if(optr.Size())a=optr.query();
  112. else break;//符号栈为空跳出
  113. }
  114. }
  115. optr.push(s[i]);
  116. }
  117. else if(s[i]=='(')optr.push(s[i]);//左括号直接压入栈中
  118. else if(s[i]==')')//右括号不入栈,一直弹出运算直到弹出左括号
  119. {
  120. while(optr.query()!='(')opt();
  121. optr.pop();//弹出左括号
  122. }
  123. }
  124. while(optr.Size())opt();//将符号栈全部弹出,此时数栈应只剩一个数
  125. cout<<opnd.query()<<endl;//输出中缀算术表达式的结果
  126. }
  127. return ;
  128. }//程序默认输入的是正确的中缀算术表达式,如果要判断是否错误,可以通过检查栈是否越界来实现

实验二T1

上述代码已全部在OJ上编译通过。有不足或者不懂的地方可以留言交流。

数据结构必做题参考:实验一T1-20,实验2 T1的更多相关文章

  1. Reverse Linked List I&&II——数据结构课上的一道题(经典必做题)

    Reverse Linked List I Question Solution Reverse a singly linked list. Reverse Linked List I 设置三个指针即可 ...

  2. CODEVS 必做题:3149、2821、1531、3369、1230

    3149 爱改名的小融 2  时间限制: 2 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Wikioi上有个人叫小融,他喜 ...

  3. Machine learning 吴恩达第二周coding作业(必做题)

    1.warmUpExercise: function A = warmUpExercise() %WARMUPEXERCISE Example function in octave % A = WAR ...

  4. 最小割 总结&&做题记录

    模型要点: 1.一般适用于二取一问题或者01规划. 2.利用最小割=最大流,转化为最大流求之. 建议阅读胡伯涛的论文 <<最小割模型在信息学竞赛的应用>>,有精彩有序的证明和各 ...

  5. CodeM美团点评编程大赛复赛 做题感悟&题解

    [T1] [简要题意]   长度为N的括号序列,随机确定括号的方向:对于一个已确定的序列,每次消除相邻的左右括号(右左不行),消除后可以进一步合并和消除直到不能消为止.求剩下的括号的期望.\(N \l ...

  6. 20175221 MyCP(课下作业,必做)

    MyCP(课下作业,必做) 任务详情 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: - java MyCP -tx XXX1.txt XXX2 ...

  7. 20172302《程序设计与数据结构》实验四Android程序设计实验报告

    课程:<程序设计与数据结构> 班级: 1723 姓名: 侯泽洋 学号:20172302 实验教师:王志强老师 实验日期:2018年5月30日 必修/选修: 必修 1.实验内容 (1)And ...

  8. [NOIP补坑计划]NOIP2012 题解&做题心得

    场上预计得分:100+90+70+100+100+3060=490520(省一分数线245) 题解: D1T1 Vigenère 密码 题面 水题送温暖~~ #include<iostream& ...

  9. C语言程序设计做题笔记之C语言基础知识(下)

    C 语言是一种功能强大.简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定的任务.我们可以利用C语言创建程序(即一组指令),并让计算机依指令行 事.并且C是相当灵活的,用于执行计算机程序能完成的 ...

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:按钮大小

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. SSH Secure Shell 编码设置-----支持中文

    参考:https://www.cnblogs.com/hupf/p/6920323.html #vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB1803 ...

  3. 机器学习(ML)十一之CNN各种模型

    深度卷积神经网络(AlexNet) 在LeNet提出后的将近20年里,神经网络一度被其他机器学习方法超越,如支持向量机.虽然LeNet可以在早期的小数据集上取得好的成绩,但是在更大的真实数据集上的表现 ...

  4. Vim学习1移动光标

    vim adventure上面做了大量练习,是个好软件只是需要收费 HJKLWBE 首先HJKL分别是移动上下左右的 H:光标左移 J:下移 K:上移 L:右移 W:跳转到下一个单词的第一个字,注意是 ...

  5. 前端学习笔记系列一:7 在vscode中根据vue等模板生成代码

    目标:希望每次新建.vue文件后,VSCODE能够根据配置,自动生成我们想要的内容. 方法:打开VSCODE编辑器,依次选择“文件 -> 首选项 -> 用户代码片段”,此时,会弹出一个搜索 ...

  6. JdbcTemplate常用方法

    JdbcTemplate简介 JdbcTemplate是Spring JDBC的核心类,借助该类提供的方法可以很方便的实现数据的增删改查. Spring对数据库的操作在jdbc上面做了深层次的封装,使 ...

  7. 十四 数据库连接池&DBUtils

    关于数据库连接池: 1 数据库的连接对象创建工作,比较消耗性能. 2 一开始在内存中开辟一块空间,往池子里放置多个连接对象,需要连接的时候从连接池里面调用, 使用完毕归还连接,确保连接对象能够循环利用 ...

  8. 连接数据库 - (mysql-thinkphp) (2)

    1.现在conf里面写好选择的数据库 选择好了以后 2.在index里面输入 查询mysql数据库里面的表tables_priv的所有数据 public function index() { $res ...

  9. C语言三种整数类型

    1,int 是 C 语言的基本整数类型,可以满足我们处理一般数据的需求. C 语言还提供了四个可以修饰 int 的关键字:short.long.signed,以及 unsigned. 利用这四个关键字 ...

  10. windows清理命令

    总是有一些设备需要清理,第三方的软件有不可以安装,那么,就需要这两条命令来做: cleanmgr /sageset:99 #设置要被清理的东西 cleanmgr /sagerun:99 #执行清理操作 ...