1、老式的c++实现要求使用ios::boolalpha,而不是ios_base::boolalpha来作为cout.setf()的参数,而有些老式是实现甚至无法识别这两种形式。
cout.setf(ios_base::boolalpha);
通常,cout在显示bool值之前将它们转换为int,但是cout.setf(ios::boolalpha)函数设置了一个标记,该标记命令cout显示true和false,而不是1和0;
2、string()类中的size()可以获得字符串中的字符数。在反转字符串方面,string类有更加简洁的方式。
3、++X前缀位于操作数前面。X++位于操作数后面。
string word;
cin>>word;
char temp;
for(j=0,i=word.size()-1;j<i;--i,+++j)
{
temp=word[i];
word[i]=word[j];
word[j]=temp;
strcmp()比较字符串是否相等。
4、colck()
头文件ctime中, 定义了一个符号常量——CLOCKS PER SEC,这个常量等于每秒钟包含的系统时间单位数,将系统时间除以这个值,可以得到秒数,或者将描述乘以这个值,可以到的以系统时间单位为单位的时间,其次,ctime将clock_t作为clock()的返回函数的类型的别名。这说明可以将变量声明为clock_t类型,编译器将把它转换为long、unsgned int 或者适合系统的其他类型。
5、建立类型别名:
第一种:
#define BYTE byte1;
第二种:
typedef char byte;
(不会创建新的类型,而只是为已有的类型建立一个新名称。)
6、基于范围的for循环
double prices[5]
={1.99,10.99,6.91,7.21,8.12};
for(double x:prices)
cout <<x<<std::endl;
7、循环和文本输入
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
char ch;
int count = 0;
cout << "Enter characters; enter # to quit:\n";
cin >> ch;//输入字符
while (ch != '#')
{
cout << ch;
count++;
cin >> ch;//显示符合要求的字符
}
cout << endl << count << " charactere read\n";
return 0;
cin.get(char)在读取输入中的下一个字符(即使它是空格,)并将其赋给变量ch,使用这个函数调用替换cin>>ch。
while (ch != '#')
{
cout << ch;
count++;
cin.get(ch); //可以读取每个字符,包括空格。
}
8、函数重载允许对多个相关的函数使用相同的名称,这些函数以不同方式或针对不同类型执行相同的基本任务。
9、不接受任何参数的cin.get()成员函数返回输入中的下一个字符,例如:ch=cin.get();
也可以使用cout.put()函数来显示字符。参数类型是char。
第三题:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <istream>
#include <array>
using namespace std;
int main()
{
using namespace std;
int sum = 0;
int a,i=1;
while(i)
{
cin >> a;
if (a == 0)
{
i = 0;
}
else
{
sum = sum + a;
}
}
cout << "sum=" << sum;
return 0;
}
第四题:
#include <iostream>
using namespace std;
int main()
{
int year = 1;
int dep=100 , cleo = 100;
while (dep >= cleo)
{
dep =dep+10;
cleo = cleo + cleo * 0.05;
year++;
}
cout << year;
cout << "年后,cleo的投资价值将会超过dep的投资价值。" << endl;
cout << "cleo的投资价值" << cleo<<endl;
cout << "dep的投资价值" << dep << endl;
return 0;
}
第五题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a[20], i;
int sum = 0;
int book[20];
string month[12] = {"1","2","3","4","5","6","7","8","9","10","11","12"};
for (i = 0; i < 12; i++)
{
cout << month[i] << ":";
cin >> a[i];
sum = sum + a[i];
}
cout << "一年销售总量;" << sum << endl;;
for (i = 0; i < 12; i++)
{
cout << "第" << month[i] << "月销售量:";
cout << a[i] << endl;;
}
return 0;
}
第六题:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "Septempber", "October", "November", "December" };
unsigned int sales[3][12] = { 0 };
unsigned int total_sales[3] = { 0 };
for (int i = 0; i < 3; i++)
{
cout << "Enter " << i + 1 << " year(s) sales of book <C++ for Fools> each month: " << endl;
for (int j = 0; j < 12; j++)
{
cout << month[j] << ": ";
cin >> sales[i][j];
total_sales[i] += sales[i][j];
}
}
for (int i = 0; i < 3; i++)
{
cout << i + 1 << " year(s) total sales is " << total_sales[i] << endl;
}
cout << "Three years total sales is " << total_sales[0] + total_sales[1] + total_sales[2] << endl;
return 0;
}
第七题
#include <iostream>
#include <string>
using namespace std;
struct car
{
string company;
int year;
};
int main()
{
int number_car;
int i;
cout << "How many cars do you wish to catalog?\n";
cin >> number_car;
cin.get();
struct car *car_info= new struct car[number_car];
for(i = 0; i < number_car;i++ )
{
cout << "Car #" << i+1 << ":" << endl;
cout << "please enter the make:\n";
getline(cin, car_info[i].company);
cout << "please enter the year made:\n";
cin >> car_info[i].year;
cin.get();
}
cout << "Here is your collevtion: " << endl;
for (i = 0; i < number_car; i++)
{
cout << car_info[i].company << " " << car_info[i].year << endl;
}
return 0;
}
第八题
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
int count = 0;
char word[128];
while (cin>>word)
{
if (strcmp(word, "done"))
{
count++;
}
else
break;
}
cout << "You entered a total of " << count << " words.\n";
return 0;
}