《数据结构》实验一: VC编程工具的灵活使用
发布时间:2020-12-15 05:52:09 所属栏目:安全 来源:网络整理
导读:/prepre name="code" class="html"1.复习巩固VC编程环境的使用,以及C++模板设计。2.回顾并掌握VC单文件结构程序设计过程。3.回顾并掌握VC多文件工程设计过程4.掌握VC程序调试过程。5.回顾C++模板和模板的程序设计。 #includeiostreamusing namespace std;vo
</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相同。 </pre><pre name="code" class="html"> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |