![](https://i-blog.csdnimg.cn/blog_migrate/46b0c617942926fb723db1aeb0f8a9df.png)
#include <iostream>
#include <cctype>
int main() {
char input;
std::cout << "Enter the text (type '@' to stop):\n";
while (std::cin.get(input) && input != '@') {
if (std::isalpha(input) || input == ' ') {
if (std::islower(input))
input = std::toupper(input);
else
input = std::tolower(input);
std::cout << input;
}
}
std::cout << "\n";
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/c11ed659aaa3659b04c325b3bf890935.png)
#include <iostream>
#include <array>
#include <cctype>
int main() {
const int MAX_SIZE = 10;
std::array<double, MAX_SIZE> donations;
double sum = 0.0;
int count = 0;
std::cout << "Enter up to 10 donation values (non-numeric input to end):\n";
// 读取输入,直到达到最大限制或遇到非数字输入
for (int i = 0; i < MAX_SIZE; ++i) {
double input;
if (!(std::cin >> input)) {
break; // 遇到非数字输入,退出循环
}
donations[i] = input;
sum += input;
count++;
}
// 计算平均值
double average = count > 0 ? sum / count : 0.0;
// 统计大于平均值的数字个数
int aboveAverageCount = 0;
for (int i = 0; i < count; ++i) {
if (donations[i] > average) {
aboveAverageCount++;
}
}
std::cout << "Average donation: " << average << std::endl;
std::cout << "Number of donations above average: " << aboveAverageCount << std::endl;
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/caed3d6f4eb411324e138749613c5ec4.png)
#include <iostream>
#include <string>
using namespace std;
int main() {
char choice;
do {
// 显示菜单
cout << "Please enter one of the following choices:" << endl;
cout << "p) pianist" << endl;
cout << "c) carnivore" << endl;
cout << "t) tree" << endl;
cout << "g) game" << endl;
// 获取用户输入
cout << "Please enter a valid choice (p, c, t, or g): ";
cin >> choice;
// 根据用户的选择执行操作
switch (choice) {
case 'p':
cout << "You chose pianist" << endl;
break;
case 'c':
cout << "You chose carnivore" << endl;
break;
case 't':
cout << "You chose tree" << endl;
break;
case 'g':
cout << "You chose game" << endl;
break;
default:
// 处理无效输入
cout << "Invalid choice. Please enter a valid letter (p, c, t, or g)." << endl;
}
} while (choice != 'p' && choice != 'c' && choice != 't' && choice != 'g');
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/f3712ea18dfb8880c5d3930bda1a2c94.png)
#include <iostream>
#include <limits>
#include <string>
using namespace std;
const int strsize = 20;
const int MAX_MEMBERS = 5;
enum Preference { FULLNAME, TITLE, BOPNAME };
struct BOPMember {
string fullname; // 真实姓名
string title; // 头衔
string bopname; // BOP姓名
Preference preference; // 偏好,FULLNAME = 0,TITLE = 1,BOPNAME = 2
};
void showMenu();
void displayName(const BOPMember Bopmembers[MAX_MEMBERS]);
void displayTitle(const BOPMember Bopmembers[MAX_MEMBERS]);
void displayBopname(const BOPMember Bopmembers[MAX_MEMBERS]);
void displayPreference(const BOPMember Bopmembers[MAX_MEMBERS]);
int main() {
// 初始化成员结构
BOPMember Bopmembers[MAX_MEMBERS]{
{"Wimp Macho", "Junior Programmer", "LOOPY", FULLNAME},
{"Raki Rhodes", "Analyst Trainee", "MIPS", TITLE},
{"Celia Laiter", "Senior Programmer", "HOPPY", BOPNAME},
{"Hoppy Hipman", "Analyst Trainee", "GOOPY", TITLE},
{"Pat Hand", "Programmer", "PATAPOST", FULLNAME}
};
char choice;
do {
cout << endl; // 添加额外的换行符
showMenu();
cin >> choice;
switch (choice) {
case 'a':
displayName(Bopmembers);
break;
case 'b':
displayTitle(Bopmembers);
break;
case 'c':
displayBopname(Bopmembers);
break;
case 'd':
displayPreference(Bopmembers);
break;
case 'q':
cout << "Bye!\n";
break;
default:
cout << "That's not a choice.\n\n"; // 显示错误消息后换行
continue; // 继续下一次循环,显示菜单
}
// 清除输入缓冲区
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while (choice != 'q');
return 0;
}
void showMenu()
{
cout << "Benevolent Order of Programmers Report\n"
"a) display by name b) display by title\n"
"c) display by bopname d) display by preference\n"
"q) quit\n";
cout << "Enter your choice: ";
}
void displayName(const BOPMember Bopmembers[MAX_MEMBERS]) {
for (int i = 0; i < MAX_MEMBERS; ++i)
cout << Bopmembers[i].fullname << endl;
}
void displayTitle(const BOPMember Bopmembers[MAX_MEMBERS]) {
for (int i = 0; i < MAX_MEMBERS; ++i)
cout << Bopmembers[i].title << endl;
}
void displayBopname(const BOPMember Bopmembers[MAX_MEMBERS]) {
for (int i = 0; i < MAX_MEMBERS; ++i)
cout << Bopmembers[i].bopname << endl;
}
void displayPreference(const BOPMember Bopmembers[MAX_MEMBERS]) {
for (int i = 0; i < MAX_MEMBERS; ++i) {
switch (Bopmembers[i].preference) {
case FULLNAME:
cout << Bopmembers[i].fullname << endl;
break;
case TITLE:
cout << Bopmembers[i].title << endl;
break;
case BOPNAME:
cout << Bopmembers[i].bopname << endl;
break;
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/b17745ee5e1f0f7a575c1cfd95f45791.png)
![](https://i-blog.csdnimg.cn/blog_migrate/96ac7e9b2e333148271dbbf2f9c5f454.png)
#include <iostream>
using namespace std;
int main() {
const double RATE1 = 0.10;
const double RATE2 = 0.15;
const double RATE3 = 0.20;
const double BASE1 = 5000; // 第一档税收的基数
const double BASE2 = 15000; // 第二档税收的基数
const double BASE3 = 35000; // 第三档税收的基数
double income;
double tax = 0.0;
cout << "Enter your income in tvarps (negative number or non-numeric input to quit): ";
while (cin >> income && income >= 0) {
if (income <= BASE1)
tax = 0.0;
else if (income <= BASE2)
tax = (income - BASE1) * RATE1;
else if (income <= BASE3)
tax = (BASE2 - BASE1) * RATE1 + (income - BASE2) * RATE2;
else
tax = (BASE2 - BASE1) * RATE1 + (BASE3 - BASE2) * RATE2 + (income - BASE3) * RATE3;
cout << "Your tax is " << tax << " tvarps." << endl;
cout << "Enter your income in tvarps (negative number or non-numeric input to quit): ";
}
cout << "Bye!\n";
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/9b3153ac06c41b07e06e57d83336de6a.png)
#include <iostream>
#include <string>
using namespace std;
struct Donation {
string name;
double amount;
};
int main() {
int numDonors;
cout << "请输入捐献者的数目: ";
cin >> numDonors;
// 动态分配存储捐款者信息的数组
Donation* donors = new Donation[numDonors];
// 输入每个捐款者的姓名和款项
for (int i = 0; i < numDonors; i++) {
cout << "请输入捐献者的姓名: ";
cin >> donors[i].name;
cout << "请输入捐献的款项: ";
cin >> donors[i].amount;
}
bool grandPatronExist = false;
// 显示捐款超过 10000 的捐款者的姓名及其捐款数额
cout << "\nGrand Patrons:" << endl;
for (int i = 0; i < numDonors; i++) {
if (donors[i].amount > 10000) {
grandPatronExist = true;
cout << donors[i].name << ": " << donors[i].amount << " tvarps" << endl;
}
}
if (!grandPatronExist) {
cout << "None" << endl;
}
// 显示其他的捐款者
bool patronExist = false;
cout << "\nPatrons:" << endl;
for (int i = 0; i < numDonors; i++) {
if (donors[i].amount <= 10000) {
patronExist = true;
cout << donors[i].name << ": " << donors[i].amount << " tvarps" << endl;
}
}
if (!patronExist) {
cout << "None" << endl;
}
delete[] donors;
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/c398acf7dc324285ba8b7e109caa54e2.png)
#include <iostream>
#include <cctype> // 提供isalpha() 函数
#include <string>
using namespace std;
bool isVowel(char c) {
c = tolower(c);
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
int main() {
string word;
int vowels = 0, consonants = 0, others = 0;
cout << "Enter words (q to quit):" << endl;
while (cin >> word && word != "q") {
if (!isalpha(word[0])) { // 检查是否以字母打头
others++;
continue;
}
if (isVowel(word[0]))
vowels++;
else
consonants++;
}
cout << vowels << " words beginning with vowels" << endl;
cout << consonants << " words beginning with consonants" << endl;
cout << others << " others" << endl;
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/f3211063e6750e1b098daeeff6706653.png)
#include <iostream>
#include <fstream> // 文件输入输出流
using namespace std;
int main() {
char filename[100];
cout << "Enter the filename: ";
cin.getline(filename, 100); // 获取文件名
ifstream file(filename); // 打开文件
if (!file.is_open()) { // 检查文件是否成功打开
cout << "Error opening file. Check if the file exists or if you have permission to read it." << endl;
return 1; // 退出程序
}
int count = 0;
char ch;
while (file.get(ch)) { // 逐个字符地读取文件
count++;
}
cout << "Number of characters in the file: " << count << endl;
file.close(); // 关闭文件
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/cd485c020ca2eca627cedacba350df91.png)
#include <iostream>
#include <fstream> // 文件输入输出流
#include <vector>
#include <string>
using namespace std;
struct Donation {
string name;
double amount;
};
int main() {
char filename[100];
cout << "Enter the filename: ";
cin.getline(filename, 100); // 获取文件名
ifstream file(filename); // 打开文件
if (!file.is_open()) { // 检查文件是否成功打开
cout << "Error opening file. Check if the file exists or if you have permission to read it." << endl;
return 1; // 退出程序
}
int numDonors;
file >> numDonors; // 读取捐款人数
vector<Donation> donations; // 用于存储捐款信息的向量
// 读取捐款人信息
for (int i = 0; i < numDonors; ++i) {
Donation donor;
file.ignore(); // 忽略换行符
getline(file, donor.name); // 读取捐款人姓名
file >> donor.amount; // 读取捐款数额
donations.push_back(donor); // 将捐款人信息添加到向量中
}
bool grandPatronsExist = false;
bool patronsExist = false;
// 显示捐款超过 10000 的捐款者的姓名及其捐款数额
cout << "Grand Patrons:" << endl;
for (const Donation& donor : donations) {
if (donor.amount > 10000) {
cout << donor.name << ": " << donor.amount << endl;
grandPatronsExist = true;
}
}
if (!grandPatronsExist) {
cout << "none" << endl;
}
// 显示其他捐款者的姓名及其捐款数额
cout << "Patrons:" << endl;
for (const Donation& donor : donations) {
if (donor.amount <= 10000) {
cout << donor.name << ": " << donor.amount << endl;
patronsExist = true;
}
}
if (!patronsExist) {
cout << "none" << endl;
}
file.close(); // 关闭文件
return 0;
}