Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.86 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. struct student{
  7.     char nazwisko[20];
  8.     int nr, ects, pop; // nr - numer semestru, ects - deficyt ECTS, pop - oplata za poprawki
  9. };
  10. int szukaj(struct student *tab, int i){
  11.     int j;
  12.     char *buff;
  13.     printf("Podaj numer porzadkowy studenta: \n");
  14.     scanf("%d",&j);
  15.     return j;
  16. }
  17. struct student dodaj(){
  18.     int j,x;
  19.     struct student *tab;
  20.     tab=(struct student*)calloc(1,sizeof(struct student));
  21.     printf("Podaj nazwisko: \n");
  22.     scanf("%s",tab->nazwisko);
  23.     x=strlen(tab->nazwisko);
  24.     for(j=0;j<x;j++){
  25.         if((isdigit(tab->nazwisko[j]))!=0){
  26.             printf("W imieniu nie moze byc cyfr \n");
  27.             return *tab;
  28.         }
  29.     }
  30.     printf("Podaj numer semestru: \n");
  31.     scanf("%d",&tab->nr);
  32.     if(tab->nr>9){
  33.         printf("Nie ma takiego numeru semestru \n");
  34.         return *tab;
  35.     }
  36.     if(tab->nr<0){
  37.         printf("BLAD \n");
  38.         return *tab;
  39.     }
  40.     printf("Podaj deficyt punktow ects: \n");
  41.     scanf("%d",&tab->ects);
  42.     if(tab->ects>30){
  43.         printf("Ten student juz dawno pracuje w biedronce \n");
  44.         return *tab;
  45.     }
  46.     if(tab->ects<0){
  47.         printf("Nie mozna miec ujemnej liczby punktow deficytu \n");
  48.         return *tab;
  49.     }
  50.     tab->pop=0;
  51.     return *tab;
  52. }
  53. int usun(struct student *tab,int i, int j){
  54.     int z;
  55.     for(z>j;z<i;z++){
  56.         return z;
  57.     }
  58. }
  59. void zal(struct student *tab, int j){
  60.     int z,e;
  61.     z=tab[j].nr;
  62.     e=tab[j].ects;
  63.     if(z==1){
  64.         printf("Nie da sie miec deficytu na 1 semestrze \n");
  65.         return;
  66.     }
  67.     if(z==2){
  68.         if(e>11){
  69.             printf("Student nie zdal \n");
  70.             return;
  71.         }
  72.         if(e<11){
  73.             printf("Student zdal \n");
  74.             return;
  75.         }
  76.     }
  77.     if(z==3 || z==4){
  78.         if(e>15){
  79.             printf("Student nie zdal \n");
  80.             return;
  81.         }
  82.         if(e<15){
  83.             printf("Student zdal \n");
  84.             return;
  85.         }
  86.     }
  87.     if(z==5){
  88.         if(e>12){
  89.             printf("Student nie zdal \n");
  90.             return;
  91.         }
  92.         if(e<12){
  93.             printf("Student zdal \n");
  94.             return;
  95.         }
  96.     }
  97.     if(z==6){
  98.         if(e>9){
  99.             printf("Student nie zdal \n");
  100.             return;
  101.         }
  102.         if(e<9){
  103.             printf("Student zdal \n");
  104.             return;
  105.         }
  106.     }
  107.     if(z==7 || z==8){
  108.         if(e>6){
  109.             printf("Student nie zdal \n");
  110.             return;
  111.         }
  112.         if(e<6){
  113.             printf("Student zdal \n");
  114.             return;
  115.         }
  116.     }
  117.     if(z==9){
  118.         if(e>0){
  119.             printf("Student nie zdal \n");
  120.             return;
  121.         }
  122.         if(e<0){
  123.             printf("Nie da sie miec ujemnej liczby punktow deficytowych \n");
  124.             return;
  125.         }
  126.     }
  127. }
  128. void powt(struct student **tab, int i){
  129.     int j;
  130.     for(j=0;j<i;j++){
  131.         tab[j]->pop=tab[j]->ects*20;
  132.     }
  133. }
  134. void print(struct student *tab, int j){
  135.     printf("Student %s zaplaci za powtorki %d zl \n",tab[j].nazwisko,tab[j].pop);
  136. }
  137. void zapis(struct student *tab, int i, char *path){
  138.     FILE *f;
  139.     int j;
  140.     if(tab==NULL) return;
  141.     f=fopen(path,"wt");
  142.     if(f==NULL) return;
  143.     fprintf(f,"%d \n",i);
  144.     for(j=0;j<i;j++){
  145.             fprintf(f,"%s ;%d ;%d ;%d \n",tab[j].nazwisko,tab[j].nr,tab[j].ects,tab[j].pop);
  146.         }
  147.     fclose(f);
  148. }
  149. struct student *wczytaj(int i,char *path){
  150.     FILE *f;
  151.     int j;
  152.     struct student *tab;
  153.     f=fopen(path,"rt");
  154.     if(f==NULL) return tab;
  155.     if(fscanf(f,"%d",&i)!=1) return tab;
  156.     tab=(struct student*)calloc(i,sizeof(struct student));
  157.     for(j=0;j<i;j++){
  158.         fscanf(f,"%s ;%d ;%d ;%d",tab[j].nazwisko,&tab[j].nr,&tab[j].ects,&tab[j].pop);
  159.         }
  160.     fclose(f);
  161.     return tab;
  162. }
  163. void fre(struct student **tab, int i){
  164.     int j;
  165.     if(*tab==NULL) return;
  166.     for(j=0;j<i;j++){
  167.         free(tab[j]);
  168.     }
  169.     free(*tab);
  170. }
  171. void druk(struct student *tab, int i){
  172.     int j;
  173.     for(j=0;j<i;j++){
  174.         printf("%d %s %d %d %d \n",(j+1),tab[j].nazwisko,tab[j].nr,tab[j].ects,tab[j].pop);
  175.     }
  176.     printf("\n");
  177. }
  178. int main(){
  179.     FILE *f;
  180.     int z, i=0,j;
  181.     struct student *tab;
  182.     tab=(struct student*)malloc(1*sizeof(struct student));
  183.     while(1){
  184.         printf("0: Wyjdz z programu \n"
  185.             "1: Dodaj studenta \n"
  186.             "2: Usun studenta \n"
  187.             "3: Sprawdz czy student zdal \n"
  188.             "4: Oblicz ile studenci zaplaca za powtorki \n"
  189.             "5: Zobacz ile dany student zaplaci za powtorki \n"
  190.             "6: Zapisz studentow do pliku \n"
  191.             "7: Pobierz studentow z pliku \n"
  192.             "8: Wydrukuj liste studentow \n");
  193.         scanf("%d",&z);
  194.         if(z==0) break;
  195.         switch(z){
  196.             case 1:{
  197.                 tab=(struct student*)realloc(tab,(i+1)*sizeof(struct student));
  198.                 tab[i]=dodaj();
  199.                 i++;
  200.                 break;
  201.                    }
  202.             case 2:{
  203.                 j=szukaj(tab,i);
  204.                 for(z=j-1;z<i;z++){
  205.                     tab[z]=tab[z+1];
  206.                 }
  207.                 tab=(struct student*)realloc(tab,(i-1)*sizeof(struct student));
  208.                 i--;
  209.                 break;
  210.                    }
  211.             case 3:{
  212.                 zal(tab,szukaj(tab,i));
  213.                 break;
  214.                    }
  215.             case 4:{
  216.                 powt(&tab,i);
  217.                 break;
  218.                    }
  219.             case 5:{
  220.                 print(tab,szukaj(tab,i));
  221.                 break;
  222.                    }
  223.             case 6:{
  224.                 zapis(tab,i,"studenci.csv");
  225.                 break;
  226.                    }
  227.             case 7:{
  228.                 fre(&tab,i);
  229.                 tab=wczytaj(i,"studenci.csv");
  230.                 break;
  231.                    }
  232.             case 8:{
  233.                 druk(tab,i);
  234.                 break;
  235.                    }
  236.             default:{
  237.                 printf("Nie ma takiego polecenia \n");
  238.                 break;
  239.                     }
  240.         }
  241.     }
  242.     fre(&tab,i);
  243.     return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement