您的位置:首页 > 理论基础 > 数据结构算法

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

2014-09-27 13:34 405 查看
一..实验目的

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

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

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

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

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

二.实验时间

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

三.实验内容

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

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

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

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

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

  1)使用类模板

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

代码一:

#include<iostream>
using namespace std;
void result(int ,int );
void result(double ,double );

void result_1(int c,int d)
{
cout<<"the result of multiplication is: "<<c*d<<endl;
cout<<endl;
cout<<"the result of the conbimed is: "<<c+d<<endl;
}

void result_2(int c,double f)
{
cout<<"the result of multiplication is: "<<c*f<<endl;
cout<<endl;
cout<<"the result of the conbimed is: "<<c+f<<endl;
}

void result_1(double j,int d)
{
cout<<"the result of multiplication is: "<<j*d<<endl;
cout<<endl;
cout<<"the result of the conbimed is: "<<j+d<<endl;
}

void result_2(double j,double k)
{
cout<<"the result of multiplication is: "<<j*k<<endl;
cout<<endl;
cout<<"the result of the conbimed is: "<<j+k<<endl;
}
int main()
{
int a,b;
double x,y;
cout<<"input two numble: ";
cin>>x>>y;
a=x,b=y;
if(a==x)
{
if(b==y)
result_1( a, b);
else
result_2( a, y);
}
else
if(b==y)
result_1( x, b);
else
result_2( x, y);
return 0;
}



代码二:

#include<iostream>
using namespace std;

template<class T>
T result(T a,T b)
{
cout<<"the result of multiplication is: "<<a*b<<endl;
cout<<endl;
cout<<"the result of the conbimed is: "<<a+b<<endl;

return a*b,a+b;
}

int main()
{
double x,y;
cout<<"input two numble: ";
cin>>x>>y;
result(x,y);
return 0;
}



总结:

这次实验非常失败,以前的东西全忘光了,看了书也只能搞定前两个,类模板以为实在不熟悉,怎么也对不上号,只能暂时空着,看来需要好好恶补。

函数重载的特征:1、两个或两个以上函数名相同,但是形参的个数或形参的类型不应相同,而不仅仅是返回值类型不同;2、如果某个函数参数有默认值,必须保证其参数默认后调用形式不与其他函数混淆;3、函数的算法不应相异。

函数模板的特征:1、必须先说明函数模板,然后实例化成相应的模板函数进行调用;2、template<模板参数表>尖括号不能为空,参数可以有多个;3、如果类型多于一个,则每个类型形参都要使用class或typename。<模板函数参数表>中的参数必须是唯一的,而且在<函数定义体>中至少出现一次。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息