栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++编写重载函数统计0的个数及最大值

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++编写重载函数统计0的个数及最大值

#include 
using namespace std;

int fun(unsigned n, int &zero) //统计无符号整数
{
    int max = 0, t;
    zero = 0;
    do
    {
        t = n % 10;
        if (t == 0)
            zero++;
        if (max < t)
            max = t;
        n = n / 10;
    } while (n);
    return max;
}
int fun(int n, int &zero) //统计有符号整数
{
    int max = 0, t;
    zero = 0;
    do
    {
        t = n % 10;
        if (t == 0)
            zero++;
        if (max < -t)
            max = -t;
        n = n / 10;
    } while (n < -1);
    return max;
}
int fun(double d, int &zero) //统计实数,要求四舍五入
{
    int nd = 0;
    nd = d * 10;
    if (nd % 10 >= 5)
        nd = nd / 10 + 1;
    else if (nd % 10 > 0 || nd % 10 > -5)
        nd = nd / 10;
    else if (nd % 10 <= -5)
        nd = nd / 10 - 1;
    int max = 0, t;
    zero = 0;
    if (nd < 0)
    {
        do
        {
            t = nd % 10;
            if (t == 0)
                zero++;
            if (max < -t)
                max = -t;
            nd = nd / 10;
        } while (nd < -1);
    }
    else
    {
        do
        {
            t = nd % 10;
            if (t == 0)
                zero++;
            if (max < t)
                max = t;
            nd = nd / 10;
        } while (nd > 1);
    }
    return max;
}
#ifndef __COMPLEX__
#define __COMPLEX__

class complex;

class complex
{
public:
    double real, image;
private:
};
#endif
int fun(complex c, int &zero)
{
    int nd = 0;
    nd = c.real * 10;
    if (nd % 10 >= 5)
        nd = nd / 10 + 1;
    else if (nd % 10 > 0 || nd % 10 > -5)
        nd = nd / 10;
    else if (nd % 10 <= -5)
        nd = nd / 10 - 1;
    int max = 0, t;
    zero = 0;
    if (nd < 0)
    {
        do
        {
            t = nd % 10;
            if (t == 0)
                zero++;
            if (max < -t)
                max = -t;
            nd = nd / 10;
        } while (nd < -1);
    }
    else
    {
        do
        {
            t = nd % 10;
            if (t == 0)
                zero++;
            if (max < t)
                max = t;
            nd = nd / 10;
        } while (nd > 1);
    }
    return max;
}
//统计复数的实部



int main()

{

unsigned uNum;

int iNum;

int zero,max;

double dNum;

complex c;

      cin>>uNum>>iNum>>dNum>>c.real>>c.image;

max=fun(uNum,zero);

cout<<"nThe result of unsigned: max="<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/303216.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号