第五章代码C++ primer Plus

++递增和–递减

#include<iostream>
int main()
{
    using std::cout;
    using std::endl;
    int a = 20;
    int b = 20;
    cout << "a =" << a << ": b =" << b << "\n";
    cout << "a++=" << a++ << ":++b = " << ++b << "\n";
    cout << "a =" << a << ":b =" << b << "\n";

    double arr[5] = { 1.1,2.3,4.2,5.3,5.7 };
    double* pt = arr;//pt points to arr[0],1.1
    double* p1 = arr;
    double* p2 = arr;
    double *p3 = arr;
    cout << arr<<"\n";
    cout << *pt<<"\n";//arr[0]
    cout << ++pt << "\n";//arr[1]
    cout<<*++pt<<"\n";//从右到左,先将++应用于pt ,再将*应用于被递增后的pt  arr[2]
    cout << ++*p1 << "\n";//先取p1所指向的值,然后将这个值加1
    cout << (*p2)++ << "\n";//圆括号表示先解除引用,得到1.1,然后运算符将这个值递增到2.1,p2仍指向arr[0]
    cout << *p3++ << "\n";//对指针p3增加,arr[0],但p3的值为arr[1]的地址
    cout << *p3 << "\n";//但p3的值为arr[1]的地址

    int guests = 6;
    while (guests++ < 10)
        cout << guests << endl;

    int k = 5;
    k += 3;
    cout << k << "\n";
    int* pa = new int[10];
    cout << pa << "\n";
    pa[4] = 12;
    pa[4] += 6;
    cout << pa[4] << "\n";
    *(pa + 4) += 7;
    cout << pa<< "\n";
    pa += 2;
    cout << pa << "\n";
   // 34 += 10;

   
    return 0;
}

for复合语句

#include<iostream>
int main()
{
	using namespace std;
	cout << "The amazing accounto will sum and average";
	cout << "five numbers for you .\n";
	cout << "please enter five values :\n";
	double number;
	double sum = 0.0;
	for (int i = 1;i <= 5;i++)//两个;是必须要的
	//for(init-expression;test-expression;update-expression)
	{
		cout << "value " << i << ":";
		cin >> number;
		sum += number;
	}//花括号代表for中的语句块  花括号里面是 statement(s)
	cout << "five exquisite choices indeed!";
	cout << "They sum to " << sum << endl;
	cout << "and average to" << sum / 5 << ".\n";
	cout << "the amazing accounto bids you adieu!\n";

	int x = 20;
	{
		cout << x << endl;
		int x = 100;
		cout << x << endl;
	}
	cout << x << endl;
	return 0;
}
//x的输出
20
100
20

反转字符串 逗号运算符

//
#include<iostream>
#include<string>
int main()
{
	using namespace std;
	cout << "Enter a word :";
	string word;//string 对象
	cin >> word;

	char temp;//对char 反转
	int i, j;
	for (j = 0, i = word.size() - 1;j < i;--i, ++j)//第一个逗号只是分隔符
	{
		temp = word[i];
		word[i] = word[j];
		word[j] = temp;
	}
	cout << word << "\nDone\n";

	int x = 20, y = 2 * x;//先计算x再计算y(,是一个顺序点)
	cout << x<< "\n";
	cout << y << "\n";
	//int z = 12,11;
	int z;
	z = 12, 240;//,的优先级最低 此处240不起作用 z为12
	cout << z<< "\n";
	int m1 = (2, 31);//m1设置为31 即逗号右侧的表达式值
	cout << m1 << "\n";


	return 0;
}

C-风格字符串比较

#include<iostream>
#include<cstring>
int main()
{
    using namespace std;
    char word[5] = "?ate";

    for (char ch = 'a';strcmp(word, "mate");ch++)//ch加1后对应的字母 赋值给word的第一个字母
        //strcmp()函数 根据字符串中字母排序 返回0 正数负数
    {
        cout << word << endl;
        word[0] = ch;
    }
   
    //strcmp("amte", "mate") <0;
    cout << "After loop ends,word is" << word << endl;


    char big[80] = "Daffy";
    char little[6] = "Daffy";
    cout << big << endl;
    cout << little << endl;

    char ch1;
    for (ch1 = 'a';ch1 <= 'z';ch1++)//关系运算符比较 字符 大小
        cout << ch1;
    return 0;
}

比较string类字符串

#include<iostream>
#include<string>
int main()
{
    using namespace std;
    string word = "?ate";
    for (char ch = 'a';word != "mate";ch++)
        //使用的是string 对象  不是char数组 string类重载运算符!=
    {
        cout << word << endl;
        word[0] = ch;
    }
    cout << "After loop ends,word is " << word << endl;
    return 0;
}

while 循环— 没有初始化和更新部分的for循环 只有测试条件和循环体

#include<iostream>
const int ArSize = 20;
int main()
{
    using namespace std;
    char name[ArSize];
    cout << "Your first name,please";
    cin >> name;
    cout << "Here is your name,verticalized and ASCIIized:\n";
    int i = 0;//init-expression
  //  while (name[i]!= '\0')//字符是否为空值字符
    while (name[i])//这个比上一行更快 结果一样
    //while(test-expression)
    {
        cout << name[i] << ":" << int(name[i]) << endl;//statement(s)
        i++;//update-expression
    }

    int j = 0;
    while (j < 10);//该行加了;是一个空循环 分号将结束while 去掉分号可以显示j
    {
        cout << j;
        j++;
    }
    return 0;
}

编写延时循环

#include<iostream>
#include<ctime>
int main()
{
    using namespace std;
    cout << "Enter the delay time ,in seconds:";
    float secs;
    cin >> secs;
    clock_t delay = secs * CLOCKS_PER_SEC;//2*2000
    cout<< CLOCKS_PER_SEC<<endl;
    cout << "starting\a\n";//\a警报符
    clock_t start = clock();
    while (clock() - start < delay)
        ;
    cout << "done \a\n";
    return 0;

}
#include<iostream>
#define char_point char *//不加分号
typedef char* byte_pointer;
int main()
{
    using namespace std;
    byte_pointer pa, pb;
    cout << "typedef:\n";
    cout << sizeof(pa) << endl;
    cout << sizeof(pb) << endl;

	char_point  pc, pd;
   cout << "#define:\n";
    cout << sizeof(pc) << endl;
    cout << sizeof(pd) << endl;
    return 0;

}

奇特的for 循环

#include<iostream>
int main()
{
    using namespace std;
    int i = 0;
    for (;;) //空测试条件,视为true
    {
        i++;
        if (30 >= i)
            break;
    }
    cout << i << endl;

    int j = 0;
    for (;;j++)//for(init-expression;test-expression;update-expression)
     //空测试条件,视为true
    {
        if (30 >= j) break;
    }
    cout << j << endl;

    int k = 0;
    do {
        k++;
    }while (30 > k);
    cout << k << endl;

    int m=0;
    while (m < 30)
    {
        m++;
    }
    cout << m << endl;

    return 0;
}

基于范围的for循环

#include<iostream>
int main()

{
    using namespace std;
    double prices[5] = { 4.99,10.99,6.87,7.99,8.49 };
    for (double x : prices)//x遍历prices
        cout << x << endl;

    for (double& x : prices)//&表明x是一个引用变量
        cout<< "x ="<< x * 0.80<<endl;

    for (int x1 : {3, 5, 2, 6})//初始化列表{3,5,2,6}
        cout << x1<< "";
    cout << endl;

    return 0;

}

使用原始的cin进行输入

#include<iostream>
int main()
{
    using namespace std;
    char ch;
    int count = 0;
    cout << "Enter characters;enter # to quit:\n";
    cin >> ch;
    while (ch != '#')//遇到#就停止循环
    {
        cout << ch;
        ++count;//计数
        cin >> ch;
    }
    cout << endl << count << "characters read\n";
    return 0;
}

使用cin.get(char)进行补救

#include<iostream>
int main()
{
    using namespace std;
    char ch;
    int count = 0;

    cout << "Enter characters;enter # to quit:\n";
    cin.get(ch);//将全部字符计算在内,包括空格
    while (ch != '#')
    {
        cout << ch;
        ++count;
        cin.get(ch);
    }
    cout << endl << count << "characters read\n";
    return 0;
}

文件尾条件

#include<iostream>
int main()
{
    using namespace std;
    char ch;
    int count = 0;
    
    cin.get(ch);//windows下ctrl+z相当于EOF(结束输入)
    while (cin.fail() == false)//while(!cin.fail())
    {
        cout << ch;
        ++count;
        cin.get(ch);
    }
    //上述循环可以精简为下面的格式:
    //while(cin.get(ch))//while input is successful
    {
           ...//do stuff
    }
    cout << endl << count << "characters read\n";
    return 0;
}

另一个cin.get()版本

//textin4.cpp--reading chars with cin.get()
#include<iostream>
int main(void)
{
    using namespace std;
    int ch;
    int count = 0;

    while ((ch=cin.get())!=EOF)//test for end-of-file
        //while(ch=cin.get()!=EOF)// !=的优先级高于= 先将cin.get()返回值和EOF比较
        //cin.get(ch1).get(ch2);//cin.get(ch1)返回一个cin对象,然后通过该对象调用get(ch2) 
    {
        cout.put(char(ch));
        ++count;
    }
    cout << endl << count << "characters read\n";
    return 0;
}

嵌套循环和二维数组

//nested.cpp---nested loops and 2-D array
#include<iostream>
const int Cities = 5;
const int Years = 4;
int main()
{
    using namespace std;
    const char* cities[Cities] =//array of pointers
    {                            //to 5 strings
          "Gribble City",
          "Gribbletown",
          "New Gribble",
          "San Gribble",
          "Gribble Vista"
    };
     
    int maxtemps[Years][Cities] =//2-D array 
        //包含4个元素的数组 每一个元素由5个整数组成
    {
        {96,100,87,101,105},//maxtemps[0]
        {96,98,91,107,104},
        {97,101,93,108,107},
        {98,103,95,109,108}
    };

    cout << "Maximum temperatures for 2008-2011\n\n";
    for (int city = 0;city < Cities;++city)//外循环year
    {
        cout << cities[city] << ":\t";
        for (int year = 0;year < Years;++year)//内循环当year为0时  显示maxtemps[0]的5个数
            cout << maxtemps[year][city] << "\t";
        cout << endl;
    }
    //cin.get();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值