海轧王 海轧王
我都懂,懂哥本人
关注数: 9 粉丝数: 6 发帖数: 1,518 关注贴吧数: 26
这个链表程序为什么没有输出?不是程序本身的问题吗? #include<iostream> using namespace std; template <class T> class Node{ public: T data; Node<T> *prev; Node<T> *next; }; template <class T> class List{ private: Node<T> *head;Node<T> *tail; int length; public: List(); List(const List&lis); void add(T d); void ascSort(); void remove(T n); void Show(); }; template <class T> List<T>::List(){ head=new Node<T>; tail=new Node<T>; head->prev =0; head->next =tail; tail->prev =head; tail->next =0; length=0; } template <class T> List<T>::List(const List&lis){ head=new Node<T>; tail=new Node<T>; head->prev =0; head->next =tail; tail->prev =head; tail->next =0; length=0; Node<T> *temp=lis.head; Node<T> *p=head; while(temp->next!=lis.tail){ temp=temp->next; tail->data =temp->data ; Node<T> *p; tail->next =p; p->prev =tail; tail=p;//p的前面是tail先前的地址,在这之后tail的地址是新节点p的地址 length++; } tail->next =0; } template <class T> void List<T>::add(T d){ tail->data =d; Node<T> *p; tail->next=p; p->prev =tail; tail=p; tail->next =0; length++; } template <class T> void List<T>::ascSort(){ Node<T> *f,*b; int i,j,t; for(i=1;i<=length;i++){ f=head->next ;b=f->next ; for(j=1;j<=length-i;j++){ if(f->data>b->data ){ t=f->data ; f->data =b->data ; b->data =t; } f=b; b=b->next ; } } } template <class T> void List<T>::remove(T n){ Node<T>*p=head->next ; if(length==0) cout<<"这是空链。";return; while(p->next !=0){ if(p->data ==n){ Node<T> *temp1=p->prev; Node<T> *temp2=p->next; temp1->next =temp2;temp2->prev =temp1; delete p;length--;return; } p=p->next; } cout<<"没找到。"; } template <class T> void List<T>::Show(){ int i; Node<T> *p=head; for(i=1;1<=length;i++){ p=p->next; std::cout<<p->data<<" "; } } int main(){ int i; List<int> lis; for(i=6;i>=1;i--){ lis.add(i); } List<int> lis2(lis); lis2.Show(); lis.ascSort(); lis.Show(); return 0; }
编写了一个分数类,结果一直是0/1,小白求教 #include<iostream> #include<cmath> using namespace std; class ZFS{ private: int FM,FZ; public: ZFS(int a,int b){ this->FZ=a;this->FM=b; if(FM<0&&FZ<0)FZ=-FZ;FM=-FM; YF(); } ZFS(ZFS &zfs):FZ(zfs.FZ),FM(zfs.FM){ if(FM<0&&FZ<0)FZ=-FZ;FM=-FM; YF(); } void YF(){ int sign=1,a,b,c; if(FZ!=0){ if(FM*FZ<0)FM=abs(FM);FZ=abs(FZ);sign=-1; if(FM>FZ){ b=FM; a=FZ;} else b=FZ;a=FM; do{ c=b%a; if(c==0)break; else b=a;a=c; }while(1); FZ=sign*FZ/a; FM=FM/a; } } ~ZFS(){ } void TF(ZFS &zfs){ int asign=1,bsign=1,afm,afz,bfm,bfz; afm=abs(FM);afz=abs(FZ);bfm=abs(zfs.FM );bfz=abs(zfs.FZ ); if(FM*FZ<0)asign=-1; if(zfs.FM *zfs.FZ<0)bsign=-1; if(afm!=bfm){ FM=afm*bfm; FZ=asign*afz*bfm; zfs.FM =afm*bfm; zfs.FZ =bsign*afm*bfz; } } ZFS operator +(ZFS &zfs){ ZFS c(0,1); TF(zfs); c.FZ =FZ+zfs.FZ;c.FM =FM; c.YF(); return c; } ZFS operator -(ZFS &zfs){ ZFS c(0,1); TF(zfs); c.FZ =FZ-zfs.FZ;c.FM =FM; c.YF(); return c; } ZFS operator *(ZFS &zfs){ ZFS c(0,1); c.FZ =FZ*zfs.FZ;c.FM =FM*zfs.FM ; c.YF(); return c; } ZFS operator /(ZFS &zfs){ ZFS c(0,1); c.FZ =FZ*zfs.FM;c.FM =FM*zfs.FZ; c.YF(); return c; } const ZFS& operator =(const ZFS&zfs){ FZ=zfs.FZ; FM=zfs.FM; return zfs; } void ShowZFS(){ cout<<FZ<<"/"<<FM<<endl; } int operator ==(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ ==d.FZ)return 1; else return 0; } int operator <(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ <d.FZ)return 1; else return 0; } int operator >(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ >d.FZ)return 1; else return 0; } int operator >=(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ >=d.FZ)return 1; else return 0; } int operator <=(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ <=d.FZ)return 1; else return 0; } }; int main(){ ZFS a(1,2),s(1,3),c(0,1); s.ShowZFS(); return 0; }
编写了一个分数类,但结果一直是0/1,求教。 #include<iostream> #include<cmath> using namespace std; class ZFS{ public: int FM,FZ; ZFS(int a=0,int b=1){ FZ=a;FM=b; if(FM<0&&FZ<0)FZ=-FZ;FM=-FM; YF(); } ZFS(ZFS &zfs):FZ(zfs.FZ),FM(zfs.FM){ if(FM<0&&FZ<0)FZ=-FZ;FM=-FM; YF(); } void YF(){ int sign=1,a,b,c; if(FZ!=0){ if(FM*FZ<0)FM=abs(FM);FZ=abs(FZ);sign=-1; if(FM>FZ){ b=FM; a=FZ;} else b=FZ;a=FM; do{ c=b%a; if(c==0)break; else b=a;a=c; }while(1); FZ=sign*FZ/a; FM=FM/a; } } ~ZFS(){ } void TF(ZFS &zfs){ int asign=1,bsign=1,afm,afz,bfm,bfz; afm=abs(FM);afz=abs(FZ);bfm=abs(zfs.FM );bfz=abs(zfs.FZ ); if(FM*FZ<0)asign=-1; if(zfs.FM *zfs.FZ<0)bsign=-1; if(afm!=bfm){ FM=afm*bfm; FZ=asign*afz*bfm; zfs.FM =afm*bfm; zfs.FZ =bsign*afm*bfz; } } ZFS operator +(ZFS &zfs){ ZFS c; TF(zfs); c.FZ =FZ+zfs.FZ;c.FM =FM; c.YF(); return c; } ZFS operator -(ZFS &zfs){ ZFS c; TF(zfs); c.FZ =FZ-zfs.FZ;c.FM =FM; c.YF(); return c; } ZFS operator *(ZFS &zfs){ ZFS c; c.FZ =FZ*zfs.FZ;c.FM =FM*zfs.FM ; c.YF(); return c; } ZFS operator /(ZFS &zfs){ ZFS c; c.FZ =FZ*zfs.FM;c.FM =FM*zfs.FZ; c.YF(); return c; } const ZFS& operator =(const ZFS&zfs){ FZ=zfs.FZ; FM=zfs.FM; return zfs; } void ShowZFS(){ cout<<FZ<<"/"<<FM<<endl; } int operator ==(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ ==d.FZ)return 1; else return 0; } int operator <(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ <d.FZ)return 1; else return 0; } int operator >(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ >d.FZ)return 1; else return 0; } int operator >=(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ >=d.FZ)return 1; else return 0; } int operator <=(ZFS &zfs){ ZFS c(FZ,FM) ;ZFS d(zfs); c.TF(d); if(c.FZ <=d.FZ)return 1; else return 0; } }; int main(){ ZFS a(1,2),b(1,3),c; c=a-b; b.ShowZFS(); return 0; }
1 下一页