如何将文本文件中的值分配给C中的数组结构?
我必须用C语言做一个选举程序 共有7名候选人和365张选票。我需要使用一组结构来完成这项工作。我需要从一个文本文件中读取每个候选人的名字和他们获得的选票数。最后,我需要输出选举的获胜者 这是我迄今为止的代码示例如何将文本文件中的值分配给C中的数组结构?,c,arrays,struct,C,Arrays,Struct,我必须用C语言做一个选举程序 共有7名候选人和365张选票。我需要使用一组结构来完成这项工作。我需要从一个文本文件中读取每个候选人的名字和他们获得的选票数。最后,我需要输出选举的获胜者 这是我迄今为止的代码示例 #include <stdio.h> #include <stdlib.h> #include <string.h> struct candidates { char name[20];
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct candidates {
char name[20];
int votes;
};
int main()
{
//Counter
int i = 0;
int gVotes = 0;
//Votes counter
int v = 0;
//Sploit Vote
int spVote = 0;
struct candidates electionCandidate[7];
FILE *fp;
fp = fopen("elections.txt", "r");
for(i=0;i<7;i++)
{
char * aNames = fgets(electionCandidate[i].name, 20, fp);
}
//for testing each candidate gots their name
for(i=0;i<7;i++)
{
printf("%d. Candidate is %s\n\n", i+1, electionCandidate[i]);
}
//For 365 Votes
while (!feof(fp))
{
int iVoteFor = 0;
fscanf(fp, "%d", &iVoteFor);
electionCandidate[iVoteFor-1].votes++;
//gVotes is my counter for the number of entries.
printf("%d ", ++gVotes);
}
system("pause");
return 0;
}
//Ideas of what to use
编辑:
每个候选人在他们的第二次选举中获得+1票候选人[0]在每一次选举中获得一票,其余的依次类推。总共有365名选民
我可以从文本文件中输入每个候选人的名字。现在的问题是把每一票投给相应的候选人。此外,任何高于7的投票都是被破坏的投票,我正试图在上述代码中计算。代码可以编译,但会崩溃
我正在使用while(!feof),但它似乎不起作用,或者这不是正确的方法。任何建议
编辑:
我正在使用调试器,发现它在while(!feof)期间运行了几次
但在其中一个例子中,它给出了错误并停止
编辑:
通过注释掉行选举候选人[iVoteFor-1]。投票++;程序读取到365的值
如何为每个候选人分配投票权?为了从文件中读取字符串,您需要使用fgets而不是fscanf,因为fgets读取整个字符串。因此,字符串的读取应该如下所示:
int main()
{
int i = 0;
// initialize everything to zeroes
// otherwise they should be initialized manually in a loop
struct candidates electionCandidate[7] = {0};
FILE *file = fopen("elections.txt", "r");
for (i = 0; i<7; i++)
{
char * res = fgets(electionCandidate[i].name, 100, file);
printf("%s\n", res);
}
//strcpy(electionCandidate[0].name, "Daniel Guzman");
//electionCandidate[0].votes = 41;
//printf("%s got %d Votes\n\n", electionCandidate[0].name, electionCandidate[0].votes);
system("pause");
return 0;
}
我相信事后很容易找到获胜者
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct candidate
{
char name[20];
int votes;
};
enum { MAX_CANDIDATES = 7 };
static int vote_cmp(const void *vp1, const void *vp2)
{
int votes1 = ((const struct candidate *)vp1)->votes;
int votes2 = ((const struct candidate *)vp2)->votes;
if (votes1 < votes2)
return -1;
else if (votes1 > votes2)
return +1;
else
return 0;
}
int main(void)
{
struct candidate electionCandidate[MAX_CANDIDATES];
char buffer[4096];
size_t i;
for (i = 0; fgets(buffer, sizeof(buffer), stdin) != 0; i++)
{
if (strlen(buffer) < 2)
break; // Blank line
if (i >= MAX_CANDIDATES)
{
fprintf(stderr, "Too many candidates: %s", buffer);
return 1;
}
size_t len = strlen(buffer);
if (len >= sizeof(electionCandidate[i].name))
{
fprintf(stderr, "Candidate name too long: %s", buffer);
return 1;
}
memmove(electionCandidate[i].name, buffer, len - 1);
electionCandidate[i].name[len] = '\0';
electionCandidate[i].votes = 0;
}
size_t n_cand = i;
int spoiled = 0;
unsigned votefor;
while (scanf("%u", &votefor) == 1)
{
if (votefor == 0 || votefor > n_cand)
spoiled++;
else
electionCandidate[votefor-1].votes++;
}
qsort(electionCandidate, n_cand, sizeof(electionCandidate[0]), vote_cmp);
for (i = 0; i < n_cand; i++)
printf("%20s: %3d\n", electionCandidate[i].name, electionCandidate[i].votes);
putchar('\n');
printf("%20s: %3d\n", "Spoiled votes", spoiled);
return 0;
}
显然,“被破坏的选票”是正式选举的获胜者。请不要使用
void main()
,即使在支持它的Windows上也是如此。正确的返回类型是int
,在C89系统(如Microsoft的编译器)上,应该包含返回0
在main()
的末尾。您需要在,意思如下。关联的数字是候选人的索引,因此您获取与候选人索引对应的记录,获取该索引的投票数(初始化后为0),并使用运算符++将其增加为1;“1”表示我投票给名单中的第一个,所以这是第一个记录。然而,C从0开始计数,这就是为什么“iVoteFor-1”索引的原因;当我放这行时,它显示正确的输出,但是程序崩溃了。我一直试图以不同的方式格式化它,但是我的程序崩溃了使用fgets中添加
时对btw1)文件*
(electionCandidate[i].name,22,fp)代码>当选举候选人[i]的大小22
?2) 不要使用时。名称==20
,使用feof()
中的返回值。3) 代码没有测试被破坏的投票。代码在(!feof(fp)){…}fscanf()
执行大的非否操作。
之前的feof()
变为true无法读取任何投票,因此fscanf()
保持为0。代码不进行范围检查,然后继续执行iVoteFor
electionCandidate[0-1]。投票++代码>它位于
-->Oops之外。好奇:什么参考文献建议使用electionCandidate[]
?!feof不读取我正在读取的整个文件的内容吗?我在另一个帖子中看到了这一点。我不太明白你是如何选举候选人[iVoteFor]的。投票在这里起作用,因为每个候选人都有一个与其相关联的数字。例如,“1”数字表示我在为我名单中的第一个候选人投票。这是怎么说的。有一只虫子。现在是选举候选人[iVoteFor-1]。投票+feof()
,而(!feof(file))…
是错误的。使用返回值表单fscanf()
。
while (!feof(file))
{
int iVoteFor = 0;
fscanf(file, "%d", &iVoteFor);
electionCandidate[iVoteFor-1].votes++;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct candidate
{
char name[20];
int votes;
};
enum { MAX_CANDIDATES = 7 };
static int vote_cmp(const void *vp1, const void *vp2)
{
int votes1 = ((const struct candidate *)vp1)->votes;
int votes2 = ((const struct candidate *)vp2)->votes;
if (votes1 < votes2)
return -1;
else if (votes1 > votes2)
return +1;
else
return 0;
}
int main(void)
{
struct candidate electionCandidate[MAX_CANDIDATES];
char buffer[4096];
size_t i;
for (i = 0; fgets(buffer, sizeof(buffer), stdin) != 0; i++)
{
if (strlen(buffer) < 2)
break; // Blank line
if (i >= MAX_CANDIDATES)
{
fprintf(stderr, "Too many candidates: %s", buffer);
return 1;
}
size_t len = strlen(buffer);
if (len >= sizeof(electionCandidate[i].name))
{
fprintf(stderr, "Candidate name too long: %s", buffer);
return 1;
}
memmove(electionCandidate[i].name, buffer, len - 1);
electionCandidate[i].name[len] = '\0';
electionCandidate[i].votes = 0;
}
size_t n_cand = i;
int spoiled = 0;
unsigned votefor;
while (scanf("%u", &votefor) == 1)
{
if (votefor == 0 || votefor > n_cand)
spoiled++;
else
electionCandidate[votefor-1].votes++;
}
qsort(electionCandidate, n_cand, sizeof(electionCandidate[0]), vote_cmp);
for (i = 0; i < n_cand; i++)
printf("%20s: %3d\n", electionCandidate[i].name, electionCandidate[i].votes);
putchar('\n');
printf("%20s: %3d\n", "Spoiled votes", spoiled);
return 0;
}
Arthur Smith: 17
Sean O'Rielly: 21
Michelle Dawn: 33
John Brown: 36
Robert Bloom: 39
Michael Hall: 40
Carl White: 64
Spoiled votes: 77