C++编程
第六章编程练习
1、
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
char ch;
cin.get(ch);
while(ch!='@')
{
if(isdigit(ch))
cin.get(ch);
else
{
if(islower(ch))
ch=toupper(ch);
else
ch=tolower(ch);
cout<<ch;
cin.get(ch);
}
}
system("pause");
return 0;
}
2、
#include <iostream>
int main()
{
using namespace std;
double sum=0;
double average=0;
double num[10];
int i=0,total=0;
double tmp;
while(cin>>tmp&&i<10)
{
num[i]=tmp;
sum+=num[i];
++i;
}
if(i!=0)
average=sum/i;
for(int j=0;j<i;++j)
if(num[j]>average)
++total;
cout<<"这些数字的平均值为"<<average<<endl;
cout<<"并且共有"<<total<<"个数字大于平均值。\n";
system("pause");
return 0;
}
3、
#include <iostream>
int main()
{
using namespace std;
cout<<"Please enter one of the following choices:\n"
<<"c)carnivore p)pianist\n"
<<"t)tree g)game\nf\n";
cout<<"Please enter a c, p, t, or g: ";
char ch;
cin>>ch;
while(ch!='c'&&ch!='p'&&ch!='t'&&ch!='g')
{
cout<<"Please enter a c, p, t, or g: ";
cin>>ch;
}
switch(ch)
{
case 'c':
cout<<"A maple is a tree.\n";
break;
case 'p':
cout<<"A maple is a pianist.\n";
break;
case 't':
cout<<"A maple is a tree.\n";
break;
case 'g':
cout<<"A maple is a game.\n";
}
system("pause");
return 0;
}