我该如何分配从文本文件中值阵列在C结构? [英] how do I assign values from text file to a structure of arrays in C?

查看:104
本文介绍了我该如何分配从文本文件中值阵列在C结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C.做一个竞选纲领

有7名候选人和365票总。我要做到这一点使用结构的数组。我需要从一个文本文件读取每个候选人,他们得到的票数的名字。最后我需要输出大选的获胜者。

下面是我的code样品至今

 的#include<&stdio.h中GT;
    #包括LT&;&stdlib.h中GT;
    #包括LT&;&string.h中GT;    结构候选人{
        焦炭名[20];
        INT票;
    };
    诠释的main()     {
        //计数器
        INT I = 0;
        INT gVotes = 0;
        //投票计数器
        INT V = 0;
        // Sploit投票
        INT spVote = 0;结构候选electionCandidate [7];
FILE * FP;
FP = FOPEN(elections.txt,R);
对于(I = 0; I&下; 7;我+ +)
    {
      字符* aNames =与fgets(electionCandidate [I] .name和20,FP);
    }//用于测试每个候选GOTS他们的名字
对于(I = 0; I&下; 7;我+ +)
    {
        的printf(%d个候选人为%s \\ n \\ n,I + 1,electionCandidate [I]);
    }
 // 365票 而(!的feof(FP))
       {
            INT iVoteFor = 0;
            的fscanf(FP,%D,&安培; iVoteFor);
            electionCandidate [iVoteFor-1] .votes ++;
            // gVotes是我的条目数计数器。
            的printf(%d个,++ gVotes);
        }        系统(暂停);        返回0;
    }
            要使用什么//思路

下面是我当前的elections.txt

 罗伯特·布卢姆
约翰·布朗
米歇尔黎明
迈克尔·霍尔
肖恩O'Rielly
亚瑟·史密斯
卡尔·怀特
3 3 81 1 2 3 1 2 4 5
1 6 12 9 6 5 0 2
8 46 6 8 3 2 8 0 12 6 1 8
3 11 7 5 5 8 9 10 12 1 3 12
2 23 2 5 7 4 8 11 6 11 12
9 11 7 9 3 1 2 10 12
12月7日11 9 6 6 0 1 10 7 11 2 8
0 12 8 10 11 2 2 8 4 2 12 3 2 9 1
4 88 7 7 4 12 2 10 10 9 4 12 9 3 12
0 0 48 6 5 9 0 5 3 11 6 0 3 0 1 2 3
4 1 1 2 3 3 3 3 3 3 3 3 3 3 8 4 5
9 1 2 12 1 7 7 7 7 7 7 7 7 7 7 7 4 7 1 2
4 5 1 2 3 1 2 8 7 12 95 41 1 7 5 4 4 4 4 4
4 4 4 4 4 4 4 4 4 1 1 1 1 6 6 6 6 6 7 7 7 7
7 8 8 9 9 8 7 7 1 1 2 3 5 4 4 6 8 7 52 1 4 7
5 2 5 4 7 7 7 7 7 7 7 4 7 7 7 1 2 5 4 7 8 7 4 1
4 7 8 7 4 1 5 2 5 2 3 6 5 3 2 1 2 1 2 3
2 2 5 1 4 7 7 7 7 7 7 7 7 7 7 7 7 1 2 1
3 4 5 1 2 3 1 2 3 1 4 5 8 1 2 4 1 4 2 5
6 7 8 1 2 3 3 4 7 7 7 7 7 7 7 8 1 2 3 4

编辑:

每个候选人获得+1投票选出他们的人数electionCandidate [0]每一个他得到一票,等了休息。 365选民总数。

我能够输入从文本文件每名候选人的名字。现在的问题是把每张票给相应的候选人。同时,任何投票是在7以上是我试图在上面code算一个被宠坏的票。在code编译,但它崩溃。

我使用一段时间(!的feof),但似乎它不工作或这不是正确的方法。任何建议。

编辑:
我使用调试器,发现它运行的周围,而几次(!的feof)
但在其中一个实例它提供了错误并停止。

编辑:
通过注释掉线electionCandidate [iVoteFor-1] .votes ++;程序读取一路365值。

我怎么分配每个投票给每个候选人?


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;结构候选人
{
    焦炭名[20];
    INT票;
};枚举{MAX_CANDIDATES = 7};静态INT vote_cmp(常量无效* VP1,常量无效* VP2)
{
  INT votes1 =((const的结构候选人*)VP1) - GT;票;
  INT votes2 =((const的结构候选人*)VP2) - GT;票;
  如果(votes1< votes2)
    返回-1;
  否则,如果(votes1> votes2)
    返回+1;
  其他
    返回0;
}INT主要(无效)
{
  结构候选人electionCandidate [MAX_CANDIDATES]  字符缓冲区[4096];
  为size_t我;
  对于(i = 0;与fgets(缓冲区,缓冲区尺寸,标准输入)= 0;!我++)
  {
    如果(strlen的(缓冲器)的2)
      打破; //空行
    如果(ⅰ&GT = MAX_CANDIDATES)
    {
      fprintf中(标准错误,太多的人选:%S,缓冲区);
      返回1;
    }
    为size_t LEN = strlen的(缓冲);
    如果(LEN> = sizeof的(electionCandidate [我]。名称))
    {
      fprintf中(标准错误,候选人名太长:%S,缓冲区);
      返回1;
    }
    memmove与(electionCandidate [I] .name和缓冲,LEN - 1);
    electionCandidate [我]。名称[LEN] ='\\ 0';
    electionCandidate [I] .votes = 0;
  }  为size_t n_cand = I;
  INT撒娇= 0;
  无符号votefor;
  而(scanf函数(%U,&安培; votefor)== 1)
  {
    如果(votefor == 0 || votefor> n_cand)
      撒娇++;
    其他
      electionCandidate [votefor-1] .votes ++;
  }  的qsort(electionCandidate,n_cand,sizeof的(electionCandidate [0]),vote_cmp);  对于(i = 0; I< n_cand;我++)
    的printf(%20秒:%3D \\ n,electionCandidate [I] .name和electionCandidate [I] .votes);
  的putchar('\\ n');
  的printf(%20秒:%3D \\ n,撒娇票,被宠坏的);  返回0;
}

鉴于样本数据,从UTF-8 ASCII改性O'Rielly先生的名字报价

,输出是:

 亚瑟·史密斯:17
   肖恩O'Rielly:21
   米歇尔黎明:33
      约翰·布朗:36
    罗伯特·布鲁姆:39
    迈克尔·霍尔:40
      卡尔·怀特:64   被宠坏的票数:77

显然,撒娇​​票是正式当选冠军。

I have to do an election program in C.

There are 7 candidates and 365 votes in total. I need to do this using an array of structures. I need to read from a text file each of the names of the candidate and the number of votes they get. At the end i need to output the winner of the election.

Here is a sample of my code so far

    #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

Here is my current elections.txt

Robert Bloom
John Brown
Michelle Dawn
Michael Hall
Sean O’Rielly
Arthur Smith
Carl White


3 3 81 1 2 3 1 2 4 5 
1 6 12 9 6 5 0 2
8 46 6 8 3 2 8 0 12 6 1 8 
3 11 7 5 5 8 9 10 12 1 3 12 
2 23 2 5 7 4 11 8 6 11 12 
9 11 7 9 3 1 2 10 12 
12 7 11 9 6 6 0 1 10 7 11 2 8 
0 12 8 10 11 2 2 8 4 2 12 3 2 9 1 
4 88 7 7 4 12 2 10 10 9 4 12 9 3 12 
0 48 0 6 5 9 0 5 3 11 6 0 3 0 1 2 3 
4 1 1 2 3 3 3 3 3 3 3 3 3 3 8 4 5 
9 1 2 12 1 7 7 7 7 7 7 7 7 7 7 7 4 7 1 2 
4 5 1 2 3 1 2 8 7 12 95 41 1 7 5 4 4 4 4 4 
4 4 4 4 4 4 4 4 4 1 1 1 1 6 6 6 6 6 7 7 7 7
7 8 8 9 9 8 7 7 1 1 2 3 5 4 4 6 8 7 52 1 4 7 
5 2 5 4 7 7 7 7 7 7 7 4 7 7 7 1 2 5 4 7 8 7 4 1
4 7 8 7 4 1 5 2 5 2 3 6 5 3 2 1 2 1 2 3 
2 2 5 1 4 7 7 7 7 7 7 7 7 7 7 7 7 1 2 1 
3 4 5 1 2 3 1 2 3 1 4 5 8 1 2 4 1 4 2 5 
6 7 8 1 2 3 3 4 7 7 7 7 7 7 7 8 1 2 3 4 

EDIT:

Each candidate gets +1 vote for their number electionCandidate[0] for each one he gets one vote and so on for the rest. 365 voters in total.

I was able to input the name for each Candidate from the text file. Now the problem is putting each vote to the corresponding candidate. Also any vote that is above 7 is a spoilt vote which i am trying to count in the above code. The code compiles but it crashes.

I am using while(!feof) but it seems that its not working or this is not the correct way. Any suggestions.

EDIT: I am using the debugger and find that it runs for a few times around the while(!feof) but in one of the instances it gives the error and stops.

EDIT: By commenting out the line electionCandidate[iVoteFor-1].votes++; the program reads all the way to 365 value.

how do I assign each to vote to each candidate?

解决方案

#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;
}

Given the sample data, with the quote in Mr O'Rielly's name modified from UTF-8 to ASCII, the output is:

    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

Clearly, "Spoiled votes" is the duly elected winner.

这篇关于我该如何分配从文本文件中值阵列在C结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆