一个小的c++逻辑错误,在线等待!!

来源:百度知道 编辑:UC知道 时间:2024/06/19 18:52:33
#include<iostream.h>
#include<string.h>
#include"H_nizhi.h"
void InitList_Sq(Sqlist &L,int maxsize= LIST_INIT_SIZE,int increlist=LISTINCREMENT)
{
L.data=new Elemtype[maxsize];
L.length=0;
L.listsize=maxsize;
L.incrementsize=increlist;
}
void Destroylist_Sq(Sqlist &L)
{
delete[]L.data;
L.data=NULL;
}
void exchange(Sqlist &L)
{
int j=L.length;
for(int i=0;i<j;i++,j--)
{
Elemtype w='\0';
w=L.data[i];L.data[i]=L.data[j-1];L.data[j-1]=w;

}
cout<<"逆置后为:";
for( i=0;i<j;i++)
cout<<L.data[i]<<endl;
}
void main()
{
Sqlist a;
InitList_Sq(a,100,10);
cin>>a.data;
a.length=strlen(a.data);
exchange(a);
Destroylist_Sq(a);
}
头文件为:const LIST_INIT_SIZE=100;
const LISTINCREMENT=10;
typedef char Elemtype;
ty

void exchange(Sqlist &L)
{
int j=L.length;
for(int i=0;i<j;i++,j--)
{
Elemtype w='\0';
w=L.data[i];L.data[i]=L.data[j-1];L.data[j-1]=w;

}
j=L.length; 《----需要重新设置j的值
cout<<"逆置后为:";
for( i=0;i<j;i++)
cout<<L.data[i]<<endl;
}

你第二个for循环没把j给初始化,只显示一个。改成
for( i=0,j=L.length;i<j;i++)
cout<<L.data[i]<<endl;
}
就可以了

上面说的很对
高手``