为什么这里只改变一个【关键词】或【语句位置】会导致结果错误,求解!!!

MewX 2012-08-07 11:29:14
这个是正确的代码(gcc编译器、Dev-C++编译环境)

#include <stdio.h>
/*
Input:
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit

*/

void moveAontoB( int a, int b ) { }
void moveAoverB( int a, int b ) { }
void pileAontoB( int a, int b ) { }
void pileAoverB( int a, int b ) { }


int main( )
{
int n, i, j;//Read the value: n | P.s. [ 0, n - 1 ] | 1 <= n && n <= 24; i, j are for loop;

scanf( "%d", &n );

char TempString[ 5 ];
short TempShort[ 2 ];

while( 1 ) {
scanf( "%s", TempString );
//Keyword1: m(o)ve; p(i)le; q(u)it;
//Keyword2: o(n)to; o(v)er;
if( TempString[ 1 ] == 'o' ){//Judge it 1"move"
scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );
printf( "%d %s %d\n", TempShort[ 0 ], TempString, TempShort[ 1 ] );//TEST
if( TempString[ 1 ] == 'n' ) moveAontoB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"onto"
else if( TempString[ 1 ] == 'v' ) moveAoverB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"over"
else { }
}
else if( TempString[ 1 ] == 'i' ){//Judge it 1"pile"
scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );
printf( "%d %s %d\n", TempShort[ 0 ], TempString, TempShort[ 1 ] );//TEST
if( TempString[ 1 ] == 'n' ) pileAontoB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"onto"
else if( TempString[ 1 ] == 'v' ) pileAoverB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"over"
else { }
}
else if( TempString[ 1 ] == 'u' ) break;//Judge it 1"quit"
else { }
}

//Output them
for( i = 0; i < n; i ++ ) {
printf( "%d:", i );
for( j = 0; j < 1; j ++ ) {}
printf( "\n" );
}

system( "PAUSE" );
}

运行结果:
10
move 9 onto 1
9 onto 1
move 8 over 1
8 over 1
move 7 over 1
7 over 1
move 6 over 1
6 over 1
pile 8 over 6
8 over 6
pile 8 over 5
8 over 5
move 2 over 1
2 over 1
move 4 over 9
4 over 9
quit
Press any key to continue . . .



注意看:main( )下面的第一行。
1.我若将它改为short型,则不能输出TempString,调试发现TempString = "\0\0on"
2.或者我将这整句话,保留int,移到main( )之外也会失败。


具体代码:
情况1

#include <stdio.h>
/*
Input:
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit

*/

void moveAontoB( int a, int b ) { }
void moveAoverB( int a, int b ) { }
void pileAontoB( int a, int b ) { }
void pileAoverB( int a, int b ) { }

int main( )
{
short n, i, j;//Read the value: n | P.s. [ 0, n - 1 ] | 1 <= n && n <= 24; i, j are for loop;

scanf( "%d", &n );

char TempString[ 5 ];
short TempShort[ 2 ];

while( 1 ) {
scanf( "%s", TempString );
//Keyword1: m(o)ve; p(i)le; q(u)it;
//Keyword2: o(n)to; o(v)er;
if( TempString[ 1 ] == 'o' ){//Judge it 1"move"
scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );
printf( "%d %s %d\n", TempShort[ 0 ], TempString, TempShort[ 1 ] );//TEST
if( TempString[ 1 ] == 'n' ) moveAontoB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"onto"
else if( TempString[ 1 ] == 'v' ) moveAoverB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"over"
else { }
}
else if( TempString[ 1 ] == 'i' ){//Judge it 1"pile"
scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );
printf( "%d %s %d\n", TempShort[ 0 ], TempString, TempShort[ 1 ] );//TEST
if( TempString[ 1 ] == 'n' ) pileAontoB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"onto"
else if( TempString[ 1 ] == 'v' ) pileAoverB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"over"
else { }
}
else if( TempString[ 1 ] == 'u' ) break;//Judge it 1"quit"
else { }
}

//Output them
for( i = 0; i < n; i ++ ) {
printf( "%d:", i );
for( j = 0; j < 1; j ++ ) {}
printf( "\n" );
}

system( "PAUSE" );
}


情况2

#include <stdio.h>
/*
Input:
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit

*/

void moveAontoB( int a, int b ) { }
void moveAoverB( int a, int b ) { }
void pileAontoB( int a, int b ) { }
void pileAoverB( int a, int b ) { }

int n, i, j;//Read the value: n | P.s. [ 0, n - 1 ] | 1 <= n && n <= 24; i, j are for loop;

int main( )
{
scanf( "%d", &n );

char TempString[ 5 ];
short TempShort[ 2 ];

while( 1 ) {
scanf( "%s", TempString );
//Keyword1: m(o)ve; p(i)le; q(u)it;
//Keyword2: o(n)to; o(v)er;
if( TempString[ 1 ] == 'o' ){//Judge it 1"move"
scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );
printf( "%d %s %d\n", TempShort[ 0 ], TempString, TempShort[ 1 ] );//TEST
if( TempString[ 1 ] == 'n' ) moveAontoB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"onto"
else if( TempString[ 1 ] == 'v' ) moveAoverB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"over"
else { }
}
else if( TempString[ 1 ] == 'i' ){//Judge it 1"pile"
scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );
printf( "%d %s %d\n", TempShort[ 0 ], TempString, TempShort[ 1 ] );//TEST
if( TempString[ 1 ] == 'n' ) pileAontoB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"onto"
else if( TempString[ 1 ] == 'v' ) pileAoverB( TempShort[ 0 ], TempShort[ 1 ] );//Judge it 2"over"
else { }
}
else if( TempString[ 1 ] == 'u' ) break;//Judge it 1"quit"
else { }
}

//Output them
for( i = 0; i < n; i ++ ) {
printf( "%d:", i );
for( j = 0; j < 1; j ++ ) {}
printf( "\n" );
}

system( "PAUSE" );
}

以上两种错误的运行结果均为:
10
move 9 onto 1
9 1
move 8 over 1
8 1
move 7 over 1
7 1
move 6 over 1
6 1
pile 8 over 6
8 6
pile 8 over 5
8 5
move 2 over 1
2 1
move 4 over 9
4 9
quit
Press any key to continue . . .


怎么解释这两种改变引起的结果?求解!!!
...全文
212 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
MewX 2012-08-11
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]

LZ有个东西叫做debugger,可以跟踪下是哪个语句产生问题的。
基本上既然用了short,那么scanf是应该要用%h的。
[/Quote]

[Quote=引用 8 楼 的回复:]

short TempShort[ 2 ];
注意:你在这里定义的是short类型,而你输入输出用的是%d,所以有错,改成%hd就可以了。
搞了一个中午,楼主记得给分啊!!
[/Quote]

果然问题出在TempShort[ 2 ],用%hd就可以解决上述问题,谢谢各位啊!~
下面贴出修改过的代码,这次可以改成上述情况都不出错。
#include <stdio.h>
/*
Input:
10
move 9 onto 1

*/

void moveAontoB( int a, int b ) { }
void moveAoverB( int a, int b ) { }
void pileAontoB( int a, int b ) { }
void pileAoverB( int a, int b ) { }


int main( )
{
int n;//可以修改了!~解决
scanf( "%d", &n );

char TempString[ 5 ];
short TempShort[ 2 ];
scanf( "%s", TempString );

scanf( "%hd%s%hd", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );
printf( "%d %s %d\n", TempShort[ 0 ], TempString, TempShort[ 1 ] );//TEST

system( "PAUSE" );
return 0;
}
ForestDB 2012-08-10
  • 打赏
  • 举报
回复
另外这些变量在内存中是连着的,如果对一个变量赋值(或者变相赋值,比如scanf,就是改变某变量的值),但是恰好又赋值不当,就会把隔壁的也改变了。
这个,用debugger也是看的出来的。
ForestDB 2012-08-10
  • 打赏
  • 举报
回复
LZ有个东西叫做debugger,可以跟踪下是哪个语句产生问题的。
基本上既然用了short,那么scanf是应该要用%h的。
MewX 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

引用 11 楼 的回复:

引用 10 楼 的回复:

这种问题一般不是什么很大的问题,你再改改输入输出,或者把
scanf( "%d%s%d", &amp;amp;TempShort[ 0 ], TempString, &amp;amp;TempShort[ 1 ] );//问题所在!!!
这句分开来写试试。自己多试,问题总会得到解答的。祝你好运!


嗑……
[/Quote]

我在7楼缩短了程序。
MewX 2012-08-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

这种问题一般不是什么很大的问题,你再改改输入输出,或者把
scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );//问题所在!!!
这句分开来写试试。自己多试,问题总会得到解答的。祝你好运!
[/Quote]

嗑。。。
我试了几十遍变法,才定位到那个定义int n 的语句上的。
分开输出我也试了,没用额。
能不能再帮想想啊!~谢谢!~
linuxblack125125 2012-08-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

引用 10 楼 的回复:

这种问题一般不是什么很大的问题,你再改改输入输出,或者把
scanf( "%d%s%d", &amp;TempShort[ 0 ], TempString, &amp;TempShort[ 1 ] );//问题所在!!!
这句分开来写试试。自己多试,问题总会得到解答的。祝你好运!


嗑。。。
我试了几十遍变法,才定位到那个定义int……
[/Quote]

你搞个小的测试程序测试一下啊。把你认为可能的错误都测试一下就可以了。不要老拿着这个程序搞,很容易昏的
linuxblack125125 2012-08-08
  • 打赏
  • 举报
回复
在linux里面测试过了,没有你说的那种情况
MewX 2012-08-08
  • 打赏
  • 举报
回复
求问那!!!!!!!!!!!!!!!!!!!!!!!
MewX 2012-08-08
  • 打赏
  • 举报
回复
在缩短一点:
#include <stdio.h>
/*
Input:
10
move 9 onto 1

*/

int main( )
{
int n;//Read the value: n | P.s. [ 0, n - 1 ] | 1 <= n && n <= 24; i, j are for loop;
char TempString[ 5 ];
short TempShort[ 2 ];
scanf( "%d%s", &n, TempString );

scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );//问题所在!!!
printf( "%d %s %d\n", TempShort[ 0 ], TempString, TempShort[ 1 ] );//TEST

system( "PAUSE" );
return 0;
}


能得到正确的结果:
10
move 9 onto 1
9 onto 1
Press any key to continue . . .


注意看:main( )下面的第一行。
1.我若将它改为short型,则不能输出TempString,调试发现TempString = "\0\0on"
2.或者我将这整句话,保留int,移到main( )之外也会失败。
linuxblack125125 2012-08-08
  • 打赏
  • 举报
回复
这种问题一般不是什么很大的问题,你再改改输入输出,或者把
scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );//问题所在!!!
这句分开来写试试。自己多试,问题总会得到解答的。祝你好运!
MewX 2012-08-08
  • 打赏
  • 举报
回复
缩短后基本上能定位到问题了:
#include <stdio.h>
/*
Input:
10
move 9 onto 1

*/

void moveAontoB( int a, int b ) { }
void moveAoverB( int a, int b ) { }
void pileAontoB( int a, int b ) { }
void pileAoverB( int a, int b ) { }

int main( )
{
int n;//Read the value: n | P.s. [ 0, n - 1 ] | 1 <= n && n <= 24; i, j are for loop;
scanf( "%d", &n );

char TempString[ 5 ];
short TempShort[ 2 ];
scanf( "%s", TempString );

scanf( "%d%s%d", &TempShort[ 0 ], TempString, &TempShort[ 1 ] );
printf( "%d %s %d\n", TempShort[ 0 ], TempString, TempShort[ 1 ] );//TEST

system( "PAUSE" );
}

能得到正确的结果:
10
move 9 onto 1
9 onto 1
Press any key to continue . . .


注意看:main( )下面的第一行。
1.我若将它改为short型,则不能输出TempString,调试发现TempString = "\0\0on"
2.或者我将这整句话,保留int,移到main( )之外也会失败。
MewX 2012-08-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

情况1是有可能出错,因为你scanf给的参数不符,%d但参数是short,情况2应该是不可能的
[/Quote]

但是反复测试发现情况1跟情况2的错误结果一样。
nice_cxf 2012-08-08
  • 打赏
  • 举报
回复
情况1是有可能出错,因为你scanf给的参数不符,%d但参数是short,情况2应该是不可能的
MewX 2012-08-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

在linux里面测试过了,没有你说的那种情况
[/Quote]

请问为什么在win32的GCC里面测试会出现这种情况???
MewX 2012-08-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

short TempShort[ 2 ];
注意:你在这里定义的是short类型,而你输入输出用的是%d,所以有错,改成%hd就可以了。
搞了一个中午,楼主记得给分啊!!
[/Quote]

首先谢谢啊,但是我这里用Dev-C++内置的GCC编译仍然有上面同样的问题额。。。
能不能再帮想想,谢谢!~
linuxblack125125 2012-08-08
  • 打赏
  • 举报
回复
short TempShort[ 2 ];
注意:你在这里定义的是short类型,而你输入输出用的是%d,所以有错,改成%hd就可以了。
搞了一个中午,楼主记得给分啊!!

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧