一些简单的c++问题(1.0)

大学C++课的作业,清理电脑的时候把它整理出来留作纪念。

Hello world!

描述

Hello world~

代码实现

//test1_1.cpp
#include<iostream>
using namespace std;
int main()
{
    int a;
    cout<<"Hello!\n";
    cout<<"This is a program."<<endl;
    cin>>a;
    return 0;
}

求和

描述

输入两个数,输出sum值

代码实现

#include<iostream>
using namespace std;
int add(int a, int b);
int main()
{
    int x, y, sum;
    cout << "Enter two numbers:" << endl;
    cin >> x;
    cin >> y;
    sum = add(x, y);
    cout << "The sum is:" << sum << endl;
    return 0;

}

int add(int a, int b)
{
    return a + b;
}

z=x*5+y*5

描述

如题

代码实现

#include<iostream>
using namespace std;
int main()
{
    void fun(int,int&);
    int x,y;
    fun(3,x);
    fun(4,y);
    cout<<"x+y="<<x+y<<endl;
    return 0;

}
void fun(int m,int &n) //&:引用
{
    n=m*5;
}

求max

描述

函数名相同,返回值和参数类型不同

代码实现

#include<iostream>
using namespace std;
int max(int m, int n)
{
    if (m>n)
        return m;
    else
        return n;
}
int max(int m, int n, int p)
{
    int q, o;
    q = max(m, n);
    o = max(q, p);
    return o;

}
double max(double m, double n)
{
    if (m>n)
        return m;
    else
        return n;
}
double max(double m, double n, double h)
{
    double w, v;
    w = max(m, n);
    v = max(w, h);
    return v;
}
int main()
{
    int a, b, x, y, z, max1, max2;
    double c, d, e, f, g, max3, max4;
    cout << "a=:" << endl;
    cin >> a;
    cout << "b=" << endl;
    cin >> b;
    cout << "x=:" << endl;
    cin >> x;
    cout << "y=" << endl;
    cin >> y;
    cout << "z=" << endl;
    cin >> z;
    cout << "c=:" << endl;
    cin >> c;
    cout << "d=" << endl;
    cin >> d;
    cout << "e=:" << endl;
    cin >> e;
    cout << "f=" << endl;
    cin >> f;
    cout << "g=" << endl;
    cin >> g;
    max1 = max(a, b);
    max2 = max(x, y, z);
    max3 = max(c, d);
    max4 = max(e, f, g);
    cout << "max" << a << "&" << b << "=" << max1 << endl;
    cout << "max" << x << "&" << y << "&" << z << "=" << max2 << endl;
    cout << "max" << c << "&" << d << "=" << max3 << endl;
    cout << "max" << e << "&" << f << "&" << g << "=" << max4 << endl;
    return 0;

}

swap

描述

输入两个数,按从小到大的顺序输出

代码实现

#include<iostream>
using namespace std;
void swap(int &m,int &n)
{
    int temp;
    temp=m;
    m=n;
    n=temp;
}

int main()
{
    int x,y;
    cout<<"x="<<endl;
    cin>>x;
    cout<<"y="<<endl;
    cin>>y;
    if(x<y)
    swap(x,y);
    cout<<"按从大到小输出为:"<<x<<" "<<y<<endl;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值