6.1
该代码我有个疑问:为啥空格无法输出呢???(貌似是空格使用 cin 是无法输入的)
<span style="font-size:18px;">#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
while (cin >> ch && ch != '@')
{
if (isdigit(ch))
continue;
if (isupper(ch))
cout << (char)tolower(ch);
else if (islower(ch))
cout << (char)toupper(ch);
else
cout << ch;
}
cout << endl;
system("pause"); //使用 cin.get() 的缺陷是:不知道有多少空白,所以不知道需要设置多少个 cin.get(),使用该指令更直接。
return 0;
}</span>
6.2
<span style="font-size:18px;">#include <iostream>
#include <array>
using namespace std;
const int MaxSize = 10;
int main()
{
array<double, MaxSize> donation;
int i = 0, count = 0;
double sum = 0.0;
double ave;
int num_big_than_ave = 0;
cout << "Please enter numbers: (others to terminate enter)" << endl;
while (i < MaxSize && cin >> donation[i])
{
++count;
sum += donation[i];
++i;
}
ave = sum / count;
for (int k = 0; k < count;++k)
if (donation[k]>ave)
++num_big_than_ave;
cout << "You have entered " << count << " numbers.\n";
cout << "Sum = " << sum << endl;
cout << "Average = " << ave << endl;
cout << "There are " << num_big_than_ave << " numbers which are bigger than average.\n";
system("pause");
return 0;
}</span>
6.3
<span style="font-size:18px;">#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch;
string maple = "";
cout << "Please enter one of the following choices: " << endl;
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) game" << endl;
//该程序中不区分大小写。
while (maple == "")
{
cout << "Please enter a c, p, t or g: ";
cin >> ch;
switch (ch)
{
case 'c':
case 'C': maple = "carnivore"; break;
case 'p':
case 'P': maple = "pianist"; break;
case 't':
case 'T': maple = "tree"; break;
case 'g':
case 'G': maple = "game"; break;
default: ;
}
}
cout << "A maple is a " << maple << ".\n";
system("pause");
return 0;
}</span>
6.4
感觉代码有点冗余,哪位高人能指点一下进行优化?
<span style="font-size:18px;">#include <iostream>
using namespace std;
const int strsize = 20;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main()
{
bop bops[5] = {
{ "Wimp Macho", "Student", "Wimp", 0},
{ "Raki Rhodes", "Junior Programmer", "Raki", 1},
{ "Celia Laiter", "Student", "MIPS", 2},
{ "Hoppy Hipman", "Analyst Trainee", "HOPPY", 1},
{ "Pat Hand", "Student", "LOOPY", 2}
};
cout << "Benevolent Order of Programmers Report:" << 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;
char ch;
cout << "Enter your choice: ";
cin >> ch;
while ('q' != ch)
{
switch (ch)
{
case 'a':
for (int i = 0; i < 5; ++i)
cout << bops[i].fullname << endl;
break;
case 'b':
for (int i = 0; i < 5; ++i)
cout << bops[i].title << endl;
break;
case 'c':
for (int i = 0; i < 5; ++i)
cout << bops[i].bopname << endl;
break;
case 'd':
for (int i = 0; i < 5; ++i)
{
if (0 == bops[i].preference)
cout << bops[i].fullname << endl;
else if (1 == bops[i].preference)
cout << bops[i].title << endl;
else if (2 == bops[i].preference)
cout << bops[i].bopname << endl;
else
cout << "Wrong preference! Wrong record!" << endl;
}
break;
}
cout << "Next choice: ";
cin >> ch;
}
cout << "Goodbye!" << endl;
system("pause");
return 0;
}</span>
6.5
#include <iostream>
using namespace std;
int main()
{
int income;
double tax;
cout << "Please enter your income: ";
while ((cin >> income) && income >= 0)
{
if (income <= 5000)
tax = 0.0;
else if (income <= 15000)
tax = (double)(income - 5000)*0.10;
else if (income <= 35000)
tax = 1000 + (double)(income - 15000)*0.15;
else
tax = 4000 + (double)(income - 35000)*0.20;
cout << "The tax is: " << tax << endl;
cout << "Please enter again: ";
cin.clear(); //与前面的整型共同作用忽略了小数,且不让小数点等影响后续的输入。
cin.sync();
}
cout << "Goodbye!" << endl;
system("pause");
return 0;
}
6.6
<pre name="code" class="cpp">#include <iostream>
#include <string>
using namespace std;
struct donate
{
string name;
double fund;
};
int main()
{
int size;
int i, k = 0;
cout << "How many patrons there are? ";
(cin >> size).get();
donate* pat = new donate[size];
cout << "Please enter the information of the patrons: " << endl;
for (i = 0; i < size; ++i)
{
cout << "Patron " << i+1 << ":\n";
getline(cin, pat[i].name);
(cin >> pat[i].fund).get();
}
cout << "Grand Patrons: " << endl;
for (i = 0; i < size;++i)
if (pat[i].fund > 10000)
{
cout << pat[i].name << "\t" << pat[i].fund << endl;
++k;
}
if (0 == k)
cout << "None!" << endl;
cout << "Other Patrons: " << endl;
for (i = 0; i < size; ++i)
if (pat[i].fund <= 10000)
{
cout << pat[i].name << "\t" << pat[i].fund << endl;
++k;
}
if (0 == k)
cout << "None!" << endl;
system("pause");
delete[]pat;
return 0;
}
6.7
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str;
int vowels = 0, consonants = 0, others = 0;
cout << "Enter words (q to quit):" << endl;
while ((cin >> str) && !(str.size() == 1 && str[0] == 'q'))
{
if (!isalpha(str[0]))
++others;
else if (str[0] == 'a'
|| str[0] == 'e'
|| str[0] == 'i'
|| str[0] == 'o'
|| str[0] == 'u')
++vowels;
else
++consonants;
}
cout << vowels << " words beginning with vowels" << endl;
cout << consonants << " words beginning with consonants" << endl;
cout << others << " others" << endl;
system("pause");
return 0;
}
6.8
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
string filename;
char c;
int count = 0;
ifstream readFile;
cout << "Please enter the filename: ";
getline(cin, filename);
readFile.open(filename);
if (!readFile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
while (readFile >> c)
{
++count;
cout << c << " ";
}
cout << endl;
if (readFile.eof())
cout << "End of file reached.\n";
else if (readFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated by unknown reason.\n";
if (0 == count)
cout << "No data processed.\n";
else
cout << "There are " << count << " characters in the file " << filename << ".\n";
system("pause");
return 0;
}
6.9
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
struct donate
{
string name;
double fund;
};
int main()
{
string filename;
cout << "Please enter the filename: ";
cin >> filename;
ifstream infile;
infile.open(filename);
if (!infile.is_open())
{
cout << "Could not open the file " << filename << ".\n";
cout << "Program terminated.\n";
exit(EXIT_FAILURE);
}
string temp;
int size;
int i, k = 0;
(infile >> temp).get();
size = atoi(temp.c_str());
donate* pat = new donate[size];
for (i = 0; i < size; ++i)
{
if (!infile.good())
break;
getline(infile, pat[i].name);
(infile >> temp).get();
pat[i].fund = atoi(temp.c_str());
}
if (infile.good())
cout << "Get the enough data!\n";
else if (infile.eof())
cout << "Reach the end of the file.\n";
else if (infile.fail())
cout << "Read file error!\n";
else
cout << "Unknown error!\n";
infile.close();
cout << "Grand Patrons: " << endl;
for (i = 0; i < size; ++i)
if (pat[i].fund > 10000)
{
cout << pat[i].name << "\t" << pat[i].fund << endl;
++k;
}
if (0 == k)
cout << "None!" << endl;
cout << "Other Patrons: " << endl;
for (i = 0; i < size; ++i)
if (pat[i].fund <= 10000)
{
cout << pat[i].name << "\t" << pat[i].fund << endl;
++k;
}
if (0 == k)
cout << "None!" << endl;
system("pause");
delete[]pat;
return 0;
}