Advertisement
Guest User

Untitled

a guest
May 17th, 2015
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.45 KB | None | 0 0
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <iomanip>
  5. #include <fstream.h>
  6. #include <stdlib.h>
  7. #include <windows.h>
  8.  
  9.  
  10. const int L=31, N=100;
  11. struct apteka
  12. {
  13.   char nazv [L];
  14.   char adres [L];
  15.   char poztav [L];
  16.   int kolvo;
  17.   char preparat [L];
  18. };
  19.  
  20. class pharmacy
  21. {
  22.   private:int n;
  23.   apteka *px;
  24.   void udal(int j);
  25. public:
  26.   pharmacy(){}
  27.   ~pharmacy(){if(n>0)delete[]px;}
  28.   int k;
  29.   void create();
  30.   void reading();
  31.   void output();
  32.   void record();
  33.   void find();
  34.   void adding();
  35.   void del();
  36.   void correct();
  37.   void sorting();
  38. };
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. void main()
  46. {
  47.   SetConsoleOutputCP(1251);
  48.   SetConsoleCP(1251);
  49.   int w;
  50.   pharmacy a;
  51.   while(1)
  52.   {
  53.     clrscr();
  54.     cout<<endl;
  55.     cout<<"1. Open database.\n";
  56.     cout<<"2. Create database.\n";
  57.     cout<<"3. Show database at screen.\n";
  58.     cout<<"4. Add note to database.\n";
  59.     cout<<"5. Delete note from database.\n";
  60.     cout<<"6. Edit note in database.\n";
  61.     cout<<"7. Find note in database.\n";
  62.     cout<<"8. Sort database. \n";
  63.     cout<<"9. Save database to file.\n";
  64.     cout<<"0. Exit.\n"<<endl;
  65.     cout <<"Type menu number: ";
  66.     cin>>w;
  67.     switch(w)
  68.     {
  69.       case 1:a.reading();
  70.       break;
  71.       case 2:a.create();
  72.       break;
  73.       case 3:a.output();
  74.       getch();
  75.       break;
  76.       case 4:a.adding();
  77.       break;
  78.       case 5:a.del();
  79.       break;
  80.       case 6:a.correct();
  81.       break;
  82.       case 7:a.find();
  83.       getch();
  84.       break;
  85.       case 8:a.sorting();
  86.       break;
  87.       case 9:a.record();
  88.       break;
  89.       case 0:exit(0);
  90.       default:cout<<"Error!";getch();
  91.     }
  92.   }
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. void pharmacy::adding()
  101. {
  102.   apteka *s;
  103.   int i;
  104.   s=new apteka[n+1];
  105.   for(i=0;i<n;i++)
  106.   {
  107.     s[i]=px[i];
  108.   }
  109.   cout<<"Vvedite dannie:\n";
  110.   cout<<"Naimenovanie:\n";
  111.   cin>>s[n].nazv;
  112.   cout<<"Adress:\n";
  113.   cin>>s[n].adres;
  114.   cout<<"Postavshik:\n";
  115.   cin>>s[n].poztav;
  116.   cout<<"Kol-vo:\n";
  117.   cin>>s[n].kolvo;
  118.   cout<<"Preparat:\n";
  119.   cin>>s[n].preparat;
  120.   n++;
  121.   delete[]px;
  122.   px=s;
  123.   cout<<"Note has been added! ";
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. void pharmacy::reading()
  132. {
  133.   ifstream in;
  134.   char file[N];
  135.   int k, i;
  136.   apteka u;
  137.   cout<<"File name:\n";
  138.   cin>>file;
  139.   in.open(file);
  140.   if(!in)
  141.   {
  142.     cout<<file<<"Can't open file with database.\n";
  143.     getch();
  144.     exit(1);
  145.   }
  146.   n=0;
  147.   k=1;
  148.   char next;
  149.  
  150.  
  151.   /*
  152.   do
  153.   {
  154.     in>>u[k].nazv;
  155.     next = in.peek();
  156.     if (next == EOF) break;
  157.     //if (next == '\n')
  158.    // {
  159.    //   u.nazv<< '\n';
  160.    // }
  161.     in>>u.adres;
  162.     next = in.peek();
  163.     if (next == EOF) break;
  164.     in>>u.poztav;
  165.     next = in.peek();
  166.     if (next == EOF) break;
  167.     in>>u.kolvo;
  168.     next = in.peek();
  169.     if (next == EOF) break;
  170.     in>>u.preparat;
  171.     next = in.peek();
  172.     if (next == EOF) break;
  173.     n++;
  174.     k++;
  175.   }
  176.   while(in.good());
  177.  
  178.  
  179.   */
  180.   n--;
  181.   in.close();
  182.   px = new apteka[n];
  183.   if(px==NULL)
  184.   {
  185.     cout<<"Error: No memory!!!\n";
  186.     getch();
  187.     exit(1);
  188.   }
  189.   else
  190.   {
  191.     in.open(file);
  192.   /*  if(in.fail())
  193.     {
  194.       cout<<"Error: "<<file<<" is not opened.\n";
  195.       getch();
  196.       exit(1);
  197.     } */
  198.     for(i=0;i<n;i++)
  199.     {
  200.       in>>px[i].nazv;
  201.       next = in.peek();
  202.       if (next == EOF) break;
  203.      //if (next == '\n')
  204.       //{
  205.       //  u.nazv<< '\n';
  206.      // }
  207.       in>>px[i].adres;
  208.       next = in.peek();
  209.       if (next == EOF) break;
  210.       in>>px[i].poztav;
  211.       next = in.peek();
  212.       if (next == EOF) break;
  213.       in>>px[i].kolvo;
  214.       next = in.peek();
  215.       if (next == EOF) break;
  216.       in>>px[i].preparat;
  217.       next = in.peek();
  218.       if (next == EOF) break;
  219.     }
  220.     in.close();
  221.     cout<<"File opened! Show database?\n 1-Yes | 2-No\n";
  222.     cin>>i;
  223.     if (i=1)
  224.     {
  225.       output();
  226.     }
  227.   }
  228.  
  229.   getch();
  230. }
  231.  
  232.  
  233.  
  234.  
  235.  
  236. void pharmacy::create()
  237. {
  238.   clrscr();
  239.   cout<<"Create database."<<endl;
  240.   int i;
  241.   px=new apteka[n];
  242.   if(px==NULL)
  243.   {
  244.     cout<<"No memory.";
  245.     getch();
  246.     exit(1);
  247.   };
  248.   n=0;
  249.   adding();
  250.   do
  251.   {
  252.     cout<<"Add one more note?\n";
  253.     cout<<"1-Yes |";
  254.     cout<<" 2-No\n";
  255.     cin>>i;
  256.     if(i==1)
  257.     {
  258.       clrscr();
  259.       adding();
  260.     }
  261.   }
  262.   while(i==1);
  263. }
  264.  
  265.  
  266.  
  267.  
  268.  
  269. int sravn(apteka x,apteka y);
  270.  
  271.  
  272. void pharmacy::output()
  273. {
  274.   clrscr();
  275.   int i;
  276.   cout<<"********************************************************************"<<endl;
  277.   cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
  278.   cout<<"********************************************************************"<<endl;
  279.   for(i=0;i<n;i++)
  280.   {
  281.     cout<<i+1<<"."<<setw(3)<<setw(10)<<px[i].nazv<<" "<<setw(15)<<px[i].adres<<setw(15)<<px[i].poztav<<" "<<setw(5)<<px[i].kolvo<<" "<<setw(10)<<px[i].preparat<<endl;
  282.   }
  283.   cout<<""<<endl;
  284.   cout<<"Press any button to continue...";
  285. }
  286.  
  287.  
  288.  
  289.  
  290. void pharmacy::find()
  291. {
  292.   int b,i,k;
  293.   char p [20];
  294.   cout<<"Search notes.\n";
  295.   cout<<"1)Find by 'Naimenovanie'.\n"; cout<<"2)Find by 'Adress'.\n"; cout<<"3)Find by 'Postavshik'.\n"; cout<<"4)Find by 'Kol-vo'.\n"; cout<<"5)Find by 'Preparat'.\n";
  296.   cout<<"Type menu number: ";
  297.   cin>>b;
  298.   if(b==1)
  299.   {
  300.     cout<<"Naimenovanie:\n";cin>>p;
  301.     cout<<"********************************************************************"<<endl;
  302.     cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
  303.     cout<<"********************************************************************"<<endl;
  304.     for(i=0;i<n;i++)
  305.     if (strcmp(px[i].nazv,p)==0)
  306.     {
  307.       cout<<i+1<<"."<<setw(3)<<setw(10)<<px[i].nazv<<" "<<setw(15)<<px[i].adres<<setw(15)<<px[i].poztav<<" "<<setw(5)<<px[i].kolvo<<" "<<setw(10)<<px[i].preparat<<endl;
  308.     }
  309.   }
  310.   if(b==2)
  311.   {
  312.     cout<<"Adress:\n";cin>>p;
  313.     cout<<"********************************************************************"<<endl;
  314.     cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
  315.     cout<<"********************************************************************"<<endl;
  316.     for(i=0;i<n;i++)
  317.     if (strcmp(px[i].adres,p)==0)
  318.     {
  319.       cout<<i+1<<"."<<setw(3)<<setw(10)<<px[i].nazv<<" "<<setw(15)<<px[i].adres<<setw(15)<<px[i].poztav<<" "<<setw(5)<<px[i].kolvo<<" "<<setw(10)<<px[i].preparat<<endl;
  320.     }
  321.   }
  322.   if(b==3)
  323.   {
  324.     cout<<"Postavshik:\n";cin>>p;
  325.     cout<<"********************************************************************"<<endl;
  326.     cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
  327.     cout<<"********************************************************************"<<endl;
  328.     for(i=0;i<n;i++)
  329.     if (strcmp(px[i].poztav,p)==0)
  330.     {
  331.       cout<<i+1<<"."<<setw(3)<<setw(10)<<px[i].nazv<<" "<<setw(15)<<px[i].adres<<setw(15)<<px[i].poztav<<" "<<setw(5)<<px[i].kolvo<<" "<<setw(10)<<px[i].preparat<<endl;
  332.     }
  333.   }
  334.   if(b==4)
  335.   {
  336.     cout<<"Kol-vo:\n";cin>>k;
  337.     cout<<"********************************************************************"<<endl;
  338.     cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
  339.     cout<<"********************************************************************"<<endl;
  340.     for(i=0;i<n;i++)
  341.     if (px[i].kolvo==k)
  342.     {
  343.       cout<<i+1<<"."<<setw(3)<<setw(10)<<px[i].nazv<<" "<<setw(15)<<px[i].adres<<setw(15)<<px[i].poztav<<" "<<setw(5)<<px[i].kolvo<<" "<<setw(10)<<px[i].preparat<<endl;
  344.     }
  345.   }
  346.   if(b==5)
  347.   {
  348.     cout<<"Preparat:\n";cin>>p;
  349.     cout<<"********************************************************************"<<endl;
  350.     cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
  351.     cout<<"********************************************************************"<<endl;
  352.     for(i=0;i<n;i++)
  353.     if (strcmp(px[i].preparat,p)==0)
  354.     {
  355.       cout<<i+1<<"."<<setw(3)<<setw(10)<<px[i].nazv<<" "<<setw(15)<<px[i].adres<<setw(15)<<px[i].poztav<<" "<<setw(5)<<px[i].kolvo<<" "<<setw(10)<<px[i].preparat<<endl;
  356.     }
  357.   }
  358. }
  359.  
  360.  
  361.  
  362.  
  363.  
  364. void pharmacy::record()
  365. {
  366.   int i;
  367.   ofstream out;
  368.   char file[N];
  369.   cout<<"Type file name:\n";
  370.   cin>>file;
  371.   out.open(file);
  372.   if(out==NULL)
  373.   {
  374.     cout<<file<<"File not created.";
  375.     getch();
  376.     exit(1);
  377.   }
  378. //  out<<"********************************************************************"<<endl;
  379. //  out<<""<<setw(10)<<"Наименование"<<" "<<setw(15)<<"Адрес"<<" "<<setw(15)<<"Поставщик"<<" "<<setw(5)<<"Кол-во"<<" "<<setw(10)<<"Препарат"<<endl;
  380. //  out<<"********************************************************************"<<endl;
  381.   for(i=0;i<n;i++)
  382.   {
  383.     out<<i+1<<"."<<setw(3)<<setw(10)<<px[i].nazv<<" "<<setw(15)<<px[i].adres<<setw(15)<<px[i].poztav<<" "<<setw(5)<<px[i].kolvo<<" "<<setw(10)<<px[i].preparat<<endl;
  384.   }
  385.   getch();
  386.   out.close();
  387. }
  388.  
  389.  
  390. int sravn(apteka x,apteka y)
  391. {
  392.   if(strcmp(x.nazv,y.nazv)>0)return 1;
  393.   if(strcmp(x.nazv,y.nazv)<0)return -1;
  394.   return 0;
  395. }
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404. void pharmacy::correct()
  405. {
  406.   int i,q,j;
  407.   output();
  408.   cout<<"Type string number to edit: ";
  409.   cin>>j;
  410.   for(i=0;i<n;i++)
  411.   {
  412.     if(i==j-1)
  413.     {
  414.       cout<<"Введите изменённую информацию:\n";
  415.       cout<<"Naimenovanie:\n"<<""<<px[i].nazv<<endl<<"Edit 'Naimenovanie'?\n"<<"1-Yes.0-No\n";
  416.       cin>>q;
  417.       if(q==1)
  418.       {
  419.         cout<<"Type new 'Naimenovanie':\n";
  420.         cin>>px[i].nazv;
  421.       }
  422.       cout<<"Adress:\n"<<""<<px[i].adres<<endl<<"Edit 'Adress'?\n"<<"1-Yes.0-No\n";
  423.       cin>>q;
  424.       if(q==1)
  425.       {
  426.         cout<<"Type new 'Adress':\n";
  427.         cin>>px[i].adres;
  428.       }
  429.       cout<<"Postavshik:\n"<<""<<px[i].poztav<<endl<<"Edit 'Postavshik'?\n"<<"1-Yes.0-No\n";
  430.       cin>>q;
  431.       if(q==1)
  432.       {
  433.         cout<<"Type new 'Postavshik':\n";
  434.         cin>>px[i].poztav;
  435.       }
  436.       cout<<"Kol-vo:\n"<<""<<px[i].kolvo<<endl<<"Edit 'Kol-vo'?\n"<<"1-Yes.0-No\n";
  437.       cin>>q;
  438.       if(q==1)
  439.       {
  440.         cout<<"Type new 'Kol-vo':\n";
  441.         cin>>px[i].kolvo;
  442.       }
  443.       cout<<"Preparat:\n"<<""<<px[i].preparat<<endl<<"Edit 'Preparat'?\n"<<"1-Yes.0-No\n";
  444.       cin>>q;
  445.       if(q==1)
  446.       {
  447.         cout<<"Type new 'Preparat':\n";
  448.         cin>>px[i].preparat;
  449.       }
  450.       cout<<endl<<"Done!\n.";
  451.       break;
  452.     }
  453.   }
  454.   output();
  455.   getch();
  456. }
  457.  
  458.  
  459.  
  460.  
  461. void pharmacy::del()
  462. {
  463.   int i,h,fl;
  464.   output();
  465.   cout<<"Type note number: ";
  466.   cin>>h;
  467.   fl=0;
  468.   for(i=0;i<n;i++)
  469.   {
  470.     if(i==h-1)
  471.     {
  472.       fl=1;
  473.       udal(i);
  474.       cout<<endl<<"Done!\n";
  475.     }
  476.   }
  477.   if(fl==0)
  478.   {
  479.     cout<<"Note not found.\n";
  480.   }
  481.   output();
  482.   getch();
  483. }
  484.  
  485.  
  486.  
  487.  
  488. void pharmacy::udal(int h)
  489. {
  490.   int i;
  491.   if(h<0||h>=n)
  492.   {
  493.     cout<<"This string doesn't exist.";
  494.     getch();
  495.     return;
  496.   }
  497.   for(i=h+1;i<n;i++)
  498.     px[i-1]=px[i];
  499.   n--;
  500. }
  501.  
  502.  
  503.  
  504.  
  505. void pharmacy::sorting()
  506. {
  507.   int i,p,fl;
  508.   apteka r;
  509.   cout<<"Sort.\n";
  510.   cout<<"1.Sort by 'Naimenovanie'\n";
  511.   cout<<"2.Sort by 'Adress'\n";
  512.   cout<<"3.Sort by 'Postavshik'\n";
  513.   cout<<"4.Sort by 'Kol-vo'\n";
  514.   cout<<"5.Sort by 'Preparat'\n";
  515.   cout<<"Type menu number: ";
  516.   cin>>p;
  517.   do
  518.   {
  519.     for(i=0;i<(n-1);i++)
  520.     {
  521.       if(p==1)
  522.       {
  523.         fl=0;
  524.         if(strcmp(px[i+1].nazv,px[i].nazv)<0)
  525.         {
  526.           r=px[i];
  527.           px[i]=px[i+1];
  528.           px[i+1]=r;
  529.           fl=1;
  530.           break;
  531.         }
  532.       }
  533.       if(p==2)
  534.       {
  535.         fl=0;
  536.         if(strcmp(px[i+1].adres,px[i].adres)<0)
  537.         {
  538.           r=px[i];
  539.           px[i]=px[i+1];
  540.           px[i+1]=r;
  541.           fl=1;
  542.           break;
  543.         }
  544.       }
  545.       if(p==3)
  546.       {
  547.         fl=0;
  548.         if(strcmp(px[i+1].poztav,px[i].poztav)<0)
  549.         {
  550.           r=px[i];
  551.           px[i]=px[i+1];
  552.           px[i+1]=r;
  553.           fl=1;
  554.           break;
  555.         }
  556.       }
  557.       if(p==4)
  558.       {
  559.         fl=0;
  560.         if(px[i+1].kolvo<px[i].kolvo)
  561.         {
  562.           r=px[i];
  563.           px[i]=px[i+1];
  564.           px[i+1]=r;
  565.           fl=1;
  566.           break;
  567.         }
  568.       }
  569.       if(p==5)
  570.       {
  571.         fl=0;
  572.         if(strcmp(px[i+1].preparat,px[i].preparat)<0)
  573.         {
  574.           r=px[i];
  575.           px[i]=px[i+1];
  576.           px[i+1]=r;
  577.           fl=1;
  578.           break;
  579.         }
  580.       }
  581.     }
  582.   }
  583.   while(fl==1);
  584. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement