文档库 最新最全的文档下载
当前位置:文档库 › 第七章c++程序设计

第七章c++程序设计

第七章c++程序设计
第七章c++程序设计

第七章异常及其处理

(exception and its handling)

7.1 程序运行错误及其处理

程序在运行中可能出错,例如在除法运算中用0作除数、实数运算中求负数的开方根、在计算机监控

系统中所采集的数据越限,等等。当程序出错时,一般要求用户进行处理,如重新输入数值或给出新的

控制量等。必要时则中断程序。程序首先应检测到出错,然后才能提示用户进行处理或由程序自动进行

处理。

传统的错误处理方式唯有依靠函数返回提示错误用的局部标志数据或者全局标志数据来提示出错(全

局数据的安全性差,极易受到破坏)。这类做法的困难是程序只能在函数返回主程序后才能检查标志数据

的提示内容。

错误处理的一种新的方式是使用“异常”(exception)。异常处理是由程序设计语言提供的运行时刻错

误处理的一种方式。一旦程序出现错误,随即引发和抛出“异常”,程序能在一处或多处合适的地方自动

地捕获异常并进行必要的处理。

[例1]求负数的开方根时传统的错误处理方式:

// sqrt_negative_1.cpp

// error occurs when the square root of a negative number is calculated

// system subroutine used

#include

#include // ptototype: double sqrt( double );

#include // ptototype: void exit(int);

double sqroot(double number)

{

if ( number < 0 )

{

cout << "Error! negative input number : " << number << '\n';

cout << "Program terminated!" << '\n';

exit( -1 );

}

return sqrt( number ); //system subroutine

}

void main()

{

cout<<"Sqrt of 1.5129 is "<

cout<<"Sqrt of -4 is "<

cout<<"Sqrt of 16 is "<

}

/* Results:

Sqrt of 1.5129 is 1.23

Error! negative input number : -4

Program terminated!

*/

以上程序中,当准备求取负数的平方根时,就出错,程序非正常结束。为使程序非正常结束,须要调

用系统子程序exit( ),其函数原型在头文件stdlib.h内。

其中主函数中第二句运行过程中,因调用函数sqroot(-4)而导致程序意外中断。但在sqroot(-4)之前的字符串"Sqrt of -4 is "却没有显示。这类现象将在以后众多程序中出现。它是由于输出语句的整体性所决定的。请见第八章§8.1.2.2“非缓冲输出流”中的详细解释。

[例2]第五章§5.3.1.3“除法运算符重载”中的[例1]

该程序中的子程序

double division::operator / ( double denominator )

{

if ( denominator == 0 )

235

{

cout<<"attempted to divide by zero"<

return -999999;

}

return (numerator/denominator);

}

将-999999的返回值作为错误标志。

void main()

{

double numerator, denominator;

double result;

division num(numerator);

result = num/denominator;

//i.e. result=num. operator / ( denominator )

if ( result == -999999 )

{

cout << "The quotient is meaningless!" << endl;

cout << "T ry again!" << endl;

}

……

}

主函数main( )在子函数double division::operator / ( double denominator )返回主函数后立即检查返回值,如发现出错,就加以显示。

如果程序中出错地方较多,这类处理方法就有其局限性:当调用函数的级联数量较多(即一个函数调用下一个函数,而下一个又调用再下一个的级联方式)而又需要在其它地方(例如上级的上级的上级)统一进行处理时,逐级传递错误码太繁琐。因此须要借助于异常处理。请参阅后面本章§7.2.1“抛出异常”中的[例3]中的程序exception_5.cpp。

[例3]依靠异常来处理求负数开方根的例子(当然此例中并不一定需要异常处理):

// exception_sqrt_1.cpp

// exception occurs when the square root of a negative number is calculated

#include

#include // ptototype: double sqrt( double );

//#include //不再需要exit( )

double sqroot(double number)

{

if ( number < 0 ) throw number; //exception object

return sqrt( number ); //system subroutine

}

void main()

{

try {

cout<<"Sqrt of 1.5129 is "<

cout<<"Sqrt of -4 is "<

cout<<"Sqrt of 16 is "<

}

catch ( double )

{ // exception handler

cout << "Error! negative input number" << '\n';

cout << "Program terminated!" << '\n';

//exit( -1 ); // 因已到程序结尾,故不再需要此函数}

}

/* Results:

Sqrt of 1.5129 is 1.23

Error! negative input number

Program terminated!

*/

一种较好的编程方式是首先编写正常运行的代码,然后再添加用于处理各类异常情况的语句。

236

7.2 异常及其处理

7.2.1 抛出异常

从上节[例2]中可以看出,当监测到程序出错时,就抛出异常。其格式为:

try

{ ……

此处监测到(或在被调用的程序中监测到)程序出错时,throw 异常对象;

……

}

其中被抛出的异常对象可以是预定义数据类型(例如int、double、char*等)的变量及其指针和引用,但更经常使用的是各种异常类的对象及其指针和引用。使用这些异常类对象的优点是能够提供更多关于程序错误的信息,包括处理各类错误的方法。这些异常类可以是系统所提供的,更多情况下可以是用户自己定义的。

应该注意:此处要求一定在try程序块中或它所调用的函数中抛出异常。以下是不在try程序块中而在try程序块所调用的函数中抛出异常的例子:

[例1] 在try程序块所调用的函数quotient( )中抛出异常。

// exception_1.cpp

// A simple exception handling example.

// Checking for a divide-by-zero exception

// and throwing an intetger as an exception

#include

// Definition of function quotient. Demonstrates throwing

// an exception when a divide-by-zero exception is encountered.

double quotient( double numerator, double divisor )

{

if ( divisor == 0 ) throw divisor;

//被抛出的异常对象是预定义类型即double型数据

return numerator / divisor;

}

// Driver program

void main()

{

// the try block wraps the code that may throw an

// exception and the code that should not execute

// if an exception occurs

try {

cout << "The quotient of 18/9 is " << quotient ( 18, 9 ) << endl;

cout << "The quotient of 18/0 is " << quotient ( 18, 0 ) << endl;

cout << "The quotient of 4.5/9 is " << quotient ( 4.5, 9 ) << endl;

}

catch ( double dd )

{ // exception handler

cout << "Exception : divisor " << dd << " used!" << endl;

}

}

/* Results:

The quotient of 18/9 is 2

Exception : divisor 0 used!

*/

从以上例子中可以看出,当程序不出错时,程序按照顺序控制结构逐条语句地向下执行,并返回除法运算结果。而当程序出错时(即当除数为零时),程序就在抛出异常处中断,不再执行以后的语句(不执行第三句),并跳转至catch程序块再继续往下执行程序。

237

[例2]使用系统的异常类class exception,为此,须包含头文件

// exception_3.cpp

// same as exception_1.cpp, only differing in

// throwing an object of a class instead of a double value

#include

#include

// Definition of function quotient. Demonstrates throwing

// an exception when a divide-by-zero exception is encountered.

double quotient( double numerator, double divisor )

{

if ( divisor == 0 ) throw exception( );

//被抛出的异常对象是异常类即class exception的对象,

//class exception is declared in the include file

return numerator / divisor;

}

// Driver program

void main()

{

try {

cout << "The quotient of 18/9 is " << quotient ( 18, 9 ) << endl;

cout << "The quotient of 18/0 is " << quotient ( 18, 0 ) << endl;

cout << "The quotient of 4.5/9 is " << quotient ( 4.5, 9 ) << endl;

}

catch ( exception ex )

{ // exception handler

cout << ex.what( ) << '\n';

}

}

/* Results:

The quotient of 18/9 is 2

Unknown exception */

以上程序中class exception是系统所提供的异常类,在抛出异常时建立该异常类exception的对象。为使用class exception,在程序中应包含头文件,在§7.3中将会详细介绍它。catch程序块中内容将在下一小节§7.2.2中介绍。

以上程序中,当出现异常时,抛出异常,程序流即自动退出try程序块,进入catch程序块,进行处理后即进入catch程序块以后的语句,不再继续执行try程序块中抛出异常语句后的其他语句(上例中不再执行第三条语句“……quotient ( 4.5, 9 )……”)。

以上程序中无法显示异常内容,但只需将以上程序略加修改,即可显示异常内容。如exception_4.cpp (未显示整个程序)中,只需将

double quotient( int numerator, int divisor ) 函数的第一句

“if ( divisor == 0 ) throw exception( );”

改为:

“if ( divisor == 0 )

throw exception( "Divide-by-zero exception" );

//使用系统的异常类的对象并初始化”

即可!也就是在建立class exception的对象时,使用字符串"divide-by-zero exception"将该异常对象的字符串数据初始化,输入用于显示异常性质的字符串。在调用异常对象中的what( )函数时就能显示该字符串。

这就获得如下更明确的运行结果:

/* Results:

The quotient of 18/9 is 2

Divide-by-zero exception */

以前提到过,当函数级联调用时,使用显示出错的函数返回值,要逐级向上递送,很不方便。而使用异常处理就方便了。

238

[例3] 函数级联调用时的异常处理

// exception_5.cpp

// cascaded invocation of sub-routines

#include

#include

// Definition of function quotient. Demonstrates throwing

// an exception when a divide-by-zero exception is encountered.

double quotient( double numerator, double divisor )

{

if ( divisor == 0 ) throw exception( "Divide-by-zero exception" );

//使用系统的异常类

return numerator / divisor;

}

double h( double numerator, double divisor )

{

double result = quotient( numerator, divisor );

cout << "function h( ) in return path!" << endl;

return result;

}

double g( double numerator, double divisor )

{

double result = h( numerator, divisor );

cout << "function g( ) in return path!" << endl;

return result;

}

double f( double numerator, double divisor )

{

double result = g( numerator, divisor );

cout << "function f( ) in return path!" << endl;

return result;

}

// Driver program

void main()

{

try {

cout << "The quotient of 18/9 is " << f ( 18, 9 ) << endl;

cout << "The quotient of 18/0 is " << f ( 18, 0 ) << endl;

cout << "The quotient of 4.5/9 is " << f ( 4.5, 9 ) << endl;

}

catch ( exception ex )

{ // exception handler

cout << ex.what( ) << '\n';

}

}

/* Results:

function h( ) in return path!

function g( ) in return path!

function f( ) in return path!

The quotient of 18/9 is 2

Divide-by-zero exception

*/

以上程序中正常调用函数的路径是:main( ) -> f( ) -> g( ) -> h( ) ->quotient( ),在函数返回时则采取相反路径,即quotient( ) -> h( ) -> g( ) -> f( ) -> main( )。但在出现异常时,也即调用主函数中以下语句

cout << "The quotient of 18/0 is " << f ( 18, 0 ) << endl;

时,就不再按步就班,而是直接从quotient ( )返回至main( )中的catch程序块。

239

其流程如下图所示:

正常运行流程引发异常流程

其中

表示函数调用路径

表示正常返回路径

表示出现异常时的返回路径

7.2.2 捕获异常

捕获异常以便进行处理时,通常使用catch程序块,其格式为:

catch ( 数据或类的对象)

{ 程序块}

可在该程序块中显示相关信息和进行必要处理。

[例1]使用异常检查整型数组下标越限

// exception_7.cpp

// 使用异常检查整型数组下标越限

#include

int arr[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int LEN; // number of elements

int & check(int i)

{

if ( (i>=LEN) || (i<0) ) throw i;

return arr[i];

}

void main()

{

// Number of elements is determined first

LEN = sizeof(arr)/sizeof(arr[0]);

try {

cout<<"arr[1]="<

cout<<"arr[4]="<

cout<<"arr[-2]="<

}

catch (int x)

{ cout << "Exception:subscript "<< x

240

<<" beyond limits (number of elements = "<

}

}

/* Results:

arr[1]=2

arr[4]=5

Exception:subscript -2 beyond limits (number of elements = 10)

*/

以上程序中,主程序首先自动地确定数组单元数。无论该数组类型如何,都可正确地求得。

当下标越限时,就抛出异常。稍后在catch程序块中捕获异常,并显示该下标的数值。

以上是捕获一个异常的例子,以下是捕获两个异常并作不同显示的例子:

[例2]输入两个数值,两者相除,求其商。如分母为零,抛出第一类异常。如错误地输入错误类型的变量

例如字符变量,则抛出第二类异常。这两类异常由一个catch程序块捕获,而能作不同显示处理。

// exception_6.cpp

// different exceptions caught by one block

#include

#include

double quotient( double numerator, double divisor )

{

if ( divisor == 0 )

throw exception( "Divide-by-zero exception" );

//使用系统的异常类并初始化其对象

return numerator / divisor;

}

void main()

{

double numerator, divisor;

cout << "Enter two values: ";

cin >> numerator >> divisor;

try {

if (!cin) throw exception( "Wrong input type exception" );

cout << "The quotient of "<

<< quotient ( numerator, divisor ) << endl;

cout << "The quotient of 4.5/9 is " << quotient ( 4.5, 9 ) << endl;

}

catch ( exception ex )

{ // exception handler

cout << ex.what( ) << '\n';

}

}

/* Three kinds of results:

(1)Enter two values: 18 9

The quotient of 18/9 is 2

The quotient of 4.5/9 is 0.5

(2)Enter two values: 18 0

Divide-by-zero exception

(3)Enter two values: 18 p

Wrong input type exception

*/

上例中如分母为零,抛出一个异常类对象。如错误地输入字符变量,则抛出另一个异常类对象。这两类异常类对象的差别在于其初始化字符串内容不同。它们都能由同一个catch程序块捕获,进行处理,显示不同内容:对第一个异常类对象显示“Divide-by-zer o exception”,对另一个异常类对象则显示“Wrong input type exception”。

程序中(!cin)是“operator !”(一个重载运算符函数)和“cin”(一个输入流对象)的组合。当输入操作成功,(!cin)返回零值;如输入操作失败,则(!cin)返回非零值。

(!cin)的功能详见第八章§8.1.2.4 “缓冲输入流”[例5]后的解释。

附录十九是多个地方抛出的相同内容的异常在同一个catch程序块内处理的另一个例子。

241

还有一种格式catch ( … )可用于捕获各类异常,或用于捕获其它catch()格式所不能捕获的异常。有兴趣者可参阅附录二十。

C++的异常处理机制可简叙如下:

1.如果try块中没有抛出异常,则所有与try块相关的catch块将被忽略,程序跳过所有catch块后继

续往下运行。

2.catch块一般位于try块之后。如果try块中抛出异常,try块中的剩余语句将被跳过而忽略。程序转

至其参数类型与异常类型相匹配的catch块中,由其处理该异常。执行完毕后,跳过其它catch块,再执行所有catch块以后的其它语句。

3.如果没有其参数类型与异常类型相匹配的catch块,则由catch ( … )块处理该异常。如无相对应的catch

处理快,则程序运行出错。

7.3 用于处理异常的类

7.3.1 系统提供的异常类

exception_2.cpp中抛出异常的语句是:

if ( divisor == 0 ) throw exception( );

而其运行结果显示异常性质为Unknown exception。

而exception_3.cpp(未显示整个程序)中抛出异常的语句是:

if ( divisor == 0 )

throw exception( "divide-by-zero exception" );

而其运行结果显示异常性质为

divide-by-zero exception。

这是建立异常类的对象的两种不同方式。

这是什么异常类?先看一下系统的异常类。系统在头文件exception(没有exception.h)中声明了用于处理异常的类exception,该类的接口如下:

class exception

{

public:

exception();

exception(const char* &);

exception(const exception&);

exception& operator= (const exception&);

virtual ~exception();

virtual char* what( ) const;

private:

char* _m_what;

int _m_doFree;

};

exception_2.cpp中抛出异常的语句是:

if ( divisor == 0 ) throw exception( );

它在建立对象时调用重载的构造函数exception( ),没有任何初始化参数。而字符串“Unknown exception”则是该异常类构造函数的缺省参数。

而exception_3.cpp中抛出异常的语句是:

if ( divisor == 0 )

throw exception( "divide-by-zero exception" );

它在建立对象时调用重载的构造函数exception ( const char* & ),将字符串"divide-by-zero exception"

用作初始化参数,写入exception:: _m_what字符串中。而以后主程序中调用该对象ex的成员函数ex.what( )时,即在运行结果中显示该字符串,指出异常性质为

divide-by-zero exception。

用户除使用系统的异常类之外,也可自己定义该系统异常类class exception的派生类。

[例1] 用户自己定义系统class exception的派生异常类

// exception_derived_loop_1.cpp

// derived class of system class exception

#include

242

#include

double denominator;

class Except_derive : public exception

{

char *message;

public:

Except_derive(char * ptr) { message = ptr; }

const char * what( ) const { return message; }

void handling ( ) {

cout << "Enter denominator(分母)again : ";

cin >> denominator; }

};

void main()

{

double numerator;

bool flag = true;

cout << "Enter two data : ";

cin >> numerator >> denominator;

while ( flag )

{

try

{

if ( denominator == 0 ) throw Except_derive( "divide by zero!" );

cout<< "The quotient is: " <<(numerator/denominator)<

flag = false;

}

catch ( Except_derive ex )

{ // exception handler

cout << "Exception : " << ex.what( ) << '\n';

ex.handling ( );

}

} //while

}

/* Results:

Enter two data : 4 0

Exception : divide by zero!

Enter denominator(分母)again : 8

The quotient is: 0.5 */

以上程序中使用了循环方式,当出现异常时可循环运行,以便要求用户再次输入正确数据。只当输入的两个数据都正确时,程序才给出运算结果并正常结束。

7.3.2 用户自定义异常类

上例只说明用户可定义系统异常类的派生类,但并无实际意义。还不如用户自己定义异常类。如下例:

[例1] 在给定温度条件下进行除法运算。先检查温度,如温度过高或过低,则调整温度至给定范围。然后在除法运算中检查零除数。异常类的基类用于处理零除数;异常类的派生类则用于处理温度过高或温度过低,并能自动地每次将温度调整10度。

// exception_derived_loop_2.cpp

// user-defined class except and its derived classes

#include

#define MAX_T 28

#define MIN_T 16

int temperature;

double denominator;

class except

{

char *message;

243

public:

except (char * ptr) { message = ptr; }

const char * what( ) const { return message; }

virtual void handling ( ) {

cout << "Enter a non-zero denominator again : ";

cin >> denominator; }

void action ( ) { cout << "Exception : " << what( ) << '\n';

handling( ); }

};

class except_derive : public except

{

public:

except_derive(char * ptr) : except (ptr) { }

virtual void handling ( ) {

if ( temperature > MAX_T )

cout <<"启动温控系统,将温度降至" << (temperature -= 10) << endl;

else

cout << "启动温控系统,将温度升至" << (temperature += 10) << endl;

}

};

double quotient( double numerator, double denominator )

{

if ( denominator == 0 ) throw except( "divide by zero!" );

return numerator / denominator;

}

void main()

{

double numerator, result;

bool flag = true;

char * mes_low = {"T emperature too low!"};

char * mes_high = {"T emperature too high!"};

cout << "Measured temperature : ";

cin >> temperature;

cout << "Enter numerator and denominator : ";

cin >> numerator >> denominator;

while ( flag )

{

try {

if (( temperature > MAX_T )||( temperature < MIN_T ))

throw except_derive(( temperature > MAX_T )?( mes_high ):( mes_low ));

result = quotient ( numerator, denominator );

cout << "The quotient is: " << result << endl;

flag = false;

}

// The following order should not be reversed

catch ( except_derive ex ) { ex.action( ); }

catch ( except ex ) { ex.action( ); }

} // while

}

/* At least three kinds of results:

(1)Measured temperature : 20

Enter numerator and denominator : 2 4

The quotient is: 0.5

(2)Measured temperature : 40

Enter numerator and denominator : 4 8

Exception : T emperature too high!

启动温控系统,将温度降至30

Exception : T emperature too high!

启动温控系统,将温度降至20

244

The quotient is: 0.5

(3)Measured temperature : 5

Enter numerator and denominator : 4 0

Exception : T emperature too low!

启动温控系统,将温度升至15

Exception : T emperature too low!

启动温控系统,将温度升至25

Exception : divide by zero!

Enter a non-zero denominator again : 4

The quotient is: 1 */

当以上程序中出现异常时,任何一个捕获块都调用基类的action( ),而此action( )函数能够根据不同对象指针来调用相应的虚函数handling( ),实现第五章中所述“多态性”中的“派生类成员函数的定向调用”。

另外,同一个派生类同时用于处理温度过高或过低,并区别地采取相应措施。

上面程序中,捕获程序块的顺序不能颠倒为:

catch ( except ex ) { ex.action( ); }

catch ( except_derive ex ) { ex.action( ); }

这时编译系统将给出警告为:

warning: 'class except_derive' is caught by base class ('class except')

这样一来,其运行结果将不正确,成为:

(2)Measured temperature : 40

Enter numerator and denominator : 4 8

Exception : T emperature too high!

Enter a non-zero denominator again : 4

Exception : T emperature too high!

Enter a non-zero denominator again : 2

Exception : T emperature too high!

Enter a non-zero denominator again :

…………

这是因为第一个捕获程序块也能捕获异常派生类的对象,从而导致出错。

为何第一个捕获程序块也能捕获异常派生类的对象,但却又给出错误运行结果?见第五章§5.5.1.2“派生类对基类引用的继承”中的[例1],如下:

[例2]形参为基类对象的函数所使用实参为派生类对象时的错误

// obj_inh_1.cpp

// T o show object of base class can't be inherited

#include

class Point

{

double x, y;

public:

Point (double i, double j) { x=i; y=j; }

virtual double Area( ) const { return 0; }

};

class Rectangle : public Point

{

double w, h;

public:

Rectangle (double i, double j, double k, double l) :

Point (i, j) { w=k; h=l; }

double Area( ) const { return w*h; }

};

void fun(Point s)

{

cout<

245

}

void main( )

{

Point p (5, 6);

Rectangle rec (3.5, 15.2, 5, 28);

cout<<"Area of Point is : ";

fun (p);

cout<<"Area of Rectangle is : ";

fun (rec);

}

/* Result:

Area of Point is : 0

Area of Rectangle is : 0(当函数形参是对象而非对象指针或引用时,无法调用派生类部分的虚函数)

*/

程序中fun( )的形参是基类class Point的对象,但它也能使用派生类对象作为实参,即fun (rec)。但因其形参已定义为基类对象,因此函数体内的s.Area( )只能调用基类的Area( )函数,得出错误的运行结果。此处fun (rec)是合法的但却错误的。

因此,在exception_derived_loop_2.cpp程序中,如错误地将

catch ( except ex ) { ex.action( ); }

放置于前,则它既会捕获异常基类又会捕获异常派生类的对象。而在捕获异常派生类对象时,将导致错误的运行结果。因此它也是是合法的但却错误的。

第五章§5.5.1.2“派生类对基类引用的继承”中指出,为纠正[例1]的错误,可将void fun(Point s)的形参定义为对象s的引用。见下一节。

7.3.3 异常类基类指针和引用的继承

以上§7.3.2“用户自定义异常类”[例1]的exception_derived_loop_2.cpp程序中,为了捕获两个不同类(基类和派生类)的对象,使用两个catch程序块,即:

catch ( except_derive ex ) { ex.action( ); }

catch ( except ex ) { ex.action( ); }

这两个程序块的形参都是异常类的对象。能否合并为一个,例如catch(…)程序块?可以!但此合并后的程序块无法捕获对象名称,也就无法区别并调用相应类的对象的虚函数handling( ),因这涉及基类对象的继承问题。第五章§5.5.1.2“派生类对基类引用的继承”中指出,可将函数的形参定义为对象s的引用(当然指针也可)。如下:

[例1] 异常类基类指针的继承

// exception_derived_loop_3.cpp

// user-defined class except and its derived classes

// 和exception_derived_loop_2.cpp不同之处用下划线标出

#include

#define MAX_T 28

#define MIN_T 16

int temperature;

double denominator;

class except

{

char *message;

public:

except (char * ptr) { message = ptr; }

const char * what( ) const { return message; }

virtual void handling ( ) {

cout << "Enter a non-zero denominator again : ";

cin >> denominator; }

void action ( ) { cout << "Exception : " << what( ) << '\n';

handling( ); }

};

246

class except_derive : public except

{

public:

except_derive(char * ptr) : except (ptr) { }

virtual void handling ( ) {

if ( temperature > MAX_T )

cout <<"启动温控系统,将温度降至" << (temperature -= 10) << endl;

else

cout << "启动温控系统,将温度升至" << (temperature += 10) << endl;

}

};

double quotient( double numerator, double denominator )

{

if ( denominator == 0 ) throw new except( "divide by zero!" );

//此抛出语句等同于以下两句:

// except *ptr = new except( "divide by zero!" );

// throw ptr;

//其中第一句对应于int *ptr = new int (123);

return numerator / denominator;

}

void main()

{

double numerator, result;

bool flag = true;

char * mes_low = {"T emperature too low!"};

char * mes_high = {"T emperature too high!"};

cout << "Measured temperature : ";

cin >> temperature;

cout << "Enter numerator and denominator : ";

cin >> numerator >> denominator;

while ( flag )

{

try {

if (( temperature > MAX_T )||( temperature < MIN_T ))

throw new except_derive(( temperature > MAX_T )?( mes_high ):( mes_low ));

result = quotient ( numerator, denominator );

cout << "The quotient is: " << result << endl;

flag = false;

}

catch ( except *m ) //异常类基类对象的指针

{ m->action( ); delete m; }

//catch ( except_derive ex ) { ex.action( ); }

//catch ( except ex ) { ex.action( ); }

} // while

}

/* At least three kinds of results:

(1)Measured temperature : 20

Enter numerator and denominator : 2 4

The quotient is: 0.5

(2)Measured temperature : 40

Enter numerator and denominator : 4 8

Exception : T emperature too high!

启动温控系统,将温度降至30

Exception : T emperature too high!

启动温控系统,将温度降至20

The quotient is: 0.5

(3)Measured temperature : 5

Enter numerator and denominator : 4 0

247

Exception : T emperature too low!

启动温控系统,将温度升至15

Exception : T emperature too low!

启动温控系统,将温度升至25

Exception : divide by zero!

Enter a non-zero denominator again : 4

The quotient is: 1 */

请注意:抛出异常后,在catch块中必须调用delete运算符函数将空间归还给堆区。[例2]异常类基类对象引用的继承

// exception_derived_loop_4.cpp

// improved version of exception_derived_loop_2.cpp

// user-defined class except and its derived class

// reference to base class is used for all derived class objects

// 和exception_derived_loop_2.cpp不同之处用下划线标出

#include

#define MAX_T 28

#define MIN_T 16

int temperature;

double denominator;

class except

{

char *message;

public:

except (char * ptr) { message = ptr; }

const char * what( ) const { return message; }

virtual void handling ( ) {

cout << "Enter a non-zero denominator again : ";

cin >> denominator; }

void action ( ) { cout << "Exception : " << what( ) << '\n';

handling( ); }

};

class except_derive : public except

{

public:

except_derive(char * ptr) : except (ptr) { }

virtual void handling ( ) {

if ( temperature > MAX_T )

cout <<"启动温控系统,将温度降至" << (temperature -= 10) << endl;

else

cout << "启动温控系统,将温度升至" << (temperature += 10) << endl;

}

};

double quotient( double numerator, double denominator )

{

if ( denominator == 0 )

{

except &ref = except( "divide by zero!" );

throw ref;

}

return numerator / denominator;

}

void main()

{

double numerator, result;

bool flag = true;

char * mes_low = {"T emperature too low!"};

char * mes_high = {"T emperature too high!"};

cout << "Measured temperature : ";

248

cin >> temperature;

cout << "Enter numerator and denominator : ";

cin >> numerator >> denominator;

while ( flag )

{

try {

if (( temperature > MAX_T )||( temperature < MIN_T ))

{

except_derive &ref_d =

except_derive(( temperature > MAX_T )?( mes_high ):( mes_low ));

throw ref_d;

}

result = quotient ( numerator, denominator );

cout << "The quotient is: " << result << endl;

flag = false;

}

catch ( except & ex ) { ex.action( ); } //异常类基类对象的引用

//catch ( except_derive & ex ) { ex.action( ); }

} // while

}

/* At least three kinds of results:

(1)Measured temperature : 20

Enter numerator and denominator : 2 4

The quotient is: 0.5

(2)Measured temperature : 40

Enter numerator and denominator : 4 8

Exception : T emperature too high!

启动温控系统,将温度降至30

Exception : T emperature too high!

启动温控系统,将温度降至20

The quotient is: 0.5

(3)Measured temperature : 5

Enter numerator and denominator : 4 0

Exception : T emperature too low!

启动温控系统,将温度升至15

Exception : T emperature too low!

启动温控系统,将温度升至25

Exception : divide by zero!

Enter a non-zero denominator again : 4

The quotient is: 1 */

上一程序中,语句except &ref = except( "divide by zero!" ); 中等式的左侧用于定义一个class except

对象的引用ref,而等式右侧则用于建立一个class except的对象。

从以上两个程序可以看出它们的不同点:

(1)使用异常类对象指针捕获异常时,一般在堆区中分配空间,因此当异常处理完毕后,必须删除堆区内的异常对象,否则将造成资源泄漏。但其优点是表达式简洁,一条语句即可,使用方便。

(2)使用异常类对象引用捕获异常时,由于异常类对象是在栈区中建立的,因此当异常处理完毕后程序退出捕获程序块时,异常对象将被自动删除。

*7.4 异常处理中的构造函数和析构函数

现在看一下异常类的构造函数和析构函数,以及该异常类对象和其它类的构造函数和析构函数的关系。[例1]自定义异常类except与构造函数和析构函数。捕获异常时使用异常类对象的指针。

// exception_ctor_destor_3.cpp

// partly copied from 马光志…s book, p.213 - 214

# include

# include

249

class except //自定义的异常类

{

char msg[30];

public:

except (const char *s) { strcpy (msg, s);

cout<<"Exception constructor"<

~except ( ) { cout<<"Exception destructor : an exception occured to "<

class person

{ protected:

char * name;

int age;

public:

person(const char * nm, int ag)

{ name = new char[strlen(nm) + 1];

strcpy (name, nm);

age = ag;

cout<

}

~person( )

{

cout<

delete []name;

}

void info( ) { cout<<"name:"<

cout<<"age:"<

};

class adult : public person

{

char *position;

public:

adult(char *nm, char *p, int ag);

~adult ( )

{

cout<

delete [ ]position;

}

void info( ) { person::info( );;

cout<<"position:"<

};

adult::adult(char *nm, char *p, int ag):person(nm, ag)

{

if (ag<25)

{

cout<

throw new except (name); //相等于以下三条语句:

// except *ptr; (类似于int *ptr;)

// ptr=new except (name); (类似于ptr=new int(6);)

// 以及throw ptr;

}

position = new char[strlen(p)+1];

strcpy (position, p);

cout<

}

void main( )

{

try

{

// adult wang ("Wang", "professor", 24);

250

adult wang ("Wang", "professor", 40);

https://www.wendangku.net/doc/4c1826488.html,( );

adult liu ("Liu", "engineer", 50);

// adult liu ("Liu", "engineer", 15);

https://www.wendangku.net/doc/4c1826488.html,( );

}

catch (const except *m)

{ delete m; } // in order to invoke destructor }

/* Three kinds of results:

(1)For the code:

try

{

adult wang ("wang", "professor", 40);

adult liu ("liu", "professor", 50);

}

the result is:

Wang's 'person' constructor

Wang's 'adult' constructor

name:Wang

age:40

position:professor

Liu's 'person' constructor

Liu's 'adult' constructor

name:Liu

age:50

position:engineer

Liu's 'adult' destructor

Liu's 'person' destructor

Wang's 'adult' destructor

Wang's 'person' destructor

(2)For the code:

try

{

adult wang ("Wang", "professor", 24);

adult liu ("Liu", "engineer", 50);

}

the result is:

Wang's 'person' constructor

Wang's 'adult' constructor throws an exception Exception constructor

Wang's 'person' destructor

Exception destructor : an exception occured to Wang (3)For the code:

try

{

adult wang ("Wang", "professor", 40);

adult liu ("Liu", "professor", 15);

}

the result is:

Wang's 'person' constructor

Wang's 'adult' constructor

name:Wang

age:40

position:professor

Liu's 'person' constructor

Liu's 'adult' constructor throws an exception

Exception constructor

Liu's 'person' destructor

251

Wang's 'adult' destructor

Wang's 'person' destructor

Exception destructor : an exception occured to Liu

*/

在上例中,class except是用户自己定义的异常类。

用户的自定义类是基类class person。后来由于其数据成员不够,又派生出class adult。本来在基类中对年龄并无限制,但在派生类中增加“职位”position后,对年龄有限制,要求必须大于25岁。由于基类的定义不能再改,就只能在派生类的构造函数中检查年龄是否符合规定。如不符合规定则抛出异常,具体讲,是使用new在堆区内分配一个空间放置该class except的对象并将它初始化。

程序运行有三种情况:

(1)建立两个用户对象“Wang”和“Liu”时一切正常,不抛出异常。程序结束时调用两个类的各部分的四个析构函数,用于将两个类的四个构造函数所分配的堆区内空间全部退还给堆区。

此种情况下共建立了两个对象:wang和liu。

(2)建立第一个用户对象“wang”时不正常,抛出异常;没有分配堆区给position而只分配给name,此时没有建立对象“wang”,因而只调用了其基类部分“person”的构造函数。因此程序结束时也只调用它的基类部分“person”的析构函数,将所分配的name空间退还给堆区,而不调用它的派生类部分“adult”的析构函数。

由于建立第一个用户对象“wang”时抛出异常,因而也就不建立第二个用户对象“liu”。

在抛出异常时使用new分配堆区给class except的对象中的msg字符串name即“wang”。所以在捕获异常后就应该执行语句{ delete m; },从而删除异常类对象,调用异常类对象的析构函数,将该堆区空间释放掉,与此同时也就显示一句“An exception occured to wang”。

此种情况下只建立了一个对象:class except的对象。

那么class adult的对象建立了吗?答案是中途夭折了。事实上,只调用了基类部分的构造函数,建立了一半对象就抛出异常。其内存存储内容也只建立了一半,即:

栈区堆区

尚有

(3)建立第一个用户对象“wang”时正常,而建立第二个用户对象“liu”时不正常,抛出异常;没有分配堆区给position而只分配给name。程序结束时调用第一个对象“Wang”的两个析构函数,而只调用第二个对象“Liu”的基类部分的析构函数,将所分配的name空间退还给堆区。

在捕获异常后,删除异常类对象,调用它的的析构函数,将堆区空间释放掉,与此同时也就显示一句“An exception occured to liu”。

此种情况下建立了两个对象:是wang和class except的对象。

为何在程序最后才调用异常类的析构函数。因为所建立的用户对象在退出try程序块时就被删除,同时就调用其析构函数。而异常类对象直至退出catch程序块时才被删除,其析构函数也就在最后才被调用。

在第二类和第三类情况中,由于在构造函数中抛出异常,在程序结束时无法调用对象中派生类部分的析构函数。因此必须注意:派生类部分的构造函数中抛出异常之前所做的工作应妥善处理,就是说,如在抛出异常之前调用了new,则在捕获异常时必须调用d elete。因此,最好避免在构造函数中抛出异常。

(第七章完)

252

第七章思考题

(一)填空:

1.当用户使用系统提供的异常类class exception时,应在程序中包含头文件<()>。

2.抛出异常时,一定要在关键词()程序块中或它所调用的函数中使用关键词()语句。

3. 被抛出的异常对象可以是()类型的数据及其指针或引用,但更经常使用的是()自定义类

型例如类的对象及其指针或引用。

4. 被抛出的类对象可以是()所提供的异常类class exception的或它的派生类的对象,也可以是

()自己所定义的类的及其派生类的对象。

5. 在捕获异常以便进行处理时,通常使用关键词()程序块。

(二)判断下列各种描述是否正确,对者划√,错者划

述是否正确,对者划√,错者划

否正确,对者划√,错者划

,错者划

者划

1. 异常处理是由程序设计语言提供的运行时刻错误处理的一种方式。

2. 当用户自己定义异常类时,也可以将它定义为系统异常类class exception的派生类。

3. 当程序不出错时,程序按照顺序控制结构逐条语句地向下执行。而当程序出错时,程序就地抛出异常,

并在该处中断,不再执行以后的语句,而是跳转到catch程序块,进而处理异常。

4. 如果被抛出的异常对象是预定义数据类型的数据,当double型数据异常对象被抛出时,可由catch

( double d ) 程序块捕获异常。当整型数据异常对象被抛出时,可由catch ( int i ) 程序块捕获异常。

5. 格式为catch ( … )的程序块可用于捕获多种类型的异常

6. 一个catch块最多只可以有一个catch块参数。

7. C++中,任何具有处理异常功能的类都可用作异常类,它不一定必须是系统异常类class exception或它

的派生类。

(三)请给出以下程序的运行结果

// exer_ch7_3.cpp

// cascaded invocation of sub-routines

#include

#include

double quotient( int numerator, int divisor )

{

if ( divisor == 0 ) throw exception( "divide-by-zero" );

return ( double ) numerator / divisor;

}

double g( int numerator, int divisor )

{

double result = quotient( numerator, divisor );

cout << "function g( ) in return path!" << endl;

return result;

}

double f( int numerator, int divisor )

{

double result = g( numerator, divisor );

cout << "function f( ) in return path!" << endl;

return result;

}

void main()

{

double result;

try {

result = f( 1, 4 );

cout << "The quotient is: " << result << endl;

}

catch ( exception ex )

{

cout << ex.what() << " exception!" << '\n';

253

}

}

(四)请给出以下程序的运行结果

本题的程序exer_ch7_4.cpp和第三题exer_ch7_3.cpp只差一个数据,

原为:result = f( 1, 4 );

现改为:result = f( 1, 0);

(五)请给出以下程序的运行结果

// exer_ch7_5.cpp

#include

#define MAX_T 30

#define MIN_T 10

int temperature = 35;

class except

{

char *message;

public:

except( ) { message = NULL; }

except(char * ptr) { message = ptr; }

virtual const char * what( ) const { return message; }

virtual void handling ( ) {

cout << "T emperature incorrect. Control it again!"<

temperature += 10;

}

void action ( ) { cout << "Exception : " << what( ) << '\n';

handling( ); }

};

class except_derive : public except

{

char *message;

public:

except_derive(char * ptr)

{ message = ptr; }

virtual const char * what( ) const { return message; }

virtual void handling ( ) {

cout << "T emperature incorrect. Control it again!"<

temperature -= 10;

}

};

void main()

{

char * mes_low = {"T emperature too low!"};

char * mes_high = {"T emperature too high!"};

bool flag = true;

while ( flag )

{

try {

if ( temperature < MIN_T ) throw new except( mes_low );

// 相当于两句:

// except * ptr = new except( mes_low );

// throw ptr;

if ( temperature > MAX_T ) throw new except_derive( mes_high );

cout << "The temperature equals to " << temperature << endl;

flag = false;

}

catch ( except *ptr ) { ptr->action( ); delete ptr; } } // while

}

//请注意:本题中使用循环结构。

254

C语言程序设计第四版第七章答案 谭浩强

第七章函数 7.1写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果两个整数由键盘输入。 maxyueshu(m,n) int m,n; { int i=1,t; for(;i<=m&&i<=n;i++) {if(m%i==0&&n%i==0) t=i; } return(t); } minbeishu(m,n) int m,n; {int j; if(m>=n) j=m; else j=n; for(;!(j%m==0&&j%n==0);j++); return j; } main() {int a,b,max,min; printf("enter two number is: "); scanf("%d,%d",&a,&b); max=maxyueshu(a,b); min=minbeishu(a,b); printf("max=%d,min=%d\n",max,min); } 7.2求方程的根,用三个函数分别求当b2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。 #include"math.h" float yishigen(m,n,k) float m,n,k; {float x1,x2; x1=(-n+sqrt(k))/(2*m); x2=(-n-sqrt(k))/(2*m); printf("two shigen is x1=%.3f and x2=%.3f\n",x1,x2); } float denggen(m,n) float m,n; {float x; x=-n/(2*m); printf("denggen is x=%.3f\n",x); }

C语言程序设计基础试题及答案

第一部分C语言基础 一、选择题 1、以下正确得C语言标识符就是() A。%X B。a+b?C.a123 D.test! 2、已定义int a,b;则以下不正确得C语句就是() A.a*=5;??B。b/=2;? C.a+=1、75;?D.b&&=a; 3、若x、i、j与k都就是整型变量,则执行下面表达式后x得值为( ) x=(i=4,j=16,k=32) A.4?B.16 ?C.32 D.52 4、C语言中得标识符只能由字母、数字与下划线三种字符组成,且第一个字符( ) A.必须就是字母???B.必须为下划线? C.必须为字母或下划线D.可以就是字母、数字、下划线中得任一字符 5、下面正确得字符常量就是( ) A.“c”? B。’\\”? C.‘w’??D.” 6、设int a=2,b=0,c;则执行语句c+=b&&a--后, c得结果为( )。 A.0,1B.1,0 ?C.2,0 D.不确定 7、以下不正确得C语言标识符就是()?A) int B)a_1_2?C)ab1exe D)_x 8、以下就是正确得C语言标识符就是( )。?A)#define?B)_123 C) %d D) \n 9、下列四组字符串中都可以用作C语言程序标识符得一组就是() 。?A) prin tB)i\am C)Pxq D)str_l ???oodbs tart$it line# _3d one_half My-〉bookCpp ? pow 10、下面各选项组中,均就是C语言关键字得组就是()。?A)auto,enu

m,include B)switch,typedef,continue?C)signed,union,scanf?D)if,s truct,type 11、下列不属于C语言关键字得就是( ) 。 A)default?B)register C)enum ?D)external 12、C语言程序从main()函数开始执行,所以这个函数要写在( )。?A) 程序文件得开始?B)程序文件得最后?C)它所调用得函数得前面 D) 程序文件得任何位置 13、下列关于C语言得叙述错误得就是( ) A)大写字母与小写字母得意义相同?B) 不同类型得变量可以在一个表达式中 C)在赋值表达式中等号(=)左边得变量与右边得值可以就是不同类型?D) 同一个运算符号在不同得场合可以有不同得含义 14、在C语言中,错误得int类型得常数就是( ) 。?A)32768?B)0 C)037?D) 0xAF 15、将字符g赋给字符变量c,正确得表达式就是( )。 A) c=\147 ?B)c="\147" ?C)c='\147' 16、下列转义字符中错误得一个就是( )。 A) ’\000’ B)'\0014' ?C) ’\x111’ D) ’\2’ 17、将空格符赋给字符变量c,正确得赋值语句就是( )。 A) c=’\0’ B) c=NULL C)c=0 D)c=32 18、已知:char a=’\70’;则变量a中()。 A)包含1个字符?B)包含2个字符?C) 包含3个字符?D) 说明非法 19、字符串"\”EOF\n=-\61\””得长度就是( )。 A)8 ?B) 9?C)14?D)非法字符串

高级语言程序设计实验C

高级语言程序设计实验 C Coca-cola standardization office【ZZ5AB-ZZSYT-ZZ2C-ZZ682T-ZZT18】

陕西理工大学 《高级语言程序设计(C)》 实验报告 院系: 班级: 学号: 姓名:

目录

实验一:C开发环境与顺序结构程序设计 1.实验目的: (1) 了解集成开发环境VC++的使用方法,理解相关命令的含义,掌握编辑、编译、连接以及运行调试的方法,掌握程序的基本结构,掌握输入输出的方式。 (2) 掌握程序设计的基本要素中的数据类型、变量、运算符以及表达式的运用。 (3) 学会正确使用逻辑运算符和逻辑表达式以及关系运算符与关系表达式,掌握在程序设计中灵活使用顺序结构。 2.实验环境: (1) 硬件环境 CPU: Inter Pentium(R)4 CPU 以上 内存:2GByte (2) 软件环境 操作系统:Microsoft Windows 7 编译系统:Microsoft Visual C++ 3.实验步骤: 按如图所示流程进行实验内容的 调试。 (1) 在XP操作系统中,启动VC++ 编译环境; (2) 在VC++编译界面输入C源程 序; (3) 选择编译、组建、执行命令 进行编译、链接和运行,并记录 实验数据; (4) 按以上方法依次输入其他源 程序并调试、运行和记录实验数 据。

4.实验内容: 输入并运行下列程序,查看程序运行的结果。 #include<> void main() { printf(“* * * * * * * * * *\n”); printf(“Programing in c A Labrtry Maual\n”); printf(“* * * *\n”); printf(“*\n”); } 查看程序运行的结果 #include<> void main() { int i,j,m,n; i=8; j=10; m=++i; n=j--; printf(“%d,%d,%d,%d\n”,i,j,m,n); } 找出下列程序中的错误,改正后写出程序运行结果. #include<> void main() { Int x,y=z=5,aver; x=7; AVER=(x+y+z)/3 printf(“AVER=%d\n”,aver); }

C程序设计第七章课后练习答案

C程序设计(第三版)谭浩强著课后习题答案第7章 2009-05-16 10:56 7.7 打印“魔方阵”。所谓魔方阵是指这样的方阵,它的每一行、每一列和对角线之 和均相等。例如,三阶魔方阵为 8 1 6 3 5 7 4 9 2 要求打印出由1 到n2 的自然数构成的魔方阵。 /******************************************************************** **算法思路:魔方阵中各数的排列规律如下: **(1)将1放在第一行中间一列 **(2)从2开始直到n×n止各数依次按下列规则存放:每一个数存放的行比前一个数的** 行数减1,列数加1 **(3)如果上一个数的行数为1,则下一个数的行数为n(指最下一行) **(4)当上一个数的列数为n时,下一个数的列数应为1,行数减1 **(5)如果按上面的规则确定的位置数已有数,或者上一个数是第1行第n列时,则把 ** 下一个数放在上一个数的下面 ********************************************************************/ #include void main(){ int a[16][16],i,j,k,p,m,n; /*初始化*/ p=1; while(p==1){ printf("input n.(0n)){

年C语言程序设计基础期末考试试卷‘B’及答案

年C语言程序设计基础期 末考试试卷‘B’及答案 The following text is amended on 12 November 2020.

一、单项选择题 1. 在C语言中,下面字符串能用作变量名的是( )。 A、a+b B、auto C、2-and D、a2 2. 若double x=2,y;则执行y=x+3/2;后,y的值是( )。 A、 B. C. D. 3 3. 下列关于C语言中字符常量与字符串常量的叙述中,错 误的是( )。 A、字符常量是用单撇号括起来的一个字符 B、空格字符也可用作字符常量 C、字符串常量是用单撇号括起来的一个字符序列 D、字符串常量是用双撇号括起来的一个字符序列 4. 若有以下变量说明和数据的输入方式,则正确的输入语 句为( )。

变量说明:float x1,x2; 数据的输入方式:<回车> <回车> A、scanf(“%f,%f”,&x1,&x2); B、scanf(“%f%f”,&x1,&x2); C、scanf(“%,%”,&x1,&x2); D、scanf(“%%”,&x1,&x2); 6. 在一个源文件中定义的全局变量的作用域为()。 A. 本文件的全部范围 B. 从定义该变量开始至本文件结束 C. 本函数的全部范围 D. 本程序的全部范围

7. 当调用函数时,实参是一个数组名,则向函数传送的是( )。 A、数组的长度 B、数组的首地址 C、数组每一个元素的地址 D、数组每个元素中的值 8.若j为整型变量,则下面while循环( )。 j=10; while(j!=0) j=j-1; A、执行0次 B、执行1次 C、执行10次 D、执行无限次 9. 在while(x)语句中的x与下面条件表达式等价的 是:( )。 A、x!=0 B、 x==1 C、x!=1 D、x==0 10. 以下对C语言函数的描述中,正确的是( )。 A、调用函数时,参数可以是表达式

高级语言程序设计C

高级语言程序设计(C)模拟题四 一、选择题(本题共20道小题,每题2分,共40分。) 1.以下有4组用户标识符,其中合法的一组是()。 A) For -sub Case B) 4d DO Size C) f2_G3 IF abc D) WORD void define 答案:C 解析:标识符的命名规则:由字母、数字、下划线组成,第一个字符必须是字母或者下划线,标示符的命名不能同C语言的关键字相同。 2. 若有下面的程序片段: int a[12]={0}, *p[3], **pp, i; for(i=0; i<3; i++) p[i]=&a[i*4]; pp=p; 则对数组元素的错误引用是 A) pp[0][1] B) a[10] C) p[3][1] D) *(*(p+2)+2) 答案:C 解析:A. pp[0]表示p[0]的地址,而pp[0]也为一个指针,它和pp用法相同,故pp[0][1]则表示a[1]的地址,正确。 B.正确。 C.数组p的最大下标为2,错误。 D. *(*(p+2)+2)表示a[10],正确。 3. 若要求在if后一对圆括号中表示a不等于0的关系,则能正确表示这一关系的表达式为 A) a<>0 B) !a C) a=0 D) a 答案:D 解析:对if(a)而言,如果a的值不为0,则if语句括号中表达式的值不为0,if为真,选D。 4. 若已定义: int a[ ]={0,1,2,3,4,5,6,7,8,9], *p=a,i; 其中0≤i≤9, 则对a数组元素不正确的引用上 A) a[p-a] B) *(&a[i]) C) p[i] D) a[10] 答案:D 解析: A. a[p-a]表示a[0],正确。 B. *(&a[i])表示a[i],正确。 C. p[i]表示&a[i],正确。 D. a数组最大下标为9,错误。

(完整版)《C语言程序设计》基本知识点

《C语言程序设计》教学基本知识点 第一章C语言基本知识 1.C源程序的框架 尽管各个C源程序的功能千变万化,但框架是不变的,主要有:编译预处理、主函数()、函数n()等,主函数的位置不一定在最前面,可以在程序的中部或后面,主函数的名字固定为main。 2.C语言源程序的书写规则: (1)C源程序是由一个主函数和若干个其它函数组成的。 (2)函数名后必须有小括号,函数体放在大括号内。 (3)C程序必须用小写字母书写。 (4)每句的末尾加分号。 (5)可以一行多句。 (6)可以一句多行。 (7)可以在程序的任何位置加注释。 3.语句种类 语句是程序的基本成分,程序的执行就是通过一条条语句的执行而得以实现的,根据表现形式及功能的不同,C语言的基本语句可以分为五大类。 (1)流程控制语句 流程控制语句的功能是控制程序的走向,程序的流程有三种基本结构:顺序结构、分支结构和循环结构,任何复杂的程序都可以由这三种基本结构复合而成。其中后两种结构要用特定的流程控制语句实现。 (2)表达式语句 表达式语句的形式是:表达式;,即表达式后跟一分号“;”,分号是语句结束符,是一个语句必不可少的成分。表达式和表达式语句的区别在于表达式代表的是一个数值,而表达式语句则代表一种动作。最常见的表达式语句是赋值语句。 (3)函数调用语句 函数调用语句实际上也是一种表达式语句,形式为:在一次函数调用的小括号后面加上一个分号。 (4)空语句 空语句的形式就是一个分号,它不代表任何动作,常常作为一个意义转折点使用。 (5)复合语句 复合语句从形式上看是多个语句的组合,但在语法意义上它只相当于一个语句,在任何单一语句存在的地方都可以是复合语句。注意复合语句中最后一个语句末尾的分号不能少。复合语句右大括号后面没有分号。 4.运算符 用来表示数据各种操作的符号称为运算符。运算符实际上代表了一种类型数据的运算规则。不同的运算符具有不同的运算规则,其操作的数据类型必须符合该运算符的要求,运算结果的数据类型也是固定的。 根据参加操作的数据个数多少,可以将C语言的运算符分为单目运算符,双目运算符和三目运算符(三目运算符只有条件运算符一个)。 根据运算对象和运算结果的数据类型可分为算术运算符、关系运算符、逻辑运算符等。 5.表达式 表达式是由常量、变量、函数,通过运算符连接起来而形成的一个算式。一个常量,一个变量或一个函数都可以看成是一个表达式。 表达式的种类有: 算术表达式、关系表达式、逻辑表达式、赋值表达式、字位表达式、强制类型转换表达式、逗号

《高级语言程序设计》复习题及答案

一、选择题 1.以下叙述正确的是( ): A)在C程序中,main函数必须位于程序的最前面 B) C程序的每行只能写一条语句 C) C语言使用库函数实现输入输出操作 D)注释中的拼写错误会影响编译结果 2、以下说法中正确的是() A) C语言程序总是从第一个的函数开始执行 B) C语言程序总是从main()函数开始执行 C)在C语言程序中,要调用的函数必须在main()函数中定义 D)C语言程序中的main()函数必须放在程序的开始部分 3、下列选项种说法正确的是( ) A)函数由声明部分和执行部分两部分组成 B)语言中main函数一定在程序的开始部分 C)C语言程序总是从第一个定义的函数开始执行 D)C程序的每行中只能写一条语句 4、设有说明:int a;float b; char c; double d; 则表达式1.3*a+2*b*c+d*(int) 2.6 值的类型为( )。 A)doubleB) char C) floatD) int 5、C语言的标识符只能由字母、数字和下划线三种字符组成,且第一个字符( ) A)必须为字母 B)必须为下划线 C)必须为字母或下划线 D)可以是字母、数字和下划线中任一种字符 6、以下不正确的C语言标识符是( )。 A) ABC B) abc C)a_bc D) void 7、下列运算符中优先级最高的是( ) A)< B)+ C)&& D)!= 8、以下选项中属于C语言中合法关键字的是( ) A)Float B)abc C)int D)CASE 9、若x、i、j和k都是int型变量,计算下面表达式后,x的值为( ) x=(i=4,j=16,k=32) A)4 B)16 C)32 D)52 10、在C语言中,要求数据必须是整型的运算符是( ) A)/ B)+ + C)!=D) % 11、若整型变量x的值为8,则下列表达式中值为1的表达式是 A)x+=x-=x B) x%=x-1 C) x%=x%=3 D) x/=x+x 12、若w=1,x=2,y=3,z=4,则条件表达式“w > x? w : y< z ? y : z”的值是( ) A)4 B)3 C)2 D)1 13、有以下程序,程序运行后的输出结果是。 main() {inti=1,j=2,k=3; if(i++==1&&(++j==3||k++==3)) printf("%d %d %d\n",i,j,k); }

C语言程序设计基础

题型及分数: 单选题:25分 判断题:10分 填空题:15分 程序分析题:20分 编程题:30分 其中:课后习题及复习大纲中相类似题所占比例在70%-80%。 带*号的内容和复习题为较难部分,为非重点考察内容。 第一章C语言程序设计基础(出题比例4-6%) 1.程序设计语言的基本概念 ?程序 ?程序设计语言 ?常用程序设计语言及其类型 ?源程序、编译、目标文件、连接、可执行程序 ?编程的步骤 2.C语言程序的基本结构 (1) C源程序的框架 C源程序的框架,主要有:编译预处理、主函数()、函数n()等,主函数的位置不一定在最前面,可以在程序的中部或后面,主函数的名字固定为main。 (2) C语言源程序的书写规则: ?C源程序是由一个主函数和若干个其它函数组成的; ?函数名后必须有小括号,函数体放在大括号内; ?C程序对大、小写字母书写敏感; ?每句的末尾加分号; ?可以一行多句; ?可以一句多行; ?可以在程序的任何位置加注释,注释的方式。 3.C语言程序设计的的基本步骤和调试过程 复习题:/***** 1. 每个C语言程序中有且仅有一个函数,它是程序的入口和出口。 2. 引用C语言标准库函数,一般要用预处理命令将其头文件包含进来。 3. C语言属于() A.高级语言 B. 低级语言 C.中级语言 D. 机器语言 4. C语言规定了若干有特定意义、为C语言专用的单词,称为关键字。 5. C语言的语句以结尾。 6. C语言源程序经过编译以后生成文件,生成的文件名后缀为,经过连接后生成文件,后缀为 7.C语言中不能使用关键字作为标识符。(√)

第二章基本数据类型,运算符与表达式(出题比例20-30%) 关键知识点: 1.常量,变量: a)标识符的命名规则 b)常量,变量的概念:直接常量,符号常量,变量必须先定义后使用 c)变量名和变量值的区别。 2.C语言中的基本数据类型 a)各种数据类型的数据在内存中的存放方式 b)掌握各种数据类型数据的常量使用方法,特别注意字符常量和字符串常量的区别使 用 c)整型,字符型,浮点型变量的定义和引用方法 d)数据的数据类型,数据的取值范围,数据在内存中存放方式三者之间的联系 3.运算符和表达式的使用方法 a)自增、自减运算符 b)赋值运算符 c)算术运算符及表达式 d)逗号运算符及表达式 e)*位运算符及表达式 f)sizeof运算符及表达式 g)*强制数据类型转换符及表达式 4.运算符的优先级和结合性 本章复习题: 课后习题:一、单选题,二、填空题 1. C语言的标识符命名规则中,第一个字符可以为字符‘#’或‘&’ .(×)。 2.下列全部属于合法的用户标识符的是() A.A1 P_0 dS B. float 2la _A C. *a hy kk D. _123 temp main 3. C语言对字母的大小写不敏感。(×) 4. C语言中,符号常量由宏定义命令#define来定义。(√) 5. 在VC 6.0中,数据类型int,char,float和double所占的内存字节数分别是、、和。 6.下列四个选项中,均是合法的整型常量的选项是A A.160 –0XFFFF 011 B。-0XCDF 01A 0XE C.-01 986,012 0688 D。-0X48a 2e5 0x

华南理工大学高级语言程序设计(C++I)试卷及答案

华南理工大学计算机学院 高级语言程序设计(C++I)试卷 学生证号:____________________ 姓名: ____________________ 注意事项:1. 本试卷共五大题,满分100分,考试时间120分钟; 2. 所有答案请写在答卷上,试卷和答卷同时提交; 一.单项选择题(每小题2分,共20分) 1.下列字符列中,合法的长整型常量是: (A) 4.18e3 (B) 0.46793 (C) 4L (D) 956738 2.设变量x,y,a,b,c,d的值为1,计算表达式(x=a!=b)&&(y=c!=d)后,变量x,y的值分别是: (A) 0,0 (B) 0,1 (C) 1,0 (D) 1,1 3.以下运算符中,优先级最低的是: (A) *= (B) ?:: (C) && (D) >= 4.在C++中,char类型数据在内存的存储形式是: (A) 原码(B) 反码(C) 补码(D) ASCII码 5.设变量定义int x = 6, y, z; 执行语句 y = z = x--; y = x == z; 后变量y的值是:(A) 0 (B) 1 (C) 5 (D) 6 6.下面的变量定义中,不正确的是: (A) char *p=”string”(B) int a[]={‘A’,’B’,’C’} (C) float *q=&b, b; (D) doudle a, *r=&a; 7.设有变量定义 doubleb[5], *p=b; 能正确表示b数组中元素地址的表达式是: (A) b (B) b+5 (C) *b[0] (D) &b 8.设有变量定义 int a[]={2,3,5,9,11,13},*pa=a, w; 执行语句w=++(*++pa); 后,变量w和*pa的值是: (A) 3和3 (B) 4和3 (C) 3和4 (D) 4和4 9.语句if(w) … ; else … ; 中的表达式w的等价表示是: (A) w==0 (B) w==1 (C) w!=0 (D) w!=1 10.设有函数说明和变量定义:int max(int x,int y); int(*p)(int,int)=max; int a, b; 以下正确调用函数的代码是: (A) *p(a,b) (B) p(a,b) (C) *(p(a,b)) (D) p(&a,&b)

C语言程序设计第二版第四章第七章课后答案

1、输入函数scanf的参数错误,应该为:scanf("%f",&k); 2、|1234 1234 | 3、ff10 4、1,3,1 5、原字符串左边加空格再加字符串本省,字符个数总和为5个 6、scanf("%d,%d,%c,%c",&a1,&a2,&c1,&c2); 7、 printf("a+b=%d\n",a+b); printf("a-b=%d\n",a-b); printf("a*b=%d\n",a*b); printf("a/b=%d\n",a/b); printf("(float)a/b=%f\n",(float)a/b); printf("a%b=%d\n",a%b); 8、 void main() { float r; float s,c; printf("please input the number:"); scanf("%f",&r); if(r>=0) { s = 3.14*r*r; c = 2*3.14*r; printf("s = %f, c = %f\n",s,c); } else printf("you input number is error!"); } 9 void main() {int n; printf("please input the number:"); scanf("%d",&n); if(n>=100 && n <= 999) printf("%d%d%d",n%10,(n/10)%10,n/100); else printf("you input number is error!"); } 10、 void main() { int i,j,k; scanf("%d,%d,%d",&i,&j,&k); ((i%2 != 0?1:0) + (j%2 != 0?1:0)+(k%2 != 0?1:0)) == 2?printf("YES\n"):printf("NO\n"); } 11、 void main()

《C语言程序设计基础》 试卷A及参考答案

《c语言程序设计基础》试卷 (A卷) 考试时间:90分钟闭卷任课老师: 班级:学号:姓名:成绩: (每小题2分,共30分) 、由C++源文件编译生成的目标文件的缺省扩展名为() A、cpp B、exe C、obj D、lik 2、下列中,不是C++合法的标识符() A、2mn B、m123 C、kk D、a_1 3、C++中,基本数据类型不包括() A、int B、float C、char D、class 4、x和y均为bool量,则x| |y为真的条件是() A、它们均为真 B、其中一个为真 C、它们均为假 D、其中一个为 5、C++中,一个字符型数据在内存中占()字节。 A、4 B、2 C、1 D、8 6、字符串常量”hello”的长度是() A、5 B、6 C、7 D、8 7、以下语句,int i=2,j=2,m,n;m=i++;n=++j;则m,n,i,j的值分别为() A、2,3,2,2 B、2,3,3,3 C、2,2,3,3 D、3,3,2,2 8、假定a为一个整型数组名,则元素a[4]的字节地址为()

A、a+4 B、a+8 C、a+16 D、a+32 9、 x>0 || y==5的相反表达式为()。 A、 x<=0 || y!=5 B、 x<=0 && y!=5 C、 x>0 || y!=5 D、 x>0 && y==5 10、在下面的函数声明中,存在着语法错误的是() A、void BC(int a , int) B、void BD(int , int) C、void BE(int , int=5) D、int BF(int x ; int y) 11、循环体至少被执行一次的语句为()。 A、for循环 B、while循环 C、do循环 D、任一种循环 12、已知一函数中有下列变量定义,其中属于静态变量的是() A、int a=5; B、static double b; C、register char c; D、auto short d; 13、do语句能够改写为()语句。 A、复合 B、if C、switch D、 while 14、要使语句: p=new int[10]; 能够正常执行,p应定义为() A、int p; B、int p[10]; C、int *p; D、int (*p)[10]; 15、编写C++程序一般需经过的几个步骤依次是( )。 A、编译、编辑、连接、调试 B、编辑、编译、连接、调试 C、编译、调试、编辑、连接 D、编辑、调试、编辑、连接 二、填空题(每小题1分,共10分) 1、使用const 语句定义一个标识符常量时,则必须对它同时进行。 2、用于输出表达式值的标准输出流对象是_________,用于从键盘上为变量输入

高级语言程序设计实验 C

陕西理工大学 《高级语言程序设计(C)》 实验报告 院系: 班级: 学号: 姓名:

目录

实验一:C开发环境与顺序结构程序设计 1.实验目的: (1) 了解集成开发环境VC++的使用方法,理解相关命令的含义,掌握编辑、编译、连接以及运行调试的方法,掌握程序的基本结构,掌握输入输出的方式。 (2) 掌握程序设计的基本要素中的数据类型、变量、运算符以及表达式的运用。 (3) 学会正确使用逻辑运算符和逻辑表达式以及关系运算符与关系表达式,掌握在程序设计中灵活使用顺序结构。 2.实验环境: (1) 硬件环境 CPU:Inter Pentium(R)4 CPU 以上 内存:2GByte (2) 软件环境 操作系统:Microsoft Windows 7 编译系统:Microsoft Visual C++ 3.实验步骤: 按如图所示流程进行实验内容的 调试。 (1) 在XP操作系统中,启动VC++ 编译环境; (2) 在VC++编译界面输入C源程 序; (3) 选择编译、组建、执行命令进 行编译、链接和运行,并记录实验 数据; (4) 按以上方法依次输入其他源程 序并调试、运行和记录实验数据。 4.实验内容: 输入并运行下列程序,查看程序运行的结果。 #include<>

void main() { printf(“* * * * * * * * * *\n”); printf(“Programing in c A Labrtry Maual\n”); printf(“* * * *\n”); printf(“*\n”); } 查看程序运行的结果 #include<> void main() { int i,j,m,n; i=8; j=10; m=++i; n=j--; printf(“%d,%d,%d,%d\n”,i,j,m,n); } 找出下列程序中的错误,改正后写出程序运行结果. #include<> void main() { Int x,y=z=5,aver; x=7; AVER=(x+y+z)/3 printf(“AVER=%d\n”,aver); }

C语言程序设计基础知识 习题一及参考答案

第一章:程序设计基础知识 一、单项选择题 1、以下( )是面向过程的程序设计语言。 A)机器语言B)汇编语言C)高级语言D)第四代语言 2、程序设计一般包含以下四个步骤,其中其中首先应该完成的是( )。 A)设计数据结构和算法B)建立数学模型 C)编写程序D)调试和运行程序 3、以下常用算法中适合计算等差级数的算法是( )。 A)枚举法B)递推法C)分治法D)排序法 4、以下不属于算法基本特征的是( )。 A)有穷性B)有效性C)可靠性D)有一个或多各输出 5、以下描述中不正确的是( )。 A)程序就是软件,但软件不紧紧是程序。 B)程序是指令的集合,计算机语言是编写程序的工具。 C)计算机语言都是形式化的语言,它有一个语法规则和定义。 D)计算机语言只能编写程序而不能表示算法。 6、下面描述中,正确的是( ) A)结构化程序设计方法是面向过程程序设计的主流。 B)算法就是计算方法。 C)一个正确的程序就是指程序书写正确。 D)计算机语言就是编写程序的工具而不是表示算法的工具。 7、下面描述中,不正确的是( )。 A)递归法的关键是必须有一个递归终止的条件。

B)递归算法要求语言具有反复自我调用子程序的能力。 C)对于同一个问题,递推算法比递归算法的执行时间要长。 D)递推算法总可以转换为一个递归算法。 8、N-S图与传统流程图比较,主要优点是( )。 A)杜绝了程序的无条件转移。 B)具有顺序、选择和循环三种基本结构。 C)简单、只管。 D)有利于编写程序。 A)B)C)D) 二、填空题 1、在流程图符号中,判断框中应该填写的的是________。(判断条件) 2、结构化程序设计是__________应遵循的方法和原则。(面向过程编程) 3、结构化程序必须用__________程序设计语言来编写。(具有结构化控制语句) 4、可以被连续执行的一条条指令的集合称为计算机的________。(程序) 5、只描述程序应该“做什么”,而不必描述“怎样做”的语言被称为________。(面向对象) 6、任何简单或复杂的算法都是由_____和_____这两个要素组成。(数据结构算法) 7、算法的_______特征是指:一个算法必须在执行有限个操作步骤后终止。(有穷性) 8、在三种基本结构中,先执行后判断的结构被称为_________。(直到型循环结构) 9、在程序设计中,把解决问题确定的方法和有限的步骤称为______。(算法) 10、程序设计风格主要影响程序的________。(可读性) 11、用模块组装起来的程序被成为________结构化程序。(模块化) 12、采用自上而下,逐步求精的设计方法便于____________。(结构化和模块化) 三、应用题

华南理工 《高级语言程序设计C 》平时作业

一、分析程序,写输出结果 1.#include #include void main() {int m, k, i ; for( m=1; m<=10; m+=2 ) { k = m/3; for( i=2; i<=k; i++ ) if( m%i ) cout << m << " "; } } 解:m 的取值为1,3,5,7,9 对应k的取值为0,1,1,2,3, 第二个for循环:只有当k=2和k=3时才执行,当k=2,i=2,m%i等于1为真,输出m为7 当k=3,i=2,m%i等于1为真,输出m为9, i=3,m%i等于0为假,无输出结果为: 7 9 2.#include void fun(); void main() {int i; for(i=1;i<5;i++) fun(); cout< int fun(int n) { if(n==0) return 1; return 2*fun(n-1); } void main()

{ int a=5; cout< void main() { char *cp="word"; for (int i=0 ; i<4; i++ ) cout< void main() { int max; int a[10]={76,55,95,87,85,83,65,90,77,85}; int *p= a ; max=*p; for( ; p< &a[10] ; p++) if( *p>max ) max= *p ; cout<<"max= "<

华工高级语言程序设计C++_随堂练习答案

华工高级语言程序设计C++_随堂练习答案答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D.

答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D.

答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D.

答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D.

答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D. 答题: A. B. C. D.

C程序设计书面作业

C 程序设计 第七章习题 1、分析并写出下面程序的运行结果。 #include “stdio.h” int Square(int i){ return i*i; } int main(){ int i=0; i=Square(i); for( ; i<3;i++){ static int i=1; i+=Square(i); printf(“%d,”,i); } printf(“%d\n”,i); return 0; } 2、用全局变量模拟编程显示一个数字式时钟,然后上机验证。#include int hour,minute,second; /*定义全局变量*/ void update(){ seconde++; if(second==60){ ① minute++; } if( ②){ minute=0; hour++; } if(hour==24) ③; } void display(){ printf(“④“,hour,minute,second); } void delay(){ int t; for(t=0;t<10000000; t++); /*用循环体为空语句的循环实现延时*/

} int main(){ int i; ⑤ ; for(i=0;i<10000000;i++){ /*利用循环结构控制时钟运行的时间*/ update(); /*更新时、分、秒显示值*/ display(); /*显示时、分、秒*/ delay(); /*模拟延迟时间为1秒*/ } return 0; } 3、用函数编程计算两整数的最大值,在主函数中调用该函数计算并输出从键盘输入的两整数的最大值。 4、请分别用以下三种方法编写计算最大公约数的函数Gcd(),在主函数中调用该函数计算并输出从键盘任意输入的两整数的取大公约数。 (1)穷举法。由于a和b的最大公约数不可能比a和b中的较小都还大,否则一定不能整除它,因此,先找出a和b中的较小者,然后从该数开始逐次减1尝试每种可能,即检验该数到1的所有整数,第一个满足公约数条件的值就是a和b的最大公约数。 (2)欧几里得算法,也称辗转相除法。对正整数a和b,连续进行求余运算,直到余数为0为止,此时非0的除数就是最大公约数。(3)递归方法。对正整数a和b,当a>b时,若a中含有与b相同的公约数,则a中去掉b后剩余的部分a-b中也应含有与b相同的公约数,对a-b和b计算公约数就相当于对a和b计算公约数。反复使用最大公约数的如下3条性质,直到a和b相等为止,这时a或b就是它们的取大公约数。 性质1 如果a>b,则a和b与a-b和b的最大公约数相同,即Gcd(a,b)=Gcd(a-b,b). 性质2 如果b>a,则a和b与a和b-a的最大公约数相同,即Gcd(a,b)=Gcd(a,b-a). 性质3 如果a=b,则a和b的最大公约数与a值和b值相同,即Gcd(a,b)=a=b. 5、编写一个递归函数,用以实现从1到n这n个数的累加和。 6、编写一个递归函数,用以实现n的阶乘。 7、中国古代民间有一个游戏:两个人从1开始轮流报数,每人每次可以报一个数或两个连续的数,谁先报到30,谁为胜方。若要改成游戏者与计算机做这个游戏,则首先需要决定谁先报数,可以通过生成一个随机整数来决定计算机和游戏者谁先报数。计算机报数的原则为:若剩下数据的个数除以3,余数为1,则报1个数,若剩下数除以3,余数为2,则报2个数,否则随机报1个或2个数。游戏者通过键盘输入自己报的数,所报的数必须符合游戏的规则。如果计算机和游戏者都未报到30,则可以接着报数。先到到30的一方为胜。请

《C语言程序设计基础》教材参考答案-20140211要点

《C语言程序设计基础》教材 参考答案 包括各单元: 一.随堂练习 二.单元总结 三.单元练习 四.实训指导

单元一程序设计宏观认识 一.随堂练习 【随堂练习1-1】 1.根据上述示例模仿编程,输入正方形的边长,计算其面积。 #include void main( ) { int a,s; //数据准备,边长a,面积s printf("请输入正方形的边长:"); scanf("%d",&a); //数据输入 s=a*a; //数据计算 printf("该正方形的面积为:%d.\n",s); } 【随堂练习1-2】 1.下面哪些标识符属于合法的用户自定义标识符: Main、void、_num、my$、a*、N4、3m、a-2 答:合法的用户自定义标识符是:Main、_num、N4 2.结合【例1.2】指出程序代码中所用到的标识符哪些是关键字,哪些是预定义标识符,哪些是用户自定义标识符。 答:关键字:void、int 预定义标识符:include、main、printf、scanf 用户自定义标识符:a、b、c 3.分析【例1.2】中函数的结构,包括函数首部(函数返回值类型、函数名、形式参数)、函数体语句(说明语句、可执行语句)。 答:函数首部:void main( ),其中函数返回值类型为void、函数名为main、形式参数无; 函数体语句:{}内的语句为函数体,其中:说明语句为int a,b,c;,其余为可执行语句。 【随堂练习1-3】 1.在VC++6.0环境中完成【例1.2】程序开发过程。 (略) 2.查阅【例1.2】所对应的工程文件夹,了解相关文件的含义。

相关文档
相关文档 最新文档