实验五 类和对象-3

1.ex3.cpp

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5 
 6 // 函数声明 
 7 void output1(vector<string> &);  
 8 void output2(vector<string> &);  
 9 
10 int main()
11 {
12     vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes
13     
14     // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) 
15     // 补足代码 
16     // 。。。 
17      likes.push_back("favorite book");
18     likes.push_back("music");   
19     likes.push_back("film");
20     likes.push_back("paintings");
21  
22     
23     cout << "-----I like these-----" << endl;
24     // 调用子函数输出vector<string>数组对象likes的元素值 
25     // 补足代码
26     // 。。。 
27     output1(likes);
28     
29     // 为vector<string>数组对象dislikes添加元素值 
30     // 补足代码 
31     // 。。。 
32      dislikes.push_back("anime");
33       dislikes.push_back("sport");
34     dislikes.push_back("sportsman");
35     
36     cout << "-----I dislike these-----" << endl;
37     // 调用子函数输出vector<string>数组对象dislikes的元素值 
38     // 补足代码
39     // 。。。 
40     output2(dislikes);
41     
42     
43     // 交换vector<string>对象likes和dislikes的元素值 
44     // 补足代码
45     // 。。。 
46     likes.swap(dislikes);
47     
48     
49     cout << "-----I likes these-----" << endl;
50     // 调用子函数输出vector<string>数组对象likes的元素值 
51     // 补足代码
52     // 。。。 
53     output1(likes);
54     cout << "-----I dislikes these-----" << endl;
55     // 调用子函数输出vector<string>数组对象dislikes的元素值 
56     // 补足代码
57     // 。。。 
58     output2(dislikes);
59                         
60     return 0;
61 }
62 
63 
64 // 函数实现 
65 // 以下标方式输出vector<string>数组对象v的元素值  
66 void output1(vector<string> &v) {
67     // 补足程序
68     // 。。。 
69     for(int i=0;i<v.size();i++)
70     cout<<v[i]<<",";
71     cout<<endl;
72 }
73 
74 // 函数实现
75 // 以迭代器方式输出vector<string>数组对象v的元素值 
76 void output2(vector<string> &v) {
77     // 补足程序
78     // 。。。
79     vector<string>::iterator  iter;
80     for(iter=v.begin() ;iter!=v.end() ;iter++)
81     cout<<*iter<<",";
82     cout<<endl;
83 }

6-17

 1 #include <iostream>
 2 #include <stdlib.h>
 3 using namespace std;
 4 int main()
 5 { // int *p;定义了一个指针p,然而p并没有指向任何地址,所以当使用*p时是没有任何地址空间对应的,所以 *p=9 就会导致,不知道把这个1赋值给哪个地址空间了
 6     int *p=NULL;
 7     int a = 9;
 8     p =&a;
 9     cout << "The value at 不加上p: " << *p;
10     system("pause");
11     return 0;
12 }
13 /*不加上#include <stdlib.h>和system("pause"); 我vs2017的cmd框会闪退,求解惑.*/

6-18

 1 #include <iostream>
 2 #include "stdlib.h "
 3 using namespace std;
 4 int fn1() {
 5     int *p = new int(5);//new的值一定要delete
 6     return *p ;
 7     delete p;
 8 }
 9 int main()
10 {
11     int a = fn1();
12     cout << "the value of a is: " << a;
13     system("pause");
14     return 0;
15 }

期中考试题

第一题

 1 Dice.h
 2 
 3 class Dice {
 4 public:
 5     Dice(int n) :sides(n) {
 6 
 7     }
 8     int cast()
 9     {
10         return rand() % sides + 1;
11     }
12 
13 private:
14     int sides;
15 
16 };
17 
18 main.cpp
19 #include<iostream>
20 #include<cstdlib>
21 #include<ctime>
22 #include"Dice.h"
23 
24 using namespace std;
25 
26 int main() {
27 
28     srand(time(NULL));
29 
30     Dice A(40);
31 
32     int num = 0;
33 
34     for (int j = 0; j<500; ++j) {
35 
36         if (A.cast() == 19)num++;
37 
38     }
39 
40     double p = 0;
41 
42     p = (double)num / 500.0;
43 
44     cout << "学号 20178303019 被抽中的概率是:" << p << endl;
45 
46     return 0;
47 
48 }

第三题

book.h

 1 #ifndef BOOK_H
 2 #define BOOK_H
 3 
 4 #include <string>
 5 using std::string;
 6 
 7 class Book {
 8     public:
 9         Book(string isbnX, string titleX, float priceX);  //构造函数  
10         void print(); // 打印图书信息 
11     private:
12         string isbn;
13         string title;
14         float price;
15 };
16 #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() {cout << "Isbn: " << isbn << " Title: " << title << " Price: " << price << endl;
}

main.cpp

#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时为止 (也可以自行定义录入结束方式) // 补足程序// ... cout << "请依次录入图书信息 出版编号(isbn), 书名(title), 定价(price)" << endl;do{cin >> isbn >> title >> price;} while (cin >> isbn);// 输出入库所有图书信息// 补足程序// ... for (int i = 0; i<BOOK.size(); i++)BOOK[i].print();return 0;
}

//可能是我停止录入那边的代码写错了。。不会写,求指点

动态矩阵

matrix.h

 1 #ifndef MATRIX_H
 2 #define MATRIX_H
 3 class Matrix {
 4     public:
 5         Matrix(int n); // 构造函数,构造一个n*n的矩阵 
 6         Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 
 7         Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 
 8         ~Matrix(); //析构函数 
 9         void setMatrix(const float *pvalue); // 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值 
10         void printMatrix() const; // 显示矩阵
11         inline float &element(int i, int j); //返回矩阵第i行第j列元素的引用
12         inline float element(int i, int j) const;// 返回矩阵第i行第j列元素的值 
13         void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value
14         inline int getLines() const; //返回矩阵行数 
15         inline int  getCols() const; //返回矩阵列数 
16     private:
17         int lines;    // 矩阵行数
18         int cols;      // 矩阵列数 
19         float *p;   // 指向存放矩阵数据的内存块的首地址 
20 };
21 #endif

matrix.cpp

#include<iostream>
#include"matrix.h"
using namespace std;
int i, j;
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];for (i = 0; i<lines*cols; i++)p[i] = x.p[i];
}
Matrix::~Matrix() {delete[] p;
}
void Matrix::setMatrix(const float *pvalue) {for (i = 0; i<lines*cols; i++)p[i] = pvalue[i];
}
void Matrix::printMatrix() const {for (i = 0; i<lines; i++) {for (j = 0; j<cols; j++)cout << p[i*lines + j] << " ";cout << endl;}
}
inline float Matrix::element(int i, int j) const {cout << p[(i - 1)*lines + j - 1] << endl;
}
void Matrix::setElement(int i, int j, int value) {p[(i - 1)*lines + j - 1] = value;
}
inline int Matrix::getLines() const {cout << lines << endl;
}
inline int Matrix::getCols() const {cout << cols << endl;
}

main.cpp

#include<iostream>
#include"matrix.h"
using namespace std;
int main()
{int l, c, i, j, n;cout << "输入行数和列数" << endl;cin >> l >> c;float a[l*c];cout << "初始化矩阵" << endl;for (i = 0; i<l*c; i++)cin >> a[i];Matrix A(l, c);A.setMatrix(a);cout << "输出矩阵" << endl;A.printMatrix();cout << "输入行数和列数输出该数" << endl;cin >> i >> j;A.element(i, j);cout << "改变第几行第几列的数" << endl;cin >> n;A.setElement(i, j, n);cout << "输出这个数" << endl;A.element(i, j);cout << "返回矩阵行数与列数" << endl;A.getLines();A.getCols();return 0;
}

真的 看了很多人很多人的代码,这行都是这么写的。

但我确实编不出来啊。。。

期中考试 第二条 还不会写

就不把代码拿出来丢人了。。

转载于:https://www.cnblogs.com/wuyijie/p/9074139.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/452582.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vector用法详解

这篇文章的目的是为了介绍std::vector&#xff0c;如何恰当地使用它们的成员函数等操作。本文中还讨论了条件函数和函数指针在迭代算法中使用&#xff0c;如在remove_if()和for_each()中的使用。通过阅读这篇文章读者应该能够有效地使用vector容器&#xff0c;而且应该不会再去…

linux 共享移动硬盘,随时登陆上QQ 自带Linux移动硬盘实战

在以往我们的观念中&#xff0c;移动硬盘顶多就是个移动存储设备&#xff0c;根本谈不上有什么功能&#xff0c;但今天这款一盘通却将我们原始的观念打了一个180大转弯&#xff01;如果你的电脑支持USB设备启动&#xff0c;那么只需要在BIOS进行一下更改&#xff0c;一盘通就可…

需求分析的图形工具(层次方框 warnier IPO)

1 层次方框图 层次方框图用树形结构的一系列多层次的矩形框描绘数据的层次结构。 例如&#xff0c;描绘一家计算机公司全部产品的数据结构可以用下图层次方框图表示。 这家公司的产品由硬件、软件和服务3类产品组成&#xff0c;软件产品又分为系统软件和应用软件&#xf…

如何处理错误信息 Pricing procedure could not be determined

2019独角兽企业重金招聘Python工程师标准>>> 当给一个SAP CRM Quotation文档的行项目维护一个产品时&#xff0c;遇到如下错误信息&#xff1a;Pricing procedure could not be determined 通过调试得知错误消息在function module CRM_PRIDOC_COM_PRCPROC_DET_SEL第…

Flask爱家租房--订单(下订单)

文章目录0 、效果展示1、思路总结2、后端代码3、前端js4、前端html0 、效果展示 detail.html booking.html 1、思路总结 1&#xff09;用户打开房屋详情页detail.html之后&#xff0c;后端detail.js会判断此访问用户是否为房东&#xff0c;若不是房东&#xff0c;则在详情…

linux下各权限的细分

PS&#xff1a;有时候你发现用root权限都不能修改某个文件&#xff0c;大部分原因是曾经用chattr命令锁定该文件了。chattr命令的作用很大&#xff0c;其中一些功能是由Linux内核版本来支持的&#xff0c;不过现在生产绝大部分跑的linux系统都是2.6以上内核了。通过chattr命令修…

红帽linux lnmp搭建,Linux(redhat5.4)下lnmp环境的搭建

在前面我们已经实现了lamp架构的创建&#xff0c;今天就让我们来看一看lnmp架构是如何实现的。计划的实验步骤如下&#xff1a;1. 数据库mysql的安装2. Nginx的安装&#xff0c;libevent(编译库代码)的安装&#xff0c;pcre的安装3. Php的安装4. 测试1. Mysql 的安装//注意:小编…

为什么借助开源学习是最有效的?

导读&#xff1a;盛大创新院高级研究员庄表伟近日编撰系列文章《借助开源项目&#xff0c;学习软件开发》活动&#xff0c;引起业界关注。庄表伟认为&#xff0c;通过编撰这些文章&#xff0c;希望更多开发者能够借助开源项目提高开发效率&#xff0c;减少重复劳动并从开源软件…

redux middleware 源码分析

原文链接 middleware 的由来 在业务中需要打印每一个 action 信息来调试&#xff0c;又或者希望 dispatch 或 reducer 拥有异步请求的功能。面对这些场景时&#xff0c;一个个修改 dispatch 或 reducer 代码有些乏力&#xff0c;我们需要一个可组合的、自由增减的插件机制&…

Flsak爱家租房--订单(获取用户订单、用户评论)

文章目录0.页面效果1.思路总结2.后端代码3.前端js4.前端html0.页面效果 1.思路总结 1&#xff09;用户点击“我的订单”&#xff0c;js向后端获取数据&#xff0c;并加载在前端的模板中&#xff1b; 2&#xff09;用户点击相应订单的“去支付”按钮&#xff0c;js向引导用户…

【SCOI2005】【BZOJ1087】互不侵犯King(状压dp)

problem 在NN的棋盘里面放K个国王每个国王会攻击它周围的一圈共8个格子使他们互不攻击&#xff0c;共有多少种摆放方案N < 9solution 用01串表示某一行放置的情况 首先枚举当前做到第几行&#xff0c;以及当前一共放了几颗棋子。于是状态f[i][j][k]表示到第i行&#xff0c;一…

软件工程形式化技术简介

形式化技术在软件工程中有效的提高了开发的效率、改进了软件开发的质量、减少了开发费用。形式化的技术容易在软件的规约上取得一致性&#xff0c;它属于一种非常有效的交流方式。 (一)非形式化的缺点 用自然语言书写的系统规格说明书&#xff0c;可能存在矛盾、二义性、含糊性…

华为荣耀笔记本linux怎么下载软件,华为magic book笔记本怎么下载软件

大家好&#xff0c;我是时间财富网智能客服时间君&#xff0c;上述问题将由我为大家进行解答。华为magic book笔记本下载软件的方法如下&#xff1a;1、首先&#xff0c;点击桌面开始图标&#xff0c;找到应用商店&#xff0c;并点击。2、进入应用商店&#xff0c;点击搜索栏&a…

国内外软件开发上的差距与分析

提高自己&#xff0c;迎接好的未来。 在开始任何其他文字之前&#xff0c;首先有必要正视一个根本现实&#xff1a;国内外软件开发的水平是有差距的。 这一结论的最直接证据是每一轮新技术的发起者基本上都是国外的人或公司&#xff1a; 从方法论&#xff08;CMMI&#xff0…

Flask爱家租房--订单(房东接单、拒单)

文章目录0.效果展示1.效果展示2.后端接口3.前端js4.前端html0.效果展示 1.效果展示 1&#xff09;当房东点击“客户订单”&#xff0c;js向后端接口get_user_orders()获取数据&#xff0c;订单页面开始加载&#xff1b; 2&#xff09;当房东确定接单时&#xff0c;js会向后端…

WebView性能优化--独立进程

Android允许一个app同时存在多个进程&#xff0c;可以根据需要把不同的模块放到不同进程中处理。 一、WebView独立进程的好处 1.有效增大App的运存&#xff0c;减少由webview引起的内存泄露对主进程内存的占用。 2.避免WebView的Crash影响App主进程的运行。 3.拥有对WebView独立…

linux修改python默认版本

linux修改python默认版本 update-alternatives --config pythonposted on 2018-05-24 22:42 psycheman 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/psycheman/p/9085576.html

什么是有穷状态机

有穷状态机的作用是描述对象在它的生命周期内所经历状态序列&#xff0c;以及如何响应来自外界的事件。有穷状态机首先包含一个有限状态的集合&#xff0c;还包含了从一个状态到另外一个状态的转换。 有穷自动机看上去就像是一个有向图&#xff0c;其中状态是图的节点&#xf…

linux设置开机自启 etc rt.d,Linux下禁止服务开机自启动

一、 Upstart是兼容System V的配置方式的&#xff0c;但主要的服务配置放在 /etc/init 下&#xff0c;这也就是为什么修改 /etc/rc${runlevel}.d/ (Ubuntu默认启动runlevel2&#xff0c;也就是/etc/rc2.d/)下的MySQL启动配置并不能真正起到禁止MySQL自启动的原因(比如使用命令 …