原创 C++ Primer Plus 第六章 课后编程练习题1-9

//1题
#include
#include
int main()
{
using namespace std;
int i = 0;
char a[100];
char ch;
cout <<“请随意输入不超过100的字符,输入@退出。” << endl;
cin.get(ch);
while (ch != ‘@’ && i < 100)
{
if (islower(ch))
ch = toupper(ch);
else if (isupper(ch))
ch = tolower(ch);
if (!isdigit(ch))
{
a[i] = ch;
++i;
}
cin.get(ch);
}
cout << a << endl;

return 0;

}

//2题
#include
#include
const int Max = 10;
int main ()
{
using namespace std;
int count = 0;
int j = 0;
double total = 0, average;
array<double, Max> dao;
cout <<"请输入donation值,最多10个: “;
for (int i = 0; i < Max; ++i)
{
cin >> dao[i];
if (cin.fail())
break;
++count;
}
for (int i = count-1; i >= 0; i–)
total += dao[i];
average = total / count;
for (int i = count - 1; i >= 0; i–)
if(average < dao[i])
++j;
cout <<“donation值: “;
for (int i = 0; i < count; i++)
cout << dao[i] <<” “;
cout <<”\n平均数 = " <<average
<<” 比平均数大的有”<< j <<“个数。\n”;

return 0;

}

//3题
#include
int main()
{
using namespace std;
char ch;
cout <<“Please enter one of the following choices :\n”;
cout <<“c) carnivore p) pianist\n”;
cout <<“t) tree g) game\n”;
while (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”;
break;
default : cout <<"Please enter a c, p, t, or g: ";
continue;
}
break;
}
return 0;
}

//4题
#include
const int strsize = 40;
const int size = 5;
struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void option(void);
void Show_fullname(struct bop *p);
void Show_title(struct bop *p);
void Show_bopname(struct bop *p);
void Show_preference(struct bop *p);
int main()
{
using namespace std;

bop bopmember[size] = {
    {"WimpMacho","Junior Programmer", "ANT", 0},
    {"Raki Rhodes", "Junior Programmer", "BOMBER", 1},
    {"Celia Laiter", "Analyst Trainee", "MIPS", 2},
    {"Hoppy Hipman","Analyst Trainee", "CLOWN", 1},
    {"Pat Hand", "Analyst Trainee", "LOOPY", 2}
};
struct bop *fp = bopmember;
char ch;
option();
while(cin >> ch && ch != 'q')
{
    switch(ch)
    {
        case 'a': Show_fullname(fp);
            cout <<"Next choice: ";
            break;
        case 'b': Show_title(fp);
            cout <<"Next choice: ";
            break;
        case 'c': Show_bopname(fp);
            cout <<"Next choice: ";
            break;
        case 'd': Show_preference(fp);
            cout <<"Next choice: ";
            break;  //错误输入时提示输入正确的选择并返回循环开始
        default: cout <<"Please enter the correct choice.\n";
            option();
            continue;
    }
}
cout <<"Bye!\n";

return 0;

}

void option(void)
{
std::cout <<“Benevolent Order of Programmers Report\n”;
std::cout <<“a. display by name\t\t\t b. display by title\n”;
std::cout <<“c. display by bopname\t\t d.display by preference\n”;
std::cout <<“q. quit\n”;
std::cout <<"Enter your choice: ";

}
//打印结构数组中存储的fullname
void Show_fullname(struct bop *p)
{
for (int i = 0; i < size; i++)
std::cout <<(p+i)->fullname << std::endl;
}
//打印结构数组中存储的title
void Show_title(struct bop *p)
{
for (int i = 0; i < size; i++)
std::cout <<(p+i)->title << std::endl;
}

//打印结构数组中存储的bopname
void Show_bopname(struct bop *p)
{
for (int i = 0; i < size; i++)
std::cout <<(p+i)->bopname << std::endl;
}

//打印结构数组中个人喜欢的称呼
void Show_preference(struct bop *p)
{
for (int i = 0; i < size; i++)
{

      if(0 == (p+i)->preference)
        std::cout <<(p+i)->fullname << std::endl;
    else if(1 == (p+i)->preference)
        std::cout <<(p+i)->title << std::endl;
    else
        std::cout <<(p+i)->bopname << std::endl;
}

}

//5题
#include
const float Tax_one = 0.10;
const float Tax_two = 0.15;
const float Tax_three = 0.20;
const int Taxone = 5000;
const int Taxtwo = 15000;
const int Taxthree = 35000;
int main()
{
using namespace std;
int tvarps = 0;
int tax;
cout <<"请输入你的收入,输入负数或非数字退出: ";
while (cin >> tvarps && tvarps >=0)
{
if (tvarps <= Taxone)
tax = 0;
else if (tvarps <= Taxtwo)
tax = 0 + (tvarps - Taxone) * Tax_one;
else if (tvarps <= Taxthree)
tax = 0 + (tvarps - Taxtwo) * Tax_two + 10000 * Tax_one;
else
tax = 0 + (tvarps - Taxthree) * Tax_three + 10000 * Tax_one + 20000 * Tax_two;
cout <<"你需要缴纳的所得税为: " << tax << endl;
}
cout <<“再见!\n”;
return 0;
}

//6题
#include
#include
#include
struct member {
std::string name;
double money;
};
int main()
{
using namespace std;
int number, patrons = 0, ordinary = 0;
cout <<"Please enter the number of members of the legal rights team: ";
cin >> number;
cin.get();
vector num(number);
for (int i = 0; i < number; i++)
{
cout <<"Please enter member name: “;
getline(cin, num[i].name);
cout <<“Please enter " << num[i].name <<” donation amount:”;
cin >>num[i].money;
cin.get();
}
for (int i = number-1; i>=0; i–)
{
if (num[i].money > 10000)
{
cout <<“Patrons " << num[i].name <<” donation: " << num[i].money << endl;
++patrons;
}
else
{
cout <<num[i].name << " donation: "<< num[i].money << endl;
++ordinary;
}
}

if (0 == patrons)
    cout <<"None with a donation of more than 10000!" << endl;
if (0 == ordinary)
    cout <<"None with a donation below 10000!" << endl;

return 0;

}

//7题
//a e i o u 是元音字母
#include
#include
#include
int main()
{
using namespace std;
string word;

int num1 = 0;
int num2 = 0;
int num3 = 0;
cout <<"Enter words (q to quit):" << endl;
while(cin >> word && word != "q")
{
    if (isalpha(word[0]))
    {
        if(word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
            ++num1;
        else
            ++num2;
    }
    else
        ++num3;
}
cout << num1 <<" words beginning with vowels" << endl;
cout << num2 <<" words beginning with consonants" << endl;
cout << num3 <<" others" << endl;

return 0;

}

//8题
#include
#include
#include
const int size = 60;
int main()
{
using namespace std;
char ch;
int character = 0;
char fname[size];
ifstream inFile;
cout <<"Enter name of data file: ";
cin.getline(fname, size);
inFile.open(fname);
if(!inFile.is_open())
{
cout <<“Could not open the file " << fname << endl;
cout <<“Program terminating.\n”;
exit(10);
}
while(inFile >> ch && ch !=EOF)
{
++character;
}
cout <<“There are " <<character <<” characters in the file.\n”;
inFile.close();

return 0;

}

//9题
#include
#include
#include
#include
#include
const int size = 60;
struct member {
std::string name;
double money;
};
int main()
{
using namespace std;
ifstream inFile;
char filename[60];
cout <<“Please enter the name of the file you want to open:\n”;
cin.getline(filename, size);
inFile.open(filename);
if (!inFile.is_open())
{
cout <<"Could not open the file " << filename << endl;
cout <<“Program terminating.\n”;
exit(3);
}
int number, patrons = 0, ordinary = 0;

inFile >> number;
inFile.get();
vector<member> num(number);
for (int i = 0; i < number; i++)
{
    getline(inFile, num[i].name);
    inFile >> num[i].money;
    inFile.get();
}
inFile.close();
for (int i = number-1; i>=0; i--)
{
    if (num[i].money > 10000)
    {
        cout <<"Patrons " << num[i].name <<" donation: " << num[i].money << endl;
        ++patrons;
    }
    else
    {
        cout <<num[i].name << " donation: "<< num[i].money << endl;
        ++ordinary;
    }
}

if (0 == patrons)
    cout <<"None with a donation of more than 10000!" << endl;
if (0 == ordinary)
    cout <<"None with a donation below 10000!" << endl;

return 0;

}

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/103846410
今日推荐