C++程序设计实践指导1.3求任意整数降序数改写要求实现

改写要求1:动态生成单链表存储

#include <cstdlib>
#include <iostream>

using namespace std;
struct LinkNode
{
       int data;
      struct LinkNode *next;
};
class NUM
{
      int n;
      public:
             NUM(int x)
             {
                      n=x;
             }
              struct LinkNode * descrease();
             void show(LinkNode* pHead)
             {
                  LinkNode* p;
                  p=pHead;
                  p=p->next;
                  cout<<"n= "<<n<<endl;
                  while(p)
                  {
                          cout<<p->data;
                          p=p->next;
                  }
                  cout<<endl;
             }
};

struct LinkNode * NUM::descrease()
{
     LinkNode* pHead=new LinkNode;
     pHead->next=NULL;
     LinkNode* p;
     LinkNode* q;
     p=pHead;
     int temp;
     int x=n;
     while(x)
     {
             LinkNode* newLinkNode=new LinkNode;
             newLinkNode->next=NULL;
             newLinkNode->data=x%10;
             x=x/10;
             p->next=newLinkNode;
             p=newLinkNode;
     }
     for(p=pHead;p!=NULL;p=p->next)
         for(q=p->next;q!=NULL;q=q->next)
       {
			if(p->data<q->data)
			{
				temp=q->data;
				q->data=p->data;
				p->data=temp;
			}
      }
       return pHead;
}

int main(int argc, char *argv[])
{
    int n;
    LinkNode* pHead;
    cout<<"Input n: ";
    cin>>n;
    NUM num(n);
    pHead=num.descrease();
    num.show(pHead);
    system("PAUSE");
    return EXIT_SUCCESS;
}

  改写要求2:以最大、最小、次最大、次最小等间隔排序

#include <cstdlib>
#include <iostream>

using namespace std;
struct LinkNode
{
       int data;
      struct LinkNode *next;
};
class NUM
{
      int n;
      int sum;
      public:
             NUM(int x)
             {
                      n=x;
             }
              struct LinkNode * descrease();
             void show(LinkNode* pHead)
             {
                  LinkNode* p;
                  p=pHead;
                  p=p->next;
                  cout<<"n= "<<n<<endl;
                  cout<<"sum= "<<sum<<endl;
                  while(p)
                  {
                          cout<<p->data;
                          p=p->next;
                  }
                  cout<<endl;
             }
};

struct LinkNode * NUM::descrease()
{
     LinkNode* pHead=new LinkNode;
     pHead->next=NULL;
     LinkNode* p;
     LinkNode* q;
     p=pHead;
     int temp;
     int x=n;
     bool flag=true;
     sum=0;
     while(x)
     {
             LinkNode* newLinkNode=new LinkNode;
             newLinkNode->next=NULL;
             newLinkNode->data=x%10;
             sum+=newLinkNode->data;
             x=x/10;
             p->next=newLinkNode;
             p=newLinkNode;

     }
     for(p=pHead->next;p!=NULL;p=p->next)
    {
    if(flag)
     {
         for(q=p->next;q!=NULL;q=q->next)
       {
            if(p->data<q->data)
            {
                temp=q->data;
                q->data=p->data;
                p->data=temp;
            }
      }

     }else{
        for(q=p->next;q!=NULL;q=q->next)
       {
            if(p->data>q->data)
            {
                temp=q->data;
                q->data=p->data;
                p->data=temp;
            }
        } 

     }
     flag=!flag;
     }
       return pHead;
}

int main(int argc, char *argv[])
{
    int n;
    LinkNode* pHead;
    cout<<"Input n: ";
    cin>>n;
    NUM num(n);
    pHead=num.descrease();
    num.show(pHead);
    system("PAUSE");
    return EXIT_SUCCESS;
}
时间: 2024-08-28 19:55:32

C++程序设计实践指导1.3求任意整数降序数改写要求实现的相关文章

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.10二维数组元素换位改写要求实现

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

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

训练三:求任意整数的降序数

题目要求: 对一个五位的任意整数,求出琦降序数 算法提示:将整数的每一位分离到一维整形数组中,再将数组a的元素按照降序排列,最后输出数组元素值, 试建立一个NUM类,完成上述操作 #include<iostream> using namespace std; class NUM { public: NUM(int x);//定义一个带参数的构造函数 void NUM_fengjie(); void NUM_paixu(); void NUM_display(); private: int a[

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.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.9统计与替换字符串中的关键字改写要求实现

改写要求1:将字符数组str改为字符指针p,动态开辟存储空间 改写要求2:增加统计关键字个数的函数void CountKeyWords() 改写要求3: 增加替换函数void FindKeyWords() #include <cstdlib> #include <iostream> #include <string> using namespace std; class WORDNUM { char *p; double c; public: WORDNUM(char

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 *nex