zoukankan      html  css  js  c++  java
  • 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 LinkNode* reverse(LinkNode* pHead);
                 void print(LinkNode* pHead)
                 {
                      LinkNode* p=pHead;
                      p=p->next;
                      cout<<"n= ";
                      while(p)
                      {    
                          cout<<p->data;
                          p=p->next;
                      }
                      cout<<endl;
                 }
    };
    
    struct LinkNode* STR::itoa()
    {
         LinkNode* pHead=new LinkNode;
         pHead->next=NULL;
         LinkNode* p;
         p=pHead;
         int i=0;
         string s;
         int x=atoi(str.substr(i,1).c_str());
         while(1)
         {
                 LinkNode* NewLinkNode=new LinkNode;
                 NewLinkNode->next=NULL;
                 NewLinkNode->data=x;
                 p->next=NewLinkNode;
                 p=NewLinkNode;
                 s=str.substr(++i,1).c_str();
                 if(s.length()==0)
                 return pHead;
                 x=atoi(s.c_str());
         }
         return pHead;
        
    }
    
    struct LinkNode* STR::reverse(LinkNode* pHead)
    {
           LinkNode *p,*q;
           LinkNode *t;
           p=pHead->next;
           q=pHead->next->next;
           while(q)
           {
                t = q->next;  
                q->next = p;  
                p = q;  
                q = t;
           }
           pHead->next->next=NULL;
           pHead->next=p;
           return pHead;
    }
                  
    int main(int argc, char *argv[])
    {
        string n;
        LinkNode* pHead;
        cout<<"Input n: ";
        cin>>n;
        STR str(n);
        pHead=str.itoa();
       // pHead=str.reverse(pHead);
        str.print(pHead);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
  • 相关阅读:
    属性包装
    生成器
    迭代器
    深拷贝-浅拷贝
    装饰器-wrapper
    类别不均衡
    参数优化-学习曲线
    参数优化-验证曲线
    参数优化-API
    D. Number Of Permutations 符合条件的排列种类
  • 原文地址:https://www.cnblogs.com/c5395348/p/4271979.html
Copyright © 2011-2022 走看看