您的位置:首页 > 理论基础 > 数据结构算法

《数据结构》实验一: VC编程工具的灵活使用

2014-10-19 00:35 288 查看
</pre><pre name="code" class="html">1.复习巩固VC编程环境的使用,以及C++模板设计。

2.回顾并掌握VC单文件结构程序设计过程。

3.回顾并掌握VC多文件工程设计过程

4.掌握VC程序调试过程。

5.回顾C++模板和模板的程序设计。
#include<iostream>using namespace std;void add(int x,int y){int he;he=x+y;cout<<"这两个数的和是:"<<he<<endl;}void add(double x,double y){double he;he=x+y;cout<<"这两个数的和是:"<<he<<endl;}void plus(int x,int y){int cheng;cheng=x*y;cout<<"这两个数的积是:"<<cheng<<endl;}void plus(double x,double y){double cheng;cheng=x*y;cout<<"这两个数的积是:"<<cheng<<endl;}int main(){float a,b;cout<<"请输入两个数:"<<endl;cin>>a>>b;cout<<"这两个数是:"<<"a="<<a<<"b="<<b<<endl;add(a,b);plus(a,b);return 0;}
运行结果正确。
</pre><pre name="code" class="html">3.使用一个类来实现上述功能。要求:1)使用类模板2)使用多文件:类的声明有头文件中;类的函数定义一个源文件中,在主程序文件中设计主函数程序,在实例化输出结果。类模板代码如下:
#include<iostream>using namespace std;template<class T1,class T2>T1 plus(T1 x,T2 y){T1 cheng;cheng = x * y;cout<<"这两个数的积是:"<<cheng<<endl;return 0;}template<class H1,class H2>H1 add(H1 x,H2 y){H1 he;he= x + y;cout<<"这两个数的和是:"<<he<<endl;return 0;}int main(){float a,b;cout<<"请输入两个数:"<<endl;cin>>a>>b;cout<<"这两个数是:"<<" a="<<a<<"  b="<<b<<endl;add(a,b);plus(a,b);return 0;}
运行结果与实验一的一致。
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">#include<iostream>using namespace std;template <class T>class heji{public:void add(T x,T y){T he;he  = x + y;cout<<"这两个数的和是:"<<he<<endl;}void plus(T x,T y){T cheng ;cheng = x * y;cout<<"这两个数的积是:"<<cheng<<endl;}};  
源程序代码如下:
#include<iostream>using namespace std;#include"jiyuhe.h"int main(){float a,b;cout<<"请输入两个数:"<<endl;cin>>a>>b;cout<<"这两个数是:"<<" a="<<a<<"  b="<<b<<endl;heji<double>m;heji<double>n;m.add(a,b);n.plus(a,b);return 0;}  
运行结果与1相同。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: