上海交通大学:《程序设计基础》课程教学讲义(密西根学院)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; }
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_files_DataBase Design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Expressions and Statements.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_examples on class design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Array and its Applications.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_20 Looking Ahead.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_19 Recursion 1.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_16 MATLAB environment short.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_15 Introduction to matlab.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_Chapter 1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第二章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第三章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter8 Views, Indexes.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter7 Constraints and Triggers.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter6 The database Language SQL –as a tutorial.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter5 Algebraic and Logic Query languages.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter4 High-level Database Models.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(1/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(2/2).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 微机原理与接口技术绪论(朱兰娟).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter9 SQL in a server environment.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review Solution.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Pointer Review.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_Practice Final 1.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 2.pdf
- 上海交通大学:《程序设计基础》课程教学资源(习题集)05年期末习题_practice Final 3.pdf