#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
struct medicine {
char*name_med;
int application;
int dose;
};
struct TDisease
{ char*name;
char*symptom;
char*procedure;
int appellation;
medicine list;
TDisease*next;
TDisease*previos;
};
ostream& operator << (ostream& out, TDisease *Disease)
{
out << "Name:"<<Disease ->name <<endl
<<"Symptom:"<<Disease->symptom<<endl
<<"Procedure:"<<Disease->procedure <<endl
<<"Appellation:"<<Disease->appellation<<endl
<<"Medicine list:"<<Disease-> list.name_med <<"; "<< Disease-> list.application <<"; "<<Disease->list.dose<<endl<<endl;
return out;
}
istream& operator >> (istream& in, TDisease*Disease)
{
char *str=new char [30];
cout << "Name:";
in >> str;
Disease->name=strdup(str);
cout << "Symptom:";
in >> str;
Disease->symptom=strdup(str);
cout << "Procedure:";
in >> str;
Disease-> procedure =strdup(str);
cout << "Appellation:";
in >>Disease->appellation;
cout << "Name_med:";
in >> str;
Disease->list.name_med=strdup(str);
cout << "Application:";
in >>Disease->list.application;
cout << "Dose:";
in >>Disease-> list.dose;
delete []str;
return in;
}
TDisease* input_new (TDisease* &Disease)
{
TDisease *work=Disease;
Disease=new TDisease;
if (Disease==NULL) { Disease=work;
return NULL;
}
cin >> Disease;
if (work==NULL) Disease->previos=NULL;
else { Disease->previos=work;
work->next=Disease;
}
Disease->next=NULL;
return Disease;
}
TDisease* delete_element (TDisease* &Disease, TDisease* &begin)
{
TDisease *work;
if ((Disease->previos==NULL)&&(Disease->next==NULL))
begin=NULL;
if ((Disease->previos==NULL)&&(begin!=NULL))
{ begin=Disease->next;
Disease->next->previos=NULL;
}
if ((Disease->next==NULL)&&(begin!=NULL)) Disease->previos->next=NULL;
if ((Disease->next!=NULL)&&(Disease->previos!=NULL))
{Disease->previos->next=Disease->next;
Disease->next->previos=Disease->previos;}
work=Disease->previos;
delete []Disease->name;
delete []Disease->symptom;
delete []Disease->procedure;
delete Disease->list.name_med;
delete Disease;
Disease=work;
return work;
}
TDisease* find_to_end (TDisease* begin)
{
TDisease* work=begin;
if (work!=NULL) while ((work->next!=NULL))
work=work->next;
return work;
}
int count_to_list (TDisease* begin)
{
int count=1;
TDisease* work=begin;
if (work==NULL) count=0;
else while (work=work->next) count++;
return count;
}
TDisease* delete_all (TDisease* &end, TDisease* &begin)
{
while (begin!=NULL)
if (end==NULL) end=find_to_end(begin);
else delete_element (end, begin);
return end;
}
int wait_key(void)
{//while (!kbhit());
return getch();
}
TDisease* new_list (TDisease* &end, TDisease* &begin)
{
if (begin!=NULL) { end=find_to_end(begin);
delete_all(end,begin);
}
int code;
cout <<endl;
do {
cout <<"Input new element?(Y/N)"<<endl;
do code=wait_key();
while((code!=78)&&(code!=89)&&(code!=110)&&(code!=121));
if ((code==89)||(code==121)) { cout <<"Input data of "<<count_to_list(begin)+1<<" element:"<<endl;
if (begin==NULL) begin=input_new(end);
else input_new(end);
}
}
while((code==89)||(code==121));
cout << "Count to elements of list:"<<count_to_list (begin)<<endl;
wait_key();
return end;
}
TDisease* line_find_to_name (TDisease* current, char* str)
{
TDisease* work=current;
while (work!=NULL)
if (strcmp(work->name,str)) work=work->next;
else break;
return work;
}
int line_find_to_name_all (TDisease* begin, char* str)
{
int count=0;
TDisease* work=begin;
while (work!=NULL)
if (strcmp(work->name,str)) work=work->next;
else { cout <<work;
work=work->next;
count ++;
}
return count;
}
TDisease* line_find_to_symptom (TDisease* current, char* str)
{
TDisease* work=current;
while (work!=NULL)
if (strcmp(work->symptom,str)) work=work->next;
else break;
return work;
}
int line_find_to_symptom_all (TDisease* begin, char* str)
{
int count=0;
TDisease* work=begin;
while (work!=NULL)
if (strcmp(work-> symptom,str)) work=work->next;
else { cout <<work;
work=work->next;
count ++;
}
return count;
}
TDisease* line_find_to_procedure (TDisease* current, char* str)
{
TDisease* work=current;
while (work!=NULL)
if (strcmp(work-> procedure,str)) work=work->next;
else break;
return work;
}
int line_find_to_procedure_all (TDisease* begin, char* str)
{
int count=0;
TDisease* work=begin;
while (work!=NULL)
if (strcmp(work-> procedure,str)) work=work->next;
else { cout <<work;
work=work->next;
count ++;
}
return count;
}
TDisease* line_find_to_name_med (TDisease* current, char* str)
{
TDisease* work=current;
while (work!=NULL)
if (strcmp(work->list.name_med,str)) work=work->next;
else break;
return work;
}
int line_find_to_name_med_all (TDisease* begin, char* str)
{
int count=0;
TDisease* work=begin;
while (work!=NULL)
if (strcmp(work-> list.name_med,str)) work=work->next;
else { cout <<work;
work=work->next;
count ++;
}
return count;
}
TDisease* line_find_to_application (TDisease* current, int value)
{
TDisease* work=current;
while (work!=NULL)
if (work-> list.application!=value) work=work->next;
else break;
return work;
}
int line_find_to_ application _all (TDisease* begin, int value)
{
int count=0;
TDisease* work=begin;
while (work!=NULL)
if (work-> list.application!=value) work=work->next;
else { cout <<work;
work=work->next;
count ++;
}
return count;
}
int delete_to_name (TDisease* &begin, TDisease* &end, char* str)
{
TDisease* work=begin;
int count=0;
while (work!=NULL) {work=line_find_to_name (work,str);
if (work!=NULL)
{delete_element (work, begin);
if ((work==NULL)&&(begin!=NULL)) work=begin;
count++;
}
}
end=find_to_end(begin);
return count;
}
int delete_to_symptom (TDisease* &begin, TDisease* &end, char* str)
{
TDisease* work=begin;
int count=0;
while (work!=NULL) {work=line_find_to_symptom (work,str);
if (work!=NULL) {delete_element (work, begin);
count++;
}
}
end=find_to_end(begin);
return count;
}
int delete_to_procedure (TDisease* &begin, TDisease* &end, char* str)
{
TDisease* work=begin;
int count=0;
while (work!=NULL) {work=line_find_to_procedure (work,str);
if (work!=NULL) {delete_element (work, begin);
count++;
}
}
end=find_to_end(begin);
return count;
}
int delete_to_name_med (TDisease* &begin, TDisease* &end, char* str)
{
TDisease* work=begin;
int count=0;
while (work!=NULL) {work=line_find_to_name_med (work,str);
if (work!=NULL)
{delete_element (work, begin);
if ((work==NULL)&&(begin!=NULL)) work=begin;
count++;
}
}
end=find_to_end(begin);
return count;
}
int delete_to_application (TDisease* &begin, TDisease* &end, int application)
{
TDisease* work=begin;
int count=0;
while (work!=NULL) {work=line_find_to_ application (work, appellation);
if (work!=NULL) {delete_element (work, begin);
count++;
}
}
end=find_to_end(begin);
return count;
}
void out_of_display(TDisease* begin)
{
//clrscr();
TDisease* work=begin;
while (work!=NULL)
{ if (wherey()>20) {//wait_key();
//clrscr();
}
cout << work;
work=work->next;
}
cout <<endl<< "Total in list "<< count_to_list(begin)<< " elements!"<<endl;
wait_key();
}
void save_list (TDisease *begin, char * name_file)
{
TDisease *work=begin;
int count=count_to_list(begin);
ofstream out_file(name_file);
if (out_file) { out_file << count << endl;
for (int i=0; i<count; i++) {
out_file << work->name << endl;
out_file << work->symptom << endl;
out_file << work->procedure << endl;
out_file << work->appellation << endl;
out_file << work->list.name_med << endl;
out_file << work-> list.application << endl;
out_file << work->list.dose << endl;
work=work->next;
}
}
else { cout << "File not find!" << endl;
wait_key();
}
out_file.close();
}
void open_new_list (TDisease* &end, TDisease* &begin, char * name_file)
{
TDisease *Disease;
int count;
char *str=new char [30];
ifstream in_file(name_file);
if (in_file) { delete_all (end,begin);
in_file >> count;
for (int i=0; i<count; i++) {
Disease=new TDisease;
if (Disease){
in_file >> str;
Disease->name=strdup(str);
in_file >> str;
Disease->symptom=strdup(str);
in_file >> str;
Disease->procedure=strdup(str);
in_file >>Disease->appellation;
in_file >> str;
Disease-> list.name_med=strdup(str);
in_file >>Disease-> list.application;
in_file >>Disease-> list.dose;
if (end==NULL) { begin=Disease;
Disease->previos=NULL;
}
else { Disease->previos=end;
end->next=Disease;
}
Disease->next=NULL;
end=Disease;
}
else { cout << "memory not enough!"<<endl;
wait_key();
break;
}
}
}
else { cout << "File not find!" << endl;
wait_key();}
in_file.close();
delete []str;
}
void open_add_list (TDisease* &end, TDisease* &begin, char * name_file)
{
TDisease *Disease;
int count;
char *str=new char [30];
ifstream in_file(name_file);
if (in_file) {
in_file >> count;
for (int i=0; i<count; i++) {
Disease=new TDisease;
if (Disease){
in_file >> str;
Disease->name=strdup(str);
in_file >> str;
Disease->symptom=strdup(str);
in_file >> str;
Disease->procedure=strdup(str);
in_file >>Disease->appellation;
in_file >> str;
Disease-> list.name_med=strdup(str);
in_file >>Disease-> list.application;
in_file >>Disease-> list.dose;
if (end==NULL) { begin=Disease;
Disease->previos=NULL;
}
else { Disease->previos=end;
end->next=Disease;
}
Disease->next=NULL;
end=Disease;
}
else { cout << "memory not enough!"<<endl;
wait_key();
break;
}
}
}
else { cout << "File not find!" << endl;
wait_key();
}
in_file.close();
delete []str;
}
int swap_element(TDisease* Element1, TDisease* &end, TDisease* &begin)
{
TDisease * Element2= Element1->next;
TDisease *w_next;
TDisease *w_previos;
if (Element1&& Element2&&end&&begin) {
if (Element1==begin) begin= Element2;
if (Element2==end) end= Element1;
w_next= Element2->next;
w_previos= Element1->previos;
Element1->next=w_next;
Element2->previos=w_previos;
Element1->previos= Element2;
Element2->next= Element1;
if (Element1->next) Element1->next->previos= Element1;
if (Element2->previos) Element2->previos->next= Element2;
return 0;
}
else return 1;
}
void orend_of_name (TDisease* &end, TDisease* &begin)
{
TDisease *end_orend=NULL;
TDisease *current;
if (end&&begin) {
while (begin->next!=end_orend)
{ current=begin;
while ((current->next!=end_orend)) {
if (strcmp(current->name,current->next->name)>=0)
swap_element(current,end,begin);
else current=current->next;
}
if (!current->next) end_orend=end;
else end_orend=end_orend->previos;
}
}
}
void orend_of_symptom(TDisease* &end, TDisease* &begin)
{
TDisease *end_orend=NULL;
TDisease *current;
if (end&&begin) {
while (begin->next!=end_orend)
{ current=begin;
while ((current->next!=end_orend)) {
if (strcmp(current-> symptom,current->next-> symptom)>=0)
swap_element(current,end,begin);
else current=current->next;
}
if (!current->next) end_orend=end;
else end_orend=end_orend->previos;
}
}
}
void orend_of_procedure(TDisease* &end, TDisease* &begin)
{
TDisease *end_orend=NULL;
TDisease *current;
if (end&&begin) {
while (begin->next!=end_orend)
{ current=begin;
while ((current->next!=end_orend)) {
if (strcmp(current-> procedure,current->next-> procedure)>=0)
swap_element(current,end,begin);
else current=current->next;
}
if (!current->next) end_orend=end;
else end_orend=end_orend->previos;
}
}
}
void orend_of_name_med(TDisease* &end, TDisease* &begin)
{
TDisease *end_orend=NULL;
TDisease *current;
if (end&&begin) {
while (begin->next!=end_orend)
{ current=begin;
while ((current->next!=end_orend)) {
if (current->list.name_med -current->next-> appellation >=0)
swap_element(current,end,begin);
else current=current->next;
}
if (!current->next) end_orend=end;
else end_orend=end_orend->previos;
}
}
}
void orend_of_application(TDisease* &end, TDisease* &begin)
{
TDisease *end_orend=NULL;
TDisease *current;
if (end&&begin) {
while (begin->next!=end_orend)
{ current=begin;
while ((current->next!=end_orend)) {
if (current->list. application -current->next-> application >=0)
swap_element(current,end,begin);
else current=current->next;
}
if (!current->next) end_orend=end;
else end_orend=end_orend->previos;
}
}
}