3 习题1
新年就要到了,假定你要负责筹备一个晚会,晚会上要有抽奖活动,奖品由你设计,比如一等奖是一个双肩背的包,二等奖是一件普通的背心,三等奖是一个笔记本。奖项的人数、钱数都由你来筹划,原则是注重实效和节约。请编写一个程序计算得筹集多少钱。当然,这是你第一次编写这类程序,可以借鉴之前的程序。
//************************************************
//* Source Name: ChapterThree_ExerciseOne.cpp
//* Founction : Calculate how much money raised in the party
//* Author : Skyera
//* Create Time : 2025-7-16
//* Modify :
//* Modify Time:
//************************************************
#include <iostream>
#include <iomanip>
#include <string>
#include <unordered_map> struct Prize{std::string name;double price;
};int main(){// Define the prize and unit pricestd::unordered_map<std::string, Prize> prizePrices = {{"一等奖", {"双肩背包", 200}},{"二等奖", {"普通背心", 80}},{"三等奖", {"笔记本", 20}},{"纪念奖", {"徽章", 5}}};// Define the quantities of prizesstd::unordered_map<std::string, int> prizeQuantities = {{"一等奖", 2},{"二等奖", 5},{"三等奖", 10},{"纪念奖", 30}};double total = 0;std::cout << "抽奖活动奖品预算明细: " << std::endl;std::cout << "--------------------------------------------------" << std::endl;std::cout << std::left << std::setw(8) << "奖项"<< std::setw(12) << "奖品名称"<< std::setw(8) << "单价"<< std::setw(8) << "数量" << "小计" << std::endl;std::cout << "---------------------------------------------------" << std::endl;for(const auto& level : {"一等奖","二等奖","三等奖","纪念奖"}){const auto& info = prizePrices[level]; double price = info.price;int quantity = prizeQuantities[level];double subtotal = price * quantity;total += subtotal;std::cout << std::left << std::setw(8) << level<< std::setw(12) << info.name<< std::setw(8) << std::fixed << std::setprecision(2) << price<< std::setw(8) << quantity<< subtotal << std::endl;}std::cout << "---------------------------------------------------" << std::endl;std::cout << std::left << std::setw(28) << "总计"<< std::fixed << std::setprecision(2) << total << std::endl;std::cout << "---------------------------------------------------" << std::endl;std::cout << "\n本次抽奖活动预计需要筹集资金: " << total << " 元" << std::endl;std::cout << "备注: 此预算仅包含奖品费用, 未包含活动组织等其他费用" << std::endl;}