69,368
社区成员
发帖
与我相关
我的任务
分享
/*
个人觉得return的几点好处是
1. 可以使程序在遇到异常时提前返回
2. 可以使代码不用向右推移的写,例如用tab,或者4个space
*/
#include "stdio.h"
#define USE_RETUREN
#define ERROR_PRINT(str) printf("%s:%d %s !\n", __FILE__, __LINE__, str)
static int InitSystem(void)
{
printf("Init system successful !\n");
return 1;
}
static int InitApp(void)
{
printf("Init application successful !\n");
return 1;
}
static int InitWindow(void)
{
printf("Init window successful !\n");
return 1;
}
// 80 len
/*******************************************************************************
函 数 名 : TestInit
功 能 : 代码按照先后顺序初始化system, app, window
输入参数 : 无
返 回 值 : 成功返回1, 失败返回0
作 者 : jernymy
时 间 : 2010-01-09
*******************************************************************************/
#ifdef USE_RETUREN
// 此种方法可以看到代码还是比较清晰易见的了
int TestInit(void)
{
int nRet;
nRet = InitSystem();
if (!nRet)
{
ERROR_PRINT("Init system fail");
return 0;
}
nRet = InitApp();
if (!nRet)
{
ERROR_PRINT("Init application fail");
return 0;
}
nRet = InitWindow();
if (!nRet)
{
ERROR_PRINT("Init window fail");
return 0;
}
printf("Init all finished !\n");
return 1;
}
#else
// 此种方法,我们会看到不断的if...else,是code向右偏移
// 一行code比较短的时候还可以
// 但是code每行比较长的是时候,就不方便看代码了
// 建议一行的code长度不要超过80字节
int TestInit(void)
{
int nRet = 0;
nRet = InitSystem();
if (nRet)
{
nRet = InitApp();
if (nRet)
{
nRet = InitWindow();
if (nRet)
{
printf("Init all finished !\n");
}
else
{
ERROR_PRINT("Init window fail");
}
}
else
{
ERROR_PRINT("Init application fail");
}
}
else
{
ERROR_PRINT("Init system fail");
}
return nRet;
}
#endif
int main(void)
{
int nRet;
nRet = TestInit();
if (!nRet)
{
ERROR_PRINT("Init all fail");
return -1;
}
return 0;
}
void preorder(Tree *tree)
{
if(tree!=NULL)
{
printf("%d, ",tree->data);
preorder(tree->left);
preorder(tree->right);
}
}