forked from ginadivina/polling_booth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll.c
286 lines (266 loc) · 6.04 KB
/
poll.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NAME_SIZE 11
#define MAX_PREFERENCES 10
#define MAX_USER_LIST 5
/*Defined the amount of preferences the user chooses*/
struct user
{
char firstName[MAX_NAME_SIZE];
char lastName[MAX_NAME_SIZE];
int pref1;
int pref2;
int pref3;
int pref4;
int pref5;
int pref6;
int pref7;
int pref8;
int pref9;
int pref10;
struct node *next;
/*the users preferences are an array of ints*/
};
typedef struct user user_t;
struct temp
{
char firstName[MAX_NAME_SIZE];
char lastName[MAX_NAME_SIZE];
int above[MAX_PREFERENCES];
/*the users preferences are an array of ints*/
};
typedef struct temp temp_t;
void printMenu(void);
void voteNow(int listpos);
void printParty(void);
void printPref(int choice, int pos);
/*Prints the users preferences*/
void getPrefs(int* p);
int checkpref(int* p, int position);
void printUserlist(temp_t templist[], int listpos);
int main(void)
{
char check[10];
char exit[5] = "exit";
char debug[6] = "debug";
int listpos=0;
do
{
printf("\nPlease press enter to continue\n");
scanf("%s", check);
while (getchar()!='\n');
if (strcmp(debug,check) == 0)
printf("this is a placeholder\n");
printMenu();
voteNow(listpos);
listpos++;
}while (check != 0);
return 0;
}
void printMenu(void)
{
printf("\nWELCOME TO VOTE NOW\n");
printf("\nA TRIAL DIGITAL VOTING SYSTEM\n\n\n");
}
void voteNow(int listpos)
{
user_t * userp = NULL;
userp = malloc(sizeof(user_t));
int i;
int areyousure=5;
temp_t temp;
temp_t templist[MAX_USER_LIST];
int* p = temp.above;
printf("Please enter your first name>");
fgets(temp.firstName, sizeof(temp.firstName), stdin);
fflush(stdin);
/*char *firstPos;
if ((firstPos=strchr(temp.firstName, '\n')) != NULL)
{
*firstPos = '\0';
}*/
printf("Please enter your last name>");
fgets(temp.lastName, sizeof(temp.lastName), stdin);
fflush(stdin);
/*char *lastPos;
if ((lastPos=strchr(temp.lastName, '\n')) != NULL)
{
*lastPos = '\0';
}*/
userp = (user_t*) malloc (sizeof(user_t));
strcpy(userp -> firstName, temp.firstName);
strcpy(userp -> lastName, temp.lastName);
printf("\n");
printf("\n");
printf("Hello %s %s,\n", userp->firstName, userp->lastName);
/*
printf("\n\nSelect from the following options\n");
printf("1. I am voing above the line\n");
printf("2. I am voting below the line\n");*/
printf("You are voting in the Sydney electorate.\n");
printParty();
getPrefs(p);
/*gets the users input. can be done in function, but requires parsing
- would probably be best as a function, since if they want to redo,
this would need to be called*/
printf("Your preferences are from highest to lowest as follows:\n");
for(i=0;i<=MAX_PREFERENCES;i++)
{
printf("\n");
printPref(temp.above[i], i);
}
/*prints the users preferences*/
while(areyousure != 0)
{
printf("Are you happy with these preferences?\n");
printf("Enter 1 to confirm. Enter 2 to redo\n");
scanf("%d", &areyousure);
while (getchar()!='\n');
if(areyousure != 1)
{
printf("\n");
printParty();
getPrefs(p);
printf("Your preferences are from highest to lowest as follows:\n");
for(i=0;i<=MAX_PREFERENCES;i++)
{
printf("\n");
printPref(temp.above[i], i);
}
}
else
{
areyousure = 0;
}
}
userp -> pref1 = temp.above[1];
userp -> pref2 = temp.above[2];
userp -> pref3 = temp.above[3];
userp -> pref4 = temp.above[4];
userp -> pref5 = temp.above[5];
userp -> pref6 = temp.above[6];
userp -> pref7 = temp.above[7];
userp -> pref8 = temp.above[8];
userp -> pref9 = temp.above[9];
userp -> pref10 = temp.above[10];
userp -> next = NULL;
templist[listpos] = temp;
printf("Thank you for voting\n");
printUserlist(templist, listpos);
}
void printParty()
{
printf("Below are the parties you may choose from:\n");
printf("1. Labor\n");
printf("2. Coalition\n");
printf("3. Greens\n");
printf("4. Christian Democrats Party\n");
printf("5. Animal Justice Party\n");
printf("6. Socialist Allience\n");
printf("7. Science Party\n");
printf("8. Australian Sex Party\n");
printf("9. Sustainable Australia\n");
printf("10. Online Direct Democracy\n");
printf("Please make your selection by enetering your preferences in order\n");
}
void getPrefs(int* p)
{
int i=0;
for(i=0;i<MAX_PREFERENCES;i++)
{
printf(">");
if (1 != (scanf("%d", &p[i])))
{
printf("Error; Integer Expected\n");
printf("Please make a new choice\n");
while(getchar() != '\n');
i--;
}
else if (checkpref(p, i) != 0)
{
printf("Please make a new choice\n");
i--;
}
}
}
int checkpref(int* p, int position)
{
if(MAX_PREFERENCES < p[position] || p[position] <= 0)
{
printf("Error; please enter an integer within the range.\n");
return 1;
}
int i=0;
for(i=0;i<position;i++)
{
if (p[position] == p[i])
{
printf("Error; you have already selected that party.\n");
return 1;
}
}
return 0;
}
void printUserlist(temp_t templist[], int listpos)
{
int i=0;
int j=0;
for(i=0;i<listpos+1;i++)
{
printf("\nUser's First Name: %s", templist[i].firstName);
printf("\n");
printf("User's Last Name: %s", templist[i].lastName);
printf("\n");
for(j=0;j<=MAX_PREFERENCES-1;j++)
{
printf("%d", templist[i].above[j]);
}
}
}
void printPref(int choice, int pos)
{
switch(choice)
{
case 1:
printf("%d. ", (pos+1));
printf("Labor");
break;
case 2:
printf("%d. ", (pos+1));
printf("Coalition");
break;
case 3:
printf("%d. ", (pos+1));
printf("Greens");
break;
case 4:
printf("%d. ", (pos+1));
printf("Christian Democrats Party");
break;
case 5:
printf("%d. ", (pos+1));
printf("Animal Justice Party");
break;
case 6:
printf("%d. ", (pos+1));
printf("Socialist Allience");
break;
case 7:
printf("%d. ", (pos+1));
printf("Science Party");
break;
case 8:
printf("%d. ", (pos+1));
printf("Australian Sex Party");
break;
case 9:
printf("%d. ", (pos+1));
printf("Sustainable Australia");
break;
case 10:
printf("%d. ", (pos+1));
printf("Online Direct Democracy");
break;
}
}