C++ Primer Plus (第6版)编程练习 代码-----第六章

1.
#include "stdafx.h"
#include<iostream>
#include<cctype>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
char temp;
char sin[20];
   
int i=0;
cout<<"请输入字符: "<<endl;
cin.getline(sin,20);
cout<<"输入的字符为: ";

for(i=0;i<20;i++)
{ 
if(sin[i]!='@')
{

  if(islower(sin[i]))
 {
      temp=toupper(sin[i]);
  cout<<temp;
     
 }
 else if (isupper(sin[i]))
 {
      temp=tolower(sin[i]);
  cout<<temp;
      
 }
 else


  {
temp=sin[i];
  }
}
else
{
break;
}


}
system("pause");
return 0;
}


2.

#include "stdafx.h"
#include<iostream>


using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{


 double donation[10];
 double sum=0;
 int i=0;
 for(i=0;i<10;i++)
 {   
cout<<"请输入第"<<i+1<<"个数字:";

if(cin>>(donation[i]))
{
 sum=sum+donation[i];  
}
else
{
           cin.clear();
  break;
}
  
 
 }
 if(i==0)
 {cout<<"未输入任何数字!";}
 else
 {
  cout<<i<<"个数字的平均值为:"<<sum/i<<endl;
 } 
system("pause");
return 0;
}


3.


char inp;
   cout<<"Please enter one of the following choices:"<<endl<< "c) carnivore             p) pianist"<<endl<<"t) tree                  g) game"<<endl;
   cin>>inp;
while(inp!=' ')
   {
  switch (inp)
  {
     case 'c':cout<<"A maple is a carnivore\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";break;
 default:cout<<"Please enter a c, p, t,or g:" ;
      }
cin>>inp;  
 
   
  }
   
system("pause");
return 0;



4.没看懂什么意思


5.

double salary;
    double tax;
cout<<"Enter your salary: ";
cin>>salary;
while((cin>>salary))
{
 if(salary>=0&&salary<=5000)
 {
  tax=5000*0.0;
 }
 else if (salary>=5001&&salary<=15000)
 {

    tax=(salary-5000)*0.10;
 }
else if (salary>=15001&&salary<=35000)
{

  tax=10000*0.10+(salary-10000)*0.15;
}
else if(salary>=35001)
{
  tax=10000*0.10+25000*0.15+(salary-35000)*0.20;
}
 cout<<"The TAX is : "<<tax<<endl;
 cin.get();
 cin>>salary;   
} 





system("pause");
return 0;

6.

using namespace std;
struct donation
{
char name[20];
double money;


};
int _tmain(int argc, _TCHAR* argv[])
{
donation *da=new donation;

int c1=0,c2=0;
int number;
    cout<<"请输入捐赠者数目:";
cin>>number;
for(int i=0;i<number;i++)
{
cin.get();
  cout<<"请输入第"<<i+1<<"个捐赠者的姓名: ";
  cin.getline(da[i].name,20);
  cout<<"请输入第"<<i+1<<"个捐赠者的金额: ";
  cin>>da[i].money;

}
cout<<"Patrons          "<<"name      "<<"   money   "<<endl;
for(int i=0;i<number;i++)
{

if(da[i].money>=10000)
    {
cout<<"Grand Patrons   "<<da[i].name<<"    "<<da[i].money<<endl;
  c1++;
}
else 
{
    cout<<"Patrons   "<<da[i].name<<"    "<<da[i].money<<endl;
c2++;
}

}
  if (c1==0)
  {
  cout<<cout<<"Grand Patrons            none"<<endl;
  }
if(c2==0)
  {
  cout<<cout<<" Patrons                  none"<<endl;
  }

system("pause");
delete da;
return 0;
}



7.


using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
char word[20];
int v=0,c=0,o=0;
cout<<"Enter words (q to quit):"<<endl;
cin>>word;
int l= strlen(word);
while(!(word[0]=='q'&&l==1))
{
if(isalpha(word[0]))
{

switch(word[0])
  { case 'A': v++;break;
    case 'a': v++;break;
case 'E': v++;break;
case 'e': v++;break;
case 'I': v++;break;
case 'i': v++;break;
    case 'O': v++;break;
case 'o': v++;break;
    case 'U': v++;break;
case 'u': v++;break;
default:c++;
  }
}
else
{
   o++;
}
 cin>>word;
 l=strlen(word);

}
cout<<v<<" words beginning with vowels "<<endl;
cout<<c<<" words beginning with consonants "<<endl;
cout<<o<<" others"<<endl;

system("pause");

return 0;
}




8.


using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
char ch;
int count =0;
ifstream inf;
inf.open("test.txt");
inf>>ch;
while(!(inf.eof()))
{

count++;
   cout<<ch<<endl;
inf>>ch;
}
inf.close();

cout<<"the file has "<<count<<" characters"<<endl;

system("pause");

return 0;
}



9.


#include "stdafx.h"
#include<iostream>
#include <string> 
#include<vector>
#include<fstream>
using namespace std;

 struct donation
 {
   char name[20];


   int money;
 
 };

int _tmain(int argc, _TCHAR* argv[])
{
   int number;
   donation *p=new donation[4];
   fstream inf;
   inf.open("test1.txt");
   if(!inf.is_open())
   {
     cout<<"open failed "<<endl;
   }


   inf>>number;
   
   cout<<"the donation number is : "<<number<<endl<<endl;
  while(!(inf.eof()))
 {
     inf.get();
   inf.getline(p->name,20); 
  /* cout<<p->name<<endl;*/
   inf>>p->money;
/*cout<<p->money<<endl;*/
 
p++;
 
  }
   p=p-4;
  cout<<"Grand Patrons"<<endl<<endl;
 
  for(int i=0;i<4;i++)
  {  if((p->money)>=10000)
      {
   cout<<p->name<<endl;
   cout<<p->money<<endl;
       
}
    p++;
  }
   p=p-4;
  cout<<endl<<endl<<endl<<"Patrons "<<endl<<endl;;
   for(int i=0;i<4;i++)
  {  if((p->money)<10000)
      {
   cout<<p->name<<endl;
   cout<<p->money<<endl;
       
}
    p++;
  }


    system("pause");  

return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值