请教大佬为什么我在C++里定义的string类字符串依次赋值后长度变成零?

C++语言
用链表实现长整数加法

 #include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

struct section{
    int data;
    section *next;
};


section *getchain(string m){
    int flag,sigbit;  //flag:1代表正,-1代表负
    int length;
    if(m[0]=='-') {flag=-1; sigbit=1;length =m.length()-1;}
        else if(m[0]=='+') {flag=1;sigbit=1; length =m.length()-1;}
            else {flag=1; cout << "确认没有符号位" << endl; sigbit=0; length=m.length();}
    cout << "字符长为:  " << length << endl;
    section *head, *p, *rear;
    head=rear=new section;
    head->data=flag; cout << "符号为:" << head->data << endl;
    while(length>=5){
        p=new section;
        p->data=m[length-1+sigbit]-'0'+10*(m[length-2+sigbit]-'0')+100*(m[length-3+sigbit]-'0')+1000*(m[length-4+sigbit]-'0');
        length=length-5;
        rear->next=p;
        rear=p; cout <<"确认节点内容:" << rear->data << endl;
    }
    switch(length){
        case 0: cout << "ERROR1" << endl;break;
        case 1: p=new section; p->data=m[length-1+sigbit]-'0';rear->next=p;rear=p;cout << "!!!确认节点内容:" << rear->data << endl; break;
        case 2: p=new section;
            p->data=m[length-1+sigbit]-'0'+10*(m[length-2+sigbit]-'0');
            rear->next=p;rear=p; break;
        case 3: p=new section; p->data=m[length-1+sigbit]-'0'+10*(m[length-2+sigbit]-'0')+100*(m[length-3+sigbit]-'0');rear->next=p;rear=p; break;
        case 4: p=new section; p->data=m[length-1+sigbit]-'0'+10*(m[length-2+sigbit]-'0')+100*(m[length-3+sigbit]-'0')+1000*(m[length-4+sigbit]-'0');rear->next=p;rear=p; break;
        default: cout << "ERROR2";
    }
    p= new section; p->data=0; rear->next= p; rear=p; rear->next= NULL;

    return head;
}

//比较两长整数绝对值的大小,如果左数大就输出1,右数大就输出-1,相等输出0
int compare(string m, string n){
    int lengthm, lengthn, sigbitm, sigbitn;
    if(m[0]=='+'||m[0]=='-') {lengthm= m.length()-1; sigbitm=1;}
        else {lengthm= m.length(); sigbitm=0;}
    if(n[0]=='+'||n[0]=='-') {lengthn= n.length()-1; sigbitn=1;}
        else {lengthn= n.length(); sigbitn=0;}
    if(lengthm!=lengthn) {
        if(lengthm>lengthn) return 1;
            else if(lengthm<lengthn) return -1;
                else {cout << "ERROR3"; return 0;}
    }
        else for(int i=0;i<lengthm;++i){
            if(m[i+sigbitm]>n[i+sigbitn]) return 1;
                else if(m[i+sigbitm]<n[i+sigbitn]) return -1;
        }
    return 0;
}

section *pluschain(section *i, section *j, string m, string n){
    section *head, *p, *rear, *pi, *pj;
    pi= i; pj= j;
    head= rear= new section;
    int carrybit=0;
    //确定头结点符号
    if(pi->data==1&&pj->data==1) head->data=1;
        else if(pi->data==-1&&pj->data==-1) head->data=-1;
            else if(pi->data==1&&pj->data==-1) head->data=compare(m,n);
                else if(pi->data==-1&&pj->data==1) head->data=compare(n,m);
                    else cout << "ERROR4";
    pi= pi->next; pj= pj->next;
    p= new section;
    while(pi->next!=NULL||pj->next!=NULL){
        if(pi->next!=NULL&&pj->next!=NULL){
            if((i->data)*(j->data)==1){
                p->data= pi->data+pj->data+carrybit;
                if(p->data>9999) {p->data= p->data-10000; carrybit=1;}
                    else carrybit=0;
            }
            else if(((i->data)*(j->data)==-1)&&compare(m,n)==1){
                p->data= pi->data-pj->data+carrybit;
                if(p->data<0) {p->data= p->data+10000; carrybit=-1;}
                    else carrybit=0;
            }
            else if(((i->data)*(j->data)==-1)&&compare(m,n)==-1){
                p->data= pj->data-pi->data+carrybit;
                if(p->data<0) {p->data= p->data+10000; carrybit=-1;}
                    else carrybit=0;
            }
        }
        else if(pi->next!=NULL&&pj->next==NULL){
            if((i->data)*(j->data)==1){
                p->data= pi->data+carrybit;
                if(p->data>9999) {p->data= p->data-10000; carrybit=1;}
                    else carrybit=0;
            }
            else if(((i->data)*(j->data)==-1)&&compare(m,n)==1){
                p->data= pi->data+carrybit;
                if(p->data<0) {p->data= p->data+10000; carrybit=-1;}
                    else carrybit=0;
            }
            else if(((i->data)*(j->data)==-1)&&compare(m,n)==-1){
                cout << "ERROR5";
            }
        }
        else if(pi->next==NULL&&pj->next!=NULL){
            if((i->data)*(j->data)==1){
                p->data= pj->data+carrybit;
                if(p->data>9999) {p->data= p->data-10000; carrybit=1;}
                    else carrybit=0;
            }
            else if(((i->data)*(j->data)==-1)&&compare(m,n)==1){
                cout << "ERROR6";
            }
            else if(((i->data)*(j->data)==-1)&&compare(m,n)==-1){
                p->data= pj->data+carrybit;
                if(p->data<0) {p->data= p->data+10000; carrybit=-1;}
                    else carrybit=0;
            }
        }
        else cout << "ERROR7";

        rear->next= p;
        rear= p;
        cout << "每一个节点中的数据为:" << rear->data << endl;
        p= new section;
        if(pi->next!=NULL) pi= pi->next;
        if(pj->next!=NULL) pj= pj->next;
    }
    p->data=carrybit;
    rear->next= p;
    rear= p;
    rear->next=NULL;

    return head;
}


string getversalongint(section *n){
    section *p, *head;
    head=n;
    p=head->next;
    string z;
    if(head->data==1) {z[0]='+'; cout << "z[0]为:"<< z[0] << endl;}
        else if(head->data==-1) z[0]='-';
            else{z="0"; return z;}
    int k=0;
    do{
        if(p->data>999){
            z[4*k+1]='0'+p->data%10; p->data=p->data/10; cout << "z[4*k+1]=" << z[4*k+1] << endl;
            z[4*k+2]='0'+p->data%10; p->data=p->data/10; cout << "z[4*k+2]=" << z[4*k+2] << endl;
            z[4*k+3]='0'+p->data%10; p->data=p->data/10; cout << "z[4*k+3]=" << z[4*k+3] << endl;
            z[4*k+4]='0'+p->data; cout << "z[4*k+4]=" << z[4*k+4] << endl;
        }
        else if(p->data>99){
            z[4*k+1]='0'+p->data%10; p->data=p->data/10;
            z[4*k+2]='0'+p->data%10; p->data=p->data/10;
            z[4*k+3]='0'+p->data;
            z[4*k+4]='0';
        }
        else if(p->data>9){
            z[4*k+1]='0'+p->data%10; p->data=p->data/10;
            z[4*k+2]='0'+p->data;
            z[4*k+3]='0';
            z[4*k+4]='0';
        }
        else if(p->data>=0){
            z[4*k+1]='0'+p->data; cout << "z[4*k+1]=" << z[4*k+1] << endl;
            z[4*k+2]='0'; cout << "z[4*k+2]=" << z[4*k+2] << endl;
            z[4*k+3]='0'; cout << "z[4*k+3]=" << z[4*k+3] << endl;
            z[4*k+4]='0'; cout << "z[4*k+4]=" << z[4*k+4] << endl;
        }
        else cout << "ERROR8";
        p= p->next;
        ++k;
    }while(p->next!=NULL);
    z[4*k+5]='\r';
    cout << "ifzempty=" << z.empty() << endl;
    return z;
}


int main()
{
    //读入两个数据并记录符号
    cout << "请输入两个长整数,不能以0为最高位,从低位起每四位用英文逗号隔开" << endl;
    string x,y;
    cin >> x >> y;
    //创建链表,将数据保存,返回链表的首尾指针
    section *a, *b;
    a=getchain(x);      //!数组名是一个地址常量而不是变量,所以不能被赋值
    b=getchain(y);
    //实现两数相加
    section *c;
    c=pluschain(a, b, x, y);
    //输出结果
    string z;
    z=getversalongint(c);
    cout << "两数相加结果为:" << endl;
    //输出时注意去掉首位的0
    cout << "z=" << z << endl;
    cout << z[0];
    int zlength=z.length();
    bool flag=false;
    for(int i=zlength-1;i>0;--i){
        if(z[i]=='0') {if(flag) cout<< z[i];}
        else {flag=true; cout<< z[i];}
    }

    //至此,所有动态申请的空间均没有delete
    //输出结果也没有加逗号

    return 0;
}


依次赋值后在输出每位都没有问题,但是最后一句的输出结果总是:ifzempty=1,之后main函数输出z的时候也是什么也输出不了。
请问这是为什么啊?
怎么修改呢?

1个回答

代码不完整没法帮你调试,可能的原因有两个,一个是传入的链表n,是不是创建正确了。
另一个是,代码中是否有下标越界,把数据冲掉了。你要把main函数和完整程序都拿上来,才好调试。

weixin_42122038
暮鲲 您好,我刚才把完整程序传上去了,能再帮我看一下吗
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问