《数据结构》实验一:VC编程环境灵活应用

实验目的

复习巩固VC编程环境的使用,以及C++模板设计。

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

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

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

4.回顾C++模板和模板的程序设计。


实验内容

1. 设计一个单文件结构程序完成从键盘输入两个数,输出二者的“和”和“积”的结果。要求如下:

1)设计函数来计算“和”和“积”,在主函数中调用,并能考虑重载函数,使整数和小数均能计算。

2)分别使用单步调试和断点调试来调试程序。并多次运行力求熟练调试方法。


代码:

#include <iostream>
using namespace std;
void he(int x,int y)
{
	int sum=0;
	sum=x+y;
	cout<<"两个数的和为:"<<sum<<endl;
}
void he(double x,double y)
{
	double sum=0;
	sum=x+y;
	cout<<"两个数的和为:"<<sum<<endl;
}
void ji(int x,int y)
{
	int sum=1;
	sum=x*y;
	cout<<"两个数的积为:"<<sum<<endl;
}
void ji(double x,double y)
{
	double sum=1;
	sum=x*y;
	cout<<"两个数的积为:"<<sum<<endl;
}

int main()
{
        float a,b;
	cout<<"输入两个数:";
        cin>>a>>b;
        he(a,b);
	ji(a,b);
	cout<<endl;
	return 0;
}




调试结果:


                                                                                                              图1


2.使用函数的模板来实现上述功能。


代码:

#include<iostream>
using namespace std;

template<class T1,class T2>
T1 plus(T1 x,T2 y)
{
    T1 he;
    he=x+y;
    cout<<"两个数的和为:"<<he<<endl;
    return he;
}

template<class G1,class G2>
G1 mul(G1 x,G2 y)
{
    G1 ji;
    ji=x*y;
    cout<<"两个数的积为:"<<ji<<endl;
    return ji;
}

int main()
{
    float m1,m2;
    cout<<"输入两个数:";
    cin>>m1>>m2;
    plus(m1,m2);
    mul(m1,m2);
    return 0;
}

调试结果:参考 图1



3.使用一个类来实现上述功能。要求:

  1)使用类模板

  2)使用多文件:类的声明有头文件中;类的函数定义一个源文件中,在主程序文件中设计主函数程序,在实例化输出结果。


源文件:

#include <iostream>
using namespace std;
#include"CA.H"

int main()
{
	float m1,m2;
 	cout<<"输入两个数:";
 	cin>>m1>>m2;
	Ca<float>a;
	Ca<float>b;
	a.plus(m1,m2);
	a.mul(m1,m2);
	return 0;
}

头文件:

#include <iostream>
using namespace std;

template <class T>

class Ca
{
	public:
		void plus(T x,T y)
		{
		T he;
		he=x+y;
         	cout<<"两个数的和为:"<<he<<endl;
		}

		void mul(T x,T y)
		{
		T ji;
		ji=x*y;
		cout<<"两个数的积为:"<<ji<<endl;
		}
};


调试结果:参考 图1


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值