1.不接受任何参数的cin.get( )成员函数返回输入中的下一个字符,也就是说,可以这样使用它:
ch = cin.get() ;
该函数的工作方式与C语言中的getchar( )相似,将字符编码作为int值返回;而cin.get(ch)返回一个对象,而不是读取的字符。
2.当要写条件表达式时:
if (myNumber == 3)
有些程序员为了以防万一自己出现这样的错误:
if (myNumber = 3)
这种错误编译器回报出警告,为了能让编译器直接报错以便自己能更容易发现,干脆这么写:
if (3 == myNumber)
3.下面这种情况:
int n ;
cin >> n ;
如果用户输入一个单词,而不是一个数字。将发生4种情况:
(1)n的值保持不变
(2)不匹配的输入将被留在输入队列中
(3)cin对象中的一个错误标记被设置
(4)对cin方法的调用将返回false (如果被转换为bool类型)
第六章编程题:
(1)
#include
#include
using namespace std ;
char ch ;
int main()
{
while ((ch = cin.get()) != '@')
{
if (!(ch >= '0' && ch <= '9'))
{
if (ch >= 'A' && ch <= 'Z')
{
cout << char(tolower(ch)) ;
continue ;
}
else if (ch >= 'a' && ch <= 'z')
{
cout << char(toupper(ch)) ;
continue ;
}
cout << ch ;
}
}
}
(2)
第一种解法:
#include
using namespace std ;
const int ArSize = 10 ;
int main()
{
double array[ArSize] = {0} ;
double donation, sum = 0 ;
int count = 0 ;
while (count < ArSize && cin >> donation)
{
array[count ++] = donation ;
sum += donation ;
}
cout << "The Averge = " << sum / count << endl ;
int more = 0 ;
double average = sum / count ;
for (int i = 0; i < count; i++)
if (array[i] > average) more ++ ;
cout << more << " members beyond the average.\n" ;
}
第二种解法:
#include
#include
using namespace std ;
const int ArSize = 10 ;
int main()
{
array<double, ArSize> array ;
double donation ;
double average, sum = 0 ;
int count = 0 ;
while (count < ArSize && cin >> donation)
{
array[count++] = donation ;
sum += donation ;
}
average = sum / count ;
cout << "The average = " << average << endl ;
int more = 0 ;
for (int i=0; i<count; i++)
if (array[i] > average) more++ ;
cout << more << " numbers beyond the average.\n" ;
return 0 ;
}
(3)
#include
using namespace std ;
void showmenu() ;
int main()
{
showmenu() ;
char ch ;
while(cin >> ch && ch != '@')
{
switch (ch)
{
case 'c' :
case 'C' : cout << "A maple is a carnivore.\n" ;
break ;
case 'p' :
case 'P' : cout << "A maple is a pianist.\n" ;
break ;
case 't' :
case 'T' : cout << "A maple is a tree.\n" ;
break ;
case 'g' :
case 'G' : cout << "A maple is a game.\n" ;
break ;
default : cout << "Please enter a c, p, t, or g: " ;
continue ;
}
showmenu() ;
}
}
void showmenu()
{
cout << "Please enter one of the following choices:\n"
"c) carnivore p) pianist\n"
"t) tree g) game\n" ;
}
(4)
#include
using namespace std ;
const int strsize = 50 ;
struct bop {
char fullname[strsize] ;
char title[strsize] ;
char bopname[strsize] ;
int preference ;
} ;
const int Members = 5 ;
void showmenu() ;
void Print_by_name(bop * bopmembers) ;
void Print_by_title(bop * bopmembers) ;
void Print_by_bopname(bop * bopmembers) ;
void Print_by_preference(bop * bopmembers) ;
int main()
{
bop bopmembers[Members] = {
{
"Wimp Macho" ,
"Java programmer" ,
"ooop" ,
0
} ,
{
"Raki Rhodes" ,
"Junior Programmer" ,
"uiwieqw" ,
1
} ,
{
"Celia Laiter" ,
"C programmer" ,
"MIPS" ,
2
} ,
{
"Hoppy Hipman" ,
"Analyst Trainee" ,
"rtrer" ,
1
} ,
{
"Pat Hand" ,
"PHP programmer" ,
"LOOPY" ,
2
}
} ;
char choice ;
showmenu() ;
cout << "Enter your choice: " ;
while (cin >> choice)
{
switch (choice)
{
case 'a' :
case 'A' : Print_by_name(bopmembers) ;
break ;
case 'b' :
case 'B' : Print_by_title(bopmembers) ;
break ;
case 'c' :
case 'C' : Print_by_bopname(bopmembers) ;
break ;
case 'd' :
case 'D' : Print_by_preference(bopmembers) ;
break ;
case 'q' :
case 'Q' : goto END ;
default : goto NEXT ;
}
NEXT : cout << "Next choice: " ;
}
END : cout << "Bye!\n" ;
}
void showmenu()
{
cout << "Benevolent Order of Programmers Report\n"
"a. display by name b. display by title\n"
"c. display by bopname d. display by preference\n"
"q. quit\n" ;
}
void Print_by_name(bop * bopmembers)
{
for (int i=0; i<Members; i++)
cout << bopmembers[i].fullname << endl ;
}
void Print_by_title(bop * bopmembers)
{
for (int i=0; i<Members; i++)
cout << bopmembers[i].title << endl ;
}
void Print_by_bopname(bop * bopmembers)
{
for (int i=0; i<Members; i++)
cout << bopmembers[i].bopname << endl ;
}
void Print_by_preference(bop * bopmembers)
{
for (int i=0; i<Members; i++)
{
if (bopmembers[i].preference == 0)
cout << bopmembers[i].fullname << endl ;
else if (bopmembers[i].preference == 1)
cout << bopmembers[i].title << endl ;
else if (bopmembers[i].preference == 2)
cout << bopmembers[i].bopname << endl ;
}
}
(5)
#include
using namespace std ;
void Calculate (double income) ;
int main()
{
double income ;
double sum = 0 ;
cout << "Please enter you income: " ;
while(cin >> income)
{
if (income < 0) break ;
Calculate (income) ;
sum = 0 ;
cout << "Please enter you income: " ;
}
cout << "Done." << endl ;
return 0 ;
}
void Calculate (double income)
{
double sum = 0 ;
if (income <= 5000 )
sum += income * 0.00 ;
else {
sum += 5000 * 0.00 ;
income -= 5000 ;
if (income <= 10000) sum += income * 0.10 ;
else
{
sum += 10000 * 0.10 ;
income -= 10000 ;
if (income <= 20000) sum += income * 0.15 ;
else
{
sum += 20000 * 0.15 ;
income -= 20000 ;
sum += income * 0.20 ;
}
}
}
cout << "Your income tax = " << sum << " tvarps." << endl ;
}
(6)
#include
using namespace std ;
const int NameSize = 30 ;
struct Contributor
{
char name[NameSize] ;
double money ;
} ;
int population ;
int main()
{
cout << "How many people contirbute: " ;
cin >> population ;
cin.get() ;
Contributor * volunteer = new Contributor[population] ;
cout << "Enter their name and money:\n" ;
for (int i=0; i<population; i++)
{
cin.get(volunteer[i].name, NameSize).get() ;
cin >> volunteer[i].money ;
cin.get() ;
}
cout << "Grand Patrons:\n" ;
for (int i=0; i<population; i++)
{
if (volunteer[i].money > 10000)
{
cout << volunteer[i].name << " "
<< volunteer[i].money << endl ;
volunteer[i].money = 0 ;
}
}
cout << "Patrons:\n" ;
for (int i=0; i<population; i++)
if (volunteer[i].money )
cout << volunteer[i].name << " "
<< volunteer[i].money << endl ;
delete [] volunteer ;
}
(7)
#include
#include
#include
using namespace std ;
string vocabulary ;
int main()
{
int vowels = 0, consonants = 0, other = 0 ;
cout << "Enter words (q to quit):\n" ;
while (cin >> vocabulary && vocabulary != "q")
{
if( !isalpha(vocabulary[0]) )
{
other++ ;
continue ;
}
switch(vocabulary[0])
{
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'a' :
case 'e' :
case 'i' :
case 'o' : vowels ++ ; break ;
default : consonants ++ ; break ;
}
}
cout << vowels << " words beginning with vowels\n" ;
cout << consonants << " words beginning with consonants\n" ;
cout << other << " others\n" ;
}