面向对象c++笔记1:利用函数重载对整数数组与浮点数数组排序:

#include<iostream>
#include<iomanip>
using namespace std;
class array
{
public:
    void fun(int arr[], int n)
    {
        for (int i = 1;i <= n;i++)
        {
            for (int j = 0;j < n - i;j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    int t = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = t;
                }
            }
        }
        for (int i = 0;i < n;i++)
        {
            cout << arr[i] << " ";
        }

    };

    void fun(double arr[], int n)
    {
        for (int i = 1;i <= n;i++)
        {
            for (int j = 0;j < n - i;j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    double t = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = t;
                }
            }
        }
        for (int i = 0;i < n;i++)
        {
            cout <<setprecision(3) <<arr[i] << " ";
        }
    };


}s1;

int main()
{

    char x;
    int N, a[1000];
    double b[1000];
    cin >> x;
    cin >> N;
    if (x == 'd')
    {
        for (int p = 0;p < N;p++)
        {
            cin >> b[p];
        }

        s1.fun(b, N);
    }
    if (x == 'i')
    {
        for (int t = 0;t < N;t++)
        {
            cin >> a[t];
        }
        s1.fun(a, N);
    }

    

    return 0;
}

 

1.cout输出流输入空格直接cin>>" "

2.冒泡算法

for (int i = 1;i <= n;i++)
        {
            for (int j = 0;j < n - i;j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    int t = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = t;
                }
            }
        }

3.c语言四舍五入:

整数:int a=(int)(a+0.5)

小数:float a=0.756,b;

           int x;

           b=a*100;

           b+=0.5;

          x=b;

          b=x/100

or 直接调用round函数:#include"math.h"

                                      int y=round(x);

c++四舍五入:

#include<cmath.h>

count<<ceil(a)<<endl;向上取整

count<<floor(a)<<endl;向下取整

count<<round(a)<<endl;四舍五入

保留几位数字:

#include<iomanip>

cout <<setprecision(3) ;

4.为什么直接用array s1 的时候无法调用呢?

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值