C语言程序,数组元素无法赋值,请大家帮忙看看解决方案
C语言程序,数组元素无法赋值,请大家帮忙看看
请大家帮忙看看我的程序,明明写了赋值语句怎么会不赋值呢?
我设断点看过了,spiral[count]没有被赋成matrix[i][j]的值,每次都不变
十分不解...
我的程序:
#include<iostream.h>
#include<string.h>
#define MAXS 20
#define MAX 400
typedef char MS[MAXS][MAXS];
typedef char String[MAX];
int H;
void CreateMatrix(int width, String ptext, MS &matrix)
{//Create a Matrix with the width of key length
int l,i,j,ct,height;
//find out the height first
l=strlen(ptext);
if ((l%width)==0) height=l/width;
else height=l/width +1;
H=height;//H will be used in the procedure Tex3
ct=0;
for (i=0;i<height;i++ )
{
for (j=0;j<width;j++)
{
matrix[i][j]=ptext[ct];
ct=ct+1;
}
}
}
void SpiralString(int height,int width, MS matrix, String &spiral)
{//read the matrix spirally and return the spiral string
int i,j,count,flag,east,north,west,south;
flag=1; count=0;
east=width;
north=height;
i=0; j=0;
west=0;
south=0;
while(true)
{
if( flag==1 )//flag=1 means go Eastward and Northward
{
for (;j<east;j++)
{
spiral[count]=matrix[i][j];//create spiral string
count++;
}
j--;
i++; //move to the next line
east--;
for( ;i<north ;i++)
{
spiral[count]=matrix[i][j];//create spiral string
count++;
}
i--;
j--;
north--;
flag=-1;
}
else //flag=-1 means go Westward and Southward
{
for(;j>=west;j-- )
{
spiral[count]=matrix[i][j];//create spiral string
count++;
}
j++;
i--;
west++;
for( ;i>=south+1;i--)
{
spiral[count]=matrix[i][j];//create spiral string
count++;
}
j++;
i++;
south++;
flag=1;
}
if (count>height*width) break; //browsed all the elements
}
}
void PrintStr(String str)
{//print the string
for(int i=0; i < int(strlen(str)) ;i++)
cout<<str[i];
}
void main()
{
String str,Ptext;
MS matrix;
cout<<"Input the text:"<<endl;
cin>>Ptext;//get text
CreateMatrix(3,Ptext,matrix); //biuld matrix
cout<<endl;
SpiralString(4,3,matrix,str);
cout<<"The spiral string is:"<<SS<<endl;
PrintStr(str);
}
------解决方案--------------------
lz定义了数组有空间的,试了一下好像跳不出循环可以,改了
...
if(count>=height*width)break;
}
spiral[count]='\0' //c字串记得结尾补0
}
输入12个字符后输出如lz所要是螺旋的...
请大家帮忙看看我的程序,明明写了赋值语句怎么会不赋值呢?
我设断点看过了,spiral[count]没有被赋成matrix[i][j]的值,每次都不变
十分不解...
我的程序:
#include<iostream.h>
#include<string.h>
#define MAXS 20
#define MAX 400
typedef char MS[MAXS][MAXS];
typedef char String[MAX];
int H;
void CreateMatrix(int width, String ptext, MS &matrix)
{//Create a Matrix with the width of key length
int l,i,j,ct,height;
//find out the height first
l=strlen(ptext);
if ((l%width)==0) height=l/width;
else height=l/width +1;
H=height;//H will be used in the procedure Tex3
ct=0;
for (i=0;i<height;i++ )
{
for (j=0;j<width;j++)
{
matrix[i][j]=ptext[ct];
ct=ct+1;
}
}
}
void SpiralString(int height,int width, MS matrix, String &spiral)
{//read the matrix spirally and return the spiral string
int i,j,count,flag,east,north,west,south;
flag=1; count=0;
east=width;
north=height;
i=0; j=0;
west=0;
south=0;
while(true)
{
if( flag==1 )//flag=1 means go Eastward and Northward
{
for (;j<east;j++)
{
spiral[count]=matrix[i][j];//create spiral string
count++;
}
j--;
i++; //move to the next line
east--;
for( ;i<north ;i++)
{
spiral[count]=matrix[i][j];//create spiral string
count++;
}
i--;
j--;
north--;
flag=-1;
}
else //flag=-1 means go Westward and Southward
{
for(;j>=west;j-- )
{
spiral[count]=matrix[i][j];//create spiral string
count++;
}
j++;
i--;
west++;
for( ;i>=south+1;i--)
{
spiral[count]=matrix[i][j];//create spiral string
count++;
}
j++;
i++;
south++;
flag=1;
}
if (count>height*width) break; //browsed all the elements
}
}
void PrintStr(String str)
{//print the string
for(int i=0; i < int(strlen(str)) ;i++)
cout<<str[i];
}
void main()
{
String str,Ptext;
MS matrix;
cout<<"Input the text:"<<endl;
cin>>Ptext;//get text
CreateMatrix(3,Ptext,matrix); //biuld matrix
cout<<endl;
SpiralString(4,3,matrix,str);
cout<<"The spiral string is:"<<SS<<endl;
PrintStr(str);
}
------解决方案--------------------
lz定义了数组有空间的,试了一下好像跳不出循环可以,改了
...
if(count>=height*width)break;
}
spiral[count]='\0' //c字串记得结尾补0
}
输入12个字符后输出如lz所要是螺旋的...