改写要求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; }
时间: 2024-10-11 02:32:10