编程练习——分支语句和逻辑运算符

文章展示了C++编程中的基本操作,包括字符转换、数组使用、文件输入输出、菜单设计、数据结构(如数组和向量)以及简单的税收计算示例。
摘要由CSDN通过智能技术生成

#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;
}

#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;
}

#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;
}

#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;
        }
    }
}

#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;
}

#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;
}

#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;
}

#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;
}

#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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值