1.ex3.cpp:
#include <iostream> #include <vector> #include <string> using namespace std; // 函数声明 void output1(vector<string> &); void output2(vector<string> &); int main() { vector<string>likes(7), dislikes(7); // 创建vector<string>对象likes和dislikes likes.push_back("《巨人的陨落》"); // 为vector<string>数组对象likes添加元素值 likes.push_back("《Star Sky》");//( favorite book, music, film, paintings,anime,sport,sportsman,etc) likes.push_back("《Doctor Strange》");// 补足代码 likes.push_back("《千里江山图》");// 。。。 likes.push_back("《秦时明月》"); likes.push_back("Running"); likes.push_back("林丹"); cout << "-----I like these-----" << endl; output1(likes);// 调用子函数输出vector<string>数组对象likes的元素值 // 补足代码 // 。。。 dislikes.push_back("没什么营养的言情小说");// 为vector<string>数组对象dislikes添加元素值 dislikes.push_back("有低俗内容的音乐");// 补足代码 dislikes.push_back("纯为圈钱的商业烂片和恐怖片");// 。。。 dislikes.push_back("太抽象的作品"); dislikes.push_back("低幼动画"); dislikes.push_back("引体向上"); dislikes.push_back("无体育精神的运动员"); cout << "-----I dislike these-----" << endl; output2(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; output2(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] << " "; } 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; }
运行效果:
6-18:
#include<iostream> using namespace std; int fn1() { int *p = new int(5); return *p; } int main() { int a = fn1(); cout << "the value of a is:" << a; return 0;//new与delete未配对使用,虽然此程序可以运行,但在其他情况下可能发生严重的内存泄露。 }
解决方案:
#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; }
6-17:
#include<iostream> using namespace std; int main() { int *p; *p = 9; //上述代码没有将地址赋给p,由于p没有被初始化,它可能有任何值,因此会出错。 cout << "The value at p:" << *p; return 0; }
解决方案:
#include<iostream> using namespace std; int main() { int *p; int n=9; p = &n; cout << "The value at p:" << *p; return 0; }
动态矩阵类Matrix:
matrix.h:
#pragma once #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 { return lines; };//返回矩阵行数 inline int getCols() const { return cols; }; //返回矩阵列数 private: int lines; // 矩阵行数 int cols; // 矩阵列数 float *p; // 指向存放矩阵数据的内存块的首地址 }; #endif
matrix.cpp:
#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[lines*cols]; } Matrix::~Matrix() { //析构函数 delete[]p; } 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; } } inline float &Matrix::element(int i, int j) { return p[i*cols + j]; } inline float Matrix::element(int i, int j) const { return p[i*cols + j]; } void Matrix::setElement(int i, int j, int value) { p[i*cols + j] = value; }
main.cpp:
#include"matrix.h" #include<iostream> using namespace std; int main() { Matrix A(3); Matrix B(3, 4); const float a[9] = { 1,2,3, 4,5,6, 7,8,9 }; const float b[12] = { 1,2,3,4, 5,6,7,8, 9,10,11,12, }; const float c[12] = { 1,2,3,4, 5,6,7,8, 9,10,11,12, }; A.setMatrix(a); B.setMatrix(b); Matrix C(B); C.setMatrix(c); A.printMatrix(); B.printMatrix(); C.printMatrix(); A.setElement(1, 1, 100);B.setElement(1, 1, 100);C.setElement(1, 1, 100); A.printMatrix(); B.printMatrix(); C.printMatrix(); cout << A.getLines() << " " << B.getLines() << " " << C.getLines() << endl; cout << A.getCols() << " " << B.getCols() << " " << C.getCols() << endl; return 0; }
注:在此我要承认一件事,我在主函数中没有测试&element(int i,int j);和element(int i,int j);因为我不知道怎么用,一用就报错,求大神指点,副情况如下:
这是出错的代码:
运行效果:
期中考试:
因为我不会用Dev c,机房又没找到vs,只找到了vc++2010,加上知识掌握又不牢靠,所以……编程题全都没写,现在全都补上……
第一题:
#include<iostream> #include<cstdlib> #include<ctime> using namespace std; class Dice { private:int sides; public: Dice(int n) { sides = n; } int cast() { int m; m = (rand() % (sides - 1 + 1) + 1); return m; } }; int main() { srand((unsigned)time(NULL));//切记:srand别放在循环体里,否则效果感人…… int n,x; cout << "请输入班级人数和你的学号:"; cin >> n >> x; Dice A(n); int i; int m;double y = 0.00; for (i = 0; i < 500; i++) { if (A.cast() == x)y++; } double p = (double)y / 500.00; cout << "你的学号被抽中的概率为:" << p << endl; return 0; }
运行效果:
第二题:
#include<iostream> #include<string> using namespace std; static int n=999; class User { private: int id; string name; string password; static int CurrentID; public: User(string s1,string s2="111111") { n++; id = n; name = s1; password = s2; CurrentID++; } User(User &u1) { id = u1.id; name = u1.name; password = u1.password; CurrentID = u1.CurrentID; } void show1() { cout << "该用户信息为 id:" << id << " name:" << name << " password:" << password << endl; } static void show2(User &u1) { //输出CurrentID cout << "CurrentID:" << CurrentID << endl; cout << "最后一个新增用户为:"; u1.show1(); } void change() { //修改密码 string s3, s4; int j; j = 3; while (j) { cout << "请输入原有密码:"; cin >> s3; if (s3 == password) { cout << "请输入新密码:"; cin >> s4; password = s4; break; } if (s3 != password) { j--; cout << "输入原密码错误!" << endl; } } if (j == 0)cout << "错误次数过多,请稍后再试" << endl; } }; int User::CurrentID = 999;//给CurrentID赋初值 int main() { User u1("user1", "123456"); User u2("user2"); User u3("user3", "234567"); u1.show1(); u2.show1(); u3.show1(); User u(u3); User::show2(u3); u1.change(); u1.show1(); u2.change(); return 0; }
运行效果:
第三题:
//book.h #pragma once #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
//book.cpp #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() { //print函数的实现 cout << "isbn:" << isbn << " title:" << title << " price:" << price << endl; }
//main.cpp #include "book.h" #include <vector> #include <iostream> using namespace std; int main() { vector<Book>books; string isbn, title; float price; while (1) { cout << "请输入图书信息,若想终止输入,请输入Quit;" << endl; cin >> isbn;//先输入isbn码,判断是否执行输入操作 if (isbn != "Quit") { cin >> title >> price; } if (isbn == "Quit")break;//输入Quit终止 Book a(isbn, title, price); books.push_back(a);//向数组中加入新的对象 } int i; for (i = 0; i < books.size(); i++) { books[i].print();//输出所有图书信息 } return 0; }
运行效果:
实验总结与反思:
这次实验做得是真累,归根结底还是知识掌握不牢靠,即使你觉得你掌握的不错了,期中考试就是一个鲜活的例子。此外,光看书是没用的,因为实际操作中往往会出现很多书中没有提到的情况,让人抓狂。