实验内容1:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
string myfavorite[7]={"book", "music", "film", "paintings","anime","sport","sportsman"};
// 函数声明
void output1(vector<string> &);
void output2(vector<string> &);
int main()
{
vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes
// 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc)
likes.push_back("《肖生克的救赎》");
likes.push_back("《You Are Not Alone》");
likes.push_back("《辛德勒的名单》");
likes.push_back("《日出》");
likes.push_back("《one piece》");
likes.push_back("乒乓球");
likes.push_back("李娜");
cout << "-----I like these-----" << endl;
// 调用子函数输出vector<string>数组对象likes的元素值
output1(likes);
// 为vector<string>数组对象dislikes添加元素值
dislikes.push_back("恐怖小说");
dislikes.push_back("hip-hop");
dislikes.push_back("烂片");
dislikes.push_back("故弄玄虚");
dislikes.push_back("泡面番");
dislikes.push_back("run");
dislikes.push_back("nobody"); cout << "-----I dislike these-----" << endl;
// 调用子函数输出vector<string>数组对象dislikes的元素值
output2(dislikes);
// 交换vector<string>对象likes和dislikes的元素值
string a;
for(int i=0;i<likes.size();i++)
{
a=likes[i];
likes[i]=dislikes[i];
dislikes[i]=a;
}
cout<<"下面这个功能很鸡肋。。。"<<endl;
cout << "-----I likes these-----" << endl;
// 调用子函数输出vector<string>数组对象likes的元素值
output1(likes);
cout << "-----I dislikes these-----" << endl;
// 调用子函数输出vector<string>数组对象dislikes的元素值
output2(dislikes);
return 0;
}
// 函数实现
// 以下标方式输出vector<string>数组对象v的元素值
void output1(vector<string> &v)
{
for(int i=0;i<v.size();i++)
{
cout<<myfavorite[i]<<":"<<v[i]<<" "<<endl;
}
} // 函数实现
// 以迭代器方式输出vector<string>数组对象v的元素值
void output2(vector<string> &v)
{
int a=0;
for(auto i=v.begin();i<v.end();i++)
{
cout<<myfavorite[a++]<<":"<<*i<<" "<<endl;
}
}

实验内容2:

6-17:

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

6-18:

这题我是真没看懂

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

实验内容3:

main:

#include <iostream>
#include "matrix.h"
using namespace std;
int main()
{
Matrix A(5);
Matrix C(3,4);
float const touch[25]= {1,6,1,1,5,
1,1,6,6,5,
1,1,5,3,5,
1,1,5,3,5,
5,1,1,1,1};
A.setMatrix(touch);
A.printMatrix();
Matrix B(A);
cout<<A.element(3,4)<<endl;
A.element(3,4)=6;
cout<<A.element(3,4)<<endl;
A.setElement(3,4,3);
cout<<A.element(3,4)<<endl;
cout<<A.getLines()<<endl;
cout<<C.getCols()<<endl;
return 0;
}

matrix.h:

#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
public:
Matrix(int n); // 构造函数,构造一个n*n的矩阵
Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵
Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造
~Matrix(); //析构函数
void setMatrix(const float *pvalue); // 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值
void printMatrix() const; // 显示矩阵
inline float &element(int i, int j) //返回矩阵第i行第j列元素的引用
{
float &r=p[(i-1)*cols+j-1];
return r;
}
inline float element(int i, int j) const// 返回矩阵第i行第j列元素的值
{
float r=p[(i-1)*cols+j-1];
return r;
}
void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
inline int getLines() const //返回矩阵行数
{
return lines;
}
inline int getCols() const //返回矩阵列数
{
return cols;
}
private:
int lines; // 矩阵行数
int cols; // 矩阵列数
float *p; // 指向存放矩阵数据的内存块的首地址
};
#endif

matrix.cpp:

#include<iostream>
#include "matrix.h"
using namespace std;
Matrix::Matrix(int n):lines(n),cols(n)
{
p=new float[lines*cols];
}
Matrix::Matrix(int n, int m):lines(n),cols(m)
{
p=new float[lines*cols];
}
Matrix::Matrix(const Matrix &X):lines(X.lines),cols(X.cols),p(X.p){}
Matrix::~Matrix()
{
delete p;
}
void Matrix::setMatrix(const float *pvalue)
{
for(int i=0;i<lines;i++)
{
for(int j=0;j<cols;j++)
p[i*cols+j]=pvalue[i*cols+j];
}
}
void Matrix::printMatrix()const
{
for(int i=0;i<lines;i++)
{
for(int j=0;j<cols;j++)
cout<<p[i*cols+j]<<" ";
cout<<endl;
}
}
void Matrix::setElement(int i,int j,int value)
{
p[(i-1)*cols+j-1]=value*1.0;
}

实验内容4:

第二题,User类:

main:

#include <iostream>
#include "User.h"
using namespace std;
int main()
{
User A("BuluGuy");
User B("alien");
A.priUserinf();
A.chgpas("216541");
A.priUserinf();
User kll;
A.priCurrentID();
kll.chgname("kll");
kll.chgpas("135113513");
A.priCurrentID();
B.chgname();
B.chgpas();
B.priUserinf();
B.priCurrentID();
return 0;
}

User.h:

#ifndef USER_H_INCLUDED
#define USER_H_INCLUDED
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
class User
{
public:
User(string a);
User();
~User();
void priUserinf();
void chgpas();
void chgpas(string);
void chgname(string );
void chgname();
void priCurrentID();
static int CurrentID;
static string Curment[2];
private:
int id;
string name;
string password;
}; #endif // USER_H_INCLUDED

User.cpp:

#include "User.h"
using namespace std;
//递归的判断条件
bool base1=0;
int tine=0;
//类变量的共有属性,使用静态变量
int User::CurrentID=999;
//使用静态变量组记录最后用户的信息
string User::Curment[2]={"User","111111"};
//构造函数
User::User(string a):id(++CurrentID),name(a),password("111111")
{
Curment[0]=name;
Curment[1]=password;
}
User::User():id(++CurrentID),name("User"),password("111111")
{
Curment[0]=name;
Curment[1]=password;
}
User::~User(){}
//用户信息输出
void User::priUserinf()
{
cout<<endl<<"User's ID : "<<id<<endl
<<"User's name : "<<name<<endl
<<"User's password : "<<password<<endl;
}
/*--------------------------------------------*/
//识别密码
string getpass()
{
string m;
cin>>m;
return m;
}
/*--------------------------------------------*/
void User::chgpas()
{
//根据是否递归决定输出语句
if(base1==0)
cout<<"Please input current password : ";
else
cout<<"Password wrong! Please input again : ";
//旧密码匹配成功
if(getpass()==password)
{
do
{
cout<<"Please input new password :";
string cx;
cin>>cx;
cout<<"please input new password again :";
if(getpass()==cx)
{
password=cx;
if(id==CurrentID)Curment[1]=password;
cout<<"Password has changed!"<<endl;
break;
}
else
{
cout<<"Passwords are different! Please try again."<<endl;
}
}while(1);
}
//旧密码匹配失败
else
{
tine++;
if(tine==3)
{
tine=0;
cout<<"Password has been input wrong three times, Please try again later."<<endl;
}
else
{
base1=1;
chgpas();
}
}
}
//程序修改密码
void User::chgpas(string a)
{
password=a;
if(id==CurrentID)Curment[1]=password;
}
//程序修改用户名
void User::chgname(string a)
{
name=a;
if(id==CurrentID)Curment[0]=name;
}
//用户修改用户名
void User::chgname()
{
cout<<"Please input new user's name : "<<endl;
string a;
cin>>a;
name=a;
if(id==CurrentID)Curment[0]=name;
cout<<"User's name has been changed."<<endl;
}
//打印CurrentID并输出最后一位新增用户的信息
void User::priCurrentID()
{
cout<<endl;
cout<<"CurrentID : "<<CurrentID<<endl;
cout<<"The information of last added user :"<<endl;
cout<<endl<<"User's ID : "<<CurrentID<<endl
<<"User's name : "<<Curment[0]<<endl
<<"User's password : "<<Curment[1]<<endl; }



Book类:

main.cpp:

#include <iostream>
#include<vector>
#include"Book.h"
using namespace std; int main()
{
vector<Book> books;
string cx,cv;
float pl;
while(cin>>cx>>cv>>pl)
{
Book a(cx,cv,pl);
books.push_back(a);
}
for(int i=0;i<books.size();i++)
{
books[i].print();
}
return 0;
}

Book.h:

#ifndef BOOK_H_INCLUDED
#define BOOK_H_INCLUDED
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class Book
{
public:
Book(string a,string c,float v);
void print();
private:
string isbn;
string title;
double price;
}; #endif // BOOK_H_INCLUDED

Book.cpp:

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

实验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. Flyway-使用步骤

      1.创建一个Maven项目 2.编辑当下的pom.xml,添加flyway依赖和Mysql依赖 <project ...> ... <dependencies> <d ...

  2. 403 for URL: http://www.terracotta.org/kit/reflector

    前面因为在一个项目中使用了ehcache.xml配置文件,后面启动tomcat的时候报下面的错误 java.io.IOException: Server returned HTTP response ...

  3. 【随笔】Win7下GVIM的安装与配置

    针对各种语言的编辑器千千万万,最好的就是最适合自己的,这句话一点没错. 偶然间,需要在Windows上编写代码,MyEclipse等太大,完全没有必要,所以就想起来了vim这个神器.个子小,功能强,就 ...

  4. 设计模式之第7章-外观模式(Java实现)

    设计模式之第7章-外观模式(Java实现) “鱼哥,知道怎么把大象装进冰箱里面么?”(作者按:这么简单的问题还想考我,早了几百年吧.)“把大象装进冰箱里,一共需要三步:第一步,把冰箱门打开:第二步,把 ...

  5. 游标的小知识(转载and整理)

    一.游标(用来存储多条查询数据的一种数据结构(结果集),它有一个指针,用来从上往下移动,从而达到遍历每条记录的作用) 游标也可以理解为逐行返回SQL语句的结果集 如何编写一个游标? 1.声明游标 de ...

  6. [生活] 日常英语学习笔记-NEVER HAVE I EVER游戏

    逛油管,看视频,学英语. 大家要过周末了说啥 Happy Sunday Have a restful  Sunday 有个空闲的周末 我们正在看电影 We are watching movie it ...

  7. [android] 界面切换的核心方法

    根据效果图拆分界面 主体部分 View ==> ViewGroup ==> RelativeLayout,主体部分使用RelativeLayout作为占位 View和ViewGroup的区 ...

  8. redis(4)事务

    一.事务 一般来说,事务必须满足4个条件,也就是我们常说的ACID: 1)Atomicity 原子性:一个事务中的所有操作要么全部完成,要么全部不完成,不会结束在中间的某个环节.事务在执行过程中发生错 ...

  9. JAVA中LinkedLockingQueue的简单使用

    1.相关知识的了解 阻塞队列:当队列为空时,去队列中取数据会被阻塞.当队列满时,往队列中放数据会被阻塞.   非阻塞队列:当队列为空时,去队列取数据会直接返回失败,队列满时,往队列中放数据会直接返回失 ...

  10. Error:Annotation processors must be explicitly declared now.

    环境 Android Studio 3.0 Gradle 3.0.0 gradle 4.1 Error Error:Execution failed for task ':app:javaPreCom ...