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

本文详细介绍了如何在数据结构实验中灵活运用VC++编程工具,通过实例解析了其在实现数据结构算法时的优势和操作技巧,帮助读者提升编程效率。
摘要由CSDN通过智能技术生成
</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">
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值