《数据结构》实验一 实验报告

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

一..实验目的

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

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

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

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

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

二.实验时间

第二周第二次课。2个学时。

三.实验内容

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

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

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

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

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

1)使用类模板

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


程序代码一:

#include<iostream>
#include<iomanip>
using namespace std;
float season(float a,float b)
{
float c=0;
c=a+b;
return c;
}
float junny(float a,float b)
{
float d=0;
d=a*b;
return d;
}
int main()
{
float x,y;
cout<<"请输入两个数:"<<endl;
cin>>x>>y;
cout<<"两个数的和为:"<<season(x,y)<<endl;
cout<<"两个数的积为:"<<junny(x,y)<<endl;
return 0;
}

程序二:

#include<iostream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
template<class T>
T add(T a,T b)
{
return a+b;
}
template<class P>
P multiply(P m,P n)
{
return m*n;
}
int main()
{
float x,y;
cout<<"请输入两个数:"<<endl;
cin>>x>>y;
cout<<"两个数的和为:"<<add(x,y)<<endl;
cout<<"两个数的积为:"<<multiply(x,y)<<endl;
system("pause");
return 0;
}

程序三:

#include<iostream.h>
class number
{
public:
void set(float a,float b)
{
apple=a;baby=b;
float c;
c=a+b;
float d;
d=a*b;
cout<<"两个数的和为:"<<c<<endl;
cout<<"两个数的积为:"<<d<<endl;
}
private:
float apple;
float baby;
};
void main()
{
float x,y;
cout<<"请输入两个数"<<endl;
cin>>x>>y;
number s;
s.set(x,y);
}

相关文章

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