#include <iostream>
#include <windows.h>
#include <stdio.h>
using namespace std;
const int color=system("color F0");
int menu();
struct leaf
{
char sn[50]; //student name
int sid; //student number
};
struct list
{
leaf x;
list *next;
list *prev;
};
list *list_end=NULL;
list *dobav_nach(list *p); //dobavit v nachalo
list *udal_nach(list *p); //udalit iz nachala
list *dobav_proizv(list *p); //dobavit v proizvolnoe mesto
list *udal_proizv(list *p); //udalit iz proizvolnogo mesta
list *clean_list(list *p); //ochistit spisok
void proverka(list *p); //proverka, pust li spisok
void print(list *p); //vyiti
int printfl(list *p, char *fname);
int readfl(list *p, char *fname);
list *dob(list *f, leaf t);
//------------------------------------------------------------
int main()
{
list *l=NULL;
bool ex=true;
char FN[30];
while (ex)
{
switch(menu())
{
case 1: l=dobav_nach(l); break;
case 2: l=udal_nach(l); break;
case 3: dobav_proizv(l); break;
case 4:
if (udal_proizv(l)==NULL)
l=NULL;
break;
case 5: proverka(l); break;
case 6: l=clean_list(l); break;
case 7: print(l); break;
case 8:
cout<<" Please write the filename:\n";
cin>>FN;
if (readfl(l,FN))
{
cout<<" File was opened!\n";
l=list_end->next;
}
else
cout<<" ERROR: File not open\n";
break;
case 9:
cout<<"Please write the filename:\n";
cin>>FN;
if (readfl(l,FN))
cout<<" Successfully written to file!\n";
else
cout<<" ERROR: List not written to file!\n";
break;
case 0: ex=false; break;
default: cout<<" DEFAULT: Please select item!"<<endl; break;
}
}
return 0;
}
//-----------------------------------------------
int menu()
{
int m;
cout<<"\n \2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1"<<endl;
cout<<" Please select item: \n\n";
cout<<" 1 - Add to the top\n";
cout<<" 2 - Remove from the top\n";
cout<<" 3 - Add to arbitrary place\n";
cout<<" 4 - Remove from arbitrary place\n";
cout<<" 5 - Check: whether the list is empty?\n";
cout<<" 6 - Clear the list\n";
cout<<" 7 - Print list\n";
cout<<" 8 - Enter a list from file\n";
cout<<" 9 - Withdraw a list to file\n";
cout<<" 0 - Quit\n";
cout<<" \2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1\2\1"<<endl<<endl;
cout<<" ";
cin>>m;
return m;
}
//-----------------------------------------------
list *dobav_nach(list *p)
{
list *t;
t=new list;
cout<<endl<<" PLEASE ENTER THE STUDENT NAME: ";
cin>>t->x.sn;
cout<<" PLEASE ENTER THE STUDENT NUMBER: ";
cin>>t->x.sid;
if (p==NULL)
{
t->next=t;
t->prev=t;
p=t;
list_end=t;
cout<<endl<<" ...Added successfully!\n\n";
}
else
{
t->next=p;
t->prev=list_end;
list_end->next=t;
cout<<endl<<" ...Added successfully!\n\n";
}
return t;
}
//-----------------------------------------------
list *udal_nach(list *p)
{
list *t;
if(p==NULL)
cout<<" Spisok pyst!";
else
{
if(p==list_end)
{
delete p;
p=NULL;
list_end=NULL;
cout<<endl<<" ...Deleted successfully!\n\n";
}
else
{
t=p;
p=p->next;
p->prev=list_end;
list_end->next=p;
delete t;
cout<<endl<<" ...Deleted successfully!\n\n";
}
}
return p;
}
//-----------------------------------------------
list *dobav_proizv(list *p)
{
int sd;
list *s,*t;
if(p==NULL)
cout<<" Spisok pyst!";
else
{
t=p;
cout<<endl<<"Vvedite nomer studenta, posle kotorogo budet vstavlen etot: ";
cin>>sd;
do
{
if(t->x.sid==sd)
{
s=new list;
cout<<"Vvedite imya studenta: ";
cin>>(s->x.sn);
cout<<"Vvedite nomer studenta: ";
cin>>s->x.sid;
s->next=t->next;
t->next->prev=s;
s->prev=t;
t->next=s;
cout<<endl<<" ...Added successfully!\n\n";
if(s->next==p)
list_end=s;
break;
}
t=t->next;
}
while(t!=p);
}
return s;
}
//-----------------------------------------------
list *udal_proizv(list *p)
{
int sd;
list *s,*t;
if(p==NULL)
cout<<" Spisok pyst!";
else
{
t=p;
cout<<endl<<"Vvedite nomer studenta, kotoruy budet udalen: ";
cin>>sd;
while(t->next!=p)
{
if(t->next->x.sid==sd)
{
s=t->next;
t->next=s->next;
s->next->prev=t;
delete s;
cout<<endl<<" ...Deleted successfully!\n\n";
if(t->next==p)
{
list_end=t;
}
break;
}
t=t->next;
}
}
return t;
}
//-----------------------------------------------
void proverka(list *p)
{
if(p==NULL)
cout<<endl<<" Spisok pyst!\n\n";
else
cout<<endl<<" Spisok ne pyst!\n\n";
}
//-----------------------------------------------
list *clean_list(list *p)
{
list *t;
if(p==NULL)
cout<<" Spisok pyst!";
else
{
while(p!=list_end)
{
t=p;
p=p->next;
delete t;
}
delete p;
p=NULL;
list_end=NULL;
cout<<endl<<" ...Cleaned successfully!\n\n";
}
return p;
}
//-----------------------------------------------
void print(list *p)
{
list *t_p=p;
if(t_p)
{
do
{
cout<<" \20\20\20\20\20\20\20\20\20\20\20\20\20\20\20\20\20"<<endl;
cout<<" STUDENT NAME: "<<t_p->x.sn<<endl;
cout<<" STUDENT NUMBER: "<<t_p->x.sid<<endl;
cout<<" \21\21\21\21\21\21\21\21\21\21\21\21\21\21\21\21\21\n\n";
t_p=t_p->next;
}
while(t_p!=list_end->next);
}
else
cout<<" Spisok pyst!\n\n";
}
//-----------------------------------------------
int printfl(list *p, char* fname)
{
FILE *Q;
list *t=p;
Q=fopen(fname,"wb");
if(!Q)
return 0;
do
{
fwrite(&(t->x),sizeof(leaf),1,Q);
t=t->next;
}
while(t!=p);
fclose(Q);
return 1;
}
//-----------------------------------------------
int readfl(list *p, char *fname)
{
leaf t;
FILE *Q;
Q=fopen(fname,"rb");
if (!Q)
return 0;
while((fread(&t,sizeof(leaf),1,Q))>0)
p=dob(p,t);
fclose(Q);
return 1;
}
//-----------------------------------------------
list *dob(list *f, leaf n)
{
list *t;
if(f==NULL)
{
f=new list;
f->x=n;
f->next=f;
list_end=f;
return f;
}
t=new list;
t->x=n;
t->next=list_end->next;
list_end->next=t;
list_end=t;
return f;
}
не работает вывод\ввод списков из фаила
пожалуйста помогите разобраться что я сделал не так.. не хочет выводить\вводить из\в фаил списки, а список у меня кольцевой двусвязный, спасибо заранее!
Хочу попенять за транслит. Надо, надо учить английский!
А ещё пункты меню Enter a list from file и Withdraw a list to file режут глаз, хоть я и сам не бог весть какой знаток английского. Особенно withdraw не в тему. Лучше привычные Write to file, Read from file.
насчет английского спасибо), просто использовал машинный перевод, английский очень плохо знаю, в школе был французский)