• 范磊 C++ 第4章 C++数据类型


      1 // section_4.cpp : Defines the entry point for the console application.
      2 //范磊C++ 第4章 C++数据类型
      3 //c++有6种数据类型
      4 /*
      5 布尔型 bool .布尔型可表示两个逻辑值0和1.即"真" 和 "假".
      6 字符型 char .
      7 双字节行 w_char
      8 整形 int 
      9 单精度浮点型 float
     10 双精度浮点型 double
     11 */
     12 
     13 #include "stdafx.h"
     14 #include "iostream"
     15 #include "locale"     //为了使用setlocale( )函数,把本机语言设置为中文简体.
     16 #include "iomanip"    //为了使用setprecision( )函数,用于设输出的精度(输出多少位数字)
     17 
     18 
     19  void fun1()  //4.3 布尔型变量
     20  {
     21      using namespace std;
     22      bool check;
     23      
     24      check = true;
     25 
     26      if(check == true)
     27      {
     28          cout << "check = " << check << endl;
     29          cout << "--------------------------
    " ;
     30      }
     31  }
     32 
     33 
     34  void fun2()
     35  {
     36      using namespace std;
     37 
     38      char ch;
     39      ch = 'A';
     40      
     41      cout << ch << endl;
     42      //因为定义的ch是字符型变量,所以输出ch时,是以一个字符的形式输出.
     43      cout << (int)ch << endl;
     44      //因为在ch前面多了个(int)意思就是说即使定义的ch是char变量,但是在输出前把char转成了Int 型
     45      //所以输出时,是以int的形式输出.
     46 
     47      for (int i = 32; i < 128; i++)
     48      {
     49          cout << char(i);
     50      }
     51      cout << "--------------------------
    " ;
     52  }
     53 
     54  void fun3()  //4.5 双字节型变量
     55  {
     56         using namespace std;
     57 
     58         setlocale(LC_ALL, "chs");
     59         wchar_t wt[] = L"";
     60 
     61         wcout << wt << endl;  //wcout性质和cout 一样,只不过wcout 用于输出宽字符.
     62 
     63         cout << "--------------------------
    " ;
     64  }
     65 
     66  void fun4()  //4.6 整形概述
     67  {
     68      using namespace std;
     69 
     70      cout << "bool : 	" << sizeof(bool) << endl;
     71      cout << "int : 	" << sizeof(int) << endl;
     72      cout << "shotr : " << sizeof(short) << endl;
     73      cout << "long : 	" << sizeof(long) << endl;
     74      cout << "char : 	" << sizeof(char) << endl;
     75      cout << "float : " << sizeof(float) << endl;
     76      cout << "double : " << sizeof(double) << endl;
     77      cout << "--------------------------
    " ;
     78  }
     79 
     80  void fun5()  //4.7 整形变量的定义
     81  {
     82      using namespace std;
     83 
     84      int a;
     85      int b;
     86      short c;
     87      long d;
     88      unsigned long e;
     89 
     90      a = -1;
     91      b = -2;
     92      c = -3;
     93      d = -4;
     94 
     95      e = a + b + c + d;
     96 
     97      cout << "a:" << a << "
    " << "b:" << b << "
    " << "c:" << c << "
    " << "d:" << d << "
    "
     98           << "e:" << e << endl;
     99      cout << "--------------------------
    " ;
    100  }
    101  //计算机内部没有正负数之分,只是显示出来给人看的是负数.
    102  //当b = -2 时.
    103  //如果b变量的类型是int型,那么b最终的值是显示出来的是"-2".
    104  //如果b是unsigned 型的话,b显示出来的值是"4294967294". 也就是说b的内存单元里面一直都是存放着是0FFFFFFFEh这个数.
    105  //看程序员需要计算机怎么处理这个数,总之内存放着就是0FFFFFFFEh.要清蒸还是红烧?就看定的是什么类型了.
    106 
    107  void fun6()  //4.8 浮点型变量
    108  {
    109      using namespace std;
    110      float a ;
    111 
    112      a = 12.34567890;
    113 
    114      cout << setprecision(15) << a << "
    ";
    115      cout << "--------------------------
    " ;
    116  }
    117 
    118  void fun7()  //4.9 枚举型常量
    119 //利用枚举常亮,可以用文字代替数字,最终目的是让程序变得更加易懂.
    120 //enum 名字{zero, one, two, three, fou};
    121  {
    122      using namespace std;
    123 
    124      enum day {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
    125      day today;
    126      today = Sun;
    127      
    128      if(today == Sun || today == Sat)
    129      {
    130          cout << "周末休息!" << "
    ";
    131      }
    132      else
    133      {
    134          cout << "工作日,别偷懒好吧! " << "
    " ;
    135      }
    136      cout << "--------------------------
    " ;
    137 }
    138 
    139 int main(int argc, char* argv[])
    140 {
    141     fun1();  //4.3 布尔型变量
    142     fun2();  //4.4 字符型变量
    143     fun3();  //4.5 双字节型变量
    144     fun4();  //4.6 整形概述
    145     fun5();  //4.7 整形变量的定义
    146     fun6();  //4.8 浮点型变量
    147     fun7();  //4.9 枚举型常量
    148 
    149     return 0;
    150 }
  • 相关阅读:
    Coursera Algorithm II PA2 Q2
    Coursera Algorithm Part II PA2
    实现 memcpy 函数
    超人
    Proxy 模式
    【6】锋利的 jQuery 笔记
    【3】Chrome 的一些常用操作
    HTML 待解决与已解决问题
    CSS 待解决问题
    JS 一些常用技巧
  • 原文地址:https://www.cnblogs.com/adalovelace/p/3998869.html
Copyright © 2020-2023  润新知