第六章习题摘录
1.输出为:
对于本题:
ch = cin.get()将空格和换行都存储在ch里;
并且条件中(ch = “&”)是赋值运算符不是比较运算符,所以ch会被赋值为$然后输出,所以条件永远为true,ct2和ct1是相等的。
cin.get(ch)或cin.get()读取一个字符会读取所有的输入包括,空格和换行符;而cin>>ch会跳过空格和换行符
2.++ch和ch+1得到的数值相同
但++ch的类型为char,将作为字符打印,而ch+1是int类型(因为char和int相加),将作为数字打印
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
char temp, temp_change;
cout << "请输入字符:";
cin >> temp;
while (temp != '@' )//while ((ch = cin.get()) != '@')
{
if (isdigit(temp))
{
cout << "请重新输入:";
cin >> temp;
continue;
}
else if (temp >= 'a' && temp <= 'z')
temp_change = toupper(temp);
else
temp_change = tolower(temp);
cout << temp_change << endl;
cout << "请输入字符:";
cin >> temp;
}
system("pause");
return 0;
}
运行结果
cctype的应用
#include<iostream>
#include<string>
#include<array>
using namespace std;
int main()
{
double donation[10];//const unsigned int SIZE = 10;
//array<double, SIZE> donation;
int i = 0;
double averg;
double sum =0.0;
int num=0;
cout << "Please enter less than 10 numbers:"; //cout << "Please enter up to ten double value, Non-digital to exit: " << endl;
while (i<10 && cin >> donation[i]) {
sum += donation[i];
i++;
}
averg = sum / i;
for (int j = 0; j < i; j++)
{
if (donation[j] > averg)
{
num++;
}
}
cout << "The average value is " << averg << ", and there are " << num << " double value large than agerage value!" << endl;
system("pause");
return 0;
}
输出结果
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout << "Please enter one of the following choices:" << endl;
cout << "c)carnivorce p)pianist" << endl;
cout << "t)tree g)game" << endl;
/*
cout << "Please enter one of the following choices: " << endl;
cout.flags(ios::left);制符表左对齐
cout << setw(20) << "c) carnivore" << "p) pianist" << endl;
cout << setw(20) << "t) tree" << "g) game" << endl;
setw(int n)是c++中在输出操作中使用的字段宽度设置,设置输出的域宽,n表示字段宽度。只对紧接着的输出有效,紧接着的输出结束后又变回默认的域宽。 当后面紧跟着的输出字段长度小于n的时候,在该字段前面用空格补齐;当输出字段长度大于n时,全部整体输出。
*/
char letter;
cin >> letter;
bool exit = false;//引入布尔值,控制Switch的退出
while (letter&&exit!=true)
{
switch (letter) {
case 'c':cout << "A maple is a carnivore" << endl;
exit = true;
break;
case 'p':cout << "A maple is a pianist" << endl;
exit = true;
break;
case 't':cout << "A maple is a tree" << endl;
exit = true;
break;
case'g':cout << "A maple is a game" << endl;
exit = true;
break;
default:cout << "Please enter a c,p,t,or g:";
break;
}
cin >> letter;
}
system("pause");
return 0;
}
输出结果
#include<iostream>
#include<string>
using namespace std;
int main()
{
double income=0.0;
double tax = 0;
cout << "Please enter your income:";
cin >> income;
while (income >= 0)
{
if (income <= 5000)
tax = 0;
else if (income > 5000 && income <= 15000)
tax = 0.10 * (income - 5000);
else if (income > 15000 && income <= 35000)
tax = 10000 * 0.10 + (income - 15000) * 0.15;
else
tax = 10000 * 0.10 + 20000 * 0.15 + (income - 35000) * 0.20;
cout << "Your tax is:" << tax;
cin >> income;
}
system("pause");
return 0;
}
isdigital函数的范围是-1到255
输出结果为
#include<iostream>
#include<string>
using namespace std;
struct inflatable {
char name[20];
double money;
};
int main()
{
int donators;
cout << "Please enter the number of donators:";
cin >> donators;
inflatable* donation = new inflatable[donators];
for (int i = 0;i < donators;i++) {
cout << "Please enter the name of donator:";
cin >> donation[i].name;
cin.get();
cout << "Please enter the money of donation:";
cin >> donation[i].money;
cin.get();
}
cout << "Grand Patrons:" << endl;
int k = 0;
int j = 0;
for (int i = 0;i <donators;i++) {
if (donation[i].money > 10000) {
cout << donation[i].name << endl;
k++;
}
}
if(k==0)
cout << "none"<<endl;
cout << "Patrons:" << endl;
for (int i= 0;i <donators;i++) {
if (donation[i].money <= 10000) {
cout << donation[i].name << endl;
j++;
}
}
if (j==0)
cout << "none"<<endl;
system("pause");
return 0;
}
输出结果
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word;
int vowels = 0;
int consonants = 0;
int others = 0;
cout << "Enter words (q to quit):" << endl;
while (cin >> word && word != "q") {
if (isalpha(word[0])) {
if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
vowels++;
else
consonants++;
}
else
others++;
}
cout << vowels << " words beginning with vowels" << endl;
cout << consonants << " words beginning with consonants" << endl;
cout << others << " others" << endl;
system("pause");
return 0;
}
输出结果