📅  最后修改于: 2023-12-03 15:25:51.209000             🧑  作者: Mango
如今,随着科技的发展和人们对民主、公平的追求,投票系统在社会中已经变得越来越普遍。投票系统的菜单驱动程序就是一个用于控制投票系统的程序,它可以通过菜单选项实现投票、查询投票结果等基本功能。
投票系统菜单驱动程序是一个功能比较简单的程序,使用C语言来实现是比较合适的选择。C语言编译速度快、运行效率高,可以满足程序运行效率的要求。
我们可以使用命令行界面来实现菜单驱动程序,这样可以充分利用系统资源,也使得程序易于维护和扩展。菜单驱动程序的主要界面包括以下几个部分:
菜单驱动程序的核心是菜单选项,通过菜单选项,用户可以进行各种操作。下面简单列举一下各个功能模块的菜单选项。
投票模块菜单选项:
投票模块:
1. 投票
2. 返回主菜单
查询模块菜单选项:
查询模块:
1. 统计投票结果
2. 返回主菜单
管理模块菜单选项:
管理模块:
1. 添加候选人信息
2. 删除候选人信息
3. 修改候选人信息
4. 返回主菜单
下面是一个简单的C语言代码示例,用于实现投票系统菜单驱动程序的主要功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义候选人结构体
typedef struct Candidate {
int id; // 候选人ID
char name[20]; // 候选人姓名
int votes; // 候选人得票数
struct Candidate *next; // 指向下一个候选人的指针
} Candidate;
// 声明候选人链表的头指针
Candidate *head = NULL;
// 打印欢迎菜单
void print_menu() {
printf("\n\n");
printf("-------------------\n");
printf(" welcome to vote system\n");
printf("-------------------\n");
printf("1. voting\n");
printf("2. query vote result\n");
printf("3. candidate management\n");
printf("4. exit\n");
}
// 添加候选人信息
void add_candidate() {
Candidate *new_candidate = (Candidate *) malloc(sizeof(Candidate));
printf("enter candidate ID:\n");
scanf("%d", &(new_candidate->id));
printf("enter candidate name:\n");
scanf("%s", new_candidate->name);
new_candidate->votes = 0;
new_candidate->next = NULL;
if (head == NULL) {
head = new_candidate;
} else {
Candidate *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_candidate;
}
printf("candidate added successfully.\n");
}
// 删除候选人信息
void delete_candidate() {
Candidate *p = head;
if (p == NULL) {
printf("there is no candidate recorded.\n");
} else {
int candidate_id;
printf("enter candidate ID to delete:\n");
scanf("%d", &candidate_id);
if (p->id == candidate_id) {
Candidate *temp = head;
head = head->next;
free(temp);
} else {
while (p->next != NULL && p->next->id != candidate_id) {
p = p->next;
}
if (p->next != NULL) {
Candidate *temp = p->next;
p->next = temp->next;
free(temp);
} else {
printf("candidate not found.\n");
}
}
printf("candidate deleted successfully.\n");
}
}
// 修改候选人信息
void modify_candidate() {
Candidate *p = head;
if (p == NULL) {
printf("there is no candidate recorded.\n");
} else {
int candidate_id;
printf("enter candidate ID to modify:\n");
scanf("%d", &candidate_id);
while (p != NULL && p->id != candidate_id) {
p = p->next;
}
if (p != NULL) {
printf("new name for candidate %d (%s):\n", p->id, p->name);
scanf("%s", p->name);
printf("candidate information updated successfully.\n");
} else {
printf("candidate not found.\n");
}
}
}
// 输出所有候选人信息
void print_candidates() {
Candidate *p = head;
if (p == NULL) {
printf("there is no candidate recorded.\n");
} else {
printf("candidate ID | candidate name |votes\n");
while (p != NULL) {
printf("%-12d|%-17s|%d\n", p->id, p->name, p->votes);
p = p->next;
}
}
}
// 投票操作
void vote() {
int candidate_id, vote_num;
printf("enter candidate ID to vote for:\n");
scanf("%d", &candidate_id);
printf("enter number of votes:\n");
scanf("%d", &vote_num);
Candidate *p = head;
while (p != NULL && p->id != candidate_id) {
p = p->next;
}
if (p != NULL) {
p->votes += vote_num;
printf("vote for candidate %d (%s) succeeded.\n", p->id, p->name);
} else {
printf("candidate not found.\n");
}
}
// 查询投票结果
void query_results() {
print_candidates();
}
// 释放候选人链表内存
void free_candidates() {
while (head != NULL) {
Candidate *p = head;
head = head->next;
free(p);
}
}
int main() {
int user_choice = 0;
while (user_choice != 4) {
print_menu();
printf("enter your choice:\n");
scanf("%d", &user_choice);
switch (user_choice) {
case 1:
vote();
break;
case 2:
query_results();
break;
case 3:
int manage_choice;
printf("choose management option:\n");
printf("1. add candidate\n");
printf("2. delete candidate\n");
printf("3. modify candidate\n");
printf("4. view all candidates\n");
scanf("%d", &manage_choice);
switch (manage_choice) {
case 1:
add_candidate();
break;
case 2:
delete_candidate();
break;
case 3:
modify_candidate();
break;
case 4:
print_candidates();
break;
default:
printf("invalid option selected.\n");
}
break;
case 4:
printf("thanks for using vote system.\n");
break;
default:
printf("invalid option selected.\n");
}
}
// 释放候选人链表内存
free_candidates();
return 0;
}
返回的markdown格式如上所示。