题目描述我的代码在运行一部分后会意外终止,我知道是内存出现了问题,但到底是哪句话出了错?这是我们学校的一道练习实验题相关代码#ifndefADDRESS_BOOK_H#defineADDRESS_BOOK_H#include usingstd::cin;usingstd::cout;usingstd::endl;usingstd::string;structaddress{stringname;longlongphone_number;longlongclassroom;longlongdormitory;};classbook{protected:address*a=NULL;intlistsize;intarraylength;public:book();book(constbook&a);//复制构造函数;~book();voidinsert(book&a);void_delete(book&a,stringc);voidcompile(book&a,stringc);boolfind(constbook&a,stringc);voidprint(constbook&a,intn);};#endifbook::book(){listsize=0;arraylength=50;a=newaddress[arraylength];for(inti=0;i{ a[i].classroom=0;a[i].dormitory=0;a[i].phone_number=0;}}book::book(constbook&c){arraylength=c.arraylength;listsize=c.listsize;a=newaddress[arraylength];for(inti=0;i{ a[i].name=c.a[i].name;a[i].classroom=c.a[i].classroom;a[i].dormitory=c.a[i].dormitory;a[i].phone_number=c.a[i].phone_number;}}voidbook::insert(book&b){if(b.listsize==b.arraylength){delete[]b.a;b.a=newaddress[2*arraylength];for(inti=b.arraylength;i<2*b.arraylength;i++){b.a[i].classroom=0;b.a[i].dormitory=0;b.a[i].phone_number=0;}b.arraylength=b.arraylength*2;}cin>>b.a[listsize].name>>b.a[listsize].phone_number>>b.a[listsize].classroom>>b.a[listsize].dormitory;listsize++;}voidbook::_delete(book&b,stringc){for(inti=0;i{ if(b.a[i].name==c){for(i=i;i{ b.a[i].classroom=b.a[i+1].classroom;b.a[i].dormitory=b.a[i+1].dormitory;b.a[i].phone_number=b.a[i+1].phone_number;b.a[i].name=b.a[i+1].name;}a[listsize].classroom=0;a[listsize].dormitory=0;a[listsize].phone_number=0;break;}}}voidbook::compile(book&b,stringc){intn;for(inti=0;i{ if(b.a[i].name==c){cin>>n;if(n==1){longlongn1;cin>>n1;b.a[i].phone_number=n1;}elseif(n==2){intn2;cin>>n2;b.a[i].classroom=n2;}elseif(n==3){intn3;cin>>n3;b.a[i].dormitory=n3;}}}}boolbook::find(constbook&b,stringc){inti;for(i=0;i{ if(b.a[i].name==c)returntrue;}returnfalse;}voidbook::print(constbook&b,intn){longlongsum=0;for(inti=0;isum=sum^b.a[i].dormitory; cout<} book::~book(){for(inti=0;i{ delete[]a;}}intmain(){intk=0,nn1;intnn2;cin>>nn1;bookb;stringc;for(inti=0;i{ cin>>nn2;if(nn2==0)b.insert(b);elseif(nn2==1){cin>>c;b._delete(b,c);}elseif(nn2==2){cin>>c;b.compile(b,c);}elseif(nn2==3){cout<} elseif(nn2==4){intnnn;cin>>nnn;b.print(b,nnn);}}return0;}麻烦大佬我到底是哪里的错误,谢谢!
慕哥9229398
相关分类