实验5
= = = = = = = = = = = = = = = = = = = =
vector
- - - - - - - - - - - - - - - - - - - - - - - - - -
#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");
likes.push_back("music");
likes.push_back("film");
likes.push_back("paintings");
likes.push_back("anime");
likes.push_back("sports");
likes.push_back("sportsman");
// 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc)
// 补足代码
// 。。。
cout << "-----I like these-----" << endl;
output1(likes);
// 调用子函数输出vector<string>数组对象likes的元素值
// 补足代码
// 。。。
cout<< "-----What you dislike-----"<<endl;
string n;
while(1){
cin>>n;
if(n=="over") break;
dislikes.push_back(n);
}
// 为vector<string>数组对象dislikes添加元素值
// 补足代码
// 。。。
cout << "-----I dislike these-----" << endl;
output1(dislikes);
// 调用子函数输出vector<string>数组对象dislikes的元素值
// 补足代码
// 。。。
likes.swap(dislikes);
// 交换vector<string>对象likes和dislikes的元素值
// 补足代码
// 。。。
cout << "-----I likes these-----" << endl;
output1(likes);
// 调用子函数输出vector<string>数组对象likes的元素值
// 补足代码
// 。。。
cout << "-----I dislikes these-----" << endl;
output1(dislikes);
// 调用子函数输出vector<string>数组对象dislikes的元素值
// 补足代码
// 。。。
return 0;
}
// 函数实现
// 以下标方式输出vector<string>数组对象v的元素值
void output1(vector<string> &v) {
for(int i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
// 补足程序
// 。。。
}
// 函数实现
// 以迭代器方式输出vector<string>数组对象v的元素值
void output2(vector<string> &v) {
vector<string>::iterator it;
for(it=v.begin();it!=v.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
// 补足程序
// 。。。
}
string
- - - - - - - - - - - - - - - - - - - - - - - - -
#include<iostream>
using namespace std;
int main(){
int *p;
//*p=9;//指针指向地址,不是一个值
//改正如下
int a=9;
*p=a;//将a的地址给p
cout<<"The value at p:"<<*p;
return 0;
}
#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;
//去掉返回值
}
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; // 显示矩阵
float &element(int i, int j){return *(p+(i-1)*cols+j-1);} //返回矩阵第i行第j列元素的引用
float element(int i, int j) const{return *(p+(i-1)*cols+j-1);}// 返回矩阵第i行第j列元素的值
void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
int getLines() const{return lines;} //返回矩阵行数
int getCols() const{return cols;} //返回矩阵列数
private:
int lines; // 矩阵行数
int cols; // 矩阵列数
float *p; // 指向存放矩阵数据的内存块的首地址
};
#endif
类的实现
#include<iostream>
#include "Matrix.h"
using namespace std;
//构造函数n*n
Matrix::Matrix(int n):lines(n),cols(n){
p=new float[lines*cols];
}
//构造函数n*m
Matrix::Matrix(int n,int m):lines(n),cols(m){
p=new float[lines*cols];
}
//析构函数
Matrix::~Matrix(){
delete []p;
}
//复制构造函数
Matrix::Matrix(const Matrix &X):lines(X.lines),cols(X.cols){
p=new float[lines*cols];
}
//矩阵赋初值
void Matrix::setMatrix(const float *pvalue){
for(int i=0;i<lines*cols;i++)
{
p[i]=pvalue[i];
}
}
//显示矩阵
void Matrix::printMatrix() const{
cout<<"The Matrix is:"<<endl;
for(int i=0;i<lines;i++)
{
for(int j=0;j<cols;j++)
{
cout<<p[i*cols+j]<<" ";
}
cout<<endl;
}
}
//置矩阵第i行第j列元素值为value
void Matrix::setElement(int i, int j, int value){
*(p+(i-1)*cols+j-1)=value;
}
主函数
#include <iostream>
#include "Matrix.h"
using namespace std;
int main() {
float a[]={
1,2,3,
4,5,6,
7,8,9};
Matrix b(3);
b.setMatrix(a);
Matrix c(b);
b.printMatrix();//打印矩阵
cout<<b.element(1,1)<<endl;//返回1行1列的值
b.setElement(1,1,6);//改变1行1列的值
cout<<b.element(1,1)<<endl;//再次返回
float *p=&b.element(1,1);
cout<<p<<endl;//返回引用
cout<<b.getLines()<<endl;//返回行数
cout<<b.getCols()<<endl; //返回列数
return 0;
}
在这个地方我的i行j列返回出来的是i+1行j列,但setElement改变的是i行j列的值,没搞懂问题出在哪
期中考试 Book
- - - - - - - - - - - - - - - - - -
类的声明
#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>类对象
// 补足程序
// ...
vector<Book>a;
string isbn, title;
float price;
// 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
// 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式)
// 补足程序
// ...
int c;
while(cin>>isbn>>title>>price)
{
Book b(isbn,title,price);
a.push_back(b);
}
cin.clear();
// 输出入库所有图书信息
// 补足程序
// ...
for(int i=0;i<a.size();i++)
{
a[i].print();
}
return 0;
}