Advertisement
Hadix

Projekt kasa fiskalna v2

Jan 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.75 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <locale.h>
  6. #include <string.h>
  7. #include <time.h>
  8.  
  9.  
  10. struct produkt
  11. {
  12.     char nazwa[100];
  13.     char cena[100];
  14.     char numer[100];
  15.  
  16. };
  17.  
  18. struct paragon
  19. {
  20.     struct produkt zakupiony;
  21.     struct paragon* poprzedni;
  22. };
  23.  
  24. struct magazyn
  25. {
  26.     struct magazyn* poprzedni;
  27.     struct produkt artykul;
  28.     struct magazyn* nastepny;
  29. };
  30.  
  31.  
  32. void dodawanieDoListy(struct magazyn** gora, struct produkt dane)
  33. {
  34.     struct magazyn* nowy = (struct magazyn*)malloc(sizeof(struct magazyn));
  35.     strcpy(nowy->artykul.nazwa, dane.nazwa);
  36.     strcpy(nowy->artykul.cena, dane.cena);
  37.     strcpy(nowy->artykul.numer, dane.numer);
  38.     nowy->poprzedni = 0;
  39.     nowy->nastepny = *gora;
  40.     if (*gora != NULL)
  41.         (*gora)->poprzedni = nowy;
  42.     (*gora) = nowy;
  43.  
  44. }
  45.  
  46. void odczytywamieZpliku(struct magazyn **gora, int *ile)
  47. {
  48.     char znak[200];
  49.     char nazwa[300] = "Magazyn.csv";
  50.     FILE* plik;
  51.     plik = fopen(nazwa, "r");
  52.     if (plik == NULL)
  53.     {
  54.         perror("Nie udalo sie otworzyc magazynu");
  55.         return 1;
  56.     }
  57.     else {
  58.         puts("Sukces! Poprawnie wczytano produkty z magazynu");
  59.     }
  60.     struct produkt artykul;
  61.     while (fscanf(plik, "%s", znak) != EOF) {
  62.         sscanf(znak, "%[^;] ;%[^;] ;%[^;] ;%s ", artykul.nazwa, artykul.numer, artykul.cena);
  63.         dodawanieDoListy(gora, artykul);
  64.         (*ile)++;
  65.     }
  66. }
  67.  
  68. void wyswietlaniemagazynu(struct magazyn *gora)
  69. {
  70.     if (gora == NULL) {
  71.         puts("Magazyn jest pusty");
  72.     }
  73.     while (gora != NULL) {
  74.         printf("%s %s %s\n", (gora)->artykul.nazwa, (gora)->artykul.cena, (gora)->artykul.numer);
  75.         (gora) = (gora)->nastepny;
  76.     }
  77.  
  78. }
  79.  
  80.  
  81. void push(struct paragon** gora1, struct magazyn* produkt)
  82. {
  83.  
  84.     struct paragon* nowy = (struct paragon*)malloc(sizeof(struct paragon));
  85.     strcpy(nowy->zakupiony.nazwa, produkt->artykul.nazwa);
  86.     strcpy(nowy->zakupiony.cena, produkt->artykul.cena);
  87.    
  88.     nowy->poprzedni = NULL;
  89.     if (*gora1 == NULL)
  90.         *gora1 = nowy;
  91.     else {
  92.         nowy->poprzedni = *gora1;
  93.         *gora1 = nowy;
  94.     }
  95. }
  96.  
  97. void wyswietlparagon(struct paragon* gora, int *suma, int *reszta)
  98.  
  99. {  
  100.     time_t current_time;
  101.     char* c_time_string;
  102.     current_time = time(NULL);
  103.     c_time_string = ctime(&current_time);
  104.     puts("---------------------------------");
  105.     puts("SKLEP JASZEXPOL");
  106.     puts("Paragon:");
  107.     while (gora != NULL) {
  108.         printf("%s, %s\n", gora->zakupiony.nazwa, gora->zakupiony.cena);
  109.         gora = gora->poprzedni;
  110.     }
  111.     printf("Suma: %d\n", *suma);
  112.     printf("Reszta: %d\n", *reszta);
  113.     printf("Paragon wydany %s", c_time_string);
  114.     puts("---------------------------------");
  115.        
  116. }
  117.  
  118.  
  119.  
  120. void dodawaniedoparagaonu(struct magazyn** gora, struct paragon** gora1, int liczikzakup, int *suma)
  121. {
  122.  
  123.     if (*gora == NULL || liczikzakup <= 0)
  124.         return;
  125.     struct magazyn* aktywny = *gora;
  126.     int i;
  127.     for (int i = 1; aktywny != NULL && i < liczikzakup; i++)
  128.         aktywny = aktywny->nastepny;
  129.     if (aktywny == NULL)
  130.         return;
  131.     *suma += atoi(aktywny->artykul.cena);
  132.     push(gora1, aktywny);
  133. }
  134.  
  135. void wyszuakniezakupu(struct magazyn* gora)
  136. {
  137.     while ((gora) != NULL)
  138.     {
  139.         (gora) = (gora)->nastepny;
  140.     }
  141.     printf("%s %s %s\n", (gora)->artykul.nazwa, (gora)->artykul.cena, (gora)->artykul.numer);
  142. }
  143.  
  144. void freszta(int *suma, int *reszta, int *gotowka)
  145. {
  146.     puts("Wprowdz kwote jaka zaplacil klient:");
  147.     while (scanf("%d", gotowka) != 1 || *gotowka<*suma)
  148.     {
  149.         puts("Kwota jest za niska lub ma bledny format!");
  150.         int c;
  151.         while ((c = getchar()) != '\n' && c != EOF );
  152.     }
  153.     *reszta = *gotowka - *suma;
  154.     printf("Reszta: %d\n", *reszta);
  155. }
  156.  
  157. void drukujParagon(struct paragon* gora, int *suma, int *reszta, int *zaplacono)
  158. {
  159.     time_t current_time;
  160.     char* c_time_string;
  161.     current_time = time(NULL);
  162.     c_time_string = ctime(&current_time);
  163.     FILE *plik;
  164.     plik = fopen("G:\\paragon.txt", "w");
  165.     fputs("-----------------------------------\n", plik);
  166.     fputs("Paragon FISKALNY\n", plik);
  167.     fputs("SKLEP JANUSZOP\n", plik);
  168.     fputs("-----------------------------------\n", plik);
  169.     while (gora != NULL) {
  170.         fprintf(plik, "%s, %szl\n", gora->zakupiony.nazwa, gora->zakupiony.cena);
  171.         gora = gora->poprzedni;
  172.     }
  173.     fprintf(plik,"Suma: %dzl\n", *suma);
  174.     fprintf(plik, "Zaplacono: %dzl\n", *zaplacono);
  175.     fprintf(plik,"Reszta: %dzl\n", *reszta);
  176.     fprintf(plik, "Paragon wydany %s", c_time_string);
  177.     fclose(plik);
  178. }
  179.  
  180.     int main()
  181.     {
  182.         int gotowka = 0;
  183.         int reszta = 0;
  184.         int licznikdel = 0;
  185.         int licznikdod = 0;
  186.         int suma = 0;
  187.         setlocale(LC_ALL, "");
  188.         struct paragon* dol = NULL;
  189.         struct magazyn* gora = NULL;
  190.         struct listakontatkow* nowy = NULL;
  191.         struct listakontaktow* usun = NULL;
  192.         char wybor[30];
  193.         int a = 1;
  194.         int licznik = 0;
  195.         int ilosczakupow;
  196.         int zakup;
  197.         int czyZakup = 0;
  198.         int czyWczytana = 0;
  199.         int czyZaplacone = 0;
  200.         int wybsort = 0;
  201.         puts("----------------------------");
  202.         puts("KASA FISKALNA");
  203.         puts("----------------------------");
  204.         puts("Wpisz:\nq - Wczytaj magazyn\nw - Wyswietl zawartosc magazynu\nz - Zakupy\ny - Wyswietl paragon\nr - Wydaj reszta\nc - Czyszczenie ekranu\nk - Zamknij");
  205.         while (a == 1) {
  206.             do {
  207.                 scanf("%s", &wybor);
  208.             } while (wybor[0] != 'q' && wybor[0] != 'c' && wybor[0] != 'z' && wybor[0] != 'y' && wybor[0] != 'k' && wybor[0] != 'w'  && wybor[0] != 't' && wybor[0] != 'f'  && wybor[0] != 's' && wybor[0] != 'd' && wybor[0] != 'f' && wybor[0] != 'r');
  209.             switch (wybor[0]) {
  210.             case 'q':
  211.                 odczytywamieZpliku(&gora, &licznik);
  212.                 czyWczytana = 1;
  213.                 break;
  214.             case 'w':
  215.                 if (czyWczytana == 1) {
  216.                     wyswietlaniemagazynu(gora);
  217.                 }
  218.                 else {
  219.                     puts("Najpierw wczytaj asortyment");
  220.                 }
  221.                 break;
  222.             case 'z':
  223.                 if (czyWczytana == 1) {
  224.                     srand(time(NULL));
  225.                     ilosczakupow = 1 + rand() % 10;
  226.                     //printf("ilosc zakupow:%d\n", ilosczakupow);
  227.                     for (int i = 1; i <= ilosczakupow; i++) {
  228.  
  229.                         zakup = 1 + rand() % licznik;
  230.                         //printf("zakup: %d\n", zakup);
  231.                         dodawaniedoparagaonu(&gora, &dol, zakup, &suma);
  232.                     }
  233.                     printf("Do zaplaty: %d\n", suma);
  234.                     czyZakup = 1;
  235.                 }
  236.                 else {
  237.                     puts("Najpierw wczytaj asorytyment");
  238.                 }
  239.                 break;
  240.             case 'y':
  241.                 if (czyZaplacone == 1) {
  242.                     wyswietlparagon(dol, &suma, &reszta);
  243.                 }
  244.                 else {
  245.                     puts("Klient musi najpierw zapalcic!");
  246.                 }
  247.                 break;
  248.             case 'r':
  249.                 if(czyZakup==1)
  250.                 freszta(&suma, &reszta, &gotowka);
  251.                 else {
  252.                     puts("Najpierw skasuj zakupy");
  253.                 }
  254.                 czyZaplacone = 1;
  255.                 break;
  256.             case 'd':
  257.                 if (czyZaplacone == 1) {
  258.                     drukujParagon(dol, &suma, &reszta, &gotowka);
  259.                 }
  260.                 else {
  261.                     puts("Klient musi najpier zaplacic!");
  262.                 }
  263.                 break;
  264.             case 'k':
  265.                 a = 0;
  266.                 break;
  267.             case 'c':
  268.                 system("cls");
  269.                 break;
  270.             }
  271.             if (a == 1)
  272.                 puts("Wpisz:\nq - Wczytaj magazyn\nw - Wyswietl zawartosc magazynu\nz - Zakupy\ny - Wyswietl paragon\nr - Wydaj reszta\nc - Czyszczenie ekranu\nk - Zamknij");
  273.         }
  274.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement