vector3
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 函数声明
void output1(vector<string> &);
void output2(vector<string> &);
int main()
{
vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes
likes.push_back("favorite book, music, film, paintings, anime, sport, sportsman, etc"); // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc)
// 补足代码
// 。。。
cout << "-----I like these-----" << endl;
output1(likes);
// 调用子函数输出vector<string>数组对象likes的元素值
// 补足代码
// 。。。
dislikes.push_back("study");
// 为vector<string>数组对象dislikes添加元素值
// 补足代码
// 。。。
cout << "-----I dislike these-----" << endl;
output2(dislikes);
// 调用子函数输出vector<string>数组对象dislikes的元素值
// 补足代码
// 。。。
likes.swap(dislikes);
// 交换vector<string>对象likes和dislikes的元素值
// 补足代码
// 。。。
cout << "-----I likes these-----" << endl;
output2(likes);
// 调用子函数输出vector<string>数组对象likes的元素值
// 补足代码
// 。。。
cout << "-----I dislikes these-----" << endl;
output1(dislikes);
// 调用子函数输出vector<string>数组对象dislikes的元素值
// 补足代码
// 。。。
return 0;
}
// 函数实现
// 以下标方式输出vector<string>数组对象v的元素值
void output1(vector<string> &v) {
int i;
for (i = 0; i<v.size(); i++)
cout << v[i] << endl;
// 补足程序
// 。。。
}
// 函数实现
// 以迭代器方式输出vector<string>数组对象v的元素值
void output2(vector<string> &v) {
vector<string>::iterator it;
for (it = v.begin(); it!= v.end(); it++)
cout << *it << endl;
// 补足程序
// 。。。
}
![]()
6-17
#include<iostream>
using namespace std;
int main()
{
int *p;
//*p = 9;
int i = 9;
p = &i;
cout << "the value of a is :" << *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 << endl;
return 0;
}
//new申请的内存要用delete释放内存
![]()
Matrix
#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列元素的引用
inline float element(int i, int j) const;// 返回矩阵第i行第j列元素的值
void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
inline int getLines() const; //返回矩阵行数
inline int getCols() const; //返回矩阵列数
private:
int lines; // 矩阵行数
int cols; // 矩阵列数
float *p; // 指向存放矩阵数据的内存块的首地址
};
#endif
#include "matrix.h"
#include <iostream>
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 = new float[X.lines*X.cols];
int i;
for (i = 0; i < X.lines*X.cols; i++)
p[i] = X.p[i];
}
Matrix::~Matrix() {
delete[] p;
}
void Matrix::setMatrix(const float *pvalue) {
int i;
for (i = 0; i < lines*cols; i++)
p[i] = pvalue[i];
}
void Matrix::printMatrix() const {
int i, j;
for (i = 0; i < lines; i++)
{
for (j = 0; j < cols; j++)
{
cout << p[i*lines + j] << " ";
}
cout << endl;
}
}
void Matrix::setElement(int i, int j, int value) {
p[i*cols + j] = value;
}
inline int Matrix::getLines() const {
cout << lines << endl;
}
inline int Matrix::getCols() const {
cout << cols << endl;
}
int main() {
int m, n, k;
cout << "输入一个m*n矩阵的行数(列数)" << endl;
cin >> m >> n;
float* a= new float[m*n];
for (int i = 0; i<m*n; i++)
cin >> a[i];
Matrix x1(m, n);
x1.setMatrix(a);
x1.printMatrix();
cin >> n >> m;
cout << x1.element(n, m) << endl;
cin >> k;
x1.setElement(n, m, k);
x1.printMatrix();
cout << endl << x1.getLines() << endl;
cout << endl << x1.getCols() << endl;
delete[] a;
return 0;
}
![]()
1.
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
class Dice {
public:
Dice(int n);
int cast();
private:
int sides;
};
Dice::Dice(int n)
{
sides = n;
}
int Dice::cast()
{
int val;
val = (rand() % (sides + 1) + 1);
return val;
}
int main() {
srand((unsigned)time(NULL));
int a, b, c, d = 0;
cout << "输入班级人数和学号" << endl;
cin >> a >> b;
Dice x1(a);
for (int i = 0; i<500; i++) {
c = x1.cast();
if (c == b) {
d++;
}
}
cout << "被抽中的概率为" << endl;
cout << d / 500.0 << endl;
return 0;
}
![]()
2.
#ifndef USER_H
#define USER_H
#include<iostream>
#include<string>
using namespace std;
class User
{
public:
User(string a,string b);
void Print();
void Change();
void getCurrentID();
int currentID();
private:
string name;
int id;
int CurrentID;
string password;
};
#endif
#include<iostream>
#include"user.h"
#include<string>
using namespace std;
User::User(string a,string b):name(a),password(b)
{
int static CurrentID=999;
id=++CurrentID;
}
void User::Print()
{
cout<<"id:"<<id<<endl;
cout<<"用户名:"<<name<<endl;
cout<<"密码:"<<password<<endl;
}
void User::Change()
{
string p;
cout<<"请输入原密码:"<<endl;
cin>>p;
while(p!=password)
{
cout<<"please try agian!"<<endl;
cin>>p;
}
cout<<"请输入新密码:"<<endl;
cin>>p;
password=p;
cout << "修改成功,当前密码为:" << p << endl;
}
void User::getCurrentID()
{
cout<<id<<endl;
}
#include <iostream>
#include"user.h"
#include <string>
using namespace std;
int main()
{
int m,n,k;
string name,password;
cout<<"请输入用户名:"<<endl;
cin>>name;
cout<<"请输入密码:"<<endl;
cin>>password;
User a(name,password);
a.Print();
char key;
cout<<"按c可以修改密码:"<<endl;
cin>>key;
if(key=='c')
a.Change();
cout<<"当前用户ID为:";
a.getCurrentID();
a.Print();
return 0;
}
![]()
3.
#ifndef BOOK_H
#define BOOK_H
#include <string>
using std::string;
class Book {
public:
Book(string isbnX, string titleX, float priceX); //构造函数
void print(); // 打印图书信息
private:
string isbn;
string title;
float price;
};
#endif
#include "book.h"
#include <iostream>
#include <string>
using namespace std;
Book::Book(string isbnX, string titleX, float priceX){
isbn=isbnX;
title=titleX;
price=priceX;
}
void Book::print()
{
cout<<"isbn:"<<isbn<<" "<<"title:"<<title<<" "<<"price:"<<price<<endl;
}
// 构造函数
// 补足程序
// ...
// 打印图书信息
// 补足程序
// ...
#include "book.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<Book>book;
// 定义一个vector<Book>类对象
// 补足程序
// ...
string isbn,title,a,b;
float price,c;
for(int i=2;;i++)
{
cin>>a;
if(a=="stop")
break;
cin>>b>>c;
Book Book(a, b, c);
book.push_back(Book);
}
// 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
// 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式)
// 补足程序
// ...
for(int j=0;j<book.size();j++){
book[j].print();
}
// 输出入库所有图书信息
// 补足程序
// ...
return 0;
}