c语言字符数组无法赋值,C语言程序,数组元素无法赋值,请大家帮忙看看解决方案...

当前位置:我的异常网» C语言 » C语言程序,数组元素无法赋值,请大家帮忙看看解决

C语言程序,数组元素无法赋值,请大家帮忙看看解决方案

www.myexceptions.net  网友分享于:2013-03-20  浏览:13次

C语言程序,数组元素无法赋值,请大家帮忙看看

请大家帮忙看看我的程序,明明写了赋值语句怎么会不赋值呢?

我设断点看过了,spiral[count]没有被赋成matrix[i][j]的值,每次都不变

十分不解...

我的程序:

#include

#include

#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

{

for (j=0;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

{

spiral[count]=matrix[i][j];//create spiral string

count++;

}

j--;

i++; //move to the next line

east--;

for( ;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<

}

void main()

{

String str,Ptext;

MS matrix;

cout<

cin>>Ptext;//get text

CreateMatrix(3,Ptext,matrix); //biuld matrix

cout<

SpiralString(4,3,matrix,str);

cout<

PrintStr(str);

}

------解决方案--------------------

lz定义了数组有空间的,试了一下好像跳不出循环可以,改了

...

if(count>=height*width)break;

}

spiral[count]='\0' //c字串记得结尾补0

}

输入12个字符后输出如lz所要是螺旋的...

文章评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值