关于二叉树的问题!我想知道为什么scanf跳过执行了

\u4e8c\u53c9\u6811\u7684\u521b\u5efa\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u6b63\u5e38\u6267\u884c\u5462\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u8df3\u8fc7scanf\u8bed\u53e5

scanf("%c",&flag);
\u540e\u9762\u52a0\u4e0agetchar();
\u7528\u6765\u63a5\u6536\u8f93\u5165\u65f6\u6700\u540e\u7684\u56de\u8f66\u7b26\uff01

\u4f60\u5148\u627e\u627e\u6587\u4ef6\u5934\u5427 \u6709\u53ef\u80fd\u662f\u6587\u4ef6\u5934\u7684\u95ee\u9898 \u7ed9\u4f60\u4e00\u4e2astdio.h\u7684\u6587\u4ef6\u5934 \u4fdd\u5b58\u8bb0\u4e8b\u672c\u5728vc2008\u7684include\u91cc\uff1a
/* stdio.h

Definitions for stream input/output.

Copyright (c) Borland International 1987,1988
All Rights Reserved.
*/
#if __STDC__
#define _Cdecl
#else
#define _Cdecl cdecl
#endif

#if !defined(__STDIO_DEF_)
#define __STDIO_DEF_

#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned size_t;
#endif
#ifndef NULL
# if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
# define NULL 0
# else
# define NULL 0L
# endif
#endif

#if !defined(__STDARG)
#include
#endif

/* Definition of the file position type
*/
typedef long fpos_t;

/* Definition of the control structure for streams
*/
typedef struct {
short level; /* fill/empty level of buffer */
unsigned flags; /* File status flags */
char fd; /* File descriptor */
unsigned char hold; /* Ungetc char if no buffer */
short bsize; /* Buffer size */
unsigned char *buffer; /* Data transfer buffer */
unsigned char *curp; /* Current active pointer */
unsigned istemp; /* Temporary file indicator */
short token; /* Used for validity checking */
} FILE; /* This is the FILE object */

/* Bufferisation type to be used as 3rd argument for "setvbuf" function
*/
#define _IOFBF 0
#define _IOLBF 1
#define _IONBF 2

/* "flags" bits definitions
*/
#define _F_RDWR 0x0003 /* Read/write flag */
#define _F_READ 0x0001 /* Read only file */
#define _F_WRIT 0x0002 /* Write only file */
#define _F_BUF 0x0004 /* Malloc'ed Buffer data */
#define _F_LBUF 0x0008 /* line-buffered file */
#define _F_ERR 0x0010 /* Error indicator */
#define _F_EOF 0x0020 /* EOF indicator */
#define _F_BIN 0x0040 /* Binary file indicator */
#define _F_IN 0x0080 /* Data is incoming */
#define _F_OUT 0x0100 /* Data is outgoing */
#define _F_TERM 0x0200 /* File is a terminal */

/* End-of-file constant definition
*/
#define EOF (-1) /* End of file indicator */

/* Number of files that can be open simultaneously
*/
#define OPEN_MAX 20 /* Total of 20 open files */
#define SYS_OPEN 20

/* Default buffer size use by "setbuf" function
*/
#define BUFSIZ 512 /* Buffer size for stdio */

/* Size of an arry large enough to hold a temporary file name string
*/
#define L_ctermid 5 /* CON: plus null byte */
#define L_tmpnam 13 /* tmpnam buffer size */

/* Constants to be used as 3rd argument for "fseek" function
*/
#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_SET 0

/* Number of unique file names that shall be generated by "tmpnam" function
*/
#define TMP_MAX 0xFFFF

/* Standard I/O predefined streams
*/
extern FILE _Cdecl _streams[];

#define stdin (&_streams[0])
#define stdout (&_streams[1])
#define stderr (&_streams[2])
#define stdaux (&_streams[3])
#define stdprn (&_streams[4])

void _Cdecl clearerr (FILE *stream);
int _Cdecl fclose (FILE *stream);
int _Cdecl fflush (FILE *stream);
int _Cdecl fgetc (FILE *stream);
int _Cdecl fgetpos (FILE *stream, fpos_t *pos);
char *_Cdecl fgets (char *s, int n, FILE *stream);
FILE *_Cdecl fopen (const char *path, const char *mode);
int _Cdecl fprintf (FILE *stream, const char *format, ...);
int _Cdecl fputc (int c, FILE *stream);
int _Cdecl fputs (const char *s, FILE *stream);
size_t _Cdecl fread (void *ptr, size_t size, size_t n, FILE *stream);
FILE *_Cdecl freopen (const char *path, const char *mode,
FILE *stream);
int _Cdecl fscanf (FILE *stream, const char *format, ...);
int _Cdecl fseek (FILE *stream, long offset, int whence);
int _Cdecl fsetpos (FILE *stream, const fpos_t *pos);
long _Cdecl ftell (FILE *stream);
size_t _Cdecl fwrite (const void *ptr, size_t size, size_t n,
FILE *stream);
char *_Cdecl gets (char *s);
void _Cdecl perror (const char *s);
int _Cdecl printf (const char *format, ...);
int _Cdecl puts (const char *s);
int _Cdecl rename (const char *oldname, const char *newname);
void _Cdecl rewind (FILE *stream);
int _Cdecl scanf (const char *format, ...);
void _Cdecl setbuf (FILE *stream, char *buf);
int _Cdecl setvbuf (FILE *stream, char *buf, int type, size_t size);
int _Cdecl sprintf (char *buffer, const char *format, ...);
int _Cdecl sscanf (const char *buffer, const char *format, ...);
char *_Cdecl strerror (int errnum);
FILE *_Cdecl tmpfile (void);
char *_Cdecl tmpnam (char *s);
int _Cdecl ungetc (int c, FILE *stream);
int _Cdecl vfprintf (FILE *stream, const char *format, va_list arglist);
int _Cdecl vfscanf (FILE *stream, const char *format, va_list arglist);
int _Cdecl vprintf (const char *format, va_list arglist);
int _Cdecl vscanf (const char *format, va_list arglist);
int _Cdecl vsprintf (char *buffer, const char *format, va_list arglist);
int _Cdecl vsscanf (const char *buffer, const char *format, va_list arglist);

#if !__STDC__
int _Cdecl fcloseall(void);
FILE *_Cdecl fdopen (int handle, char *type);
int _Cdecl fgetchar (void);
int _Cdecl flushall (void);
int _Cdecl fputchar (int c);
int _Cdecl getw (FILE *stream);
int _Cdecl putw (int w, FILE *stream);
char *_Cdecl _strerror(const char *s);
int _Cdecl unlink (const char *path);

#endif

int _Cdecl _fgetc (FILE *stream); /* used by getc() macro */
int _Cdecl _fputc (char c, FILE *stream); /* used by putc() macro */

/* The following macros provide for common functions */

#define ferror(f) ((f)->flags & _F_ERR)
#define feof(f) ((f)->flags & _F_EOF)
#define fileno(f) ((f)->fd)
#define remove(path) unlink(path)

#define getc(f) \
((--((f)->level) >= 0) ? (unsigned char)(++(f)->curp)[-1] : \
_fgetc (f))
#define putc(c,f) \
((++((f)->level) curp)[-1]=(c)) : \
_fputc ((c),f))

#define getchar() getc(stdin)
#define putchar(c) putc((c), stdout)

#define ungetc(c,f) ungetc((c),f) /* traditionally a macro */

#endif

\u001a

并不是跳过执行了,事实上它每次递归都执行。你创建树的时候输入的是一个字符串,scanf每次读取字符串中的一个字符,剩余没读取的放在缓冲区,下次递归执行到scanf语句的时候,因为缓冲区里边还有字符,它就自动读取了,而不会阻塞等待你的输入。

scanf 有一个缓存的问题,简单来说就是你第一个scanf输入一个东西然后有以下回车或是空格,对吧,那么你的下一个scanf会把回车或空格输入。你可以再每次scanf前加一个fflush(stdin),貌似是这个吧

  • 浠涔堝彨浜屽弶鏍?
    绛旓細闂涓锛氫粈涔堟槸浜屽弶鏍锛熸湁鍑犵鍒嗙被锛熻妭鐐瑰張鏄粈涔堝晩锛 鍦ㄨ绠楁満绉戝涓紝浜屽弶鏍戞槸姣忎釜缁撶偣鏈澶氭湁涓や釜瀛愭爲鐨勬湁搴忔爲銆傞氬父瀛愭爲鐨勬牴琚О浣溾滃乏瀛愭爲鈥濓紙left subtree锛夊拰鈥滃彸瀛愭爲鈥濓紙right subtree锛夈備簩鍙夋爲甯歌鐢ㄤ綔浜屽弶鏌ユ壘鏍戝拰浜屽弶鍫嗐(1)瀹屽叏浜屽弶鏍戔曗曞彧鏈夋渶涓嬮潰鐨勪袱灞傜粨鐐瑰害灏忎簬2锛屽苟涓旀渶涓嬮潰涓...
  • 璇锋暀鍏充簬浜屽弶鏍鑺傜偣鐨勯棶棰
    绛旓細鎵浠(4)=18锛屽嵆鍏锋湁鍥涗釜鑺傜偣鐨浜屽弶鏍鏈18绉嶃
  • 鍏充簬浜岀骇C璇█鐨浜屽弶鏍戦棶棰绌剁珶鎬庝箞鍥炰簨,璇︾粏瑙g瓟?
    绛旓細鍥犱负浜屽弶鏍鐨勬晥鐜囧拰绌洪棿瀛樺偍姣旇緝浼樿秺锛屼负浠涔堜紭瓒婏紝鍏蜂綋鐨勬爲鏁版嵁缁撴瀯浣犲幓鍗栦笓鐢ㄤ功鐪嬫垜涓嶅鍋氳В閲娿傝繕鏈変竴鐐癸紝涓轰粈涔堝彨浜屽弶鏍戯紵C鍙爲鏍 A B鍙彾瀛愬彧鏈変袱涓垎鏀紝N鍙夋爲涔熻杞寲涓鸿繖涓舰寮忔潵澶勭悊銆
  • 鏈夊叧 浜屽弶鏍戠殑鍑犱釜闂
    绛旓細n1=0;璇存槑鏄弧浜屽弶鏍 log2(47+1)=log2(48) 鍚戜笂鍙栨暣灏辨槸 6
  • 浜屽弶鏍戦棶棰
    绛旓細鍥犱负鍦浜屽弶鏍涓荤殑鑺傜偣涓暟姣旀荤殑搴︽暟澶1.锛堜綘鎯筹紝姣忎釜闈炴牴鑺傜偣鐨勫ご涓婇兘甯︾潃涓涓竟锛屼篃灏辨槸姣忔湁涓涓潪鏍硅妭鐐瑰氨鏈変竴涓害锛屾墍浠ヨ妭鐐规暟灏辨瘮鎬诲害鏁板浜嗕釜鏍硅妭鐐癸級鍦ㄤ簩鍙夋爲閲岄櫎浜嗗彾瀛愶紙搴︿负0锛夈佸害涓1鐨勭偣锛屽氨鏄害涓2鐨勭偣銆傝搴︿负2鐨勭偣鐨勪釜鏁颁负x锛屽垯 2x+80*1+70*0+1=x+80+70 瑙e緱 ...
  • 鏈夊叧浜屽弶鏍戠殑闂
    绛旓細鏍规嵁浜屽弶鏍戠殑鎬ц川:瀵逛簬涓妫甸潪绌虹殑浜屽弶鏍,濡傛灉鍙跺瓙鑺傜偣鏁颁负n0,搴︿负2鐨勭粨鐐规暟涓簄2,鍒檔o=n2+1.鏍规嵁瀹屽叏浜屽弶鏍戠殑瀹氫箟鍙緱:鍦ㄥ畬鍏ㄤ簩鍙夋爲涓害涓1鐨勭粨鐐筺1鍙兘鍙栦袱绉嶆儏鍐,瑕佷箞涓0,瑕佷箞涓1.鎵浠:n0+n1+n2=700 n0=n2+1;2n0=701-n1;鍥犱负缁撶偣鏁颁负鏁存暟锛屾墍浠1=1,no=350 ...
  • 浜屽弶鏍戦棶棰
    绛旓細鍙跺瓙缁撶偣涓暟n0 = n2 + 1 = 23 + 1 = 24锛屽洜姝ゅ害涓1缁撶偣涓暟n1 = 47 - 23 - 24 = 0锛屽洜姝よ浜屽弶鏍涓彧鏈夊害涓0鍜屽害涓2鐨勭粨鐐癸紝閫氱О姝h锛堟鍒欙級浜屽弶鏍戯紝鏈灏忔繁搴︾瓑浜庡悓鏍风粨鐐规暟閲忕殑瀹屽叏浜屽弶鏍=log2(n) + 1= 6锛屾渶澶ф繁搴=(47 + 1) / 2 = 24 ...
  • 浜屽弶鏍戦棶棰
    绛旓細涓鐭ラ亾杩欐牱瑙i噴浣犳槸鍚︽槑鐧姐俠涔嬫墍浠ラ敊浜嗭紝鏄洜涓哄瓙鏍戜笅闈㈣繕鏈夊瓙鏍戯紝瀛愭爲杩樺彲浠ユ湁鑷繁鐨勫瓙鏍戙浜屽弶鏍涓紝姣忎釜缁撶偣鏈澶氬彧鏈変袱涓悗缁х粨鐐癸紝浣犵悊瑙g殑鏄渶澶氬氨杩欎袱妫靛瓙鏍戯紝瀹為檯涓婏紝瀛愭爲涓殑缁撶偣閮芥槸璇ョ粨鐐圭殑瀛愬瓩锛岄偅涔堜笅闈㈢殑鎵鏈夌殑瀛愭爲閮芥槸瀹冪殑瀛愭爲銆傝嫢鏈変笉鏄庣櫧锛岃繕鍙互缁х画闂垜~鍛靛懙 ...
  • 璁$畻鏈轰簩绾鍏充簬浜屽弶鏍戠殑闂銆傛噦鐨勮繘,鎬ユ眰瑙g瓟銆
    绛旓細鐢浜屽弶鏍戠殑鎬ц川鍙煡锛歯0=n2+1锛屽垯n= n0+n1+n2锛堝叾涓璶涓哄畬鍏ㄤ簩鍙夋爲鐨勭粨鐐规绘暟锛夛紝鐢变笂杩板叕寮忔妸n2娑堝幓寰楋細n= 2n0+n1-1锛岀敱浜庡畬鍏ㄤ簩鍙夋爲涓害涓1鐨勭粨鐐规暟鍙湁涓ょ鍙兘0鎴1锛岀敱姝ゅ緱鍒皀0=锛坣+1锛/2鎴杗0=n/2锛屽氨鍙牴鎹畬鍏ㄤ簩鍙夋爲鐨勭粨鐐规绘暟璁$畻鍑哄彾瀛愮粨鐐规暟銆 鎵浠ュ彾瀛愮粨鐐规暟鐩稿悓 ...
  • 浜屽弶鏍涓彾瀛愮粨鐐逛釜鏁鐨勯棶棰,鎬庢牱瑙i噴?
    绛旓細瀹屽叏浜屽弶鏍戞湁涓涓ц川锛 鍏锋湁n涓粨鐐圭殑瀹屽叏浜屽弶鏍戠殑娣卞害涓簂og2n(2鏄笅鏍)+1銆傛牴鎹繖涓ц川锛屽氨鍙互姹傚緱瀹屽叏浜屽弶鏍戠殑娣卞害涓10 10灞傛弧浜屽弶鏍戠殑鎬荤粨鐐规暟涓1023锛屾渶鍚庝竴灞傜殑缁撶偣鏁板簲璇ユ槸2鐨9娆℃柟涓512锛屾墍浠ヨ偗瀹699涓粨鐐硅偗瀹氫笉鏄弧浜屽弶鏍戙傘傘傚彾瀛愯妭鐐瑰嚭鐜板湪鏈鍚庝袱灞備笂銆傘傘傛渶鍚庝竴灞傚彾瀛愮粨鐐逛釜鏁颁负...
  • 扩展阅读:扫一扫题目出答案 ... 二叉树已知前序中序求后序 ... 心理测试看到大树 ... 二叉树变森林的图解 ... 二叉树能解决什么问题 ... 最简单的二叉树图解 ... 二叉树图解 ... 二叉树的三种遍历图解 ... 二叉树后序遍历图解 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网