Advertisement
vesso8

library

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