《现代c++程序设计》(美)Barbara Johnston-----0

一旦你对程序进行了改动,就要对改动的部分进行测试!无论改动多么简单,必定要对它进行测试!ios

不要试图在写代码的过程当中设计程序,把须要作的东西写在纸上。c++

不要依靠编译器来保证代码的正确性,要理解所写的每一行代码ide

 

字节:计算机内存的基本单元。比特,也叫位,拥有两种状态的数据单元,好比0或1,开或关。函数

1byte=8bit测试

 

前缀和后缀自增和自减运算符:注意:i值 是6,spa

 

 c++中数据显示位数的控制:设计

#include<iostream> #include<iomanip>
using namespace std; int main() { double f = 333233.1415926535; float aa = 12.25678; float bb = 0.123456789; cout << f << endl; cout << aa << endl; cout << bb << endl; cout.setf(ios::fixed | ios::showpoint); cout.precision(3); //     cout << f << endl;    //输出3位小数,3.142
    cout << aa << endl; cin.get(); return 0; }
View Code

显示结果:code

333233
12.2568
0.123457
333233.142
12.257blog

 

c++控制语句:队列

 

c++中if语句:(4种)

1.if-else语句
if (condition)

{

              //条件为真时执行

}

else

{

              //条件为假时执行

}

2.if-else if-else语句
if (condition1)

{

              //属于条件1的语句

}

else if(condition 2)

{

              //属于条件2的语句

}

else

{

              //属于条件3的语句

}

注意,else语句不必定是必须的,也就是说:if或if--else if或者if-else if -else if....-else也是容许的;可是只能有一个else语句。

3.嵌套if-else语句

示例以下:
if (condition)                             //第一个条件

{         

          if (another condition)       //这里是嵌套的if 语句

          {

                    //语句

          }

           //更多的语句

}

4.条件运算符

int windspeed;

bool bHurricane;

if (winspeed>75)

     bHurricane=true;

else

    bHurricane=false;

 

经过条件 运算符“?”只经过一行代码就能完成上述检查与操做赋值:

int windspeed;

bool bHurricane;

bHurricane = windspeed>75? true : false

注意,运算符“?”回影响代码的 易读性,初学者不建议使用

 

c++中switch语句;

switch(此处是变量)

{

           case value1:

                   //语句块1

                   break;        //break不要忘记加上

           case value2:    case value3:(表示2或者3状况下)

                   //语句块2

                   break;

           default:            //缺省状况下

   //语句n

}

 

c++中的循环有三种:

for 循环:当清楚循环体需重复执行的次数时,多使用之

示例:输出字母表

结果:

#include<iostream>
#include<iomanip>       //为了使用setw
using namespace std;

int main()
{
    int letter_ctr, i;
    cout << "we are going to write our ABCs" << endl;

    letter_ctr = 0;
    for (i = 65; i < 91; ++i)         //A=65,z=90
    {
        //将整数转化为字符
        cout << setw(6) << static_cast<char>(i);

        letter_ctr ++;
        if (letter_ctr == 7)
        {
            cout << endl;
            letter_ctr = 0;
        }
    }

    cout << "\n there are our ABCs";


    cin.get();
    return 0;
}

 while循环:可用于在不能事先肯定循环次数的状况下对循环进行控制。

while(condition)

{

            //这些语句在条件为真时被执行

}

do while循环:与while循环相似,可是在循环体执行结束检查循环的条件

do

{

           //循环语句体

} while (condition);

 

cin.ignore()函数:

用于回车符的删除,

有时在cin>>后面跟有getline(),cin会将回车符留在输入队列中,须要将回车符移除才能正确赋值给变量

 

    int aa;
    string bb;

    cout << "how many do you have?" << endl;
    cin >> aa;
    cout << aa << endl;;

    cin.ignore();             // 加上这个getlIne才能正确赋值,遇到回车符会中止

    cout << "the color is ???" << endl;
    getline(cin,bb);
    cout << bb;
相关文章
相关标签/搜索