Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- struct Oppilas {
- int nro;
- char *nimi;
- int pisteet[7];
- };
- struct Oppilas *uusi_oppilas(struct Oppilas *lista, int nro, const char *p_nimi) {
- int i;
- for (i = 0; lista[i].nimi != NULL; i++);
- struct Oppilas *uusi_lista = realloc(lista, sizeof(struct Oppilas) * (i + 2));
- if (!uusi_lista) {
- printf("Virhe oppilaan %d lisäämisessä\n", nro);
- return NULL;
- }
- uusi_lista[i].nro = nro;
- uusi_lista[i].nimi = malloc(strlen(p_nimi) + 1);
- strcpy(uusi_lista[i].nimi, p_nimi);
- uusi_lista[i].pisteet[0] = 0;
- uusi_lista[i].pisteet[1] = '\0';
- uusi_lista[i + 1].nimi = malloc(21);
- uusi_lista[i + 1].nimi = NULL;
- printf("Oppilas %d lisätty listaan\n", nro);
- return uusi_lista;
- }
- int print_lista(struct Oppilas *a)
- {
- if (a[0].nimi == NULL) {
- printf("Lista on tyhjä\n");
- return -1;
- }
- printf("OpNro Nimi Pisteet Yhteensa\n");
- for (int i = 0; a[i].nimi != NULL; i++) {
- printf("%6d %-20s ", a[i].nro, a[i].nimi);
- for (int j = 1; j < 7; j++) {
- if (a[i].pisteet[j] == '\0') {
- while (j < 7) {
- printf(" ");
- j++;
- }
- break;
- }
- else {
- printf("%d ", a[i].pisteet[j]);
- }
- }
- printf("%3d\n", a[i].pisteet[0]);
- }
- return 1;
- }
- int toInt(char a[]) {
- int n = 0;
- for (int i = 0; a[i] != '\0'; i++) {
- n = n * 10 + a[i] - '0';
- }
- return n;
- }
- int pisteet(struct Oppilas *a, int nro, int kier, int pist) {
- int i;
- for (i = 0; a[i].nro != nro; i++) {
- if (a[i].nimi == NULL) {
- printf("Opiskelija %d ei ole listassa\n", nro);
- return -1;
- }
- }
- if (a[i].pisteet[kier] == '\0' && kier < 7) {
- a[i].pisteet[kier + 1] = '\0';
- }
- a[i].pisteet[kier] = pist;
- a[i].pisteet[0] = 0;
- for (int j = 1; j < 7; j++) {
- if (a[i].pisteet[j] == '\0') {
- return 1;
- }
- else {
- a[i].pisteet[0] += a[i].pisteet[j];
- }
- }
- return -1;
- }
- void sort(struct Oppilas *a) {
- int koko;
- struct Oppilas *temp = malloc(sizeof(struct Oppilas));
- for (koko = 0; a[koko].nimi != NULL; koko++);
- for (int i = 0; i < koko; i++) {
- for (int j = i + 1; j < koko; j++) {
- if (a[j].pisteet[0] > a[i].pisteet[0]) {
- temp->nro = a[i].nro;
- temp->nimi = malloc(strlen(a[i].nimi) + 1);
- strcpy(temp->nimi, a[i].nimi);
- memcpy(temp->pisteet, a[i].pisteet, sizeof(temp->pisteet));
- a[i].nro = a[j].nro;
- strcpy(a[1].nimi, a[j].nimi);
- memcpy(a[i].pisteet, a[j].pisteet, sizeof(a[i].pisteet));
- a[j].nro = temp->nro;
- strcpy(a[j].nimi, temp->nimi);
- memcpy(a[j].pisteet, temp->pisteet, sizeof(a[j].pisteet));
- }
- }
- }
- }
- int tallenna(struct Oppilas *a, char *nimi) {
- FILE *f;
- f = fopen(nimi, "w");
- if (!f) {
- return -1;
- }
- for (int i = 0; a[i].nimi != NULL; i++) {
- fprintf(f, "%6d %-20s ", a[i].nro, a[i].nimi);
- for (int j = 1; j < 7; j++) {
- if (a[i].pisteet[j] == '\0') {
- while (j < 7) {
- fprintf(f, " ");
- j++;
- }
- break;
- }
- else {
- fprintf(f, "%d ", a[i].pisteet[j]);
- }
- }
- fprintf(f, "%3d\n", a[i].pisteet[0]);
- }
- return 1;
- }
- int lataa(struct Oppilas *a, char *nimi) { // Oletetaan että ladataan täysin samanlaisesta tiedostosta
- FILE *f;
- char buffer[100];
- f = fopen(nimi, "r");
- if (!f) {
- printf("Virhe tiedoston avaamisessa\n");
- return -1;
- }
- free(a);
- struct Oppilas *lista = malloc(sizeof(struct Oppilas));
- lista[0].nimi = NULL;
- while (!feof(f)) {
- if (fgets(buffer, 60, f) != NULL) {
- char para1[7];
- char para2[21];
- char para3[11];
- const char s[2] = " ";
- char *token = strtok(buffer, s);
- strcpy(para1, token);
- int nro = toInt(para1);
- token = strtok(NULL, s);
- strcpy(para2, token);
- token = strtok(NULL, s);
- strcpy(para3, token);
- token = strtok(NULL, s);
- strcat(para2, s);
- strcat(para2, para3);
- lista = uusi_oppilas(lista, nro, para2);
- for (int k = 1; strchr(token, '\n') == NULL && k < 7; k++) {
- int pist = toInt(token);
- pisteet(lista, nro, k, pist);
- token = strtok(NULL, s);
- }
- }
- }
- fclose(f);
- return 1;
- }
- int numeroita(char *a) {
- for (int i = 0; a[i] != '\0' && a[i] != '\n'; i++) {
- if (isdigit(a[i]) == 0) {
- return -1;
- }
- }
- return 1;
- }
- int kirjaimia(char *a) {
- for (int i = 0; a[i] != '\0' && a[i] != '\n'; i++) {
- if (isalpha(a[i]) == 0) {
- return -1;
- }
- }
- return 1;
- }
- struct Oppilas *A(struct Oppilas *lista, char *token) {
- char para1[7];
- char para2[21];
- char para3[11];
- const char s[2] = " ";
- token = strtok(NULL, s);
- if (!token) {
- return NULL;
- }
- if (strlen(token) > 7) {
- return NULL;
- }
- if (numeroita(token) != 1) {
- return NULL;
- }
- strcpy(para1, token);
- int nro = toInt(para1);
- token = strtok(NULL, s);
- if (!token) {
- return NULL;
- }
- if (strlen(token) < 1) {
- return NULL;
- }
- if (kirjaimia(token) != 1) {
- return NULL;
- }
- strcpy(para2, token);
- token = strtok(NULL, s);
- if (!token) {
- return NULL;
- }
- if (strlen(token) <= 1 || strlen(token) > 11) {
- return NULL;
- }
- if (kirjaimia(token) != 1) {
- return NULL;
- }
- strcpy(para3, token);
- para3[(strlen(para3)) - 1] = '\0';
- strcat(para2, s);
- strcat(para2, para3);
- lista = uusi_oppilas(lista, nro, para2);
- return lista;
- }
- int U(struct Oppilas *lista, char *token) {
- char para1[7];
- char para2[2];
- char para3[11];
- const char s[2] = " ";
- token = strtok(NULL, s);
- if (!token) {
- return 0;
- }
- if (strlen(token) > 7) {
- return 0;
- }
- if (numeroita(token) != 1) {
- return 0;
- }
- strcpy(para1, token);
- int nroU = toInt(para1);
- token = strtok(NULL, s);
- if (!token) {
- return 0;
- }
- if (strlen(token) > 1) {
- return -1;
- }
- if (numeroita(token) != 1) {
- return 0;
- }
- strcpy(para2, token);
- int kierros = toInt(para2);
- if (kierros > 6) {
- return 0;
- }
- token = strtok(NULL, s);
- if (!token) {
- return 0;
- }
- if (strlen(token) > 11) {
- return 0;
- }
- if (numeroita(token) != 1) {
- return -1;
- }
- strcpy(para3, token);
- para3[(strlen(para3)) - 1] = '\0';
- int pist = toInt(para3);
- int ret = pisteet(lista, nroU, kierros, pist);
- return ret;
- }
- int main() {
- char input[100]; // Käyttäjän syöte
- struct Oppilas *lista = malloc(sizeof(struct Oppilas)); // Tyhjä lista oppilaille
- lista[0].nimi = NULL; // Alustetaan tyhjällä oppilaalla
- struct Oppilas *ret;
- int retU;
- int loop = 1;
- while (loop) {
- fgets(input, 50, stdin);
- const char s[2] = " ";
- char *token = strtok(input, s);
- switch (*token) {
- char para1[7];
- case 'A': // Lisää opiskelija
- ret = A(lista, token);
- if (ret != NULL) {
- lista = ret;
- }
- else {
- printf("Virheellinen syöte\n");
- }
- break;
- case 'U': // Päivitä pisteet
- retU = U(lista, token);
- if (retU == 0) {
- printf("Virheellinen syöte\n");
- }
- else if (retU == 1) {
- printf("Pisteet päivitetty\n");
- }
- break;
- case 'L': // Tulosta tilanne
- sort(lista);
- print_lista(lista);
- break;
- case 'W': // Tallenna tulokset
- token = strtok(NULL, s);
- if (!token) {
- printf("Virheellinen tiedostonimi\n");
- }
- strcpy(para1, token);
- printf("Tiedoston nmi on: %s\n", para1);
- int retW = tallenna(lista, para1);
- if (retW != 1) {
- printf("Virhe tallentamisessa\n");
- }
- break;
- case 'O': // Lataa tulokset
- token = strtok(NULL, s);
- if (!token) {
- printf("Virheellinen tiedostonimi\n");
- }
- strcpy(para1, token);
- printf("Tiedoston nimi on: %s\n", para1);
- int retO = lataa(lista, para1);
- if (retO != 1) {
- printf("Virhe lataamisessa\n");
- }
- break;
- case 'Q': // Poistu ohjelmasta
- printf("Poistutaan ohjelmasta");
- free(lista);
- exit(0); // Poistu ja vapauta muisti
- break;
- default:
- printf("Virheellinen syöte\n");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement