以下答案本人在linux vscode中均已亲自测试编译通过,完美运行.
6.1
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
char ch;
while(cin.get(ch) && ch != '@')
{
if(!isdigit(ch)) //判断数字
cout.put(ch);
if(islower(ch)) //A:67,a:95 判断小写字母
cout.put(ch-32);
if(isupper(ch)) //判断大写字母
cout.put(ch+32);
}
}
6.2
#include<iostream>
#include<array>
using namespace std;
int main()
{
array<double,10> money;
int n = 0, _n = 0;
double sum = 0.0,ave = 0.0;
while(cin >> money[n])
{
sum += money[n];
n++;
}
ave = sum / n;
for(int i=0; i < n; i++)
{
if(money[i] > ave)
_n++;
}
cout << "平均值ave = " << ave << endl;
cout << "大于平均值的数 = " << _n << endl;
}
6.3
#include<iostream>
using namespace std;
int main()
{
char ch;
cout << "Please enter one of the following choices:" << endl;
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) game" << endl;
cout << "Please enter a c, p, t, or g:";
while(cin >> ch && ch != '#')
{
switch (ch)
{
case 'c':
cout << "A maple is a carnivore.";
break;
case 'p':
cout << "A maple is a pianist.";
break;
case 't':
cout << "A maple is a tree.";
break;
case 'g':
cout << "A maple is a game.";
break;
default:
cout << "Please enter a c, p, t, or g:";
break;
}
}
}
6.4
#include<iostream>
#include<string>
using namespace std;
struct bop
{
string fullname;
string title;
string bopname;
int preference;
};
int main()
{
char ch;
const int size = 2;
bop * BOP = new bop [size]
{
{"wu", "爱好者", "WHB", 1},{"li", "款热者", "LSO", 0}
};
cout << "Please enter one of the following choices:" << endl;
cout << "a.display by name b.display by title" << endl;
cout << "c.display by bopname d.display by preference" << endl;
cout << "q.quit" << endl;
cout << "Enter your choice:";
while(cin >> ch)
{
switch (ch)
{
case 'a':
for(int i=0; i < size; i++)
cout << BOP[i].fullname << endl;
break;
case 'b':
for(int i=0; i < size; i++)
cout << BOP[i].title << endl;
break;
case 'c':
for(int i=0; i < size; i++)
cout << BOP[i].bopname << endl;
break;
case 'd':
for(int i=0; i < size; i++)
{
if(0 == BOP[i].preference)
cout << BOP[i].fullname << endl;
if(1 == BOP[i].preference)
cout << BOP[i].title << endl;
if(2 == BOP[i].preference)
cout << BOP[i].bopname << endl;
}
break;
case 'q':
cout << "Bye!" << endl;
break;
default:
cout << "Enter your choice:";
break;
}
if(ch == 'q')
break;
cout << "Enter your choice:";
}
}
6.5
#include<iostream>
#include<array>
using namespace std;
const double factor1 = 0.10;
const double factor2 = 0.15;
const double factor3 = 0.20;
int main()
{
double tax = 0.0, money;
cout << "请输入您的收入:";
while(cin >> money && money >= 0)
{
if(money <= 5000)
tax = 0;
else if(money > 5000 && money <= 15000)
tax = 5000 * 0.0 + (money - 5000) * factor1;
else if(money > 15000 && money <= 35000)
tax = 5000 * 0.0 + 10000 * factor1 + (money - 15000) * factor2;
else
tax = 5000 * 0.0 + 10000 * factor1 + 20000 * factor2 + (money - 35000) * factor3;
cout << "所得税是:" << tax << endl;
cout << "请输入您的收入:";
}
}
6.6
#include<iostream>
#include<string>
using namespace std;
struct information
{
string name;
double money;
};
int main()
{
int n;
cout << "请输入捐献者的人数:";
cin >> n;
information * inf = new information [n];
for(int i=0; i < n; i++)
{
cout << "请输入捐献者姓名:";
cin >> inf[i].name;
cout << "请输入捐献金额:";
cin >> inf[i].money;
}
cout << "The following patrons are Grand Patrons" << endl;
for(int i=0; i < n; i++)
{
cout << "Patron姓名:" << inf[i].name << "捐献金额:" << inf[i].money << endl;;
}
}
6.7
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string word;
int ny = 0, nf =0, nq = 0;
cout << "Enter words (q to quit) :" << endl;
cin >> word;
while(word != "q")
{
if(isalpha(word[0]))
{
if(word[0] == 'A' || word[0] == 'E' || word[0] == 'I' ||word[0] == 'O' || word[0] == 'U' ||
word[0] == 'a' || word[0] == 'e' || word[0] == 'i' ||word[0] == 'o' || word[0] == 'u' )
ny++;
else
nf++;
}
else
nq++;
cin >> word;
}
cout << ny << " words beginning with vowels\n";
cout << nf << " words beginning with consonants\n";
cout << nq << " others\n";
}
6.8 注意:自己新建一个合适的文本文档
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string name;
ifstream infile;
cout << "Enter the name of data file:";
getline(cin,name);
infile.open(name);
if(!infile.is_open())
{
cout << "警告!!打开文件失败!!!\n";
exit(EXIT_FAILURE);
}
char ch;
int count = 0;
while(infile.good())
{
infile.get(ch);
++count;
cout << ch;
}
if(infile.eof())
cout << "\n文件读取完成!!!" << endl;
if(0 == count)
cout << "此文件中没有任何数据!!!" << endl;
else
cout << "此文件中共有" << count << "个字符" << endl;
infile.close();
}
6.9 注意:自己新建一个合适的文本文档
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct information
{
string Pname;
double money;
};
int main()
{
string name;
ifstream infile;
cout << "Enter the name of data file:";
getline(cin,name);
infile.open(name);
if(!infile.is_open())
{
cout << "警告!!打开文件失败!!!\n";
exit(EXIT_FAILURE);
}
int n;
infile >> n;
information * inf = new information [n];
while(infile.good())
{
int i;
infile.get(); //换行符问题!!!注意!!!
getline(infile,inf[i].Pname);
infile >> inf[i].money;
i++;
}
if(infile.eof())
cout << "文件读取完成!!!" << endl;
cout << "一共有" << n << "名partron" << endl;
for(int i=0; i < n; i++)
{
cout << left << "Patron姓名:" << inf[i].Pname << "\t捐献金额:" << inf[i].money << endl;;
}
infile.close();
}