程序清单5.3

#include<iostream>
int main()
{
using namespace std;
int x;

cout<<"The expression x = 100 has the value ";
cout<<(x=100)<<endl;
cout<<"Now x = "<<x<<endl;
cout<<"The expression x < 3 has the value ";
cout<<(x<3)<<endl;
cout<<"The expression x > 3 has the value ";
cout<<(x>3)<<endl;
cout.setf(ios_base::boolalpha);
cout<<"The expression x < 3 has the value ";
cout<<(x<3)<<endl;
cout<<"The expression x > 3 has the value ";
cout<<(x>3)<<endl;
system("pause");
return 0;
}

<<运算符的优先级比表达式中使用的运算符高

程序清单5.4:使用循环来计算并存储前16个阶乘

#include<iostream>
const int ArSize=16;
int main()
{
long long factorials[ArSize];
factorials[1]=factorials[0]=1LL;
for (int i=2;i<ArSize;i++)
factorials[i]=i*factorials[i-1];
for (int i=0;i<ArSize;i++)
std::cout<<i<<"! = "<<factorials[i]<<std::endl;
system("pause");
return 0;
}

程序清单5.5:通过修改更新表达式来修改步长

#include<iostream>
int main()
{
using namespace std;
using std::cout;
using std::cin;
using std::endl;
cout<<"Enter an integer:";
int by;
cin>>by;
cout<<"Counting by "<<by<<"s:\n";
for (int i=0;i<100;i=i+by)
cout<<i<<endl;
system("pause");
return 0;
}

程序清单5.6:按相反的顺序显示数组的内容,用for循环

#include<iostream>
#include<string>
int main()
{
using namespace std;
cout<<"Enter a word:";
string word;
cin>>word;
for (int i=word.size()-1;i>=0;i--)
cout<<word[i];
cout<<"\nBye.\n";
system("pause");
return 0;
}

程序清单5.7:前缀版本和后缀版本的区别,如++x,+xx

#include<iostream>
int main()
{
using namespace std;
int a=20;
int b=20;
cout<<"a = "<<a<<":b = "<<b<<"\n";
cout<<"a++ = "<<a++<<":++b = "<<++b<<"\n";
cout<<"a = "<<a<<":b = "<<b<<"\n";
system("pause");
return 0;
}

++x先加1再用新值计算表达式,x++再计算表达式再加1

114页底

 

#include<iostream>
int main()
{
using namespace std;
int x=20;
{
int y=100;
cout<<x<<endl;
cout<<y<<endl;
}
cout<<x<<endl;
cout<<y<<endl;
system("pause");
return 0;
}

 如果仅在语句块定义一个新变量,则仅当程序执行该语句块中的语句时,该变量才存在,执行完即被释放,因此,cout<<y<<endl;会导致编译错误

115页顶

#include<iostream>
int main()
{
using namespace std;
int x=20;
{
cout<<x<<endl;
int x=100;
cout<<x<<endl;
}
cout<<x<<endl;
system("pause");
return 0;
}

如果在一个语句块中声明一个变量,而外部语句块中也有一个这种名称的变量,则在声明位置到内部语句块结束的范围之内,新变量将隐藏旧变量,然后旧变量再次可见。

程序清单5.9:将数组中的字符顺序反转

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

char temp;
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";
system("pause");
return 0;
}

 程序清单5.10:混淆等于运算符==与赋值运算符=

#include<iostream>
int main()
{
using namespace std;
int quizscores[10]=
{20,20,20,20,20,19,20,18,20,20};
cout<<"Doing it right:\n";
int i;
for (i=0;quizscores[i]==20;i++)
cout<<"quiz "<<i<<" is a 20\n";

cout<<"Doing it dangerously wrong:\n";
for (i=0;quizscores[i]=20;i++)//混淆等于运算符和赋值运算符会导致错误
cout<<"quiz "<<i<<" is a 20\n";
system("pause");
return 0;
}

程序清单5.11:在for循环的测试条件中使用了strcmp()

 

#include<iostream>
#include<cstring>
int main()
{
using namespace std;
char word[5]="?ate";
for (char ch='a';strcmp(word,"mate");ch++)
{
cout<<word<<endl;
word[0]=ch;
}
cout<<"After loop ends,word is "<<word<<endl;
system("pause");
return 0;
}

程序清单5.12:使用关系运算符比较string类字符串,结果与程序清单5.11的结果相同

#include<iostream>
#include<string>
int main()
{
using namespace std;
string word="?ate";
for (char ch='a';word!="mate";ch++)
{
cout<<word<<endl;
word[0]=ch;
}
cout<<"After loop ends,word is "<<word<<endl;
system("pause");
return 0;
}

程序清单5.13:使用循环遍历字符串,并显示其中的字符及其ASCII码

#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;
while(name[i]!='\0')
{
cout<<name[i]<<":"<<int(name[i])<<endl;
i++;
}
system("pause");
return 0;
}

程序清单5.16:在遇到#字符时停止读取输入。该程序计算读取的字符数,并回显这些字符

#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;//cin读取时,会忽略空格,在5.17通过cin.get(ch)解决
}
cout<<endl<<count<<" characters read\n";
system("pause");
return 0;
}

程序清单5.17:使用cin.get(ch)函数

#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";
system("pause");
return 0;
}

程序清单5.18:使用cin.fail()检测文件尾,按ctrl+z+enter来显示文本文件,并报告它包含的字符数

#include<iostream>
int main()
{
using namespace std;
char ch;
int count=0;
cin.get(ch);
while (cin.fail()==false)
{
cout<<ch;
++count;
cin.get(ch);
}
cout<<endl<<count<<" characters read\n";
system("pause");
return 0;
}

 

 posted on 2021-03-13 22:04  HuJiao粉  阅读(107)  评论(0)    收藏  举报