首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

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

2012-09-09 
为什么这里只改变一个【关键词】或【语句位置】会导致结果错误,求解!!!这个是正确的代码(gcc编译器、Dev-C++编

为什么这里只改变一个【关键词】或【语句位置】会导致结果错误,求解!!!
这个是正确的代码(gcc编译器、Dev-C++编译环境)

C/C++ code
#include <stdio.h>/*Input:10move 9 onto 1move 8 over 1move 7 over 1move 6 over 1pile 8 over 6pile 8 over 5move 2 over 1move 4 over 9quit*/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
C/C++ code
#include <stdio.h>/*Input:10move 9 onto 1move 8 over 1move 7 over 1move 6 over 1pile 8 over 6pile 8 over 5move 2 over 1move 4 over 9quit*/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
C/C++ code
#include <stdio.h>/*Input:10move 9 onto 1move 8 over 1move 7 over 1move 6 over 1pile 8 over 6pile 8 over 5move 2 over 1move 4 over 9quit*/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 . . .

怎么解释这两种改变引起的结果?求解!!!

[解决办法]
情况1是有可能出错,因为你scanf给的参数不符,%d但参数是short,情况2应该是不可能的
[解决办法]
short TempShort[ 2 ]; 
注意:你在这里定义的是short类型,而你输入输出用的是%d,所以有错,改成%hd就可以了。
搞了一个中午,楼主记得给分啊!!
[解决办法]
LZ有个东西叫做debugger,可以跟踪下是哪个语句产生问题的。
基本上既然用了short,那么scanf是应该要用%h的。

热点排行
Bad Request.