码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-02-04 12:49:35      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

改写要求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;
}

 

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

标签:

原文地址:http://www.cnblogs.com/c5395348/p/4271979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!