练习一:
#include<iostream>
#include<cctype> //字符库函数
using namespace std;
//const int SIZE=1000; //最大容量
int main()
{
char ch;
cout<<"Please enter: ";
while(cin.get(ch) && ch != '@') //判断输入是否结束
{
if(islower(ch)) //小变大
ch=toupper(ch);
else if(isupper(ch)) //大变小
ch=tolower(ch);
else if(isdigit(ch)) //忽略数字回到循环
continue;
cout<<ch;
}
cout<<endl;
return 0;
}
练习二:
#include<iostream>
using namespace std;
const int SIZE=10;
int main()
{
double donation[SIZE];
int i=0;
double sum=0; //total value
cout<<"Please enter donation value: ";
while(cin>>donation[i] && i<SIZE) //Determine whether to stop input
{
sum+=donation[i];
++i;
/*if(i<SIZE)
continue;
else
break;*/
}
double ave=(sum/i); //average value
int j=0;
int acount=0;
while(j<i) //caculate greater than the average numbers
{
if(donation[j]>ave)
++acount;
++j;
continue;
}
cout<<"Average value is: "<<ave<<endl;
cout<<"Is greater than the average number of: "<<acount<<endl;
return 0;
}
练习三:
#include<iostream>
using namespace std;
void showmenu();
int main()
{
showmenu();
char ch;
while(cin>>ch)
{
switch(ch)
{
case 'c' : cout<<"Lion is a canivore."<<endl; //lion:狮子
break;
case 'p' : cout<<"Chopin is a pianist."<<endl; //pianist:钢琴家
break;
case 't' : cout<<"A maple is a tree."<<endl;
break;
case 'g' : cout<<"I like game."<<endl;
break;
default : cout<<"Please enter a c, p, t, or g: ";
continue;
}
}
return 0;
}
void showmenu()
{
cout<<"Please enter one of the following choices: \n"
"c) carnivore p) painist\n" //c-食肉动物,p-钢琴家
"t) tree g)game\n";
}
练习四:
#include<iostream>
using namespace std;
const int strsize=20;
struct bop
{
char fullname[strsize]; //real name
char title[strsize]; //job title
char bopname[strsize]; //secret BOP name
int perference; //0 = fullname, 1 = title, 2 = bopname
};
bop everyone[4]=
{
{"hanchengcheng","Porgrammer","haidao",1},
{"suoxuyang","Accounting","nangyang",0},
{"wuxubo","Chairman","daboluo",2},
{"weijianhang","Mistress","laoer",2}
};
void name();
void title();
void bopname();
void perference();
int main()
{
//输出提示
cout<<" Benevolent Order of Programmers Report\n"
<<" a. display by name b. display by title\n"
<<" c. display by bopname d. display by perference\n"
<<" q. quit"<<endl;
char ch;
cout<<"Enter your choice: ";
cin>>ch;
while(ch!='q') //判断是否离开
{
switch(ch)
{
case 'a' : name();
break;
case 'b' : title();
break;
case 'c' : bopname();
break;
case 'd' : perference();
break;
default : cout<<"It's not a choice!!!"<<endl;
}
cout<<"Next choice: ";
cin>>ch;
}
cout<<"Bye!!!"<<endl;
return 0;
}
void name()
{
for(int i=0; i<4; ++i)
cout<<everyone[i].fullname<<endl;
}
void title()
{
for(int i=0; i<4; ++i)
cout<<everyone[i].title<<endl;
}
void bopname()
{
for(int i=0; i<4; ++i)
cout<<everyone[i].bopname<<endl;
}
void perference()
{
for(int n=0; n<4; ++n) //根据个人爱好显示
{
switch(everyone[n].perference)
{
case 0 : cout<<everyone[n].fullname<<endl;
break;
case 1 : cout<<everyone[n].title<<endl;
break;
case 2 : cout<<everyone[n].bopname<<endl;
break;
}
}
}
练习五:
#include<iostream>
using namespace std;
int main()
{
double income; //收入
double tax; //所得税
cout<<"Please enter your incom: ";
while(cin>>income && income>=0) //判断是否正确输入且输入不小于零
{
if(income<=5000)
{
cout<<"Icome tax payable to zero tvarps."<<endl;
}
else if(income>5000 && income<=15000)
{
tax=(income-5000)*0.10;
cout<<"Income tax payable is "<<tax<<" tvarps."<<endl;
}
else if(income>15000 && income<=35000)
{
tax=10000*0.10+(income-15000)*0.15;
cout<<"Income tax payable is "<<tax<<" tvarps."<<endl;
}
else
{
tax=10000*0.10+20000*0.15+(income-35000)*0.20;
cout<<"Income tax payable is "<<tax<<" tvarps"<<endl;
}
cout<<"Please enter your income: ";
continue; //回到循环
}
return 0;
}
练习六:
#include<iostream>
using namespace std;
struct patron
{
char name[20];
double money;
};
int main()
{
int i; //捐款者人数
int j=0;
cout<<"Please enter the number of patrons: ";
cin>>i;
cin.get(); //读取换行符
patron * pa=new patron[i]; //创建动态结构数组
while(j<i) //循环输入
{
cout<<"Please enter name: ";
cin.getline(pa[j].name,20);
cout<<"Please enter the amount of money $ : ";
cin>>(pa[j].money);
cin.get(); //读取换行符
++j;
continue;
}
//显示Grand Patrons
int t=0;
cout<<"Grand Patrons:"<<endl;
for(int n=0; n<i; ++n)
{
if(pa[n].money>10000)
{
cout<<pa[n].name<<" "<<endl;
++t;
}
}
if(t==0) //判断有没有这种类别捐款者
cout<<"none\n";
//显示Patrons
int f=0;
cout<<"Patrons:\n";
for(int m=0; m<i; ++m)
{
if(pa[m].money<=10000)
{
cout<<pa[m].name<<" "<<endl;
++f;
}
}
if(f==0) //判断有没有这种类别捐款者
cout<<"none\n";
return 0;
}
练习七:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word;
int i=0;
int j=0;
int others=0;
//char ch;
cout<<"Enter words (q to quit): \n";
cin>>word;
while( word != "q") //word字符串,所以用双引号
{
if(isalpha(word[0]))
{
switch(word[0])
{
case 'a' : ++i;
break;
case 'A' : ++i;
break;
case 'e' : ++i;
break;
case 'E' : ++i;
break;
case 'i' : ++i;
break;
case 'I' : ++i;
break;
case 'o' : ++i;
break;
case 'O' : ++i;
break;
case 'u' : ++i;
break;
case 'U' : ++i;
break;
default : ++j;
}
}
else
++others;
cin>>word;
}
cout<<i<<" words beginning with vowels\n";
cout<<j<<" words beginning with consonants\n";
cout<<others<<" others"<<endl;
return 0;
}
练习八:
#include<iostream>
#include<fstream> //file I/O support
#include<cstdlib> //sport for exit()
using namespace std;
const int SIZE=60;
int main()
{
char filename[SIZE];
ifstream inFile;
cout<<"Enter name od data file: ";
cin.getline(filename,SIZE);
inFile.open(filename); //associate inFile with a file
if(!inFile.is_open()) //failed to open file
{
cout<<"Could not open the file\n"
<<"Program terminating.\n";
exit(EXIT_FAILURE); //函数exit()终止程序
}
char ch;
int i=0;
inFile>>ch;
while(inFile.good()) //while input good and not at EOF
{
++i;
inFile>>ch;
}
cout<<"一共包含"<<i<<"个字符"<<endl;
inFile.close();
return 0;
}
练习九:
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib> //sport for exit()
using namespace std;
struct patron
{
string name;
int money;
};
int main()
{
char filename[20];
ifstream inFile;
cout<<"Enter the fileanme: ";
cin.getline(filename,20);
inFile.open(filename);
if(!inFile.is_open()) //failed to open file
{
cout<<"FAIL"<<endl;
exit(EXIT_FAILURE);
}
int num; //捐款人数
inFile>>num;
patron * pa=new patron[num];
while(inFile.good()) //while input good and not at EOF
{
for(int i=0; i<num; i++) //读入
{
inFile.get();
getline(inFile,pa[i].name);
inFile>>pa[i].money;
}
}
if(inFile.eof())
cout<<"End of file reached.\n";
else if(inFile.fail())
cout<<"Input terminated by data mismatch.\n";
else
cout<<"Input terminated for unknown reason.\n";
inFile.close(); //关闭文件
//显示Grand Patrons
int t=0;
cout<<"Grand Patrons:"<<endl;
for(int n=0; n<num; n++)
{
if(pa[n].money>10000)
{
cout<<pa[n].name<<endl;
cout<<pa[n].money<<endl;
++t;
}
}
if(t==0) //判断有没有这种类别捐款者
cout<<"none\n";
//显示Patrons
int f=0;
cout<<"Patrons:\n";
for(int m=0; m<num; m++)
{
if(pa[m].money<=10000)
{
cout<<pa[m].name<<endl;
cout<<pa[m].money<<endl;
++f;
}
}
if(f==0) //判断有没有这种类别捐款者
cout<<"none\n";
return 0;
}