中国高校课件下载中心 》 教学资源 》 大学文库

上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus

文档信息
资源类别:文库
文档格式:PDF
文档页数:4
文件大小:57.86KB
团购合买:点击进入团购
内容简介
上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus
刷新页面文档预览

Programming Style Guide for C++ This programming style guide is a set of mandatory requirements for C++code layout.These stylistic "laws"are not made for arbitrary reasons.Experience teaches that conforming to this style,more or less standard through the industry with few exceptions,makes code easy to maintain and allows it to be read less painfully by others.This style guide is mandatory for Vg101,and can only be relaxed under specific instructions from GSI's and Instructor. In summary,the style guide is summarizes as: Indent 4 spaces relative to the preceding for every new nesting level.Curly brackets must always align vertically.Example, int main (void) ConsoleT console: int intx,intSum; bool quit false: console.printLine (This program adds a list of integers.\n"); console.printLine ("Enter values,one per line,using "SENTINEL,"to signal the end of the list.(n"): intSum =0: while (!quit) intX console.readInt ("Please input an integer:") if (intX =SENTINEL) quit true; }else intSum +intX: }: }: console.printLine(The total is intSum,".n"); } Variable and function names are started in lowercase,terse but descriptive,enough information conveyed within the programming context,with capital letters to start new words internal to the identifier.Example, numStuds=211: degToRad 180/PI; Don't over do it!Bad example, numberOfStudentsInTheENG101Setion200Class=211; conversionFactorForDegreesToRadians=180/PI; Never use underscores in function and variable names.Bade xample, this variable name is difficult to type =211; cute but dumb true: ■ Constant names should be all in uppercase.Leading underscores are forbidden.Using underscores to separate different words.For example, const float PI =3.14159: const float EULER CONSTANT=0.577216: A variable should be initialized near where it is first used and should only be defined

Programming Style Guide for C++ This programming style guide is a set of mandatory requirements for C++ code layout. These stylistic “laws” are not made for arbitrary reasons. Experience teaches that conforming to this style, more or less standard through the industry with few exceptions, makes code easy to maintain and allows it to be read less painfully by others. This style guide is mandatory for Vg101, and can only be relaxed under specific instructions from GSI’s and Instructor. In summary, the style guide is summarizes as: „ Indent 4 spaces relative to the preceding for every new nesting level. Curly brackets must always align vertically. Example, „ Variable and function names are started in lowercase, terse but descriptive, enough information conveyed within the programming context, with capital letters to start new words internal to the identifier. Example, numStuds = 211; degToRad = 180/PI; Don’t over do it! Bad example, numberOfStudentsInTheENG101Setion200Class = 211; conversionFactorForDegreesToRadians = 180/PI; „ Never use underscores in function and variable names. Bade xample, this_variable_name_is_difficult_to_type = 211; cute_______________but_dumb = true; „ Constant names should be all in uppercase. Leading underscores are forbidden. Using underscores to separate different words. For example, const float PI = 3.14159; const float EULER_CONSTANT = 0.577216; „ A variable should be initialized near where it is first used and should only be defined

within the scope for which it is relevant.Examples const int SIZE =10: float x[SIZE],y[SIZE]; for (int i=0;i>=& and Examples, float result =x y-z; //Good float result=x+y-Z: //Bad ■ There are no spaces before and after the binary operators */and %There is no space following the unary operators-and !Examples, float result=x*y +z; //Good bool decision=!go; //Bad ■No hardwired constants! Bad example: inta[10][10]; for(i=0;i=9:i=i+1) for (j=0;j=9;j=j+1) a[i]]=1; Good example: const int SIZE 10; int a[SIZE][SIZE]; for(i=0;i<SIZE;i=i+1) for (j=0;j<SIZE;j=j+1) a]=1; Functions should be small enough so that they could fit on the screen.That's about 30 lines of code. Use of goto,break and continue is strongly disallowed. Use of non-constant global variables is discouraged. About Comments:every function should be fully documented.You should avoid adding too many comments in the language body;all the variables and functions names should be self describable and need no reference

within the scope for which it is relevant. Examples, const int SIZE = 10; float x[SIZE], y[SIZE]; for (int i = 0; i , >=, && and ||. Examples, float result = x + y - z; //Good float result=x+y-z; //Bad „ There are no spaces before and after the binary operators *, /, and %. There is no space following the unary operators - and !. Examples, float result = x*y + z; //Good bool decision = ! go; //Bad „ No hardwired constants! Bad example: int a[10][10]; for (i = 0; i == 9; i = i + 1) for (j = 0; j == 9; j = j + 1) a[i][j] = 1; Good example: const int SIZE = 10; int a[SIZE][SIZE]; for (i = 0; i < SIZE; i = i + 1) for (j = 0; j < SIZE; j = j + 1) a[i][j] = 1; „ Functions should be small enough so that they could fit on the screen. That’s about 30 lines of code. „ Use of goto, break and continue is strongly disallowed. „ Use of non-constant global variables is discouraged. „ About Comments: every function should be fully documented. You should avoid adding too many comments in the language body; all the variables and functions names should be self describable and need no reference

C++Code layout Every C++file is laid out as follows: Header comment block:describe what the program is and what the program will do and related detail like version,author and creation date. ■#include statements Constants ■Global variables Functions:Every function with own header comment block to describe what the function is and what the function will do.Describe related detail like inputs,outputs,assumptions, and implementation algorithms if necessary. Here is an example of code layout: /HEADER COMMENTS GO AT THE TOP ************************************* Copyright(C):UM-SJTU Joint Institute Project: Vg101-Class example File: C++example.cpp Purpose: Coding example to show code layout Compiler: VS2005 Created by: Samson Zhang Created ar: 2008/08/24 水****冰水*****水****冰水****水水米****水****水水****冰水***水*****冰水本***水水****来水*****水**米/ /∥NCLUDE statements*********************************** #include #include using namespace std; ∥CONSTANTS********* /No constants in this example ∥GLOBAL VARIABLES************************************** ConsoleT console; ∥FUNCTIONS Declaratic0n******************************** double pow(double x,int n); ∥PROGRAM START********米********************************** /* This program will compute the value of x n In main function,you prompt the user for a double x and int n and call a function pow to do the computation

C++ Code layout Every C++ file is laid out as follows: „ Header comment block: describe what the program is and what the program will do and related detail like version, author and creation date. „ #include statements „ Constants „ Global variables „ Functions: Every function with own header comment block to describe what the function is and what the function will do. Describe related detail like inputs, outputs, assumptions, and implementation algorithms if necessary. Here is an example of code layout: /* HEADER COMMENTS GO AT THE TOP ************************************** Copyright (C): UM-SJTU Joint Institute Project: Vg101 - Class example File: C++ example.cpp Purpose: Coding example to show code layout Compiler: VS2005 Created by: Samson Zhang Created ar: 2008/08/24 ****************************************************************************/ // INCLUDE statements ******************************************************** #include #include using namespace std; // CONSTANTS *************************************************************** // No constants in this example // GLOBAL VARIABLES ******************************************************* ConsoleT console; // FUNCTIONS Declaration ****************************************************** double pow(double x, int n) ; // PROGRAM START ********************************************************** /* This program will compute the value of x^n In main function, you prompt the user for a double x and int n and call a function pow to do the computation */

int main(void) double x,result; int n; console.printLine("A calculation of x(double)to the power of n(int)In"); console.printLine ("Input x(double)and n (int):") console.readLine (x,n); result pow (x,n); console.printLine (x,""n,"="result,endl): return 0: /* function prototype:double pow(float x,int n) Purpose:Calculate the integral power of a double using the algorithm of al-Kashi inputs:"x"for the base and "n"denotes the power to be raised Returns:a double,the result of x raised to the power n double pow(double x,int n) double result; if(0>n) { n=-n: x=1/x, result 1: while(1 <=n) { if(0=n%2) n=n/2; X=X*x; } else n=n-1: result result*x; }; return result;

int main(void) { double x, result; int n; console.printLine ("A calculation of x (double) to the power of n (int) \n"); console.printLine ("Input x (double) and n (int): "); console.readLine ( x, n); result = pow (x, n); console.printLine (x, “^”, n, " = ", result, endl); return 0; } /* function prototype: double pow(float x, int n) Purpose: Calculate the integral power of a double using the algorithm of al-Kashi inputs: "x" for the base and "n" denotes the power to be raised Returns: a double, the result of x raised to the power n */ double pow(double x, int n) { double result; if (0 > n) { n = -n; x = 1/x; }; result = 1; while(1 <= n) { if (0 == n % 2) { n = n/2; x = x*x; } else { n = n - 1; result = result*x; }; }; return result; }

已到末页,全文结束
刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档