实验五
一、补足代码
#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("sport"); 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); // 为vector<string>数组对象dislikes添加元素值 // 补足代码 // 。。。 dislikes.push_back("learning"); dislikes.push_back("writing essay"); 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) { // 补足程序 // 。。。 vector<string>::iterator it; for (it = v.begin(); it != v.end(); ++it) cout << *it << ","; cout << endl; }
二、(1)
#include "stdafx.h" #include<iostream> using namespace std; int main() { int *p; *p = 9; //此语句错误可改为 int i=9; p=&i; cout << "The value at p:" << *p; return 0; }
(2)
#include "stdafx.h" #include<iostream> using namespace std; int fn1() { int *p = new int(5); //因改为int *p; p=new int (5); return *p; } int main() { int a = fn1(); cout << "the value of a is:" << a; return 0; }
三、设计一个动态矩阵类
#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) { return p[i*lines + j]; } //返回矩阵第i行第j列元素的引用 inline float element(int i, int j) const { return p[i*lines + j]; }// 返回矩阵第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
#include "stdafx.h" #include "matrix.h" #include<iostream> using namespace std; Matrix::Matrix(int n) { lines = n; cols = n; } Matrix::Matrix(int n, int m) { lines = n; cols = m; } Matrix::~Matrix() { delete[] p; } Matrix::Matrix(const Matrix &X) { lines = X.lines; cols = X.cols; p = X.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*lines+j] << " "; cout << endl; } } void Matrix::setElement(int i, int j, int value) { element(i, j) = value; }
#include "stdafx.h" #include <iostream> #include "matrix.h" using namespace std; int main() { Matrix m(4); const float *pvalue[20] = { 0 }; m.setMatrix(*pvalue); m.setElement(2, 4, 6); m.printMatrix(); return 0; }
期中考试
一、
#include "stdafx.h" #include<iostream> #include<time.h> using namespace std; class dice { public: dice(int d); int cast(); private: int sides; }; dice::dice(int d) { sides = d; } int dice::cast() { int p = rand() % sides + 1; return p; } int main() { srand((unsigned)(time(NULL))); dice d(40); int k = 0; for (int i = 1; i <= 500; i++) { if (d.cast() == 16) k++; } cout << k << endl; return 0; }
二、不会
三、
#include "book.h" #include <iostream> #include <string> #include "stdafx.h" using namespace std; Book::Book(string isbnX, string titleX, float priceX) { isbn = isbnX; title = titleX; price = priceX; } // 构造函数 // 补足程序 // ... void Book::print() { cout << isbn << " " << title << " " << price << endl; } // 打印图书信息 // 补足程序 // ...
#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 <vector> #include <iostream> #include "stdafx.h" using namespace std; int main() { vector<Book> books; // 定义一个vector<Book>类对象 // 补足程序 // ... string isbn, title; float price; while (cin >> isbn >> title >> price) { books.push_back(Book(isbn, title, price)); } // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中 // 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) // 补足程序 // ... for (int i=0;i<books.size();i++) { books[i].print(); } // 输出入库所有图书信息 // 补足程序 // ... return 0; }

浙公网安备 33010602011771号