第五章 循环和关系表达式
- 书写规范:for ( A ; B ; C )
- C++ 中通过 cout 来实现格式输出,就类似于C语言中通过 printf() 来实现格式输出。cout.setf() 的作用是通过设置格式标志来控制输出形式,比如:
#include <iostream>
using namespace std;
int main()
{
bool a = true;
cout.setf(ios_base::boolalpha);
cout<<a;
return 0;
}
- 组合赋值操作符
- 例题字符串反向:字符串是一片连续的内存空间,所以无论是数组形式还是string类定义的字符串,均可以通过索引的方式访问每一个元素。
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
char charr[] = "hello world !";
string str = "hello world !";
int c_len = strlen(charr);
int s_len = str.size();
for(int i = 0;i < (strlen(charr)/2);i++,c_len--)
{
char temp = charr[i];
charr[i] = charr[c_len-1];
charr[c_len-1] = temp;
cout<<charr[i]<<" <-> "<<charr[c_len-1]<<endl;
}
for(int i = 0;i < strlen((charr));i++)
{
cout<<charr[i];
}
cout<<endl<<"----------------------------------------"<<endl;
for(int i = 0;i < (str.size()/2);i++,s_len--)
{
char temp = str[i];
str[i] = str[s_len-1];
str[s_len-1] = temp;
cout<<str[i]<<" <-> "<<str[s_len-1]<<endl;
}
for(int i = 0;i < str.size();i++)
{
cout<<str[i];
}
return 0;
}
输出结果:
- C - 风格字符串的比较函数:strcmp(); 不使用 word == "hello";
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
char word[] = "hello";
if(word == "hello")
{
cout<<"相等"<<endl;
}
else
{
cout<<static_cast<void*>(&word)<<":"<<&(word[0])<<endl;
cout<<(int *)"hello"<<endl;
}
if(!strcmp(word,"hello"))
{
cout<<"相等"<<endl;
}
else
{
cout<<"不相等"<<endl;
}
return 0;
}
里面有几个比较关键的点,也是我在写完这段代码后通过查找资料解决的问题,整理出来和大家分析一下:
1. 如果直接使用 == 对字符串进行比较,比较的实际上是地址,因为存储位置不同,自然比较结果是不相等的;
2. strcmp 比较两个字符串时,如果相等,函数的返回值为0(false),如果不相等,函数的返回值非零(true)【具体的说,如果第一个字符串排在第二个字符串之前,则strcmp()将返回一个负值,如果第一个字符串按子,字母排序排在第二个字符串之后,则strcpm()将返回一个正值】;
3. 如何打印出一维数组的头指针,如果我想打印出一个字符串常量的地址,我只需要cout<<(int *)"word",通过强制类型转换即可,但是如果想输出一个数组形式定义的字符串首地址该怎么做?使用cout<<a?显然不对,这样打印出来的一定是数组中的内容;那么尝试使用cout<<&(a[0]),结果还是不对,通过指针来尝试也不行,于是乎去别人博客里逛了一下,果然发现了原因,详见:https://blog.csdn.net/ZongYinHu/article/details/49512919,我这里说明一下原因,掌握如何使用即可:
C++标准库中I/O类对输出操作符<<重载,在遇到字符型指针时会将其当做字符串名来处理,输出指针所指的字符串。既然这样,我们就别让他知道那是字符型指针,所以得进行类型转换,即:希望任何字符型的指针变量输出为地址的话,都要作一个转换,即强制char *转换成void *,如下所示:
cout<<"static_cast<void *>(&c)="<<static_cast<void*>(&c)<<endl;
4. 虽然不能用关系操作符来比较字符串,但是却可以用它们来比较字符,因为字符实际上是整数。
- string类创建的对象可以使用关系操作符进行比较:word != “mate”
- 类型别名:
- do while 循环
5.5 循环和文本输入
5.5.1 使用原始的cin进行输入
5.5.2 使用 cin.get(char) 进行补救
5.5.5 另一个 cin.get() 版本
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
char ch;
string str;
cin.get(ch);
while(ch != '#')
{
str += ch; //字符运算不能使用strcat()使用
cout<<ch<<endl;
cin.get(ch);
}
cout<<str;
return 0;
}
复习题:
答:入口循环先判断条件,条件满足才可执行循环体内容,条件不满足则退出。出口条件循环条件使得循环体内的内容必须被至少执行一次,然后在进行条件的判断,选择是否继续执行。
答:01234
答:
0369
12
答:运算符前置和运算符后置
6
8
答: k = 8
#include <iostream>
using namespace std;
int main()
{
int sum = 1;
for(int i =1;i < 65;i*=2)
{
cout<<i<<"\t";
}
return 0;
}
答:{}
答:
答:
cin>>ch:每次读取一个字符,但其不能在 cin 队列中存储空格、TAB、换行字符;cin.get(ch)可以逐个单词读入,并且可以保存空格、换行、TAB字符;ch = cin.get()返回的是一个整型的值,而且也不会跳过这几种字符。
编程练习:
#include <iostream>
using namespace std;
int main()
{
int x,y,sum = 0;
cin>>x>>y;
for(int i = x;i <= y;i++)
{
sum += i;
}
cout<<sum<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int x,sum = 0;
while(x != 0)
{
cin>>x;
sum += x;
cout<<sum<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int x = 100;
float y = 100;
int i = 1;
float li = 0;
while(x > li)
{
x = 0.1 * 100 * i; //i 年 x 获得的利润
li = y * 0.05;
y += li;
i++;
cout<<x<<"\t"<<li<<endl;
}
cout<<i<<endl;
cout<<x<<"\t"<<li<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int months = 12;
string str[months] = {"1","2","3","4","5","6","7","8","9","10","11","12"};
int month[months];
int sum = 0;
for(int i = 0;i < months;i++)
{
cout<<str[i]<<" : ";
cin>>month[i];
sum += month[i];
}
cout<<sum;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int months = 12;
string str[3][months] = {{"1","2","3","4","5","6","7","8","9","10","11","12"},{"1","2","3","4","5","6","7","8","9","10","11","12"},{"1","2","3","4","5","6","7","8","9","10","11","12"}};
int month[3][months];
int sum = 0;
int sum_each[3]
;
for(int j = 0;j < 3;j++)
{
for(int i = 0;i < months;i++)
{
cout<<str[j][i]<<" : ";
cin>>month[j][i];
sum += month[j][i];
}
sum_each[j] = sum;
sum = 0;
}
for(int i = 0;i < 3;i++)
{
cout<<sum_each[i]<<"\t";
sum += sum_each[i];
}
cout<<sum<<endl;
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct Car
{
string name;
int month;
};
int main()
{
cout<<"How many cars do you wish to catalog?";
int n;
cin>>n;
Car* car= new Car [n];
for(int i = 0;i < n;i++)
{
cout<<"Car #"<<i + 1<<":"<<endl;
cout<<"Please enter the make:";
cin.ignore();
getline(cin,car[i].name);
cout<<"Please enter the year made:";
cin>>car[i].month;
}
cout<<"Here is your collection:"<<endl;
for(int i = 0;i < n;i++)
{
cout<<car[i].month<<"\t"<<car[i].name<<endl;
}
return 0;
}
ps:这里使用了cin.ignore函数,作用是清楚cin队列里的第一个字符。这是我第一次用cin.ignore(),标记一哈!
#include <iostream>
#include <string>
using namespace std;
int main()
{
char word[20];
cin>>word;
int num = 0;
while(strcmp(word,"done"))
{
cin>>word;
num++;
}
cout<<num;
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string word;
cin>>word;
int num = 0;
while("done" != word)
{
cin>>word;
num++;
}
cout<<num;
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
int rows;
cin>>rows;
int i,m,n = 0;
for(i = 0;i < rows;i++)
{
for(m = rows - 1 - i;m > 0;m--)
{
cout<<" ";
n++;
}
for(;n < rows;n++)
{
cout<<"*";
}
cout<<endl;
n = 0;
}
return 0;
}