1. 键盘输入,显示数字除外,大小写转换
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
char ch;
while ((ch = getchar()) != '@')
{
if (isdigit(ch))
continue;
else if (islower(ch))
putchar(toupper(ch));
else if (isupper(ch))
putchar(tolower(ch));
else
putchar(ch);
}
return 0;
}
2. 读取10个donation,输出平均值,大于平均值的个数
#include <iostream>
const int number = 10;
int main()
{
using namespace std;
double donations[number];
int count = 0;
double sum = 0;
while (count < number && cin >> donations[count])
{
sum += donations[count];
count++;
}
double avg = sum / count;
int max_avg = 0;
for (int i = 0; i < count; i++)
{
if (donations[i] > avg)
max_avg++;
}
cout << "sum is " << sum << endl;
cout << "avg is " << avg << endl;
cout << "donation > avg is " << max_avg << endl;
return 0;
}
3. 按照输入打印
#include <iostream>
int main()
{
using namespace std;
char ch;
string word{};
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).get())
{
switch(ch)
{
case 'c':
word = "carnivore";
break;
case 'p':
word = "pianist";
break;
case 't':
word = "tree";
break;
case 'g':
word = "game";
break;
}
if (word.length())
{
cout << "A maple is a " << word << endl;
break;
}
else
cout << "Please enter a c, p, t, or g: ";
}
return 0;
}
4. 显示BOP大会各个信息
#include <iostream>
const int strsize = 50;
const int man = 5;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
int main()
{
using namespace std;
bop bops[man] = {
{"Wimp Macho", "worker", "ladi", 0},
{"Raki Rhodes", "Junior Programer", "baby", 1},
{"Celia Laiter", "rendor", "MIPS", 2},
{"Hoppy Hipman", "Analyst Trainee", "tra", 1},
{"Pat Hand", "seller", "LOOPY", 2},
};
char ch;
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;
cout << "Enter your choice: ";
while ((cin >> ch).get())
{
if (ch == 'q')
{
cout << "Bye!" << endl;
break;
}
switch (ch)
{
case 'a':
for (int i = 0; i < man; i++)
cout << bops[i].fullname << endl;
break;
case 'b':
for (int i = 0; i < man; i++)
cout << bops[i].title << endl;
break;
case 'c':
for (int i = 0; i < man; i++)
cout << bops[i].bopname << endl;
break;
case 'd':
for (int i = 0; i < man; i++)
{
switch (bops[i].preference)
{
case 0:
cout << bops[i].fullname << endl;
break;
case 1:
cout << bops[i].title << endl;
break;
case 2:
cout << bops[i].bopname << endl;
break;
}
}
}
cout << "Next choice: ";
}
return 0;
}
5. 计算税额
#include <iostream>
int main()
{
using namespace std;
long num;
double rax = 0;
cout << "Please enter you income: ";
while (cin >> num)
{
if (num < 0)
{
cout << "your income is <0." << endl;
break;
}
if (num <= 5000)
rax = 0;
else if (num > 5000 && num <= 15000)
rax = (num - 5000) * 0.1;
else if (num > 15000 && num <= 35000)
rax = 10000 * 0.1 + (num - 15000) * 0.15;
else
rax = 10000 * 0.1 + 20000 * 0.15 + (num - 35000) * 0.2;
cout << "Your rax is " << rax << endl;
cout << "Please enter you income: ";
}
return 0;
}
6. 分类捐赠者
#include <iostream>
#include <string>
using namespace std;
struct donation
{
string name;
double money;
};
int main()
{
int num;
cout << "Please enter the num you input: ";
(cin >> num).get();
donation* dns = new donation[num];
for (int i = 0; i < num; i++)
{
cout << "The donations #" << i + 1 << ":" << endl;
cout << "Enter the name: ";
getline(cin, dns[i].name);
cout << "Enter the money: ";
(cin >> dns[i].money).get();
}
bool nongrand = true;
bool nonpatrons = true;
bool* flag = new bool[num];
cout << "Grand Patrons:" << endl;
for (int i = 0; i < num; i++)
{
if (dns[i].money > 10000)
{
nongrand = false;
cout << dns[i].name << endl;
flag[i] = false;
}
else
flag[i] = true;
}
if (nongrand)
cout << "none" << endl;
cout << "Patrons:" << endl;
for (int i = 0; i < num; i++)
{
if (flag[i])
{
cout << dns[i].name << endl;
nonpatrons = false;
}
}
if (nonpatrons)
cout << "none" << endl;
delete[] dns;
return 0;
}
7. 统计元音字母开头的单词还有其他的
#include <iostream>
#include <string>
using namespace std;
int main()
{
string words;
cout << "Enter words (q to quit):" << endl;
int vowels_num = 0;
int consonants_num = 0;
int others = 0;
cin >> words;
while (words != "q")
{
if (isalpha(words[0]))
{
char ch = tolower(words[0]);
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
consonants_num++;
break;
default:
vowels_num++;
break;
}
}
else
{
others++;
}
cin >> words;
}
cout << vowels_num << " words beginning with vowels" << endl;
cout << consonants_num << " words beginning with consonants" << endl;
cout << others << " others" << endl;
return 0;
}
8. 打开文件统计总共有多少个字符
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
string filename = "donation.txt";
char ch;
infile.open(filename.c_str());
if (!infile.is_open())
{
cout << "Could not open the file " << filename << endl;
exit(EXIT_FAILURE);
}
int sum = 0;
while (infile >> ch)
{
sum++;
}
cout << "This file contain " << sum << " char." << endl;
infile.close();
return 0;
}
9. 对第六题从文件中读取
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct donation
{
string name;
double money;
};
int main()
{
ifstream infile;
string filename = "donation.txt";
infile.open(filename.c_str());
if (!infile.is_open())
{
cout << "Could not open the file " << filename << endl;
exit(EXIT_FAILURE);
}
int num;
donation* dns = NULL;
if ((infile >> num).get())
{
dns = new donation[num];
}
if (dns)
{
for (int i = 0; i < num; i++)
{
getline(infile, dns[i].name);
(infile >> dns[i].money).get();
}
}
if (dns)
{
bool nongrand = true;
bool nonpatrons = true;
bool* flag = new bool[num];
cout << "Grand Patrons:" << endl;
for (int i = 0; i < num; i++)
{
if (dns[i].money > 10000)
{
nongrand = false;
cout << dns[i].name << endl;
flag[i] = false;
}
else
flag[i] = true;
}
if (nongrand)
cout << "none" << endl;
cout << "Patrons:" << endl;
for (int i = 0; i < num; i++)
{
if (flag[i])
{
cout << dns[i].name << endl;
nonpatrons = false;
}
}
if (nonpatrons)
cout << "none" << endl;
delete[] dns;
}
infile.close();
return 0;
}