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

来源:互联网 发布:财经网数据 编辑:程序博客网 时间:2024/06/05 11:00


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

一..实验目的

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

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

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

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

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


三.实验内容

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

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

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

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

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

  1)使用类模板

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


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

#include<iostream>
using namespace std;

int sum(int a,int b)
{
 int c;
 c = a + b;
 return c;
}

float sum(float a,float b)
{
 float c;
    c = a + b;
 return c;
}

int pro(int a,int b)
{
 int d;
 d = a * b;
 return d;
}

float pro(float a,float b)
{
 float d;
    d = a * b;
 return d;
}

float main()
{
 float a,b,c,d;
 cout<<"Please input two numbers:";
 cin>>a >>b ;
 c = sum(a,b);
 d = pro(a,b);
 cout<<a<<" + "<<b<<" = "<<c<<endl;
 cout<<a<<" * "<<b<<" = "<<d<<endl;
 return 0;
}

代码二:(2.使用函数的模板来实现上述功能)

#include<iostream> 
using namespace std; 
template<class T1,class T2> 
T1 plus(T1 x,T2 y) 

    T1 sum; 
    sum=x+y; 
    cout<<"The sum of the two numbers is:"<<sum<<endl; 
    return sum; 

 
template<class G1,class G2> 
G1 mul(G1 x,G2 y) 

    G1 pro; 
    pro=x*y; 
    cout<<"The pro of the two numbers is:"<<pro<<endl; 
    return pro; 

 
int main() 

    float m1,m2; 
    cout<<"Please input two numbers:"; 
    cin>>m1>>m2; 
    plus(m1,m2); 
    mul(m1,m2); 
    return 0; 


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

  1)使用类模板

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


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

源文件
int main() 

    float m1,m2; 
    cout<<"Please  input  two numbers:"; 
    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 sum;  
        sum=x+y; 
        cout<<"The sum of the two numbers is:"<<he<<endl; 
    } 
 
      void mul(T x,T y) 
      { 
        T pro;  
        pro=x*y; 
        cout<<"The pro of the two numbers is:"<<ji<<endl; 
      } 
}; 


2.写一个博文,总结重载和模板的知识点。

      通过这次试验,我花了些的时间去复习C++的相关知识,不过还是有很多内容不太清楚,所以程序难免有错误之处,通过参考网上资料和请问同学,终于勉强把这次试验做好,在今后的学习中,我要更加努力,花更多的时间才行。


















0 0
原创粉丝点击