#include <stdio.h> #include <string.h> #define N 10 typedef struct candidate { char name[N]; int count; }CAND; int main() { CAND cand[N] = {{"li",0},{"zhang",0},{"wang",0}}; int i,j; char name[N]; int wrong=0; int flag; for(i=0;i<N;i++) { flag = 0; printf("Input vote %d:",i+1); scanf("%s",name); for(j=0;j<3;j++) { if(strcasecmp(cand[j].name,name) == 0) { cand[j].count++; flag = 1; } } if(flag == 0) { wrong++; } } printf("Election results:\n"); for(i=0;i<3;i++) { printf("%8s:%d\n",cand[i].name,cand[i].count); } printf("Wrong election:%d\n",wrong); }
时间: 2024-05-02 09:20:49 浏览: 11
这是一个用 C 语言编写的投票程序,它使用了结构体来存储候选人的姓名和得票数。程序中先定义了一个结构体 CAND,包含了候选人的姓名和得票数。然后在 main 函数中定义了一个 CAND 类型的数组 cand,用来存储三个候选人的信息。程序使用了一个循环来进行投票,共投了10次。对于每一次投票,程序会输入投票者选择的候选人姓名,并通过一个嵌套的循环来检查所输入的姓名是否是某个候选人的姓名。如果是,则将该候选人的得票数加1。如果不是,则表示该票无效,记录错误投票数。投票结束后,程序会输出各个候选人的得票数以及错误投票数。
相关问题
五、投票程序 1、需求分析 实现键盘输入投票人数和投票结果,进行统计时实现排序功能,输出结果。 2、总体设计 该程序可以分为数据输入部分和统计部分。 3、详细设计 采用结构体: Struct { Char code; //代号 Int score; //得分 }Candidates[N]; //候选人结构体数组 这里#define N 3; 采用二维数组,则行方向对应一张投票,列方向对应各候选人得分。 Candidate[M][N]; (1)输入部分 Char tmp[3]; //存放投票的中间变量 输入投票人数n; For(i=0;i<n;++i) //投票 { Scanf(“%s”,tmp); If(tmp[0]==tmp[1]||tmp[1]==tmp[2]||tmp[0]==tmp[2]) Printf(”该票无效!”); Else { Tmp[0]号候选人加5分; Tmp[1]号候选人加3分; Tmp[2]号候选人加2分; } } (2)统计部分 对候选人按照他们的得分排序(冒泡、插入、选择等),然后比较前两名得分是否相同。如果相同,则显示“重新投票”;否则显示三个候选者得分并输出“祝贺X当选冠军”,X为最高分候选者。
好的,根据你的需求分析和总体设计,我可以为你提供一份详细设计的代码示例,如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
#define M 100
typedef struct candidate {
char code;
int score;
} Candidate;
int main() {
Candidate candidates[N] = {
{'A', 0},
{'B', 0},
{'C', 0}
};
int votes[M][N] = {0};
int n, i, j;
// 输入部分
printf("Please enter the number of voters: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Please enter the vote for voter %d (e.g. ABC): ", i + 1);
char vote[4];
scanf("%s", vote);
if (strlen(vote) != 3 || vote[0] == vote[1] || vote[1] == vote[2] || vote[0] == vote[2]) {
printf("Invalid vote!\n");
i--;
continue;
}
for (j = 0; j < 3; j++) {
switch (vote[j]) {
case 'A':
candidates[0].score += 5;
break;
case 'B':
candidates[1].score += 3;
break;
case 'C':
candidates[2].score += 2;
break;
default:
break;
}
votes[i][j] = candidates[vote[j] - 'A'].score;
}
}
// 统计部分
for (i = 0; i < N - 1; i++) {
for (j = 0; j < N - i - 1; j++) {
if (candidates[j].score < candidates[j + 1].score) {
Candidate temp = candidates[j];
candidates[j] = candidates[j + 1];
candidates[j + 1] = temp;
}
}
}
if (candidates[0].score == candidates[1].score) {
printf("Tie! Please vote again.\n");
} else {
printf("Results:\n");
for (i = 0; i < N; i++) {
printf("%c: %d votes, %.2f%%\n", candidates[i].code, candidates[i].score, (float) candidates[i].score / (float) (n * 10) * 100.0);
}
printf("Congratulations to %c for winning!\n", candidates[0].code);
}
printf("\nVoting results:\n");
for (i = 0; i < n; i++) {
printf("Voter %d: %c(%d), %c(%d), %c(%d)\n", i + 1, 'A', votes[i][0], 'B', votes[i][1], 'C', votes[i][2]);
}
return 0;
}
```
该程序采用了结构体数组来存储候选人信息,二维数组来存储每张投票的结果。在输入部分,程序会检查投票是否合法,如果不合法,则会提示用户重新输入。在统计部分,程序会对候选人得分进行排序,并比较前两名得分是否相同,如果相同,则会提示用户重新投票。最后,程序会输出投票结果和每张投票的结果。
希望这份代码示例可以帮助你完成投票程序课设。
可以用C语言写有3个候选人,每个选民只能投票一人,要求编一个统计选票的程序,先输入被选人的明细,最后输出各人得票结果。
好的,下面是一个用C语言编写的统计选票的程序,可以实现你的需求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CANDIDATES 3
#define MAX_VOTERS 100
typedef struct {
char name[20];
int votes;
} Candidate;
int num_voters;
Candidate candidates[MAX_CANDIDATES];
int get_candidate_index(char *name) {
for (int i = 0; i < MAX_CANDIDATES; i++) {
if (strcmp(name, candidates[i].name) == 0) {
return i;
}
}
return -1;
}
int main() {
printf("Enter the names of the candidates:\n");
for (int i = 0; i < MAX_CANDIDATES; i++) {
printf("Candidate %d: ", i+1);
scanf("%s", candidates[i].name);
candidates[i].votes = 0;
}
printf("Enter the number of voters: ");
scanf("%d", &num_voters);
for (int i = 0; i < num_voters; i++) {
char name[20];
printf("Voter %d, enter your choice: ", i+1);
scanf("%s", name);
int index = get_candidate_index(name);
if (index == -1) {
printf("Invalid candidate name!\n");
i--;
} else {
candidates[index].votes++;
}
}
printf("Candidate Vote Count\n");
for (int i = 0; i < MAX_CANDIDATES; i++) {
printf("%-9s %d\n", candidates[i].name, candidates[i].votes);
}
return 0;
}
```
程序先让用户输入候选人的名字,然后让用户输入投票人数和每个人的选票,最后输出各个候选人的得票结果。在程序中,我们使用了一个结构体来表示候选人,其中包含了姓名和得票数两个字段。我们还定义了一个函数 `get_candidate_index` 来查找候选人的索引,如果找到了就返回候选人的索引,如果没找到就返回 -1。
该程序可以用gcc编译器进行编译运行,输出结果如下:
```
Enter the names of the candidates:
Candidate 1: Alice
Candidate 2: Bob
Candidate 3: Charlie
Enter the number of voters: 5
Voter 1, enter your choice: Alice
Voter 2, enter your choice: Alice
Voter 3, enter your choice: Bob
Voter 4, enter your choice: Charlie
Voter 5, enter your choice: Bob
Candidate Vote Count
Alice 2
Bob 2
Charlie 1
```