第三次作业参考答案
来源:互联网 发布:比较好的vpn 知乎 编辑:程序博客网 时间:2024/05/29 19:47
一、实现一个重载的max()函数,让它接受以下参数:(a)两个整数;(b)两个浮点数;(c)两个字符串;(d)一个整数vector; (e) 一个浮点数vector; (f) 一个字符串vector; (g)一个整数数组, 以及一个表示数组大小的整数值:(h) 一个浮点数数组,以及一个表示数组大小的整数值:(i)一个字符串数组,以及一个表示数组大小的整数值。最后,撰写main()测试这些函数。
#include <iostream>#include <vector>#include <string>using namespace std;int max(int, int);double max(double, double);string max(string, string);int max(vector<int>);double max(vector<double>);string max(vector<string>);int max(int *, int);double max(double *, int);string max(string *, int);int main(){cout << "Please enter 2 integers:" << endl;int ix , iy;cin >> ix >> iy;cout << max(ix, iy) << endl;cout << "Please enter 2 floats:" << endl;double fx, fy;cin >> fx >> fy;cout << max(fx, fy) << endl;cout << "Please enter 2 strings:" << endl;string sx, sy;cin >> sx >> sy;cout << max(sx, sy) <<endl;int n;cout << "Please enter the length of a vector:" << endl;cin >> n;cout << "Please enter " << n << " integers:" << endl;vector<int> iv;for (int i = 0; i < n; i ++){int x;cin >> x;iv.push_back(x);}cout << max(iv) << endl;cout << "Please enter the length of a vector:" << endl;cin >> n;cout << "Please enter " << n << " floats:" << endl;vector<double> fv;for (int i = 0; i < n; i ++){double x;cin >> x;fv.push_back(x);}cout << max(fv) << endl;cout << "Please enter the length of a vector:" << endl;cin >> n;cout << "Please enter " << n << " strings:" << endl;vector<string> sv;for (int i = 0; i < n; i ++){string x;cin >> x;sv.push_back(x);}cout << max(sv) << endl;cout << "Please enter the length of an array:" << endl;cin >> n;cout << "Please enter " << n << " integers:" << endl;int *ia = new int[n];for (int i = 0; i < n; i ++)cin >> ia[i];cout << max(ia, n) << endl;delete ia;cout << "Please enter the length of an array:" << endl;cin >> n;cout << "Please enter " << n << " floats:" << endl;double *fa = new double[n];for (int i = 0; i < n; i ++)cin >> fa[i];cout << max(fa, n) << endl;delete fa; cout << "Please enter the length of an array:" << endl;cin >> n;cout << "Please enter " << n << " strings:" << endl;string *sa = new string[n];for (int i = 0; i < n; i ++)cin >> sa[i];cout << max(sa, n) << endl;delete sa;return 0;}int max(int v1, int v2){ return v1 > v2 ? v1 : v2;}double max(double v1, double v2){ return v1 > v2 ? v1 : v2;}string max(string v1, string v2){ return v1 > v2 ? v1 : v2;}int max(vector<int> ivec){ int tmp = ivec[0]; for (int ix = 1; ix < ivec.size(); ix ++) if (ivec[ix] > tmp) tmp = ivec[ix]; return tmp;}double max(vector<double> ivec){ double tmp = ivec[0]; for (int ix = 1; ix < ivec.size(); ix ++) if (ivec[ix] > tmp) tmp = ivec[ix]; return tmp;}string max(vector<string> ivec){ string tmp = ivec[0]; for (int ix = 1; ix < ivec.size(); ix ++) if (ivec[ix] > tmp) tmp = ivec[ix]; return tmp;}int max(int *arr, int size){ int tmp = arr[0]; for (int i = 1; i < size; i ++) if (arr[i] > tmp) tmp = arr[i]; return tmp;}double max(double *arr, int size){ double tmp = arr[0]; for (int i = 1; i < size; i ++) if (arr[i] > tmp) tmp = arr[i]; return tmp;}string max(string *arr, int size){ string tmp = arr[0]; for (int i = 1; i < size; i ++) if (arr[i] > tmp) tmp = arr[i]; return tmp;}
二、以template重新完成试题一,并对main()函数做适度的修改。
#include <iostream>#include <vector>#include <string>using namespace std;template <typename T>T Max(T v1, T v2){ return v1 > v2 ? v1 : v2;}template <typename T>T Max(vector<T> v){ T max = v[0]; for (int ix = 1; ix < v.size(); ix ++) if (v[ix] > max) max = v[ix]; return max;}template <typename T>T Max(T *v, int size){ T max = v[0]; for (int ix = 1; ix < size; ix ++) if (v[ix] > max) max = v[ix]; return max;}int main(){cout << "Please enter 2 integers:" << endl;int ix , iy;cin >> ix >> iy;cout << Max(ix, iy) << endl;cout << "Please enter 2 floats:" << endl;double fx, fy;cin >> fx >> fy;cout << Max(fx, fy) << endl;cout << "Please enter 2 strings:" << endl;string sx, sy;cin >> sx >> sy;cout << Max(sx, sy) <<endl;int n;cout << "Please enter the length of a vector:" << endl;cin >> n;cout << "Please enter " << n << " integers:" << endl;vector<int> iv;for (int i = 0; i < n; i ++){int x;cin >> x;iv.push_back(x);}cout << Max(iv) << endl;cout << "Please enter the length of a vector:" << endl;cin >> n;cout << "Please enter " << n << " floats:" << endl;vector<double> fv;for (int i = 0; i < n; i ++){double x;cin >> x;fv.push_back(x);}cout << Max(fv) << endl;cout << "Please enter the length of a vector:" << endl;cin >> n;cout << "Please enter " << n << " strings:" << endl;vector<string> sv;for (int i = 0; i < n; i ++){string x;cin >> x;sv.push_back(x);}cout << Max(sv) << endl;cout << "Please enter the length of an array:" << endl;cin >> n;cout << "Please enter " << n << " integers:" << endl;int *ia = new int[n];for (int i = 0; i < n; i ++)cin >> ia[i];cout << Max(ia, n) << endl;delete ia;cout << "Please enter the length of an array:" << endl;cin >> n;cout << "Please enter " << n << " floats:" << endl;double *fa = new double[n];for (int i = 0; i < n; i ++)cin >> fa[i];cout << Max(fa, n) << endl;delete fa; cout << "Please enter the length of an array:" << endl;cin >> n;cout << "Please enter " << n << " strings:" << endl;string *sa = new string[n];for (int i = 0; i < n; i ++)cin >> sa[i];cout << Max(sa, n) << endl;delete sa;return 0;}
三、定义三个数列,数列的求值公式分别是(1)a1=1, a2=1, an=an-1+2*an-2(n>=3),(2)a1=1, an=an-1+2*n(n>=2),(3)a1=1, an=an-1+n*n(n>=2),根据以上三个数列的求值公式,定义一个通用函数,输入n值和数列序号,输出相应数列的an值,撰写main(),并测试该函数。
关于第三小题的说明:
1.大家不要因为学了static之后就到处乱用,要注意使用场合。
#include <iostream>#include <vector>using namespace std;#define Maxn 3typedef int (*fun_p) (int);// 定义函数指针,下面用到了函数数组int function1(int iPos){ if (iPos < 1 || iPos > 32) { cerr << "The length is not supported!" << endl; return -1; } vector<int> ivec1; for (int ix = ivec1.size(); ix < iPos; ix ++){ if (ix == 0 || ix == 1) ivec1.push_back(1); else ivec1.push_back(ivec1[ix - 1] + 2 * ivec1[ix - 2]);} return ivec1[iPos - 1];}int function2(int iPos){ if (iPos < 1 || iPos > 46340) { cerr << "The length is not supported!" << endl; return -1; } vector<int> ivec2; for (int ix = ivec2.size(); ix < iPos; ix ++){ if (ix == 0) ivec2.push_back(1); else ivec2.push_back(ivec2[ix - 1] + 2 * (ix + 1));} return ivec2[iPos - 1];}int function3(int iPos){ if (iPos < 1 || iPos > 1860) { cerr << "The length is not supported!" << endl; return -1; } vector<int> ivec3; for (int ix = ivec3.size(); ix < iPos; ix ++){ if (ix == 0) ivec3.push_back(1); else ivec3.push_back(ivec3[ix - 1] + (ix + 1) * (ix + 1));} return ivec3[iPos - 1];}//post:根据数列编号和这个数在数列中的位置计算出这个数int seqs(int iPos, int order, int &iNum){ //0 means there is not such an order //-1 means the length of the order is not supported //>0 means exit successfully if (order < 1 || order > 3) { iNum = 0; return 0; }//函数指针数组,具体了解请google const fun_p fp[Maxn] = {function1, function2, function3}; iNum = fp[order - 1](iPos); return iNum;}int main(){ int iPos, order, iNum; while (true) { cin >> iPos >> order; int sign = seqs(iPos, order, iNum); if (sign > 0) cout << iNum << endl; else if (sign == 0) cerr << "There is not such an order!" << endl; } return 0;}
- 第三次作业参考答案
- 数据库第三次作业参考答案
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 第三次作业
- 使uboot支持S3C6410的SD启动
- pl/sql 远程连接orcle服务器问题
- c++经典面试题
- java 中的流
- oralce 记录历史的两样东西 日志 回滚
- 第三次作业参考答案
- getopt(win32) -- 命令行参数解析函数
- PL/SQL复合类型
- oracle 中truncate 和delete 的区别
- How:windows 7 下控制台无法正常显示中文解决
- hdu4000(一维树状数组模板)
- GetType和typeof的区别 以及一个小实例
- Session
- java代码将汉字转换成拼音