《数据结构》实验一:VC编程环境灵活应用
来源:互联网 发布:香港mac pro多少钱 编辑:程序博客网 时间:2024/05/18 01:24
1. 设计一个单文件结构程序完成从键盘输入两个数,输出二者的“和”和“积”的结果。
#include <iostream> using namespace std; void he(int a,int s) { int m; m=a+s; cout<<"两个数的和为:"<<m<<endl; } void he(double d,double f) { double n; n=d+f; cout<<"两个数的和为:"<<n<<endl; } void ji(int g,int h) { int b; b=g*h; cout<<"两个数的积为:"<<b<<endl; } void ji(double j,double k) { double v; v=j*k; cout<<"两个数的积为:"<<v<<endl; } int main() { float q,w; cout<<"输入两个数:"; cin>>q>>w; he(q,w); ji(q,w); cout<<endl; return 0; }
2.使用函数的模板来实现上述功能。
#include <iostream> using namespace std; template<class T1,class T2> T1 he(T1 a,T2 s) { T1 d; d=a+s; cout<<"两个数的和为:"<<d<<endl; return d; } template<class Y1,class Y2> Y1 ji(Y1 q,Y2 w) { Y1 e; e=q*w; cout<<"两个数的积为:"<<e<<endl; return e; } int main() { float z,x; cout<<"输入两个数:"; cin>>z>>x; he(z,x); ji(z,x); return 0; }
3.使用一个类来实现上述功能。
#include <iostream> using namespace std; template <typename T> class tem { public: int he(int x,int y) { int r; r=x+y; cout<<"两个数的和为:"<<r<<endl; return r;} int ji(int x,int y) { int v; v=x*y; cout<<"两个数的积为:"<<v<<endl; return v; } }; int main() { int a,b; cout<<"输入两个数:"; cin>>a>>b; tem<int>o; o.he(a,b); o.ji(a,b); return 0; }
PS.老师类模板那个只会做整数。。。不知道怎么写。。。好像模板也有些错误。。。
调试结果:
0 0
- 《数据结构》实验一:VC编程环境灵活应用
- 数据结构 实验一:VC编程环境灵活应用
- 数据结构:实验一 VC++编程环境的灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境的灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- 数据结构 实验一:VC编程环境灵活应用
- 《数据结构》实验一:VC编程环境灵活应用
- mysql安装
- java 遍历arrayList的四种方法
- linux权限管理命令
- 美元升值对中国资产价格的影响
- 接口、抽象类、修饰符解析
- 《数据结构》实验一:VC编程环境灵活应用
- leetcode - Word Break
- Java字节码运行浅析
- 泛型一些要记住的东西
- java操作智能卡 IC卡 连接pcsc读卡器 发送APDU指令 源代码 下载 jdk1.6满足要求 java语言 操作IC卡 操作智能卡
- day27,page50,total200
- 如何写一篇好的技术博客
- ListView Item 圆角问题
- 数据结构 实验3