noip(全国青少年信息学奥林匹克联赛)自学NOIP(C++)NOIP

C++(竞赛)【5】

2019-07-18  本文已影响24人  C入禅

<-上一篇


数据类型

在上一篇文章中我们多次提到了一个东西——数据类型
其实现在要谈的就是TA了

我们可以把我们输入到计算机内的数据都想象为放在一个个盒子里面,需要的时候拿出来用就叫做调用;而这些盒子就叫做变量;每一个盒子我们都要给TA取一个名字,就叫做变量名,我们还必须告诉计算机这个盒子叫什么,这就叫做定义或者申明,而且不同大小不同款式的盒子还有有姓吧,那就是数据类型,一个人要有名有姓而且还要户口登记,那么一个变量就要有变量名数据类型而且还要变量申明(至于有多少数据类型,我们以后分解,当然你也可以查查资料)

这个是我在上一篇文章里就提过的数据类型,下面有一个略微准确的定义:

使用编程语言进行编程时,需要用到各种变量来存储各种信息。变量保留的是它所存储的值的内存位置。这意味着,当我们创建一个变量时,就会在内存中保留一些空间。需要存储各种数据类型(比如字符型、宽字符型、整型、浮点型、双浮点型、布尔型等)的信息,操作系统会根据变量的数据类型,来分配内存和决定在保留内存中存储什么。

#include<iostream>  
#include<string>  
#include <limits>  
using namespace std;  
  
int main()  
{  
    cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);  
    cout << "\t最大值:" << (numeric_limits<bool>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;  
    cout << "char: \t\t" << "所占字节数:" << sizeof(char);  
    cout << "\t最大值:" << (numeric_limits<char>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;  
    cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);  
    cout << "\t最大值:" << (numeric_limits<signed char>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;  
    cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);  
    cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;  
    cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);  
    cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;  
    cout << "short: \t\t" << "所占字节数:" << sizeof(short);  
    cout << "\t最大值:" << (numeric_limits<short>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;  
    cout << "int: \t\t" << "所占字节数:" << sizeof(int);  
    cout << "\t最大值:" << (numeric_limits<int>::max)();  
    cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;  
    cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);  
    cout << "\t最大值:" << (numeric_limits<unsigned>::max)();  
    cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;  
    cout << "long: \t\t" << "所占字节数:" << sizeof(long);  
    cout << "\t最大值:" << (numeric_limits<long>::max)();  
    cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;  
    cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);  
    cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();  
    cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;  
    cout << "double: \t" << "所占字节数:" << sizeof(double);  
    cout << "\t最大值:" << (numeric_limits<double>::max)();  
    cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;  
    cout << "long double: \t" << "所占字节数:" << sizeof(long double);  
    cout << "\t最大值:" << (numeric_limits<long double>::max)();  
    cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;  
    cout << "float: \t\t" << "所占字节数:" << sizeof(float);  
    cout << "\t最大值:" << (numeric_limits<float>::max)();  
    cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;  
    cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);  
    cout << "\t最大值:" << (numeric_limits<size_t>::max)();  
    cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;  
    cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;  
    // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;  
    cout << "type: \t\t" << "************size**************"<< endl;  
             return 0;  
}

——[来自《信息学奥数一本通(C++版)》]

那么概念说这么多也没什么意义,就是大概了解一下有一个大致的框架,剩下的就靠实践来理解了。


在运算上这个的需求和我们之前写过的那个程序是一模一样,怪就怪在TA是有小数的运算: 那我们就直接定义一个可以装小数的变量就行了啊

/*开头的三行就不用多做解释了,
有问题请看上一篇,
后面就不做解释了。 
*/ 
#include<iostream>
using namespace std; 
int main()
{
    //我们先申明三个变量a,b,sum 
    double a,b,sum;
    //利用cin从键盘读入a,b两个变量的值 
    cin>>a>>b;
    //计算出a+b的值sum 
    sum=a+b;
    //输出sum的值 
    cout<<sum<<endl; 
    return 0; 
} 

如果不想定义三个变量也可这样:

/*开头的三行就不用多做解释了,
有问题请看上一篇,
后面就不做解释了。 
*/ 
#include<iostream>
using namespace std; 
int main()
{
    //我们先申明两个变量a,b 
    double a,b;
    //利用cin从键盘读入a,b两个变量的值 
    cin>>a>>b;
    //输出a+b的值 
    cout<<a+b<<endl; 
    return 0; 
} 

这样写是因为c++中的输出语句中是允许有表达式的,看个人喜好而定吧


关注我的专题我们一起努力


<-上一篇

上一篇 下一篇

猜你喜欢

热点阅读