面向对象程序设计(基于C++)0102 输出不同类型的最大最小值

要求

  1. ranges.cpp Write a program that outputs the largest and smallest values of the following types:
    char, wchar_t, short, int, long, float, double,
    unsigned and long long.
    编写一个程序,输出以下类型的最大值和最小值:

char,wchar,t,short,int,long,float,double,

unsigned 和long long。

知识点

可以利用库函数<limits>的模板函数
numeric_limits<T>::max()
numeric_limits<T>::min()
numeric_limits<T>::lowest()

需要注意的是 对于float和double类型,min输出的是最大负数(即能表示的最大精度负数)
所以这里得使用lowest

代码

#include<iostream>
#include<limits>

using namespace std;

int main()
{
    cout << "max of char : " << (int)numeric_limits<char>::max() <<" min of char : "<<(int)numeric_limits<char>::min()<< endl;
    cout << "max of wchar_t : " << numeric_limits<wchar_t>::max() <<" min of wchar_t : "<<numeric_limits<wchar_t>::min()<< endl;
    cout << "max of short : " << numeric_limits<short>::max() <<" min of short : "<<numeric_limits<short>::min()<< endl;
    cout << "max of int : " << numeric_limits<int>::max() <<" min of int : "<<numeric_limits<int>::min()<< endl;
    cout << "max of long : " << numeric_limits<long>::max() <<" min of long : "<<numeric_limits<long>::min()<< endl;
    cout << "max of float : " << numeric_limits<float>::max() <<" min of float : "<<numeric_limits<float>::lowest()<< endl;
    cout << "max of double : " << numeric_limits<double>::max() <<" min of double : "<<numeric_limits<double>::lowest()<< endl;
    //float和double的min代表的是能表示的最大负数,最小数是lowest
    cout << "max of unsigned : " << numeric_limits<unsigned>::max() <<" min of unsigned : "<<numeric_limits<unsigned>::min()<< endl;
    cout << "max of long long : " << numeric_limits<long long>::max() <<" min of long long : "<<numeric_limits<long long>::min()<< endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值