-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelection_clie.c
210 lines (178 loc) · 5.46 KB
/
election_clie.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "election.h"
void election_1(char *host) {
CLIENT *clnt;
char **result_1;
char register_voter_1_arg1[32];
char register_voter_1_arg2;
bool_t *result_2;
char verify_voter_1_arg1[10];
void *result_3;
char cast_vote_1_arg1[10];
char cast_vote_1_arg2[32];
candidate_ptr *result_4;
char **result_5;
bool_t *result_6;
char add_candidate_1_arg1[32];
bool_t *result_7;
char remove_candidate_1_arg1[32];
bool_t *result_8;
char invalidate_candidate_1_arg1[32];
stats *result_9;
#ifndef DEBUG
clnt = clnt_create(host, ELECTION, ELECTIONVERS, "udp");
if (clnt == NULL) {
clnt_pcreateerror(host);
exit(1);
}
#endif /* DEBUG */
int choice;
do {
printf("1. Register to Vote\n");
printf("2. Verify Voter\n");
printf("3. Vote\n");
printf("4. Tally Results\n");
printf("5. Announce Winner\n");
printf("6. Supervisor Mode\n");
printf("0. Quit\n\n");
printf("Please Enter your Choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter voter name: ");
gets(register_voter_1_arg1);
printf("Enter voter gender: ");
scanf(" %c", ®ister_voter_1_arg2);
result_1 =
register_voter_1(register_voter_1_arg1, register_voter_1_arg2, clnt);
if (result_1 == (char **)NULL)
clnt_perror(clnt, "call failed");
else
printf("Voter Registered!\nNew Voter Id = %s", *result_1);
break;
case 2:
printf("Enter voter id: ");
scanf("%s", verify_voter_1_arg1);
result_2 = verify_voter_1(verify_voter_1_arg1, clnt);
if (result_2 == (bool_t *)NULL)
clnt_perror(clnt, "call failed");
else if (*result_2 == TRUE)
printf("Voter exists\n");
else
printf("No voter exists\n");
break;
case 3:
printf("Enter voter id: ");
scanf("%s", cast_vote_1_arg1);
printf("Enter candidate name: ");
scanf("%s", cast_vote_1_arg2);
result_3 = cast_vote_1(cast_vote_1_arg1, cast_vote_1_arg2, clnt);
if (result_3 == (bool_t *)NULL)
clnt_perror(clnt, "call failed");
else if (*result_3 == TRUE)
printf("Vote was cast successfully\n");
else
printf("Vote cannot be cast\n");
break;
case 4:
result_4 = tally_results_1(clnt);
if (result_4 == (candidate_ptr *)NULL)
clnt_perror(clnt, "call failed");
else {
}
break;
case 5:
result_5 = announce_winner_1(clnt);
if (result_5 == (char **)NULL)
clnt_perror(clnt, "call failed");
else
printf("The winner is %s", *result_5);
break;
case 6:
int super_choice;
do {
printf("\n1. Add candidate\n");
printf("2. Remove a candidate\n ");
printf("3. Invalidate a candidate\n");
printf("4. Calculate statistics\n");
printf("5. Quit and return to main menu\n");
scanf("%d", &super_choice);
switch (super_choice) {
case 1:
printf("Enter candidate name: ");
scanf("%s", add_candidate_1_arg1);
result_6 = add_candidate_1(add_candidate_1_arg1, clnt);
if (result_6 == (bool_t *)NULL)
clnt_perror(clnt, "call failed");
else if (*result_6 == TRUE)
printf("Candidate added successfully!\n");
else
printf("Candidate addition failed!\n");
break;
case 2:
printf("Enter candidate name: ");
scanf("%s", remove_candidate_1_arg1);
result_7 = remove_candidate_1(remove_candidate_1_arg1, clnt);
if (result_7 == (bool_t *)NULL)
clnt_perror(clnt, "call failed");
else if (*result_7 == TRUE)
printf("Candidate removed successfully!\n");
else
printf("Candidate deletion failed!\n");
break;
case 3:
printf("Enter candidate name: ");
scanf("%s", invalidate_candidate_1_arg1);
result_8 = invalidate_candidate_1(invalidate_candidate_1_arg1, clnt);
if (result_8 == (bool_t *)NULL)
clnt_perror(clnt, "call failed");
else if (*result_8 == TRUE)
printf("Candidate added successfully!\n");
else
printf("Candidate addition failed!\n");
break;
case 4:
result_9 = calc_statistics_1(clnt);
if (result_9 == (stats *)NULL)
clnt_perror(clnt, "call failed");
else {
printf("%%age of registered that votes = %d\n",
result_9->total_vote_percent);
printf("%%age of male voters = %d\n", result_9->male_vote_percent);
printf("%%age of female that votes = %d\n",
result_9->female_vote_percent);
printf("%%age of registered that votes = %d\n",
result_9->total_vote_percent);
}
break;
case 5:
break;
default:
printf("Invalid Choice\n");
}
} while (super_choice != 5);
break;
case 0:
break;
default:
printf("Invalid Choice!\n");
}
} while (choice != 0);
#ifndef DEBUG
clnt_destroy(clnt);
#endif /* DEBUG */
}
int main(int argc, char *argv[]) {
char *host;
if (argc < 2) {
printf("usage: %s server_host\n", argv[0]);
exit(1);
}
host = argv[1];
election_1(host);
exit(0);
}