diff options
author | 2023-05-21 22:10:21 +0600 | |
---|---|---|
committer | 2023-05-21 22:43:46 +0600 | |
commit | 5942f6125ea85bc78fc3df7b2f72b41260f4b4a2 (patch) | |
tree | 5311444a5a2ed63468ef0afb837303edba0ff08b /src | |
download | c-obp-example-5942f6125ea85bc78fc3df7b2f72b41260f4b4a2.tar.gz c-obp-example-5942f6125ea85bc78fc3df7b2f72b41260f4b4a2.zip |
Initial commit
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 1 | ||||
-rw-r--r-- | src/main.c | 55 | ||||
-rw-r--r-- | src/teams.c | 94 | ||||
-rw-r--r-- | src/teams.h | 37 |
4 files changed, 187 insertions, 0 deletions
diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..0829e01 --- /dev/null +++ b/src/Makefile @@ -0,0 +1 @@ +include $(realpath $(dir $(realpath $(lastword $(MAKEFILE_LIST))))../Makefile) diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..faf76f7 --- /dev/null +++ b/src/main.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include "teams.h" +#define getline(x, y) \ + while(((x)[strcspn((x), "\r\n")] = 0) == 0 && strncmp((x), "", (y)) == 0) \ + fgets((x), (y), stdin) + +int main() +{ + unsigned num = 0, i = 0; + char line[255] = ""; + TEAM *teams; + + getline(line, 255); + sscanf(line, "%u", &num); + strncpy(line, "", 255); + teams = (TEAM *)malloc(sizeof(TEAM) * num); + + if (teams == NULL) { + printf("Failed to reserve memory"); + return 1; + } + + for(i = 0; i < num; i++) { + teams[i] = team_new(); + unsigned idx = 0, s = 0; + while (idx ++ < 6) { + getline(line, 255); + switch(idx) { + case 1: + team_set(teams[i], name, line); + break; + case 2: + team_set(teams[i], institution, line); + break; + case 3: + sscanf(line, "%u", &s); + team_set(teams[i], solved, s); + break; + case 4: // falls through + case 5: // falls through + case 6: + team_set(teams[i], member_name, idx - 3, line); + break; + default: + break; + } + strncpy(line, "", 255); + } + } + team_printf(team_find_champion(teams, num)); + return 0; +} diff --git a/src/teams.c b/src/teams.c new file mode 100644 index 0000000..f9a3fd0 --- /dev/null +++ b/src/teams.c @@ -0,0 +1,94 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define __IMPL_TEAMS_INTERNAL +#include "teams.h" + +typedef struct team_impl_struct { + char name[255]; + char members[3][255]; + char institution[255]; + uint8_t solved; +} TEAM_impl; +// base functions +TEAM team_new() +{ + return (TEAM_impl *)malloc(sizeof(TEAM_impl)); +} +void team_del(TEAM team) +{ + if(team) free(team); +} + +// Setter definitions +// @set char[]: TEAM.name -> bool +SETTER(char*, name) +{ + return !(strncpy(team->name, name, 255) == NULL); +} +// @set char[]: TEAM.institution -> bool +SETTER(char*, institution) +{ + return !(strncpy(team->institution, institution, 255) == NULL); +} +// @set char[]: TEAM.member_name, int: id -> bool +SETTER(char*, member_name, int id) +{ + return (id > 0 && id < 4 && !(strncpy(team->members[id-1], member_name, 255) == NULL)); +} +// @set int: TEAM.solved -> bool +SETTER(uint8_t, solved) +{ + if (solved <= 10) + team->solved = solved; + return (solved <= 10); +} + +// Getter definitions +// @get TEAM.name -> char[] +GETTER(char*, name) +{ + return team->name; +} +// @get TEAM.institution -> char[] +GETTER(char*, institution) +{ + return team->institution; +} +// @get TEAM.solved -> int +GETTER(uint8_t, solved) +{ + return team->solved; +} +// @get TEAM.member_name, id -> char[] +GETTER(char*, member_name, int id) +{ + return (id > 0 && id < 4)? team->members[id - 1] : "(incorrect id)"; +} + +int team_printf(TEAM team) +{ + return team_fprintf(stdout, team); +} +int team_fprintf(FILE *file, TEAM team) +{ + return fprintf(file, + "Team name : %s\n" + "Institution: %s\n" + "Solved : %u\n" + " Member 1: %s\n" + " Member 2: %s\n" + " Member 3: %s\n", + team->name, team->institution, team->solved, + team->members[0], team->members[1], team->members[2]); +} + +TEAM team_find_champion(TEAM teams[], unsigned num) +{ + TEAM champion = teams[num - 1]; + while(num --) + if ( teams[num]->solved > champion->solved ) + champion = teams[num]; + return champion; +} diff --git a/src/teams.h b/src/teams.h new file mode 100644 index 0000000..5672fda --- /dev/null +++ b/src/teams.h @@ -0,0 +1,37 @@ +#ifndef __TEAMS_H__ +# define __TEAMS_H__ +# include <stdint.h> +# include <stdbool.h> +# define GETTER(type, field, ...) type team_get_##field(TEAM team, ## __VA_ARGS__) +# define SETTER(type, field, ...) bool team_set_##field(TEAM team, ## __VA_ARGS__, type field) +# define team_get(team, field, ...) team_get_##field(team, ## __VA_ARGS__) +# define team_set(team, field, ...) team_set_##field(team, ## __VA_ARGS__) +// Type definition +typedef struct team_impl_struct * TEAM; + +// base functions +TEAM team_new(); +void team_del(TEAM team); + +// field getters +SETTER(char *, name); +SETTER(char *, institution); +SETTER(char *, member_name, int); +SETTER(uint8_t, solved); + +// field setters +GETTER(char *, name); +GETTER(char *, member_name, int); +GETTER(char *, institution); +GETTER(uint8_t, solved); + +// helper functions +int team_printf(TEAM); +int team_fprintf(FILE *, TEAM); +TEAM team_find_champion(TEAM teams[], unsigned num); + +# ifndef __IMPL_TEAMS_INTERNAL +# undef GETTER +# undef SETTER +# endif +#endif /* __TEAMS_H__ */ |