实验1:
#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("book"); likes.push_back("music"); likes.push_back("film"); likes.push_back("paintings"); likes.push_back("anime"); likes.push_back("sportsman"); likes.push_back("etc"); // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) cout << "-----I like these-----" << endl; // 调用子函数输出vector<string>数组对象likes的元素值 output1(likes); dislikes.push_back("book'"); dislikes.push_back("music'"); dislikes.push_back("film'"); dislikes.push_back("paintings'"); dislikes.push_back("anime'"); dislikes.push_back("sportsman'"); dislikes.push_back("etc'"); // 为vector<string>数组对象dislikes添加元素值 cout << "-----I dislike these-----" << endl; // 调用子函数输出vector<string>数组对象dislikes的元素值 output1(dislikes); // 交换vector<string>对象likes和dislikes的元素值 likes.swap(dislikes); cout << "-----I likes these-----" << endl; // 调用子函数输出vector<string>数组对象likes的元素值 output2(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<<v[i]<<" "; cout<<endl; } // 函数实现 // 以迭代器方式输出vector<string>数组对象v的元素值 void output2(vector<string> &v) { for(vector<string>::iterator i = v.begin() ; i<v.end() ; ++i)//使用迭代器访问vector中的元素 cout<<*i<<" "; cout<<endl; }
实验2:
#include <iostream> using namespace std; int main() { int *p; // *p=9; 指针指向不明 p=new int(9); cout<<"The value at p: "<<*p; delete p; return 0; }
#include <iostream> using namespace std; int fn1() { int *p=new int (5); delete p; //释放内存 return *p; } int main() { int a=fn1(); cout<<"The value of a is: "<<a; return 0; }
实验3:
#include <iostream> #include "matrix.h" using namespace std; int main() { Matrix x1(2, 5); x1.printMatrix(); cout << endl; Matrix x2(2, 5); float s[2*5] = { 1,2,3,4,5,6,7,8,9,10 }; x2.setMatrix(s); x2.printMatrix(); return 0; }
#include <iostream> #include "matrix.h" using namespace std; Matrix::Matrix(int n) { lines = n; cols = n; p = new float[lines*cols]; for (int i = 0; i < lines*cols; ++i) p[i] = 0; } Matrix::Matrix(int n, int m) { lines = n; cols = m; p = new float[lines*cols]; for (int i = 0; i < lines*cols; ++i) p[i] = 0; } Matrix::Matrix(const Matrix &X) { lines = X.lines; cols = X.cols; p = new float[lines*cols]; for (int i = 0; i < lines*cols; ++i) p[i] = X.p[i]; } 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 { for (int i = 0; i < cols; ++i) { for (int j = 0; j < lines; ++j) cout << p[i*lines + j] << " "; cout << endl; } } float &Matrix::element(int i, int j) { return *(p + i * lines + j); } float Matrix::element(int i, int j) const { return p[i*lines + j]; } void Matrix::setElement(int i, int j, int value) { p[i*lines + j] = static_cast<float>(value); } int Matrix::getLines() const { return lines; } int Matrix::getCols() const { return cols; }
#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
实验4:
4.1:
#include <iostream> #include "dice.h" using namespace std; int main() { int n; int k = 0; //抽中次数 cout << "请输入班级人数:"; cin >> n; Dice d1(n); for (int i = 1; i <= 500; ++i) { if ((d1.cast() + 1) == 40) ++k; } cout << "被抽中几率为:" << static_cast<double>(k) / 500.0000 << endl; return 0; }
#pragma once class Dice { private: int sides; public: Dice(int); int cast(); };
#include <iostream> #include "dice.h" #include <cstdlib> #include <ctime> Dice::Dice(int n) { sides = n; } int Dice::cast() { srand(time(NULL)+rand()); return (rand() % sides); }
4.2:
#include <iostream> #include "user.h" using namespace std; int main() { char x1[25]; cin >> x1; User user1(x1); user1.puts(); user1.change_password(); user1.puts(); return 0; }
#include <iostream> #include <string> #include "user.h" using namespace std; User::User(char *p) { static int CurrentID = 0; ++CurrentID; id = CurrentID; strcpy_s(name, p); strcpy_s(password, "111111"); } void User::puts() { cout << "CurrentID: " << id << endl; cout << "id: " << id << " , name: " << name << " , password: " << password << endl; } void User::change_password() { int i; for (i = 1; i <= 3; ++i) { cout << "请输入原密码:"; char a[25]; cin >> a; if (strcmp(a, password) == 0) break; else cout << "密码错误,重新输入(还有" << (3 - i) << "次机会)" << endl; } if (i == 4) cout << "输入错误次数过多,请稍后再尝试!" << endl; else { cout << "请输入新密码:"; char b[25]; cin >> b; strcpy_s(password, b); } }
#pragma once class User { public: User(char *); void puts(); void change_password(); private: int id; char name[25]; char password[25]; };
4.3:
#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> book; string isbn, title; float price; // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中 // 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) while(cin >> isbn >> title >> price) { Book x(isbn, title, price); book.push_back(x); } // 输出入库所有图书信息 for (int i = 0; i < book.size(); ++i) { book[i].print(); } return 0; }
#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
我自己大致认识到自己的编程速度不够快,应该是对相关内容的掌握不够牢固,而且编程训练不够多。前路还很漫长……
通过这次作业,我对函数模板的掌握更加到位了,但还有许多不太清楚的地方,在今后一定能一一克服。