C++程序设计实践指导1.6超长数列中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;
}
时间: 2024-08-28 12:22:06

C++程序设计实践指导1.6超长数列中n个数排序改写要求实现的相关文章

C++程序设计实践指导1.1删除序列中相同的数改写要求实现

改写要求1:改写为以指针为数据结构 #include <iostream> #include <cstdlib> using namespace std; class ARP { int m; int* p; int count[10]; public: ARP(int x[],int size) { m = size; p = new int [m]; for (int i =0;i<m;i++) { p[i]=x[i]; } for (int i =0;i<m;i+

C++程序设计实践指导1.5求两个整数集合并集改写要求实现

改写要求1:改写为单链表结构可以对任意长度整数集合求并集 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; LinkNode* next; }; class SET { public: struct LinkNode* creat(int x[],int len); struct LinkNode* copy(LinkNode* aHead); int no

C++程序设计实践指导1.2二维数组的操作运算改写要求实现

改写要求1:改写为以单链表表示二维数组 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int Row; int Column; int Data; LinkNode *next; }; class MATRIX { int m; int sum; public: struct LinkNode* creat(int x[][40],int k) { m=k; LinkNod

C++程序设计实践指导1.4正整数转换为字符串改写要求实现

改写要求1:改为适合处理超长整数 #include <cstdlib> #include <iostream> #include <string> using namespace std; struct LinkNode { short int data; LinkNode *next; }; class STR { string str; public: STR(string x) { str=x; } struct LinkNode* itoa(); struct

C++程序设计实践指导1.14字符串交叉插入改写要求实现

#include <cstdlib>#include <iostream>#include <cstring> using namespace std;class STRING{ char *str1; char *str2; public: STRING(char* s1,char* s2) { str1=new char[strlen(s1)]; str2=new char[strlen(s2)]; strcpy(str1,s1); strcpy(str2,s2);

C++程序设计实践指导1.13自然数集中找合数改写要求实现

改写要求1:用单链表实现 改写要求2:析构函数中依次将链表结点删除 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; LinkNode* next; }; class NOPRIME { friend struct LinkNode; LinkNode* Head; int n; public: NOPRIME(int n1) { n=n1; } void

C++程序设计实践指导1.10二维数组元素换位改写要求实现

改写要求1:改写为以单链表和双向链表存储二维数组 改写要求2:添加函数SingleLinkProcess()实现互换单链表中最大结点和头结点位置,最小结点和尾结点位置 改写要求3:添加函数DoubleLinkProcess()实现互换双向链表中最大结点和头结点位置,最小结点和尾结点位置 #include <cstdlib> #include <iostream> using namespace std; #define M 3 #define N 4 struct SingleLi

C++程序设计实践指导1.12数组中数据线性变换改写要求实现

改写要求1:分别用指针pa.pb代替数组 改写要求2:从键盘输入data元素 元素个数任意,输入0结束 #include <cstdlib> #include <iostream> using namespace std; class DATA { double *pa,*pb; double max,min; double new_max,new_min; int length; public: DATA(double a1[],double x,double y,int len

C++程序设计实践指导1.15找出回文数改写要求实现

改写要求1:用单链表实现 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; LinkNode *next; }; class PALINDROME { int low,up; int a[100]; int count; public: PALINDROME(int t1,int t2); int IsPalin(int x); LinkNode* IsPa