#include <stdio.h>
#include <stdlib.h>
// 假设最多有 5 个选项
#define OPTION_COUNT 5
// 选项名称
char *options[OPTION_COUNT] = {“选项 1”, “选项 2”, “选项 3”, “选项 4”, “选项 5”};
// 每个选项的票数统计
int votes[OPTION_COUNT] = {0};
// 显示菜单
void showMenu() {
printf(“投票系统\n”);
for (int i = 0; i < OPTION_COUNT; i++) {
printf("%d. %s\n", i + 1, options[i]);
}
printf(“6. 查看投票结果\n”);
printf(“7. 退出\n”);
}
// 投票功能
void vote() {
int choice;
printf(“请选择您要投票的选项: “);
scanf(”%d”, &choice);
if (choice >= 1 && choice <= OPTION_COUNT) {
votes[choice - 1]++;
printf("投票成功!\n");
} else {
printf("无效的选项!\n");
}
}
// 查看投票结果
void viewResults() {
printf(“投票结果:\n”);
for (int i = 0; i < OPTION_COUNT; i++) {
printf("%s: %d 票\n", options[i], votes[i]);
}
}
int main() {
int choice;
do {
showMenu();
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
case 2:
case 3:
case 4:
case 5:
vote();
break;
case 6:
viewResults();
break;
case 7:
printf("退出程序\n");
break;
default:
printf("无效的选择,请重新输入\n");
}
} while (choice!= 7);
return 0;
}