poll demo i once write for /chad/, but was never used

This commit is contained in:
anon 2024-12-10 20:26:16 +01:00
parent cd86cd6bdf
commit 1c46a7fa6c
5 changed files with 131 additions and 0 deletions

2
C_C++/anon_-_poller/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.gdb_history
poller

View File

@ -0,0 +1,2 @@
master:
gcc -Wall -Wextra -Wpedantic source/main.c -o poller -ggdb

View File

@ -0,0 +1,11 @@
// ### Poller spec ###
+ take repeated input from stdin
+ consider each terminated input line a command
+ valid commands
- poll <question>; (<max_voting>)
- poll_status
- vote [n|y]
+ the program only needs to keep track of 1 poll
+ every vote is boolean
+ <max_voting> is optional
+ if more then half of <max_voting> number of people agreed, echo that the question is decided and what the verdict is

View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include "poller.h"
int print_poll_decision(const int yes, const int no){
if (yes > no) {
puts("The voters have decided on \033[32myes\033[0m.");
} else {
puts("The voters have decided on \033[31mno\033[0m.");
}
return 0;
}
int print_poller_status(const char * const question, const int yes, const int no, const int voter_max) {
printf("\033[1m%s?\033[0m Yes: %d | No: %d\n", question, yes, no);
if (is_majority(yes, no, voter_max)) {
print_poll_decision(yes, no);
}
return 0;
}
signed main() {
poller_display_status = print_poller_status;
poller_display_decision = print_poll_decision;
char * input = NULL;
size_t lenght = 0;
do{
getline(&input, &lenght, stdin);
poller_interpret(input);
} while(1);
return 0;
}

View File

@ -0,0 +1,80 @@
#ifndef POLLER_H
#define POLLER_H
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
static char* question = NULL;
static int vote_yes_count = 0;
static int vote_no_count = 0;
static int voter_max;
int (*poller_display_status)(const char * const question, const int yes, const int no, const int voter_max) = NULL;
int (*poller_display_decision)(const int yes, const int no) = NULL;
bool is_majority(const int a, const int b, const int max) {
return (max && ((a > (max / 2)) || (b > (max / 2))));
}
int poller_interpret(const char * const s) {
if(!s){
return 0;
}
int separation = 0;
while (s[separation] && (s[separation] != ' ')) {
++separation;
}
if (!strncmp(s, "poll", separation - 1)) {
if(s[separation] == '\n'){
return 0;
}
int base = separation;
bool has_max = false;
while (s[separation] && (s[separation] != '\n')) {
if (s[separation] == ';') {
has_max = true;
break;
}
++separation;
}
const int len = ((separation - 1) - (base + 1)) + 1;
free(question);
question = malloc(len);
strncpy(question, s + base + 1, len);
if (has_max) {
sscanf(s + separation + 2, "%d", &voter_max);
} else {
voter_max = 0;
}
} else if (!strncmp(s, "poll_status", separation - 1)) {
if(poller_display_status){
poller_display_status(question, vote_yes_count, vote_no_count, voter_max);
}
} else if (!strncmp(s, "vote", separation - 1)) {
switch (s[separation + 1]) {
case 'y':
case 'Y': {
++vote_yes_count;
} break;
case 'n':
case 'N': {
--vote_yes_count;
} break;
}
if (is_majority(vote_yes_count, vote_no_count, voter_max) && poller_display_decision) {
poller_display_decision(vote_yes_count, vote_no_count);
}
}
return 1;
}
#endif