Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.76 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. struct Oppilas {
  7.     int nro;
  8.     char *nimi;
  9.     int pisteet[7];
  10. };
  11.  
  12. struct Oppilas *uusi_oppilas(struct Oppilas *lista, int nro, const char *p_nimi) {
  13.     int i;
  14.     for (i = 0; lista[i].nimi != NULL; i++);
  15.     struct Oppilas *uusi_lista = realloc(lista, sizeof(struct Oppilas) * (i + 2));
  16.     if (!uusi_lista) {
  17.         printf("Virhe oppilaan %d lisäämisessä\n", nro);
  18.         return NULL;
  19.     }
  20.     uusi_lista[i].nro = nro;
  21.     uusi_lista[i].nimi = malloc(strlen(p_nimi) + 1);
  22.     strcpy(uusi_lista[i].nimi, p_nimi);
  23.     uusi_lista[i].pisteet[0] = 0;
  24.     uusi_lista[i].pisteet[1] = '\0';
  25.     uusi_lista[i + 1].nimi = malloc(21);
  26.     uusi_lista[i + 1].nimi = NULL;
  27.     printf("Oppilas %d lisätty listaan\n", nro);
  28.     return uusi_lista;
  29. }
  30. int print_lista(struct Oppilas *a)
  31. {
  32.     if (a[0].nimi == NULL) {
  33.         printf("Lista on tyhjä\n");
  34.         return -1;
  35.     }
  36.     printf("OpNro  Nimi                 Pisteet            Yhteensa\n");
  37.     for (int i = 0; a[i].nimi != NULL; i++) {
  38.         printf("%6d %-20s ", a[i].nro, a[i].nimi);
  39.         for (int j = 1; j < 7; j++) {
  40.             if (a[i].pisteet[j] == '\0') {
  41.                 while (j < 7) {
  42.                     printf("   ");
  43.                     j++;
  44.                 }
  45.                 break;
  46.             }
  47.             else {
  48.                 printf("%d ", a[i].pisteet[j]);
  49.             }
  50.         }
  51.         printf("%3d\n", a[i].pisteet[0]);
  52.     }
  53.     return 1;
  54. }
  55. int toInt(char a[]) {
  56.     int n = 0;
  57.     for (int i = 0; a[i] != '\0'; i++) {
  58.         n = n * 10 + a[i] - '0';
  59.     }
  60.     return n;
  61. }
  62.  
  63. int pisteet(struct Oppilas *a, int nro, int kier, int pist) {
  64.     int i;
  65.     for (i = 0; a[i].nro != nro; i++) {
  66.         if (a[i].nimi == NULL) {
  67.             printf("Opiskelija %d ei ole listassa\n", nro);
  68.             return -1;
  69.         }
  70.     }
  71.     if (a[i].pisteet[kier] == '\0' && kier < 7) {
  72.         a[i].pisteet[kier + 1] = '\0';
  73.     }
  74.     a[i].pisteet[kier] = pist;
  75.     a[i].pisteet[0] = 0;
  76.     for (int j = 1; j < 7; j++) {
  77.         if (a[i].pisteet[j] == '\0') {
  78.             return 1;
  79.         }
  80.         else {
  81.             a[i].pisteet[0] += a[i].pisteet[j];
  82.         }
  83.     }
  84.     return -1;
  85. }
  86.  
  87. void sort(struct Oppilas *a) {
  88.     int koko;
  89.     struct Oppilas *temp = malloc(sizeof(struct Oppilas));
  90.     for (koko = 0; a[koko].nimi != NULL; koko++);
  91.     for (int i = 0; i < koko; i++) {
  92.         for (int j = i + 1; j < koko; j++) {
  93.             if (a[j].pisteet[0] > a[i].pisteet[0]) {
  94.                 temp->nro = a[i].nro;
  95.                 temp->nimi = malloc(strlen(a[i].nimi) + 1);
  96.                 strcpy(temp->nimi, a[i].nimi);
  97.                 memcpy(temp->pisteet, a[i].pisteet, sizeof(temp->pisteet));
  98.                 a[i].nro = a[j].nro;
  99.                 strcpy(a[1].nimi, a[j].nimi);
  100.                 memcpy(a[i].pisteet, a[j].pisteet, sizeof(a[i].pisteet));
  101.                 a[j].nro = temp->nro;
  102.                 strcpy(a[j].nimi, temp->nimi);
  103.                 memcpy(a[j].pisteet, temp->pisteet, sizeof(a[j].pisteet));
  104.             }
  105.         }
  106.     }
  107. }
  108.  
  109. int tallenna(struct Oppilas *a, char *nimi) {
  110.     FILE *f;
  111.     f = fopen(nimi, "w");
  112.     if (!f) {
  113.         return -1;
  114.     }
  115.     for (int i = 0; a[i].nimi != NULL; i++) {
  116.         fprintf(f, "%6d %-20s ", a[i].nro, a[i].nimi);
  117.         for (int j = 1; j < 7; j++) {
  118.             if (a[i].pisteet[j] == '\0') {
  119.                 while (j < 7) {
  120.                     fprintf(f, "   ");
  121.                     j++;
  122.                 }
  123.                 break;
  124.             }
  125.             else {
  126.                 fprintf(f, "%d ", a[i].pisteet[j]);
  127.             }
  128.         }
  129.         fprintf(f, "%3d\n", a[i].pisteet[0]);
  130.     }
  131.     return 1;
  132. }
  133. int lataa(struct Oppilas *a, char *nimi) { // Oletetaan että ladataan täysin samanlaisesta tiedostosta
  134.     FILE *f;
  135.     char buffer[100];
  136.     f = fopen(nimi, "r");
  137.     if (!f) {
  138.         printf("Virhe tiedoston avaamisessa\n");
  139.         return -1;
  140.     }
  141.     free(a);
  142.     struct Oppilas *lista = malloc(sizeof(struct Oppilas));
  143.     lista[0].nimi = NULL;
  144.     while (!feof(f)) {
  145.         if (fgets(buffer, 60, f) != NULL) {
  146.             char para1[7];
  147.             char para2[21];
  148.             char para3[11];
  149.             const char s[2] = " ";
  150.             char *token = strtok(buffer, s);
  151.             strcpy(para1, token);
  152.             int nro = toInt(para1);
  153.             token = strtok(NULL, s);
  154.             strcpy(para2, token);
  155.             token = strtok(NULL, s);
  156.             strcpy(para3, token);
  157.             token = strtok(NULL, s);
  158.             strcat(para2, s);
  159.             strcat(para2, para3);
  160.             lista = uusi_oppilas(lista, nro, para2);
  161.             for (int k = 1; strchr(token, '\n') == NULL && k < 7; k++) {
  162.                 int pist = toInt(token);
  163.                 pisteet(lista, nro, k, pist);
  164.                 token = strtok(NULL, s);
  165.             }
  166.         }
  167.     }
  168.     fclose(f);
  169.     return 1;
  170. }
  171. int numeroita(char *a) {
  172.     for (int i = 0; a[i] != '\0' && a[i] != '\n'; i++) {
  173.         if (isdigit(a[i]) == 0) {
  174.             return -1;
  175.         }
  176.     }
  177.     return 1;
  178. }
  179. int kirjaimia(char *a) {
  180.     for (int i = 0; a[i] != '\0' && a[i] != '\n'; i++) {
  181.         if (isalpha(a[i]) == 0) {
  182.             return -1;
  183.         }
  184.     }
  185.     return 1;
  186. }
  187. struct Oppilas *A(struct Oppilas *lista, char *token) {
  188.     char para1[7];
  189.     char para2[21];
  190.     char para3[11];
  191.     const char s[2] = " ";
  192.     token = strtok(NULL, s);
  193.     if (!token) {
  194.         return NULL;
  195.     }
  196.     if (strlen(token) > 7) {
  197.         return NULL;
  198.     }
  199.     if (numeroita(token) != 1) {
  200.         return NULL;
  201.     }
  202.     strcpy(para1, token);
  203.     int nro = toInt(para1);
  204.     token = strtok(NULL, s);
  205.     if (!token) {
  206.         return NULL;
  207.     }
  208.     if (strlen(token) < 1) {
  209.         return NULL;
  210.     }
  211.     if (kirjaimia(token) != 1) {
  212.         return NULL;
  213.     }
  214.     strcpy(para2, token);
  215.     token = strtok(NULL, s);
  216.     if (!token) {
  217.         return NULL;
  218.     }
  219.     if (strlen(token) <= 1 || strlen(token) > 11) {
  220.         return NULL;
  221.     }
  222.     if (kirjaimia(token) != 1) {
  223.         return NULL;
  224.     }
  225.     strcpy(para3, token);
  226.     para3[(strlen(para3)) - 1] = '\0';
  227.     strcat(para2, s);
  228.     strcat(para2, para3);
  229.     lista = uusi_oppilas(lista, nro, para2);
  230.     return lista;
  231. }
  232.  
  233. int U(struct Oppilas *lista, char *token) {
  234.     char para1[7];
  235.     char para2[2];
  236.     char para3[11];
  237.     const char s[2] = " ";
  238.     token = strtok(NULL, s);
  239.     if (!token) {
  240.         return 0;
  241.     }
  242.     if (strlen(token) > 7) {
  243.         return 0;
  244.     }
  245.     if (numeroita(token) != 1) {
  246.         return 0;
  247.     }
  248.     strcpy(para1, token);
  249.     int nroU = toInt(para1);
  250.     token = strtok(NULL, s);
  251.     if (!token) {
  252.         return 0;
  253.     }
  254.     if (strlen(token) > 1) {
  255.         return -1;
  256.     }
  257.     if (numeroita(token) != 1) {
  258.         return 0;
  259.     }
  260.     strcpy(para2, token);
  261.     int kierros = toInt(para2);
  262.     if (kierros > 6) {
  263.         return 0;
  264.     }
  265.     token = strtok(NULL, s);
  266.     if (!token) {
  267.         return 0;
  268.     }
  269.     if (strlen(token) > 11) {
  270.         return 0;
  271.     }
  272.     if (numeroita(token) != 1) {
  273.         return -1;
  274.     }
  275.     strcpy(para3, token);
  276.     para3[(strlen(para3)) - 1] = '\0';
  277.     int pist = toInt(para3);
  278.     int ret = pisteet(lista, nroU, kierros, pist);
  279.     return ret;
  280. }
  281.  
  282. int main() {
  283.     char input[100]; // Käyttäjän syöte
  284.     struct Oppilas *lista = malloc(sizeof(struct Oppilas)); // Tyhjä lista oppilaille
  285.     lista[0].nimi = NULL; // Alustetaan tyhjällä oppilaalla
  286.     struct Oppilas *ret;
  287.     int retU;
  288.     int loop = 1;
  289.     while (loop) {
  290.         fgets(input, 50, stdin);
  291.         const char s[2] = " ";
  292.         char *token = strtok(input, s);
  293.         switch (*token) {
  294.             char para1[7];
  295.         case 'A': // Lisää opiskelija
  296.             ret = A(lista, token);
  297.             if (ret != NULL) {
  298.                 lista = ret;
  299.             }
  300.             else {
  301.                 printf("Virheellinen syöte\n");
  302.             }
  303.             break;
  304.         case 'U': // Päivitä pisteet
  305.             retU = U(lista, token);
  306.             if (retU == 0) {
  307.                 printf("Virheellinen syöte\n");
  308.             }
  309.             else if (retU == 1) {
  310.                 printf("Pisteet päivitetty\n");
  311.             }
  312.             break;
  313.         case 'L': // Tulosta tilanne
  314.             sort(lista);
  315.             print_lista(lista);
  316.             break;
  317.         case 'W': // Tallenna tulokset
  318.             token = strtok(NULL, s);
  319.             if (!token) {
  320.                 printf("Virheellinen tiedostonimi\n");
  321.             }
  322.             strcpy(para1, token);
  323.             printf("Tiedoston nmi on: %s\n", para1);
  324.             int retW = tallenna(lista, para1);
  325.             if (retW != 1) {
  326.                 printf("Virhe tallentamisessa\n");
  327.             }
  328.             break;
  329.         case 'O': // Lataa tulokset
  330.             token = strtok(NULL, s);
  331.             if (!token) {
  332.                 printf("Virheellinen tiedostonimi\n");
  333.             }
  334.             strcpy(para1, token);
  335.             printf("Tiedoston nimi on: %s\n", para1);
  336.             int retO = lataa(lista, para1);
  337.             if (retO != 1) {
  338.                 printf("Virhe lataamisessa\n");
  339.             }
  340.             break;
  341.         case 'Q': // Poistu ohjelmasta
  342.             printf("Poistutaan ohjelmasta");
  343.             free(lista);
  344.             exit(0); // Poistu ja vapauta muisti
  345.             break;
  346.         default:
  347.             printf("Virheellinen syöte\n");
  348.             break;
  349.         }
  350.     }
  351.     return 0;
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement