Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream.h>
- #include <conio.h>
- #include <string.h>
- #include <iomanip>
- #include <fstream.h>
- #include <stdlib.h>
- #include <windows.h>
- const int L=31, N=100;
- struct apteka
- {
- char nazv [L];
- char adres [L];
- char poztav [L];
- int kolvo;
- char preparat [L];
- };
- class pharmacy
- {
- private:int n;
- apteka *px;
- void udal(int j);
- public:
- pharmacy(){}
- ~pharmacy(){if(n>0)delete[]px;}
- int k;
- void create();
- void reading();
- void output();
- void record();
- void find();
- void adding();
- void del();
- void correct();
- void sorting();
- };
- void main()
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- int w;
- pharmacy a;
- while(1)
- {
- clrscr();
- cout<<endl;
- cout<<"1. Open database.\n";
- cout<<"2. Create database.\n";
- cout<<"3. Show database at screen.\n";
- cout<<"4. Add note to database.\n";
- cout<<"5. Delete note from database.\n";
- cout<<"6. Edit note in database.\n";
- cout<<"7. Find note in database.\n";
- cout<<"8. Sort database. \n";
- cout<<"9. Save database to file.\n";
- cout<<"0. Exit.\n"<<endl;
- cout <<"Type menu number: ";
- cin>>w;
- switch(w)
- {
- case 1:a.reading();
- break;
- case 2:a.create();
- break;
- case 3:a.output();
- getch();
- break;
- case 4:a.adding();
- break;
- case 5:a.del();
- break;
- case 6:a.correct();
- break;
- case 7:a.find();
- getch();
- break;
- case 8:a.sorting();
- break;
- case 9:a.record();
- break;
- case 0:exit(0);
- default:cout<<"Error!";getch();
- }
- }
- }
- void pharmacy::adding()
- {
- apteka *s;
- int i;
- s=new apteka[n+1];
- for(i=0;i<n;i++)
- {
- s[i]=px[i];
- }
- cout<<"Vvedite dannie:\n";
- cout<<"Naimenovanie:\n";
- cin>>s[n].nazv;
- cout<<"Adress:\n";
- cin>>s[n].adres;
- cout<<"Postavshik:\n";
- cin>>s[n].poztav;
- cout<<"Kol-vo:\n";
- cin>>s[n].kolvo;
- cout<<"Preparat:\n";
- cin>>s[n].preparat;
- n++;
- delete[]px;
- px=s;
- cout<<"Note has been added! ";
- }
- void pharmacy::reading()
- {
- ifstream in;
- char file[N];
- int k, i;
- apteka u;
- cout<<"File name:\n";
- cin>>file;
- in.open(file);
- if(!in)
- {
- cout<<file<<"Can't open file with database.\n";
- getch();
- exit(1);
- }
- n=0;
- k=1;
- char next;
- /*
- do
- {
- in>>u[k].nazv;
- next = in.peek();
- if (next == EOF) break;
- //if (next == '\n')
- // {
- // u.nazv<< '\n';
- // }
- in>>u.adres;
- next = in.peek();
- if (next == EOF) break;
- in>>u.poztav;
- next = in.peek();
- if (next == EOF) break;
- in>>u.kolvo;
- next = in.peek();
- if (next == EOF) break;
- in>>u.preparat;
- next = in.peek();
- if (next == EOF) break;
- n++;
- k++;
- }
- while(in.good());
- */
- n--;
- in.close();
- px = new apteka[n];
- if(px==NULL)
- {
- cout<<"Error: No memory!!!\n";
- getch();
- exit(1);
- }
- else
- {
- in.open(file);
- /* if(in.fail())
- {
- cout<<"Error: "<<file<<" is not opened.\n";
- getch();
- exit(1);
- } */
- for(i=0;i<n;i++)
- {
- in>>px[i].nazv;
- next = in.peek();
- if (next == EOF) break;
- //if (next == '\n')
- //{
- // u.nazv<< '\n';
- // }
- in>>px[i].adres;
- next = in.peek();
- if (next == EOF) break;
- in>>px[i].poztav;
- next = in.peek();
- if (next == EOF) break;
- in>>px[i].kolvo;
- next = in.peek();
- if (next == EOF) break;
- in>>px[i].preparat;
- next = in.peek();
- if (next == EOF) break;
- }
- in.close();
- cout<<"File opened! Show database?\n 1-Yes | 2-No\n";
- cin>>i;
- if (i=1)
- {
- output();
- }
- }
- getch();
- }
- void pharmacy::create()
- {
- clrscr();
- cout<<"Create database."<<endl;
- int i;
- px=new apteka[n];
- if(px==NULL)
- {
- cout<<"No memory.";
- getch();
- exit(1);
- };
- n=0;
- adding();
- do
- {
- cout<<"Add one more note?\n";
- cout<<"1-Yes |";
- cout<<" 2-No\n";
- cin>>i;
- if(i==1)
- {
- clrscr();
- adding();
- }
- }
- while(i==1);
- }
- int sravn(apteka x,apteka y);
- void pharmacy::output()
- {
- clrscr();
- int i;
- cout<<"********************************************************************"<<endl;
- cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
- cout<<"********************************************************************"<<endl;
- for(i=0;i<n;i++)
- {
- 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;
- }
- cout<<""<<endl;
- cout<<"Press any button to continue...";
- }
- void pharmacy::find()
- {
- int b,i,k;
- char p [20];
- cout<<"Search notes.\n";
- 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";
- cout<<"Type menu number: ";
- cin>>b;
- if(b==1)
- {
- cout<<"Naimenovanie:\n";cin>>p;
- cout<<"********************************************************************"<<endl;
- cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
- cout<<"********************************************************************"<<endl;
- for(i=0;i<n;i++)
- if (strcmp(px[i].nazv,p)==0)
- {
- 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;
- }
- }
- if(b==2)
- {
- cout<<"Adress:\n";cin>>p;
- cout<<"********************************************************************"<<endl;
- cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
- cout<<"********************************************************************"<<endl;
- for(i=0;i<n;i++)
- if (strcmp(px[i].adres,p)==0)
- {
- 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;
- }
- }
- if(b==3)
- {
- cout<<"Postavshik:\n";cin>>p;
- cout<<"********************************************************************"<<endl;
- cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
- cout<<"********************************************************************"<<endl;
- for(i=0;i<n;i++)
- if (strcmp(px[i].poztav,p)==0)
- {
- 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;
- }
- }
- if(b==4)
- {
- cout<<"Kol-vo:\n";cin>>k;
- cout<<"********************************************************************"<<endl;
- cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
- cout<<"********************************************************************"<<endl;
- for(i=0;i<n;i++)
- if (px[i].kolvo==k)
- {
- 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;
- }
- }
- if(b==5)
- {
- cout<<"Preparat:\n";cin>>p;
- cout<<"********************************************************************"<<endl;
- cout<<""<<setw(10)<<"Naimenovanie"<<" "<<setw(15)<<"Adress"<<" "<<setw(15)<<"Postavshik"<<" "<<setw(5)<<"Kol-vo"<<" "<<setw(10)<<"Preparat"<<endl;
- cout<<"********************************************************************"<<endl;
- for(i=0;i<n;i++)
- if (strcmp(px[i].preparat,p)==0)
- {
- 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;
- }
- }
- }
- void pharmacy::record()
- {
- int i;
- ofstream out;
- char file[N];
- cout<<"Type file name:\n";
- cin>>file;
- out.open(file);
- if(out==NULL)
- {
- cout<<file<<"File not created.";
- getch();
- exit(1);
- }
- // out<<"********************************************************************"<<endl;
- // out<<""<<setw(10)<<"Наименование"<<" "<<setw(15)<<"Адрес"<<" "<<setw(15)<<"Поставщик"<<" "<<setw(5)<<"Кол-во"<<" "<<setw(10)<<"Препарат"<<endl;
- // out<<"********************************************************************"<<endl;
- for(i=0;i<n;i++)
- {
- 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;
- }
- getch();
- out.close();
- }
- int sravn(apteka x,apteka y)
- {
- if(strcmp(x.nazv,y.nazv)>0)return 1;
- if(strcmp(x.nazv,y.nazv)<0)return -1;
- return 0;
- }
- void pharmacy::correct()
- {
- int i,q,j;
- output();
- cout<<"Type string number to edit: ";
- cin>>j;
- for(i=0;i<n;i++)
- {
- if(i==j-1)
- {
- cout<<"Введите изменённую информацию:\n";
- cout<<"Naimenovanie:\n"<<""<<px[i].nazv<<endl<<"Edit 'Naimenovanie'?\n"<<"1-Yes.0-No\n";
- cin>>q;
- if(q==1)
- {
- cout<<"Type new 'Naimenovanie':\n";
- cin>>px[i].nazv;
- }
- cout<<"Adress:\n"<<""<<px[i].adres<<endl<<"Edit 'Adress'?\n"<<"1-Yes.0-No\n";
- cin>>q;
- if(q==1)
- {
- cout<<"Type new 'Adress':\n";
- cin>>px[i].adres;
- }
- cout<<"Postavshik:\n"<<""<<px[i].poztav<<endl<<"Edit 'Postavshik'?\n"<<"1-Yes.0-No\n";
- cin>>q;
- if(q==1)
- {
- cout<<"Type new 'Postavshik':\n";
- cin>>px[i].poztav;
- }
- cout<<"Kol-vo:\n"<<""<<px[i].kolvo<<endl<<"Edit 'Kol-vo'?\n"<<"1-Yes.0-No\n";
- cin>>q;
- if(q==1)
- {
- cout<<"Type new 'Kol-vo':\n";
- cin>>px[i].kolvo;
- }
- cout<<"Preparat:\n"<<""<<px[i].preparat<<endl<<"Edit 'Preparat'?\n"<<"1-Yes.0-No\n";
- cin>>q;
- if(q==1)
- {
- cout<<"Type new 'Preparat':\n";
- cin>>px[i].preparat;
- }
- cout<<endl<<"Done!\n.";
- break;
- }
- }
- output();
- getch();
- }
- void pharmacy::del()
- {
- int i,h,fl;
- output();
- cout<<"Type note number: ";
- cin>>h;
- fl=0;
- for(i=0;i<n;i++)
- {
- if(i==h-1)
- {
- fl=1;
- udal(i);
- cout<<endl<<"Done!\n";
- }
- }
- if(fl==0)
- {
- cout<<"Note not found.\n";
- }
- output();
- getch();
- }
- void pharmacy::udal(int h)
- {
- int i;
- if(h<0||h>=n)
- {
- cout<<"This string doesn't exist.";
- getch();
- return;
- }
- for(i=h+1;i<n;i++)
- px[i-1]=px[i];
- n--;
- }
- void pharmacy::sorting()
- {
- int i,p,fl;
- apteka r;
- cout<<"Sort.\n";
- cout<<"1.Sort by 'Naimenovanie'\n";
- cout<<"2.Sort by 'Adress'\n";
- cout<<"3.Sort by 'Postavshik'\n";
- cout<<"4.Sort by 'Kol-vo'\n";
- cout<<"5.Sort by 'Preparat'\n";
- cout<<"Type menu number: ";
- cin>>p;
- do
- {
- for(i=0;i<(n-1);i++)
- {
- if(p==1)
- {
- fl=0;
- if(strcmp(px[i+1].nazv,px[i].nazv)<0)
- {
- r=px[i];
- px[i]=px[i+1];
- px[i+1]=r;
- fl=1;
- break;
- }
- }
- if(p==2)
- {
- fl=0;
- if(strcmp(px[i+1].adres,px[i].adres)<0)
- {
- r=px[i];
- px[i]=px[i+1];
- px[i+1]=r;
- fl=1;
- break;
- }
- }
- if(p==3)
- {
- fl=0;
- if(strcmp(px[i+1].poztav,px[i].poztav)<0)
- {
- r=px[i];
- px[i]=px[i+1];
- px[i+1]=r;
- fl=1;
- break;
- }
- }
- if(p==4)
- {
- fl=0;
- if(px[i+1].kolvo<px[i].kolvo)
- {
- r=px[i];
- px[i]=px[i+1];
- px[i+1]=r;
- fl=1;
- break;
- }
- }
- if(p==5)
- {
- fl=0;
- if(strcmp(px[i+1].preparat,px[i].preparat)<0)
- {
- r=px[i];
- px[i]=px[i+1];
- px[i+1]=r;
- fl=1;
- break;
- }
- }
- }
- }
- while(fl==1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement