zoukankan      html  css  js  c++  java
  • 第3章 c++世界众生相

    第3章 c++世界众生相


    3.1 c++中的数据类型

    p54 把string作为基本数据类型,是不可以嘀。

    在C语言中,基本数据类型,只有两个:数、字符

    在Cpp中,基本数据类型,是三个:数、字符、bool

    3.2 变量和常量
    3.2.1 声明变量
    3.2.2 给变量取个好名字
    3.2.3 变量初始化

    /* p57
    Cpp和C,还是有很大不同。
    声明变量,随时,不必都放在头部。
    可以在声明时,用()赋初值。这是对象的功能,以后会说到。。
    return 0,不再成为必须。。但在VC6中,还是要的。。
    */
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
      int ia=2;
      cout<<ia<<endl;
      int ib(3);  
      cout<<ib<<endl;  
      string sa="aha";
      cout<<sa<<endl;
      string sb("haha");
      cout<<sb<<endl;
      return 0;
    }

    3.2.4 常量

    #include <iostream>
    using namespace std;
    int main()
    {
      int i;
      i=011; //前面加0,变成八进制
      cout<<i<<endl;
      i=0x11; //前面加0x,变成16进制
      cout<<i<<endl;
      return 0;
    }
    //p59
    #include <iostream>
    using namespace std;
    int main()
    {
      cout << "hello
    ";
      cout << "x:\test\test.ha
    "; //嗯,现在知道为啥路径要这样写了吧?
      cout << "aha say:"yes!"" << endl;
      return 0;
    }


    3.2.5 用宏与const关键字定义常量

    //p61
    #include <iostream>
    #define PI 3.14159 //句末木有分号!
    const double P=3.14159; //鼓励用这种
    using namespace std;
    
    int main()
    {
      double r=1.0;
      cout<<PI*r*r<<endl;
      cout<<P*r*r<<endl;
      return 0;
    }

    3.3 数值类型
    3.3.1 整型数值类型

    //p63
    #include <iostream>
    using namespace std;
    
    int main()
    {
      //严格来说,int占几个字节,取决于机型、系统。。。但现在,一般占四个字节
      //这本书的错误,不是一般的多,难怪很多人在骂。。。但找出错误,也是一种学习方法。。。
      long i(3); //写long与写int,效果一样
      cout << i << endl;
      cout << sizeof(int) << endl; //4
      cout << sizeof(short int) << endl;  
      cout << sizeof(long int) << endl; //4 long int 和 int 效果一样  
      cout << sizeof(long long int) << endl; //8 int 可不写
      return 0;
    }

    3.3.2 浮点型数值类型

    /*
    几个实数类型中,一般鼓励用double,不差几个字节
    只是在读取与输出时,要注意格式。
    */
    //p64
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    
    int main()
    {
      cout << sizeof(float) << endl; //4
      cout << sizeof(double) << endl; //8  
      cout << sizeof(long double) << endl; //12 书中说10,又错了。。
      double a,b;
      scanf("%lf%lf",&a,&b);
      printf("%0.0lf",a+b);
      return 0;
    }

    3.4 布尔类型

    /*
    书中的这个代码例子,实在不敢恭维。。
    */
    //p64
    #include <iostream>
    using namespace std;
    
    bool iseven(int i)
    {
      return( 0==i%2 );
    }
    
    int main()
    {
      int i,sum=0;
      for(i=0;i<=100;++i)
        if(iseven(i)) ++sum;
      cout << sum << endl;
      cout << iseven(3) << " " << iseven(4) << endl; //false为0,true为1
      cout << sizeof(bool) << endl; //只占一个字节。所以当整数用是很危险的
      return 0;
    }

    3.5 字符串类型
    3.5.1 字符类型

    //p66
    #include <iostream>
    using namespace std;
    int main()
    {
      char a='a';
      cout << a << endl;
      cout << a+3 << endl; //没有ord函数,字符可以直接拿来加减
      cout << sizeof(a) << endl; // 1 
      //
      wchar_t ha=L'';
      //wcout.imbue(locale("chs"));
      //wcout<<ha<<endl; //书中例子,不能输出中文。这个不要求,随意了。。
      cout << sizeof(ha) << endl; // 2
      return 0;
    }

    3.5.2 字符串类型

    //p66
    #include <iostream>
    #include <string>
    #include <wstring> //找不到这个头文件
    using namespace std;
    int main()
    {
      string str("Good Morning!");
      cout << str << endl;
      wstring strc(L"");
      wcout.imbue(locale("chs"));
      wcout<<strc<<endl;
      return 0;
    }

    3.6 数组
    3.6.1 数组的声明与初始化

    //p69
    #include <iostream>
    using namespace std;
    int main()
    {
      int i,j,a[2][3]={{0},{0}};
      for(i=0;i<2;i++)
      {
        for(j=0;j<3;j++)
          cout<<a[i][j]<<" ";  
        cout << endl;
      }
      return 0;
    }

    3.6.2 数组的使用
    3.7 枚举类型

    //p72 书里代码有语法错误
    #include <iostream>
    using namespace std;
    int main()
    {
      enum weekday
      {
        mon=3,tue,wed,thu,fri,sat,sun // mon=3
      };
      weekday nday;
      nday=sun;
      cout << nday << endl;
      return 0;
    }

    3.8 用结构体类型描述复杂的事物
    3.8.1 结构体的定义
    3.8.2 结构体的使用

    //p75 简化了一下
    #include <iostream>
    #include <string>
    using namespace std;
    
    struct emplyee
    {
      string name;
      int age;
    };
    
    int main()
    {
      emplyee a[3];
      int i;
      for(i=0; i<3; i++)
      {
        cin>>a[i].name;
        cin>>a[i].age;
      }
      for(i=0; i<3; i++)
        cout<<a[i].name<<" "<<a[i].age<<endl;
      return 0;
    }

    3.9 指向内存位置的指针
    3.9.1 指针就是表示内存地址的数据类型
    3.9.2 指针变量的定义
    3.9.3 指针的赋值和使用

    //p80
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int i(3),*p;
      p=&i;
      cout << p << endl; //会自动以16进制形式输出地址
      i=5;
      cout << *p << endl; //输出地址p中的值
      return 0;
    }
    //p80
    #include <iostream>
    using namespace std;
    
    typedef struct emplyee
    {
      string name;
      int age;
    } emplyee;
    
    int main()
    {
      emplyee a[3],*p; //p是emplyee类型的指针
      int i;
      for(i=0; i<3; i++)
      {
        cin>>a[i].name;
        cin>>a[i].age;
      }
      p=a; //即 p=&a[0],p先指向结构数组中的第一个元素
      for(i=0; i<3; i++)
      {
        cout<<p->name<<" "<<p->age<<endl;
        p++; //指针加1,会跳到下一个结构位置
      }
      return 0;
    }

    TOP

  • 相关阅读:
    第七届湘计算机程序设计竞赛的学生 报道称,数字游戏
    python IDE
    字符串处理
    Ajax得知(两)—— 一个简单的Ajax示例
    九度OJ 1068 球半径和数量 (模拟)
    centos 之7zip
    svnclient本地化和异常处理
    java web.xml listener servlet 和filter加载顺序
    Jquery zTree实例
    探索Android中的Parcel机制(上)
  • 原文地址:https://www.cnblogs.com/xin-le/p/4077678.html
Copyright © 2011-2022 走看看