C++程序设计实践指导1.7超长数列中n个数排序改写要求实现

改写要求1:将以上程序改写为适合超长整数

改写要求2:将以上程序改写为适合超长数列

改写要求3:将数列中指定位置m开始的n个结点重新按降序排序

改写要求4:输出指定位置m开始的n个结点的超长整数

#include <cstdlib>

#include <iostream>

#include <string>

using namespace std;

struct LongIntegerNode

{

       short int data;

       LongIntegerNode *next;

};



struct LinkNode

{

       LongIntegerNode *pLongIntegerNode;

       LinkNode *next;

};

class LIST

{

      public:

             struct LongIntegerNode* creat(string str);

             struct LinkNode* add(LinkNode* Head,LongIntegerNode* pHead);

             struct LinkNode* sortpart(LinkNode* Head,int m,int n);

             void output(LinkNode* Head)       

             {

                  LinkNode* p;

                  p=Head->next;

                  LongIntegerNode* q;

                  while(p)

                  {

                          q=p->pLongIntegerNode->next;

                          while(q)

                          {

                             cout<<q->data;

                             q=q->next;

                          }

                          cout<<endl;

                          p=p->next;

                  }

             }

             void show(LinkNode* Head,int n)

             {

                  LinkNode* p;

                  p=Head;

                  LongIntegerNode* q;

                  while(n)

                  {

                          q=p->pLongIntegerNode->next;

                          while(q)

                          {

                             cout<<q->data;

                             q=q->next;

                          }

                          cout<<endl;

                          p=p->next;

                          n--;

                  }

             }     



};



LinkNode* LIST::add(LinkNode* node,LongIntegerNode* pHead)

{  

       LinkNode* newLinkNode=new LinkNode;

       newLinkNode->next=NULL;

       newLinkNode->pLongIntegerNode=pHead;

       node->next=newLinkNode;

       node=newLinkNode;

       return node;

}



LongIntegerNode* LIST::creat(string str)

{

       string s=str.substr(0,1);

       int i=1;

       LongIntegerNode* pHead=new LongIntegerNode;

       pHead->next=NULL;

       LongIntegerNode* p;

       p=pHead;

       while(s.length())

       {

            LongIntegerNode* newLongIntegerNode=new LongIntegerNode;

            newLongIntegerNode->next=NULL;

            newLongIntegerNode->data=atoi(s.c_str());

            p->next=newLongIntegerNode;

            p=newLongIntegerNode;

            if(s.length()<1)

            return pHead;

            s=str.substr(i,1);

            i=i+1;  

       }

       return pHead;

} 



LinkNode* LIST::sortpart(LinkNode* Head,int m,int n)

{

    int x=m;

    int y=n;

    int temp;

    LinkNode* p=Head->next;

    LinkNode* q;

    LongIntegerNode* t,*s;

    while(x)

    {

            p=p->next;

            x--;

    }

    q=p;

    while(y)

    {  

       for(t=p->pLongIntegerNode;t!=NULL;t=t->next)

       {

       for(s=t->next;s!=NULL;s=s->next)

       {

           

            if(t->data<s->data)

            {

                  temp=t->data;

                  t->data=s->data;

                  s->data=temp;

                  

            }

       }

       }



    p=p->next;

    y--;

    }

    return q;

}



int main(int argc, char *argv[])

{

    LIST list;

    LinkNode* Head=new LinkNode;

    Head->next=NULL;

    LinkNode* sort;

    LinkNode* tail;

    tail=Head;

    string str;

    cin>>str;

   while(str!="exit")

    {

       LongIntegerNode* pHead;

       pHead=list.creat(str);



       tail=list.add(tail,pHead);

       cin>>str;

    }

    sort=list.sortpart(Head,1,2);

    list.output(Head);

    cout<<"输出指定的排序数列"<<endl;

    list.show(sort,2);

    system("PAUSE");

    return EXIT_SUCCESS;

}

 

你可能感兴趣的:(程序设计)