2.3 Procedural Programming

2.3 Procedural Programming
The original programming paradigm is:
                                            Decide which procedures you want;
                                            use the best algorithms you can find.
    The focus is on the processing – the algorithm needed to perform the desired computation. Languages support this paradigm by providing facilities for passing arguments to functions and returning values from functions. The literature related to this way of thinking is filled with discussion of
ways to pass arguments, ways to distinguish different kinds of arguments, different kinds of functions
(e.g., procedures, routines, and macros), etc.
    A typical example of ‘‘good style’’ is a square-root function. Given a double-precision
floating-point argument, it produces a result. To do this, it performs a well-understood mathematical
computation:

double sqrt_num(double n)
{
double low=0;
double high=n;
double minmal=0.000000000001;
while(low<high)
{
double mid=low+(high-low)/2.0;
double mid2=mid*mid;
if((mid2-n)<=minmal&&(mid2-n)>=-minmal)
return mid;
else if(mid2-n>0)
high=mid;
else
low=mid;
}
}

void f()
{
    double root = sqrt _num(2 );
    / / ...
}
    Curly braces, { }, express grouping in C++. Here, they indicate the start and end of the function
bodies. The double slash,/ /, begins a comment that extends to the end of the line. The keyword
v o i d indicates that a function does not return a value.
    From the point of view of program organization, functions are used to create order in a maze of
algorithms. The algorithms themselves are written using function calls and other language facilities.
The following subsections present a thumb-nail  sketch of C++’s most basic facilities for  expressing computation.

2.3.1 Variables and Arithmetic [tour.var]
Every name and every expression has a type that determines the operations that may be performed
on it. For example, the declaration
    int inch ;
specifies that inch is of type int ; that is, inch is an integer variable.
    A  declaration is a statement that introduces a name into the program. It specifies a type for that
name. A type defines the proper use of a name or an expression.
    C++ offers a variety of fundamental types, which correspond directly to hardware facilities. For
example:
bool     // Boolean, possible values are true and false
char     // character, for example, ’a’, ’z’, and ’9’
int         // integer, for example, 1, 42, and 1216
double     // double-precision  floating-point  number, for example, 3.14 and 299793.0
A c h a r variable is of the natural size to hold a character on a given machine (typically a byte), and
an i n t variable is of the natural size for integer arithmetic on a given machine (typically a word).

The  arithmetic operators can be used for any combination of these types:
+     // plus, both unary and binary
-     // minus, both unary and binary
*     // multiply
/     // divide
%     // remainder

So can the  comparison operators:
==     // equal
!=     // not equal
<     // less than
>     // greater than
<=     // less than or equal
>=     // greater than or equal

In assignments and in arithmetic operations, C++ performs all meaningful conversions between the
basic types so that they can be mixed freely:

void some_function()
{
double d=2.2;// initialize floating-point number
int i=7;// initialize integer
d=d+i;// assign sum to d
cout<<"d : "<<d<<endl;
i=d*i;// assign product to i
cout<<"i : "<<i<<endl;
}

As in C, = is the assignment operator and == tests equality.

2.3.2 Tests and Loops [tour.loop]
C++ provides a conventional set of statements for expressing selection and looping. For example,
here is a simple function that prompts the user and returns a Boolean indicating the response:

bool accept()
{
cout<<"do you want to proceed (y or n)?\n";
char answer=0;
cin>>answer;
if (answer=='y')
return true;
return false;
}

The << operator (‘‘put to’’) is used as an output operator;  cout is the standard output stream. The >> operator (‘‘get from’’) is used as an input operator;  cin is the standard input stream. The type of the right-hand operand of >> determines what input is accepted and is the target of the input operation. The \ n character at the end of the output string represents a newline.
The example could be slightly improved by taking an ‘n’ answer into account:

bool accept2()
{
cout<<"do you want to proceed (y or n)?\n";
char answer=0;
cin>>answer;
switch(answer)
{
case 'y':
return true;
case 'n':
return false;
default:
cout<<"I'll take this for no."<<endl;
return false;
}
}

A switch statement tests a value against a set of  constants. The case constants must be  distinct, and
if the value tested does not match any of them, the  default is chosen. The programmer need not
provide a  default .

bool accept3()
{
int tries=1;
while(tries<4)
{
cout<<"do you want to proceed (y or n)?\n";
char answer=0;
cin>>answer;
switch(answer)
{
case 'y':
return true;
case 'n':
return false;
default:
cout<<"I dont understand input,try again!"<<endl;
tries++;
}
}
cout<<"I'll take this for no."<<endl;
return false;
}

The while statement executes until its condition becomes false .
2.3.3 Pointers and Arrays [tour.ptr]
    An array can be declared like this:
char v [1 0 ]; // array of 10 characters
Similarly, a pointer can be declared like this:
char * p ; // pointer to character
In declarations, [] means ‘‘array of’’ and * means ‘‘pointer to.’’ All arrays have 0 as their lower bound, so v has ten elements, v [0 ]...v [9 ]. A pointer variable can hold the address of an object of
the appropriate type:
p = &v [3 ]; // p points to v’s fourth element
Unary & is the address-of operator.

Consider copying ten elements from one array to another:
void another_function ()
{
    int v1[1 0 ];
    int v2[1 0 ];
    / / ...
    for(int i=0;i<10;i++) v2[i]=v1[i];
}
This forstatement
can be read as ‘‘set i to zero, while i is less than 1 0 , copy the i th element and
increment i .’’ When applied to an integer variable, the increment operator ++ simply adds 1 .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值