c++ primer plus 6th第六章答案

//1

#include    <iostream>
#include <cctype>


int main()
{
using namespace std;
char ch;
while(cin.get(ch)&&ch!='@')
{
if(isdigit(ch))
continue;
if(islower(ch))
cout<<(char)toupper(ch);
else if(isupper(ch))
cout<<(char)tolower(ch);
else 
cout<<ch;
}
return 0;
}

//2

#include    <iostream>


int main()
{
using namespace std;
const int SIZE=10;
double donation[SIZE];
int i,num=0,values=0;
double avg=0.0;
while(num<10&&cin>>donation[num])
{
values+=donation[num];
num++;
}
avg=values/num;
cout<<"the num is greater than avg :"<<endl;
for(i=0;i<num;i++)
if(donation[i]>avg)
cout<<donation[i]<<" ";
cout<<"Avg is :"<<avg<<endl;
return 0;
}

//3

#include    <iostream>


int main()
{
using namespace std;
char ch;
bool done=true;
cout<<"Please enter one of the following choices :"<<endl;
cout<<"c) carnivore\tp) pianist"<<endl;
cout<<"t) tree     \tg) game"<<endl;

while(done&&cin>>ch)
{
switch(ch)
{
case 'c':
cout<<"A maple is a carnivore"<<endl;
done=false;
break;
case 'p':
cout<<"A maple is a pianist"<<endl;
done=false;
break;
case 't':
cout<<"A maple is a tree"<<endl;
done=false;
break;
case 'g':
cout<<"A maple is a game"<<endl;
done=false;
break;
default:
cout<<"Please enter a c,p,t or g:";
}
}
return 0;
}

//4

#include    <iostream>


int main()
{
using namespace std;
const int strsize=20;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop k[4]={
{"A0","B0","C0",1},
{"A2","B2","C2",2},
{"A3","B3","C3",1},
{"A4","B4","C4",0}};
cout<<"Benevolent Order of Programmers Report"<<endl;
cout<<"a.display by name   \tb.display by title"<<endl;
cout<<"c.display by bopname\td,display by preference"<<endl;
cout<<"q.qiut"<<endl;
char ch;
bool done=true;
cout<<"Enter your choice:";
while(done&&cin>>ch)
{
bool symble=true;
switch(ch)
{
case 'a':
for(int i=0;i<4;i++)
cout<<k[i].fullname<<endl;
break;
case 'b':
for(int i=0;i<4;i++)
cout<<k[i].title<<endl;
break;
case 'c':
for(int i=0;i<4;i++)
cout<<k[i].bopname<<endl;
break;
case 'd':
for(int i=0;i<4;i++)
{
switch(k[i].preference)
{
case 0:
cout<<k[i].fullname<<endl;
break;
case 1:
cout<<k[i].title<<endl;
break;
case 2:
cout<<k[i].bopname<<endl;
break;
default:
cout<<"false1";
}
}
break;
case 'q':
cout<<"Bye!"<<endl;
done=false;
break;
default:
symble=false;
cout<<"Please enter a a,b,c or d:";
}
if(symble)
cout<<"Next choice :";
}
return 0;
}

//5

#include    <iostream>


int main()
{
using namespace std;
double tax=0,salary=0;
cout<<"Enter your salary:";
cin>>salary;
if(salary>5000&&salary<15001)
tax=(salary-5000)*0.1;
else if(salary<35001)
tax=(salary-15000)*0.15+10000*0.1;
else
tax=(salary-35000)*0.2+10000*0.1+20000*0.15;
cout<<"your tax is:"<<tax<<endl;
return 0;
}

//6

#include    <iostream>
#include <string>
int main()
{
using namespace std;
struct Juan
{
string name;
double k;
};
int num=0,i=0;
cout<<"Enter the num of juankuangren:";
cin>>num;
const int count=num;
Juan *member=new Juan[count];
while(num--)
{
cin.get();
cout<<"Member:#"<<i+1<<endl<<"Enter the name:";
getline(cin,member[i].name);
cout<<"Enter the k:";
cin>>member[i].k;
i++;
}
cout<<"Grand Patrons:"<<endl;
num=0;
for(i=0;i<count;i++)
if(member[i].k>10000)
{
cout<<member[i].name<<endl;
num++;
}
if(num==0)
cout<<"none"<<endl;
num=0;
cout<<"Patrons:"<<endl;
for(i=0;i<count;i++)
if(member[i].k<10000)
{
num++;
cout<<member[i].name<<endl;
}
if(num==0)
cout<<"none"<<endl;
return 0;
}

//7

#include    <iostream>
#include <string>
#include <cctype>


int main()
{
using namespace std;
string s;
int cr=0,cr2=0,cr3=0;
cout<<"Enter words(q to quit):"<<endl;
while(cin>>s)
{
if(s=="q")
break;
if(isalpha(*s.begin()))
switch(*s.begin())
{
case 'a':
case 'e':
case 'i':
case 'u':
case 'o':
cr++;
break;
default:
cr2++;
}
else
cr3++;
}
cout<<cr<<" words beginning with vowels."<<endl;
cout<<cr2<<" words beginning with consonants."<<endl;
cout<<cr3<<" others."<<endl;
return 0;
}

//8

#include    <iostream>
#include <string>
#include <fstream>


int main()
{
using namespace std;
ifstream ifs;
ifs.open("1.txt");
string s;
while(ifs>>s)
cout<<s<<endl;
return 0;
}

//9

#include    <iostream>
#include <fstream>
#include <string>


int main()
{
using namespace std;
struct Juan
{
string name;
double k;
};
int num=0,i=0;
ifstream ifs;
ifs.open("2.txt");
ifs>>num;
const int count=num;
Juan *member=new Juan[count];
while(num--)
{
ifs.get();
getline(ifs,member[i].name);
ifs>>member[i].k;
i++;
}
cout<<"Grand Patrons:"<<endl;
num=0;
for(i=0;i<count;i++)
if(member[i].k>10000)
{
cout<<member[i].name<<endl;
num++;
}
if(num==0)
cout<<"none"<<endl;
num=0;
cout<<"Patrons:"<<endl;
for(i=0;i<count;i++)
if(member[i].k<10000)
{
num++;
cout<<member[i].name<<endl;
}
if(num==0)
cout<<"none"<<endl;
return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章