加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 05:52:59 所属栏目:安全 来源:网络整理
导读:1. 设计一个单文件结构程序完成从键盘输入两个数,输出二者的“和”和“积”的结果。 #include iostream using namespace std; void he(int a,int s) { int m; m=a+s; cout"两个数的和为:"mendl; } void he(double d,double f) { double n; n=d+f; cout"两

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

#include <iostream>  
using namespace std;  
void he(int a,int s)  
{  
    int m;  
    m=a+s;  
    cout<<"两个数的和为:"<<m<<endl;  
}  
void he(double d,double f)  
{  
    double n;  
    n=d+f;  
    cout<<"两个数的和为:"<<n<<endl;  
}  
void ji(int g,int h)  
{  
    int b;  
    b=g*h;  
    cout<<"两个数的积为:"<<b<<endl;  
}  
void ji(double j,double k)  
{  
    double v;  
    v=j*k;  
    cout<<"两个数的积为:"<<v<<endl;  
}  
  
int main()  
{  
        float q,w;  
    cout<<"输入两个数:";  
        cin>>q>>w;  
        he(q,w);  
    ji(q,w);  
    cout<<endl;  
    return 0;  
}  


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

#include <iostream>  
using namespace std;  
template<class T1,class T2>  
T1 he(T1 a,T2 s)  
{  
    T1 d;  
    d=a+s;  
    cout<<"两个数的和为:"<<d<<endl;  
    return d;  
}  
  
template<class Y1,class Y2>  
Y1 ji(Y1 q,Y2 w)  
{  
    Y1 e;  
    e=q*w;  
    cout<<"两个数的积为:"<<e<<endl;  
   return e;  
}  
  
int main()  
{  
    float z,x;  
    cout<<"输入两个数:";  
    cin>>z>>x;  
    he(z,x);  
    ji(z,x);  
    return 0;  
}  


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

#include <iostream>  
using namespace std;  
  
template <typename T>  
  class tem  
{  
    public:  
        int he(int x,int y)  
        {  
        int r;  
        r=x+y;  
            cout<<"两个数的和为:"<<r<<endl;  
        return r;
		}  
  
        int ji(int x,int y)  
        {  
        int v;  
        v=x*y;  
        cout<<"两个数的积为:"<<v<<endl; 
		return v;
        }  
};  
  
int main()  
{  
    int a,b;  
    cout<<"输入两个数:";  
    cin>>a>>b; 
	tem<int>o;
    o.he(a,b);  
    o.ji(a,b);  
    return 0;  
}  


?

PS.老师类模板那个只会做整数。。。不知道怎么写。。。好像模板也有些错误。。。

调试结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读