Advertisement
vesso8

library_zad

Oct 20th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. #include<iostream>
  2. #include <string.h>
  3. #include <fstream>
  4. #include <cstring>
  5. #include <string>
  6. #include <conio.h>
  7. #include <stdlib.h>
  8. #include <iomanip>
  9. using namespace std;
  10.  
  11. class item
  12. {
  13.     private:
  14.         int code;
  15.         char name[15];
  16.         int Qty;
  17.     public:
  18.         void get_item(void);
  19.         void put_item(void);
  20.         int get_code(void)
  21.         {
  22.             return code;
  23.         }
  24. void update_Qty(int num)
  25.   {
  26.     Qty=Qty-num;
  27.   }
  28. };
  29.  
  30.  
  31. void item :: get_item(void)
  32. {
  33.     cout<<"Vyvedi nomera na kniga:"; cin>>code;
  34.     fflush(stdin);
  35.     cout<<"Vyvedi zaglavie na kniga:"; cin>>name;
  36.     cout<<"Vyvedi kolichestvo:"; cin>>Qty;
  37. }
  38.  
  39. void item:: put_item(void)
  40.   {
  41.     cout<<setw(6)<<code
  42.     <<setw(15)<<name
  43.     <<setw(6)<<Qty<<endl;
  44.   }
  45.  
  46.  
  47. void add_record(void);
  48. void show_record(void);
  49. void show_All(void);
  50. void delete_record(void);
  51. void modify_record(void);
  52.  
  53. item it;
  54. fstream file;
  55.  
  56.  
  57. int main()
  58. {
  59.     int option;
  60.     while(1)
  61.     { system("cls");
  62.       cout<<"***Menu**"<<endl;
  63.       cout<<"1.Dobavi nov zapis"<<endl;
  64.       cout<<"2.Pokaji vsichki zapisi"<<endl;
  65.       cout<<"3.Pokaji izbran zapis"<<endl;
  66.       cout<<"4.Iztrii zapis"<<endl;
  67.       cout<<"5.Redaktirai zapis"<<endl;
  68.       cout<<"6.Izhod"<<endl;
  69.       cout<<"Izberi opciq No.: "; cin>>option;
  70.       switch(option)
  71.       {
  72.        case 1:{ add_record();
  73.                 cout<<"Natisni buton za vryshtane v glavnoto menu...";
  74.                 getch();
  75.                 break;
  76.             }
  77.      case 2:{
  78.                     cout<<"--------------------------------------------\n";
  79.                     cout<<setw(6)<<"Code"<<setw(15)<<"Name"<<setw(6)<<"Qty"<<endl;
  80.                     cout<<"--------------------------------------------\n";
  81.                     show_All();
  82.                     cout<<"--------------------------------------------\n";
  83.                     cout<<"Natisni buton za vryshtane v glavnoto menu...";
  84.                     getch();           
  85.                     break;
  86.             }
  87.     case 3:{
  88.                 show_record();
  89.                 cout<<"Natisni buton za vryshtane v glavnoto menu...";
  90.                 getch();
  91.                 break;
  92.             }
  93.     case 4:{
  94.                 delete_record();
  95.                 cout<<"Natisni buton za vryshtane v glavnoto menu...";
  96.                 getch();
  97.                 break;
  98.             }
  99.     case 5:{    modify_record();
  100.                 cout<<"Natisni buton za vryshtane v glavnoto menu...";
  101.                 getch();
  102.                 break;
  103.             }
  104.     case 6:{
  105.                 exit(0);
  106.             }
  107.     default:{
  108.                 cout<<"Nevalidna komanda, Natisni buton za vryshtane v glavnoto menu...";
  109.                 getch();
  110.                 break;
  111.             }
  112.         }
  113.     }
  114. return 0;
  115. }
  116.  
  117. void add_record()
  118. {
  119.     char ch= 'y';
  120.     file.open("stock.dat", ios::app | ios::binary);
  121.     while(ch=='y'|| ch=='Y')
  122.     {
  123. it.get_item();
  124. file.write((char*)&it, sizeof(it));
  125. cout<<"Dobavi oshte zapisi....(y/n)?";
  126. cin>>ch;
  127.     }
  128.     file.close();  
  129. }
  130.  
  131. void show_All(void)
  132. {
  133.     file.open("stock.dat", ios::in | ios::binary);
  134.     if(! file)
  135.     {
  136.         cout<<"Faila ne e nameren";
  137.         exit(0);
  138.     }
  139. else
  140. {
  141.     file.read((char*)&it, sizeof(it));
  142.     while(! file.eof())
  143.     {
  144.         it.put_item();
  145.         file.read((char*)&it, sizeof(it));
  146.     }
  147.    
  148. }
  149. file.close();
  150. }
  151.  
  152. void show_record(void)
  153. {
  154.     int no, flag = 0;
  155.     file.open("stock.dat", ios::in| ios::binary);
  156.     if(!file)
  157.     {
  158.         cout<<"Faila ne e nameren";
  159.         exit(0);
  160.     }
  161. else
  162. {
  163.     cout<<"Vyvedete nomera na kniga: ";
  164.     cin>>no;
  165.     file.read((char*)&it, sizeof(it));
  166.     while(!file.eof())
  167.     {
  168.         if(no==it.get_code())
  169.         {
  170.             flag=1;
  171.             cout<<"--------------------------------------------\n";
  172.             cout<<setw(6)<<"Code"<<setw(15)<<"Name"<<setw(6)<<"Qty"<<endl;
  173.             cout<<"--------------------------------------------\n";
  174.             it.put_item();
  175.             cout<<"--------------------------------------------\n";
  176.             break;
  177.         }
  178.         file.read((char*)&it, sizeof(it));
  179.     }
  180.     if(flag==0)
  181.     {
  182.         cout<<"Produkta ne e nameren...\n";
  183.     }
  184. }
  185. file.close();
  186. }
  187.  
  188. void delete_record()
  189. {
  190.     int no;
  191.     cout<<"Vyvedete nomer na kniga za iztrivane: ";
  192.     cin >> no;
  193.     ofstream file2;
  194.     file2.open("new.dat", ios::out|ios::binary);
  195.     file.open("stock.dat", ios:: in|ios::binary);
  196.     if(! file)
  197.     {
  198.         cout<<"Faila ne e nameren";
  199.         exit(0);
  200.     }
  201. else
  202. {
  203.     file.read((char*)&it, sizeof(it));
  204.     while(!file.eof())
  205.     {
  206.         if(no!= it.get_code())
  207.         {
  208.             file2.write((char*)&it, sizeof(it));
  209.         }
  210.         file.read((char*)&it, sizeof(it));
  211.        
  212.     }
  213. }
  214. file2.close();
  215. file.close();
  216. remove("stock.dat");
  217. rename("new.dat", "stock.dat");
  218. }
  219.  
  220. void modify_record(void)
  221. {
  222.     int no, num;
  223.     cout<<"Vyvedete nomer na kniga za redakciq:"; cin >> no;
  224.     cout<<"Vyvedete kolichestvo: "; cin >> num;
  225.     file.open("stock.dat", ios:: in| ios::out | ios::binary);
  226.     if(! file)
  227.     {
  228.         cout<<"Faila ne e nameren";
  229.         exit(0);
  230.        
  231.     }
  232.     while (file.read((char*)&it, sizeof(it)))
  233.     {
  234.         if(it.get_code()== no)
  235.         {
  236.             it.update_Qty(num);
  237.             int pos = sizeof(it);
  238.             file.seekp(-pos, ios::cur);
  239.             file.write((char*)&it, sizeof(it));
  240.         }
  241.     }
  242.     file.close();
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement