实验内容1:

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. string myfavorite[7]={"book", "music", "film", "paintings","anime","sport","sportsman"};
  6. // 函数声明
  7. void output1(vector<string> &);
  8. void output2(vector<string> &);
  9. int main()
  10. {
  11. vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes
  12. // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc)
  13. likes.push_back("《肖生克的救赎》");
  14. likes.push_back("《You Are Not Alone》");
  15. likes.push_back("《辛德勒的名单》");
  16. likes.push_back("《日出》");
  17. likes.push_back("《one piece》");
  18. likes.push_back("乒乓球");
  19. likes.push_back("李娜");
  20. cout << "-----I like these-----" << endl;
  21. // 调用子函数输出vector<string>数组对象likes的元素值
  22. output1(likes);
  23. // 为vector<string>数组对象dislikes添加元素值
  24. dislikes.push_back("恐怖小说");
  25. dislikes.push_back("hip-hop");
  26. dislikes.push_back("烂片");
  27. dislikes.push_back("故弄玄虚");
  28. dislikes.push_back("泡面番");
  29. dislikes.push_back("run");
  30. dislikes.push_back("nobody");
  31. cout << "-----I dislike these-----" << endl;
  32. // 调用子函数输出vector<string>数组对象dislikes的元素值
  33. output2(dislikes);
  34. // 交换vector<string>对象likes和dislikes的元素值
  35. string a;
  36. for(int i=0;i<likes.size();i++)
  37. {
  38. a=likes[i];
  39. likes[i]=dislikes[i];
  40. dislikes[i]=a;
  41. }
  42. cout<<"下面这个功能很鸡肋。。。"<<endl;
  43. cout << "-----I likes these-----" << endl;
  44. // 调用子函数输出vector<string>数组对象likes的元素值
  45. output1(likes);
  46. cout << "-----I dislikes these-----" << endl;
  47. // 调用子函数输出vector<string>数组对象dislikes的元素值
  48. output2(dislikes);
  49. return 0;
  50. }
  51. // 函数实现
  52. // 以下标方式输出vector<string>数组对象v的元素值
  53. void output1(vector<string> &v)
  54. {
  55. for(int i=0;i<v.size();i++)
  56. {
  57. cout<<myfavorite[i]<<":"<<v[i]<<" "<<endl;
  58. }
  59. }
  60. // 函数实现
  61. // 以迭代器方式输出vector<string>数组对象v的元素值
  62. void output2(vector<string> &v)
  63. {
  64. int a=0;
  65. for(auto i=v.begin();i<v.end();i++)
  66. {
  67. cout<<myfavorite[a++]<<":"<<*i<<" "<<endl;
  68. }
  69. }

实验内容2:

6-17:

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int *p;
  6. //*p=9;//不能直接将数字赋予指针,因为这时编译器并没有分配内存给数字
  7. int a=9;//此时系统才会分配内存
  8. p=&a;//将存储值的地址赋予指针
  9. cout<<"The value at p: "<<*p;
  10. return 0;
  11. }

6-18:

这题我是真没看懂

  1. #include<iostream>
  2. using namespace std;
  3. int fn1()
  4. {
  5. int *p=new int (5);
  6. return *p;
  7. delete p;//听说是因为没有释放动态分配的内存,觉得有道理,但加上去也看不出来什么啊
  8. }
  9. int main()
  10. {
  11. int a=fn1();
  12. cout<<"the value of a is: "<<++a;
  13. return 0;
  14. }

实验内容3:

main:

  1. #include <iostream>
  2. #include "matrix.h"
  3. using namespace std;
  4. int main()
  5. {
  6. Matrix A(5);
  7. Matrix C(3,4);
  8. float const touch[25]= {1,6,1,1,5,
  9. 1,1,6,6,5,
  10. 1,1,5,3,5,
  11. 1,1,5,3,5,
  12. 5,1,1,1,1};
  13. A.setMatrix(touch);
  14. A.printMatrix();
  15. Matrix B(A);
  16. cout<<A.element(3,4)<<endl;
  17. A.element(3,4)=6;
  18. cout<<A.element(3,4)<<endl;
  19. A.setElement(3,4,3);
  20. cout<<A.element(3,4)<<endl;
  21. cout<<A.getLines()<<endl;
  22. cout<<C.getCols()<<endl;
  23. return 0;
  24. }

matrix.h:

  1. #ifndef MATRIX_H
  2. #define MATRIX_H
  3. class Matrix
  4. {
  5. public:
  6. Matrix(int n); // 构造函数,构造一个n*n的矩阵
  7. Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵
  8. Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造
  9. ~Matrix(); //析构函数
  10. void setMatrix(const float *pvalue); // 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值
  11. void printMatrix() const; // 显示矩阵
  12. inline float &element(int i, int j) //返回矩阵第i行第j列元素的引用
  13. {
  14. float &r=p[(i-1)*cols+j-1];
  15. return r;
  16. }
  17. inline float element(int i, int j) const// 返回矩阵第i行第j列元素的值
  18. {
  19. float r=p[(i-1)*cols+j-1];
  20. return r;
  21. }
  22. void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
  23. inline int getLines() const //返回矩阵行数
  24. {
  25. return lines;
  26. }
  27. inline int getCols() const //返回矩阵列数
  28. {
  29. return cols;
  30. }
  31. private:
  32. int lines; // 矩阵行数
  33. int cols; // 矩阵列数
  34. float *p; // 指向存放矩阵数据的内存块的首地址
  35. };
  36. #endif

matrix.cpp:

  1. #include<iostream>
  2. #include "matrix.h"
  3. using namespace std;
  4. Matrix::Matrix(int n):lines(n),cols(n)
  5. {
  6. p=new float[lines*cols];
  7. }
  8. Matrix::Matrix(int n, int m):lines(n),cols(m)
  9. {
  10. p=new float[lines*cols];
  11. }
  12. Matrix::Matrix(const Matrix &X):lines(X.lines),cols(X.cols),p(X.p){}
  13. Matrix::~Matrix()
  14. {
  15. delete p;
  16. }
  17. void Matrix::setMatrix(const float *pvalue)
  18. {
  19. for(int i=0;i<lines;i++)
  20. {
  21. for(int j=0;j<cols;j++)
  22. p[i*cols+j]=pvalue[i*cols+j];
  23. }
  24. }
  25. void Matrix::printMatrix()const
  26. {
  27. for(int i=0;i<lines;i++)
  28. {
  29. for(int j=0;j<cols;j++)
  30. cout<<p[i*cols+j]<<" ";
  31. cout<<endl;
  32. }
  33. }
  34. void Matrix::setElement(int i,int j,int value)
  35. {
  36. p[(i-1)*cols+j-1]=value*1.0;
  37. }

实验内容4:

第二题,User类:

main:

  1. #include <iostream>
  2. #include "User.h"
  3. using namespace std;
  4. int main()
  5. {
  6. User A("BuluGuy");
  7. User B("alien");
  8. A.priUserinf();
  9. A.chgpas("216541");
  10. A.priUserinf();
  11. User kll;
  12. A.priCurrentID();
  13. kll.chgname("kll");
  14. kll.chgpas("135113513");
  15. A.priCurrentID();
  16. B.chgname();
  17. B.chgpas();
  18. B.priUserinf();
  19. B.priCurrentID();
  20. return 0;
  21. }

User.h:

  1. #ifndef USER_H_INCLUDED
  2. #define USER_H_INCLUDED
  3. #include <string>
  4. #include <vector>
  5. #include <cstring>
  6. #include <iostream>
  7. using namespace std;
  8. class User
  9. {
  10. public:
  11. User(string a);
  12. User();
  13. ~User();
  14. void priUserinf();
  15. void chgpas();
  16. void chgpas(string);
  17. void chgname(string );
  18. void chgname();
  19. void priCurrentID();
  20. static int CurrentID;
  21. static string Curment[2];
  22. private:
  23. int id;
  24. string name;
  25. string password;
  26. };
  27. #endif // USER_H_INCLUDED

User.cpp:

  1. #include "User.h"
  2. using namespace std;
  3. //递归的判断条件
  4. bool base1=0;
  5. int tine=0;
  6. //类变量的共有属性,使用静态变量
  7. int User::CurrentID=999;
  8. //使用静态变量组记录最后用户的信息
  9. string User::Curment[2]={"User","111111"};
  10. //构造函数
  11. User::User(string a):id(++CurrentID),name(a),password("111111")
  12. {
  13. Curment[0]=name;
  14. Curment[1]=password;
  15. }
  16. User::User():id(++CurrentID),name("User"),password("111111")
  17. {
  18. Curment[0]=name;
  19. Curment[1]=password;
  20. }
  21. User::~User(){}
  22. //用户信息输出
  23. void User::priUserinf()
  24. {
  25. cout<<endl<<"User's ID : "<<id<<endl
  26. <<"User's name : "<<name<<endl
  27. <<"User's password : "<<password<<endl;
  28. }
  29. /*--------------------------------------------*/
  30. //识别密码
  31. string getpass()
  32. {
  33. string m;
  34. cin>>m;
  35. return m;
  36. }
  37. /*--------------------------------------------*/
  38. void User::chgpas()
  39. {
  40. //根据是否递归决定输出语句
  41. if(base1==0)
  42. cout<<"Please input current password : ";
  43. else
  44. cout<<"Password wrong! Please input again : ";
  45. //旧密码匹配成功
  46. if(getpass()==password)
  47. {
  48. do
  49. {
  50. cout<<"Please input new password :";
  51. string cx;
  52. cin>>cx;
  53. cout<<"please input new password again :";
  54. if(getpass()==cx)
  55. {
  56. password=cx;
  57. if(id==CurrentID)Curment[1]=password;
  58. cout<<"Password has changed!"<<endl;
  59. break;
  60. }
  61. else
  62. {
  63. cout<<"Passwords are different! Please try again."<<endl;
  64. }
  65. }while(1);
  66. }
  67. //旧密码匹配失败
  68. else
  69. {
  70. tine++;
  71. if(tine==3)
  72. {
  73. tine=0;
  74. cout<<"Password has been input wrong three times, Please try again later."<<endl;
  75. }
  76. else
  77. {
  78. base1=1;
  79. chgpas();
  80. }
  81. }
  82. }
  83. //程序修改密码
  84. void User::chgpas(string a)
  85. {
  86. password=a;
  87. if(id==CurrentID)Curment[1]=password;
  88. }
  89. //程序修改用户名
  90. void User::chgname(string a)
  91. {
  92. name=a;
  93. if(id==CurrentID)Curment[0]=name;
  94. }
  95. //用户修改用户名
  96. void User::chgname()
  97. {
  98. cout<<"Please input new user's name : "<<endl;
  99. string a;
  100. cin>>a;
  101. name=a;
  102. if(id==CurrentID)Curment[0]=name;
  103. cout<<"User's name has been changed."<<endl;
  104. }
  105. //打印CurrentID并输出最后一位新增用户的信息
  106. void User::priCurrentID()
  107. {
  108. cout<<endl;
  109. cout<<"CurrentID : "<<CurrentID<<endl;
  110. cout<<"The information of last added user :"<<endl;
  111. cout<<endl<<"User's ID : "<<CurrentID<<endl
  112. <<"User's name : "<<Curment[0]<<endl
  113. <<"User's password : "<<Curment[1]<<endl;
  114. }



Book类:

main.cpp:

  1. #include <iostream>
  2. #include<vector>
  3. #include"Book.h"
  4. using namespace std;
  5. int main()
  6. {
  7. vector<Book> books;
  8. string cx,cv;
  9. float pl;
  10. while(cin>>cx>>cv>>pl)
  11. {
  12. Book a(cx,cv,pl);
  13. books.push_back(a);
  14. }
  15. for(int i=0;i<books.size();i++)
  16. {
  17. books[i].print();
  18. }
  19. return 0;
  20. }

Book.h:

  1. #ifndef BOOK_H_INCLUDED
  2. #define BOOK_H_INCLUDED
  3. #include<iostream>
  4. #include<string>
  5. #include<cstring>
  6. using namespace std;
  7. class Book
  8. {
  9. public:
  10. Book(string a,string c,float v);
  11. void print();
  12. private:
  13. string isbn;
  14. string title;
  15. double price;
  16. };
  17. #endif // BOOK_H_INCLUDED

Book.cpp:

  1. #include "Book.h"
  2. Book::Book(string a,string b,float c):isbn(a),title(b),price(c){}
  3. void Book::print()
  4. {
  5. cout<<"出版编号:"<<isbn<<" "<<"书名:"<<title<<" "<<"定价:"<<price<<" RMB"<<endl;
  6. }

实验5&期中考试后两题的更多相关文章

  1. 西安交通大学c++[mooc]课后题12章(只有后两题)

    不是从第一题开始的,因为我刚准备把代码粘到CSDN上面,可以给自己看,也有可能启发后来者. 机会是留给有准备的人的      --路易斯·巴斯德 先写下第12周慕课学习总结吧! 多态就是将运算符重载, ...

  2. 皓远的第二次博客作业(最新pta集,链表练习及期中考试总结)

    前言: 知识点运用:正则表达式,有关图形设计计算的表达式和算法,链表的相关知识,Java类的基础运用,继承.容器与多态. 题量:相较于上次作业,这几周在java方面的练习花了更多的精力和时间,所要完成 ...

  3. noip2016 小结(ac两题+学习总结)

    NOIP2016考试小结 DAY 1 T1 题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内, ...

  4. 职业生涯之完成OCM考试后的感想

    背景知识:关于OCM认证,百科是这样描述的: Oracle Certified Master(OCM) 大师认证资质是Oracle认证的最高级别.此认证是对技术.知识和操作技能的最高级别的认可.Ora ...

  5. Confusing Date Format UVALive 7711 给定mm-mm-mm格式的时间。年份(1900-1999)只给了后两位数,问有多少种合法的排列使时间正确。

    /** 题目:Confusing Date Format UVALive 7711 链接:https://vjudge.net/contest/174844#problem/A 题意:给定mm-mm- ...

  6. 清橙A1206.小Z的袜子 && CF 86D(莫队两题)

    清橙A1206.小Z的袜子 && CF 86D(莫队两题) 在网上看了一些别人写的关于莫队算法的介绍,我认为,莫队与其说是一种算法,不如说是一种思想,他通过先分块再排序来优化离线查询问 ...

  7. 退役IV次后做题记录

    退役IV次后做题记录 我啥都不会了.... AGC023 D 如果所有的楼房都在\(S\)同一边可以直接得出答案. 否则考虑最左最右两边的票数,如果左边>=右边,那么最右边会投给左边,因为就算车 ...

  8. 退役III次后做题记录(扯淡)

    退役III次后做题记录(扯淡) CF607E Cross Sum 计算几何屎题 直接二分一下,算出每条线的位置然后算 注意相对位置这个不能先搞出坐标,直接算角度就行了,不然会卡精度/px flag:计 ...

  9. 退役II次后做题记录

    退役II次后做题记录 感觉没啥好更的,咕. atcoder1219 历史研究 回滚莫队. [六省联考2017]组合数问题 我是傻逼 按照组合意义等价于\(nk\)个物品,选的物品\(\mod k\) ...

随机推荐

  1. IDEA里运行代码时出现Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger的解决办法(图文详解)

    不多说,直接上干货! 问题详情 运行出现log4j的问题 -classpath "C:\Program Files\Java\jdk1.8.0_66\jre\lib\charsets.jar ...

  2. 【c++】动态绑定

    C++的函数调用默认不使用动态绑定.要触发动态绑定,必须满足两个条件: 只有指定为虚函数的成员函数才能进行动态绑定 必须通过基类类型的引用或指针进行函数调用 因为每个派生类对象中都拥有基类部分,所以可 ...

  3. Firebird 有用的list函数

    语法: LIST ([ALL | DISTINCT] expression [, separator]) 示例: select list(u.code, ';') from m_user u 结果:查 ...

  4. in和not in

    当子查询返回的列的值是多个值,那么就不能使用比较运算符(> < = !=),使用关键字in 语法: select …..from …..where 表达式 in (子查询) 常用in替换等 ...

  5. heroku快速部署node应用

    试了一下heroku,简直碉堡了,下面介绍如何简单几步实现弄得应用的部署访问: 1.首先https://dashboard.heroku.com/进行账号注册 2.github上push一个最新的no ...

  6. 购物车之CheckBox所有事件

    html 主要是循环

  7. No mapping found for HTTP request with URI异常的原因,<mvc:default-servlet-handler/>的作用

    一.最近做的一个项目有很多静态资源文件,按照平时的配置springmvc进行配置发现访问不到静态文件,并且在我配置好controller去访问结果还是404 No mapping found for ...

  8. 静态代码块,构造代码块,main()

    静态代码块 随Class 加载而加载,为Class 作初始化: 在main() 之前加载: 只执行一次: 构造代码块 随对象的创建而加载,为对象作初始化 public class day04 { pu ...

  9. Maven --- <distributionManagement>标签

    1.<distributionManagement>的作用: 负责管理构件的发布.这是一个环境变量    <downloadUrl> URL </downloadUrl& ...

  10. Effective C++ .12 复制对象-拷贝构造函数的编写

    当我们自己编写拷贝构造函数时,编译器就不会为该类生成默认拷贝构造函数了,对于assignment operator也是如此. 1. 拷贝构造函数中记得调用父类的拷贝构造函数,或者相应复制过程 clas ...