微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

</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">

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐