以下是一个使用const文字实现C++投票程序的示例代码,其中包括中文注释。
#include
using namespace std;
int main() {
const int VOTE_YES = 1; // 投赞成票
const int VOTE_NO = 2; // 投反对票
const int VOTE_ABSTAIN = 3; // 弃权
int yesVotes = 0; // 赞成票数
int noVotes = 0; // 反对票数
int abstainVotes = 0; // 弃权票数
int vote; // 清单中的投票
cout << "欢迎来到此次会议!" << endl;
do {
cout << "请投票,赞成输入'1”,反对输入'2”,弃权输入'3”,输入'0'表示退出:" << endl;
cin >> vote;
switch (vote) {
case VOTE_YES:
yesVotes++;
break;
case VOTE_NO:
noVotes++;
break;
case VOTE_ABSTAIN:
abstainVotes++;
break;
case 0:
break;
default:
cout << "无效选项,请重新输入:" << endl;
break;
}
} while (vote != 0);
// 结果
cout << "本次投票完成。本次共有 " << yesVotes + noVotes + abstainVotes << " 人参加了投票,其中: " << endl;
cout << "赞成票 " << yesVotes << " 张。" << endl;
cout << "反对票 " << noVotes << " 张。" << endl;
cout << "弃权 " << abstainVotes << " 张。" << endl;
// 决策
if (yesVotes > noVotes) {
cout << "决议通过,欢迎参加下一次会议。" << endl;
} else if (noVotes > yesVotes) {
cout << "决议未通过,欢迎参加下一次会议。" << endl;
} else {
cout << "决议未通过,欢迎参加下一次会议。" << endl;
}
return 0;
}