说明:
1.由于第一次作业太简单就不公布参考答案。
2.每次参考答案我会尽量从同学们的优秀作业中挑选,被选中的同学在最后成绩评定中会有额外加分。
3.挑选从第四次开始,第二次和第三次作业都是林凡同学的作业,希望大家向林凡同学学习。
4.以下代码中的部分格式问题都是博客自身造成的,林凡同学打代码本身写得非常精彩。
5.代码中的注释都是我加上的,如果有哪些不足、写错或者您有更深入的体会,不妨在下面留言,供大家交流学习。
一、新建一个名为”in.txt”的文本文档,输入两行以上的英文或数字并存盘,然后编写程序,开启该文本文档,将其中的每个字符串或数字都读取到一个vector<string>中,便利该vector,将vector中元素倒序显示到另一个名为”out.txt”的文本文档中。
#include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; /* ** iostream是对屏幕上输入输出 ** fstream 是对文件输入输出 */ int main() { ifstream infile; ofstream outfile; //ofstream的用法和cout其实没太大区别 //ofstream将内存内容写出到文件(磁盘)上 //cout 将内存内容输出到显示屏上 outfile.open("in.txt"); outfile << "This is my first program about file" << endl << "Student number is 1152789" << endl << "My name is Linfan" << endl; outfile.close(); //使用文件类前必须先打开文件 infile.open("in.txt");//以相对路径形式打开文件,就是存放CPP文件的当前目录 outfile.open("out.txt");//也可以用绝对路径打开文件 //从输入文件读取内容到内存的ivec中 vector<string> ivec; string rdata; //ifstream的用法和cin其实没太大区别 //ifstream将文件(磁盘)内容写出到内存中 //cout 将键盘输入的内容写到内存中 while (infile >> rdata) ivec.push_back(rdata); //将内容倒着显示出来 while (!ivec.empty()) { outfile << ivec.back() << " "; ivec.pop_back(); } outfile << endl; //文件类使用完了之后一定要关闭 infile.close(); outfile.close(); return 0; }
二、数列的求值公式是a1=1, a2=1, an=an-1+2*an-2(n>=3),借此产生1,1,3,5,11,21等元素值。试定义一个函数,利用上述公式,输入n值,计算a1, a2,…, an的值,并将这些值存入一个vector之中,元素数目n由用户指定,请检查元素数目的有效性,太大则可能引发overflow问题,接下来撰写第二个函数,能够将所接获的vector的所有元素一一印出,最后再写一个main(),测试上述两个函数。
#include <iostream> #include <vector> using namespace std; //有效性检查 inline bool is_size_ok(int length) { if (length <= 0 || length > 32) { cerr << "Length " << length << " is not supported" << endl; return false; } return true; } //传引用一般用在大数据类中,一般的内置型变量不需要穿引用 //string\vector\list\deque\自定义的类等 //内置型变量有int double float 等 bool count(vector<int> &ivec, int size) { if (!is_size_ok(size)) return false; vector<int> tvec; if (size >= 1) tvec.push_back(1); if (size >= 2) tvec.push_back(1); for (int ix = 2; ix < size; ix ++) tvec.push_back(tvec[ix - 2] * 2 + tvec[ix - 1]); ivec = tvec; return true; } void display(const vector<int> &ivec) { for (int ix = 0; ix != ivec.size(); ix ++) cout << ivec[ix] << " "; cout << endl; } int main() { cout << "Please input an num:" << endl; int n; cin >> n; vector<int> ivec; if (!count(ivec, n)) return -1; display(ivec); return 0; }
三、数列的求值公式是a1=1,an=an-1+2*n(n>=2),写一个函数,以局部静态(local static)的vector存储数列元素。此函数返回一个const指针,指向该vector。如果vector的容量小于指定的元素数目,就扩充vector的容量。接下来再实现第二个函数,接受一个n值并返回该位置上的元素。最后,撰写main()测试这些函数。
#include <iostream> #include <vector> #define MAXN 103621 using namespace std; //有效性检查,容量是定死的,无法扩充 //此处只是练习就不强求大家 inline bool is_size_ok(int length) { if (length <= 0 || length > MAXN) { cerr << "Length " << length << " is not supported" << endl; return false; } return true; } //此处并不是预先存储好 const vector<int> *iseq(int ilength) { //此处static变量在本函数调用结束后,elems变量仍然存在, //如果再次调用的时候就不用 static vector<int> elems; for (int ix = elems.size(); ix < ilength; ix ++) { if (ix == 0) elems.push_back(1); else elems.push_back(elems[ix - 1] + 2 * (ix + 1)); } return &elems; } int seq_elem(int ipos) { if (!is_size_ok(ipos)) return -1; const vector<int> *seq = iseq(ipos); if (!seq) return 0; return (*seq)[ipos - 1]; } int main() { cout << "Please input an num:" << endl; int ipos; cin >> ipos; int elem = seq_elem(ipos); if (elem <= 0) cout << "Error" << endl; else cout << elem << endl; return 0; }