zoukankan      html  css  js  c++  java
  • Linux下程式开发2 变量数据类型的代码以及编译&运算符与表达式

    变量数据类型的代码以及编译&运算符与表达式

    实例1

    [root@localhost code]# vim float.cpp
    [root@localhost code]# g++ float.cpp
    float.cpp: In function `int main()':
    float.cpp:14: parse error before `(' token
    [root@localhost code]#

    修改如下:

    #include <iostream>
    #include<iomanip>
    using namespace std;
    int main (void)
    {
      float f;
      double d;
      long double  ld;
      f=1.23456789f;
      d=1.23456657675677657;
      ld=1.23454356465656565464555364L;
      cout <<"effect number characters"<<endl;
      cout<<"float f="<<setprecision(10)<<f<<endl;
      cout<<"double d="<<setprecision(20)<<d<<endl;
      cout<<"long double ld="<<setprecision(29)<<ld<<endl;
      f=1e36f;
      d=1e307;
      ld=1e4930L;
      cout <<endl<<"Exponent"<<endl;
      cout<<setiosflags(ios::scientific);
      cout<<"float f="<<f<<endl;
      cout<<"double d="<<d<<endl;
      cout<<"long double ld="<<ld<<endl;

    [root@localhost code]# g++ float.cpp  -o float.out
    [root@localhost code]# ./float.out
    effect number characters
    float f=1.234567881
    double d=1.2345665767567766
    long double ld=1.2345435646565656547
    Exponent
    float f=9.99999961690316245e+35
    double d=9.99999999999999986e+306
    long double ld=1.00000000000000000003e+4930

    教科书中,说c++ 存储浮常数也就是float这一类,预设类型为double,另外,爱将某浮点数值指定为float存储时候,必须在数值字未加f,F;

    float有效精度7位

    还有,使用控制精度,需加#inlcude<iomanip>

    实例2

    #include<iostream>
    using namespace std;
    int main(void)
    {
    float f=1e36f;
    if (abs(f-1e36)<1e30)
            {  f=f+5e35;
               cout<<"float f="<<f<<endl;
            }
    return 0;
    }

    [root@localhost code]# g++ compare.cpp
    [root@localhost code]# g++ compare.cpp -o compare.out
    [root@localhost code]# ./compare.out
    float f=1.5e+36

    实例3

    #include<iostream>
    using namespace std;
    int main (void)
    {
    char ch1='A';char ch2=65;
      cout<<"ch1= '"<<chl<<"'"<<endl;
      cout<<"ch2= '"<<ch2<<"'"<<endl;
      return 0;
    }

    一开始报错如下:

    char.cpp: In function `int main()':
    char.cpp:5: parse error before `char'
    char.cpp:6: `chl' undeclared (first use this function)
    char.cpp:6: (Each undeclared identifier is reported only once for each function
       it appears in.)
    char.cpp:7: `ch2' undeclared (first use this function)

    原因如下:  char ch1='A';char ch2=65; 中间的;用逗号

    修改后还是报错:

    char.cpp: In function `int main()':
    char.cpp:6: `chl' undeclared (first use this function)
    char.cpp:6: (Each undeclared identifier is reported only once for each function
       it appears in.)

    原因非常明确:  cout<<"ch1= '"<<chl<<"'"<<endl; 应该改为  cout<<"ch1= '"<<ch1<<"'"<<endl; 显然,1和l经常录错.

    这回正确了

    教科书说:A 的ASCII码为65,所以,直接用65 结果是一样的.

    实例4 转义序列

    #include


    匈牙利命名法

    变量的有效范围

    #include <iostream>
    using namespace std;
    int main(void)
    {
      int i=1;
      {
            int j=2
            cout<<"i="<<i<<",j="<<j<<endl;
      }
    cout<< "j="<<j<endl;
    return 0;
    }

    验证错误:

    [root@localhost code]# g++ bloack.cpp
    bloack.cpp: In function `int main()':
    bloack.cpp:8: parse error before `<<' token
    bloack.cpp:10: `j' undeclared (first use this function)
    bloack.cpp:10: (Each undeclared identifier is reported only once for each
       function it appears in.)

    当广域变量遇上区域变量的时候:

    广域变量暂时被区域变量盖住,若爱指定存取广域变量时候,可以运用::这个解析运算符.

    代码:

    #include<iostream>
    using namespace std;
    int i=1;
    int main(void)
    {
        int i=2;
        cout<<"i="<<i<<endl;
        cout<<"::i="<<::i<<endl;
    return 0;
    }

    运行结果

    [root@localhost code]# ./loc_glo.out
    i=2
    ::i=1

    常数,自定义数,和const常数和#define:

    #include <iostream>
    using namespace std;
    #define HI cout<<"hello!fleetwgx"<<endl;
    int main(void)
    { const int a=100;
    HI cout<< "\n" << endl; HI
    cout<<"\na="<<a<<endl;
    return 0;

    [root@localhost code]# ./hi.out
    hello!fleetwgx
    hello!fleetwgx
    a=100

    读入语句cin;

    代码:

    #include<iostream>
    using  namespace std;
    int main(void)
    {
      double  input_double;
      int input_int;
    cout<<"please input a float_point number!"<<endl;
    cin>>input_double;
    cout<<input_double<<"is inputed.\n"<<endl;
    cout<<"please input a float-point number as well as an integer number!"<<endl;
    cin>>input_double>>input_int;
    cout<<input_double<<"and"<<input_int<<"is inputed ."<<endl;
    return 0;
    }

    运行结果:

    [root@localhost code]# ./cin.out
    please input a float_point number!
    3.1415926789096456803486034344546578675075679657853875843728075
    3.14159is inputed.
    please input a float-point number as well as an integer number!
    3.14159 3
    3.14159and3is inputed .
    [root@localhost code]#

  • 相关阅读:
    java基础之冒泡排序和选择排序
    从今天开始写博客、托管代码到 Github
    九度 Online Judge 之《剑指 Offer》一书相关题目解答
    Window.ShowModalDialog使用手册
    模态窗体刷新父窗体
    彻底卸载sql2008后重新安装
    servlet 学习笔记(二)
    查看本地安装的sql server是什么版本
    servlet 学习笔记(三)
    struts2 国际化
  • 原文地址:https://www.cnblogs.com/fleetwgx/p/1444128.html
Copyright © 2011-2022 走看看