为什么这里只改变一个【关键词】或【语句位置】会导致结果异常,求解!
为什么这里只改变一个【关键词】或【语句位置】会导致结果错误,求解!!!
这个是正确的代码(gcc编译器、Dev-C++编译环境)
运行结果:
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
这个是正确的代码(gcc编译器、Dev-C++编译环境)
- C/C++ code
#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
- C/C++ code
#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" ); }