- 1
- 0
- 约1.2万字
- 约 19页
- 2017-05-10 发布于湖北
- 举报
程序版式
对齐
程序块要采用缩进风格编写,缩进的空格数为4个。使用VC提供的Tab键对齐。
“{”和“}”应独占一行并且位于同一列,同时引用他们的语句对齐
{}之内的代码块在“{”右边数格外左对齐
例:
正确 错误
void Function(int x)
{
… // program code
} void Function(int x){
… // program code
}
if (condition)
{
… // program code
}
else
{
… // program code
} if (condition){
… // program code
}
else {
… // program code
} for (initialization; condition; update)
{
… // program code
} for (initialization; condition; update){
… // program code
} While (condition)
{
… // program code
} while (condition){
… // program code
} 如果出现嵌套的{},则使用缩进对齐,如:
{
…
{
…
}
…
} 空行
每个声明之后,每个函数定义之后要加空行
在一个函数体内,逻辑上密切相关的语句之间不加空行,其它地方应加空行分隔
变量声明和代码之间加空行
函数返回语句用空行
例:
void Function1(…)
// 空行
void Function2(…)
void Function1(…)
{
…
}
// 空行
void Function2(…)
{
…
} // 空行
while (condition)
{
statement1;
// 空行
if (condition)
{
statement2;
}
else
{
statement3;
}
// 空行
Return statement4
}
代码行
一行代码只做一件事情,如只定义一个变量,或只写一条语句。
if、for、do、while、case、switch、default等语句自占一行,且if、for、do、while等语句的执行语句部分无论多少都要加括号{}
例:
示例:风格良好的代码行 示例:风格不良的代码行
int width; // 宽度
int height; // 高度
int depth; // 深度
int width, height, depth; // 宽度高度深度
x = a + b;
y = c + d;
z = e + f; X = a + b; y = c + d; z = e + f;
if (width height)
{
dosomething();
} if (width height) dosomething(); for (initialization; condition; update)
{
dosomething();
}
// 空行
other();
for (initialization; condition; update)
dosomething();
other();
空格
关键字之后要留空格:const, virtual, inline, if, while, for
函数名之后不要留空格
“(”向后紧跟“,”,“、”,“.”,“;”,“)”向前紧跟
“,”后要留空格,“”;之后如果不是一行的结束,后面要留空格
赋值操作符,比较,算术,逻辑,第二元操作符前后加空格
一元操作符!、~、++、--、—等前后不加空格
像[]、“.”、—>例:
void Func1(int x, int y, int z); // 良好的风格
void Func1 (int x,int y,int z); // 不良的风格 if (year = 2000) // 良好的风格
if(year=2000) // 不良的风格
if ((a=b) (c=d)) // 良好的风格
if(a=bc=d) // 不良的风格 for (i=0; i10; i++)
您可能关注的文档
最近下载
- 山桐子种植加工、旅游项目可行性研究报告商业计划书.docx VIP
- 山桐子种植及种苗培育种植加工可行性研究报告申请备案.doc VIP
- 和田地区2026年度地直机关公开遴选公务员、事业单位公开选聘工作人员备考题库及完整答案详解1套.docx VIP
- 山桐子合作种植协议合同.docx VIP
- 2025年香氛未来趋势报告-英敏特.docx VIP
- 报告正文2014年太阳绿宝.pdf VIP
- 2025年高考:云南物理--试题及答案.pdf VIP
- 广东省深圳市宝安区2025-2026学年五年级上学期期末学业质量评估语文试卷.docx VIP
- (2025)山桐子产业开发生产建设项目可行性研究报告(一).docx VIP
- 2024-2025学年广东省东莞市统编版三年级上册期末考试语文试卷.pdf VIP
原创力文档

文档评论(0)