#include "stdafx.h"#include <iostream>#include <vector>#include <string>#define rap(a,b)for(int a=0;a<b;++a)using namespace std;// 函数声明 void output1(vector<string> &);void output2(vector<string> &);int main(){vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikesstring Ilike[] = { "favorite book","music","film","paintings","anime","sport","sportsman","etc" };// 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) // 补足代码 // 。。。 rap(i, 8)likes.push_back(Ilike[i]);cout << "-----I like these-----" << endl;// 调用子函数输出vector<string>数组对象likes的元素值 // 补足代码// 。。。 output1(likes);// 为vector<string>数组对象dislikes添加元素值 // 补足代码 // 。。。 string Idislike[] = { "spicy food","dracula movie","rock and roll","running","dancing","brawl" };rap(i, 6)dislikes.push_back(Idislike[i]);cout << "-----I dislike these-----" << endl;// 调用子函数输出vector<string>数组对象dislikes的元素值 // 补足代码// 。。。 output2(dislikes);// 交换vector<string>对象likes和dislikes的元素值 // 补足代码// 。。。 swap(likes, dislikes);cout << "-----I likes these-----" << endl;// 调用子函数输出vector<string>数组对象likes的元素值 // 补足代码// 。。。 output1(likes);cout << "-----I dislikes these-----" << endl;// 调用子函数输出vector<string>数组对象dislikes的元素值 // 补足代码// 。。。 output2(dislikes);return 0;}// 函数实现 // 以下标方式输出vector<string>数组对象v的元素值 void output1(vector<string> &v) {rap(i, v.size())cout << v[i] << endl;// 补足程序// 。。。 }// 函数实现// 以迭代器方式输出vector<string>数组对象v的元素值 void output2(vector<string> &v) {for (vector<string>::iterator it = v.begin(); it != v.end(); ++it)cout << (*it) << endl;// 补足程序// 。。。 }
运行结果截图:
指针:
6-17
6-18
#include "stdafx.h"
#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;
}
我用vs2017写超神奇的,居然没有错误,不信你看看,真的没有问题耶。又节省了我的劳动力,棒!
第三题:这题不是很会,但看了别人的代码,研究当中。希望老师能够讲解。
期中考试:
1. 用掷骰子方式,模拟班级每个学号被随机抽点的概率。
#include<bits/stdc++.h>#define rap(a,b) for(int a=0;a<b;++a)using namespace std;class Dice {private:int sides;public:Dice(int n) :sides(n) {}int cast();};int Dice::cast() {return rand() % sides + 1;}int main() {srand(time(NULL));Dice myclass(40);rap(i, 10) {double percent = 0;int k = 0;rap(j, 500) {if (myclass.cast() == 24)k++;}percent = (double)k / 500.0;cout << "第" << i<< "次学号 20161334024 被选到的概率是:" << percent << endl;}return 0;}
3. 图书入库
#ifndef BOOK_H
#define BOOK_H
#include <string>
using std::string;
class Book {
public:
Book(string a, string b, float c); //构造函数
void print(); // 打印图书信息
private:
string isbn;
string title;
float price;
};
#endif
#include "book.h"
#include <iostream>
#include <string>
using namespace std;
Book::Book(string a, string b, float c):isbn(a),title(b),price(c){}
// 构造函数
// 补足程序
// ...
void Book::print()
{
cout<<"number: "<<isbn<<" "<<"title: "<<title<<" "<<"price: "<<price<<" yuan"<<endl;
}// 打印图书信息
// 补足程序
// ...
#include "book.h" #include <vector> #include <iostream> using namespace std; int main() { vector<Book> books; // 定义一个vector<Book>类对象 // 补足程序 // ... string isbn, title; float price; while(cin>>isbn>>title>>price) { Book a(isbn,title,price); books.push_back(a); }// 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中 for(int i=0;i<books.size();i++) { books[i].print(); } return 0;// 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) // 补足程序 // ... // 输出入库所有图书信息 // 补足程序 // ... return 0; }
运行结果:
作业花了很多时间,期中考试第二题和编程作业第三题不是很会,但我写了的作业都是我会的。有研究别人的代码。我也不偷别人的作业了。最近在学习网络方面的知识,C++有落下,但后期会补上,也会多向别人学习