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

一、实验目的

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

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

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

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

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

二、实验内容

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

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

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

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

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

  1)使用类模板

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

三、程序代码

实验内容1题:

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

#include <iostream>
using namespace std;
int sum(int a,int b)
{
	return a+b;
}
int product(int a,int b)
{
	return a*b;
}
float sum(float c,float d)
{
	return c+d;
}
float product(float c,float d)
{
	return c*d;
}

int main()
{
	int a,b;
	float c,d;
	cout<<"请输入两个整数:"<<endl;
	cin>>a>>b;
	cout<<"两个整数之和:"<<sum(a,b)<<endl;
	cout<<"两个整数之积:"<<product(a,b)<<endl;
	cout<<endl;
	cout<<"请输入两个小数:"<<endl;
	cin>>c>>d;
	cout<<"两个小数之和:"<<sum(c,d)<<endl;
	cout<<"两个小数之积:"<<product(c,d)<<endl;
	return 0;
}



实验内容2题

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

#include <iostream>
using namespace std;
template <class A,class B>
void sum(A a,B b)
{
	cout<<"a + b ="<<a+b<<endl;
}
template <typename A,typename B>
void product(A a,B b)
{
	cout<<"a * b = "<<a*b<<endl;
}
int main()
{
	float c,d;
	cout<<"请输入两个数a,b:"<<endl;
	cin>>c>>d;
	sum(c,d);
	product(c,d);
	return 0;
}


实验3题

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


//头文件  "3.h"

#include <iostream>	
using namespace std;
template <class T>
class Count {
public:
	void sum(T a,T b);
	void product(T a,T b);
private:
	T a;
	T b;
};
<span style="color:#ff6666;">
</span>

//源文件 "3.cpp"

#include "3.h"
int main()
{

	Count<float> count;
	float n,m;
	cout<<"请输入两个数:"<<endl;
	cin>>n>>m;
	count.sum(n,m);
	count.product(n,m);
	return 0;
}
template <class T>
void Count<T>::sum(T a,T b)
{
	cout<<"两个数之和为:"<<a+b<<endl;
}
template <class T>
void Count<T>::product(T a,T b)
{
	cout<<"两个数之积为:"<<a*b<<endl;
}

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...