Home > Back-end >  Strives for the code
Strives for the code

Time:09-16

Programming statistical candidates, including three candidates respectively li, zhang and wang, a total of 10 voters to take part in the election,
Program analysis: as the candidate altogether contains two different types of data items, votes and name, have more than one candidate, so use structure data sets, first define structure arrays, respectively through the loop again receive electoral vote information - the candidate's name, at the same time the electoral vote information by comparison with the candidate's name (double loop), and use different counter to count respectively, finally through the loop output respectively the candidate's name and counted,

CodePudding user response:

You can step by step in accordance with the requirements to do

CodePudding user response:

The code is not long, generation of reference
 
#include
#include
#include

Typedef struct Candidate
{
Char name [20].
Int total;
} candidate;

Int main ()
{
Candidate carray [3]={{" li ", 0}, {" zhang ", 0}, {" wang ", 0}};
int i;
Char STR [20].

for (i=0; i<10; I++)
{
Printf (" input the names of the candidates: \ n ");
The scanf (" % s ", STR);
If (STRCMP (STR, carray. [0] name)==0)
Carray [0]. Total++;
Else if (STRCMP (STR, carray. [1] name)==0)
Carray [1]. Total++;
Else if (STRCMP (STR, carray. [2] name)==0)
Carray [2]. Total++;
}

for (i=0; i<3; I++)
{
Name: printf (" % s \ t, votes: % d \ n ", carray [I]. Name, carray [I] total);
}

Getchar ();
return 0;
}
  • Related