第三章 運算式


1.算術運算 (Arithmetic Operators):

符號
意 義
+
加號(加法)
-
減號(減法)
*
乘號(乘法)
/
除號(除法)
%
餘號(求餘數)

2.算術運算的優先次序:

符號
意 義
優先次序
( )
小括號
最高
-
負號
次高
* / %
乘,除,餘
中等
+ -
加,減
較低
=
等號
最低

例如:

 

問題一:假如 a 的值是 8 ,b 的值是 2 ,c 的值是 4 ,則下列各式的值為何?

a + b % c =

-a / b =

a * -b =

問題二:下列程式會印出什麼?

#include <stdio.h>

int main(void)
{

printf("%d %d %d %d\n", 1 + 2, 5 / 2 , -2 * 4, 11 % 3);
printf("%.5f %.5f %.5f\n", 1. + 2. , 5. / 2. , -2. * 4.);
exit(0);

}


3.關係運算 (Relational Operators):

符 號
意 義
<=
小於等於
<
小於
>=
大於等於
>
大於
==
相等
!=
不相等

4.關係運算的結果只有兩種:"真" 或 "假"。在 C/C++ 語言中,"假" 的值為 0,其餘不為 0 的值皆為 "真"。

5.關係運算的優先次序全部都低於算術運算。

例如:

 

問題三:下列程式會印出什麼?

#include <stdio.h>

int main(void)
{

int n;

for ( n = 10 ; n >= 0 ; n = n - 1)

printf("%d\n", n);

printf("結束!");

}


問題四:下列程式會印出什麼?

#include <stdio.h>

int main(void)
{

int n = 10;

while ( n>= 0)
{

printf("%d\n", n);
n = n - 1;

}
printf("結束!");

}


6.邏輯運算 (Logical Operators):

符 號
意 義
&&
且 (AND)
!!
或 (OR)
!
非 (NOT)

 

7.邏輯運算的優先次序低於關係運算。

 

問題五:請寫出下列運算式的結果

1< 4 && 4 < 7 =

1 < 4 && 8 < 4 =

!( 2 <= 5 ) =

!( 1 < 3 ) || ( 2 < 4) =

!(4 <= 6 && 3 <= 7) =

問題六:請寫一程式可以印出介於 0 到 9 之間所有的數字。

問題七:請寫一程式可以印出 字元 '0' 到 '9' 之間所有的字元。

問題八:請寫一程式可以印出 A 到 Z 之間所有的字母。

問題九:請寫一程式可以印出 a 到 z 之間所有的字母。


8.位元運算 (Bitwise Logical Operators):

(1) AND 運算:

&
0
1
0
0
0
1
0
1

(2) OR 運算:

|
0
1
0
0
1
1
1
1

(3) NOT 運算:

~
0
1
1
0

(4) XOR 運算:

^
0
1
0
0
1
1
1
0

問題十:請寫出下列運算之後的結果

 
0000000001111111
&
0111010110011010
 

 

 
0000000001111111
&
0111010110011010
 

 

 
0000000001111111
&
0111010110011010
 

9.移位運算 (Shift Operators):

符 號
意 義
<<
將二進位數左移一位
>>
將二進位數右移一位

例如:

二進位數 00101011 << 1 = 01010110

二進位數 00101011 >> 1 = 00010101

十進位數 35 << 1 = 70

十進位數 35 >> 1 = 17

 


程式習作(Program Exercise)

1.試寫一轉換攝氏溫度(Centigrade)至華氏溫度(Fahrenheit)的程式,其轉換公式如下:

華氏溫度 = ( 5/ 9) * 攝氏溫度 + 32

2.根據上題所提供的轉換公式,撰寫華氏溫度至攝氏溫度的程式。

3.試撰寫一轉換英哩至公里的程式,其轉換公式如下:

1 英哩 = 1.6 公里

4.根據上題所提供的資訊,撰寫轉換公里至英哩的程式。

5.試撰寫一程式,輸入梯形的上底、下底和高,計算該梯形的面積。

6.試寫一程式輸入球的半徑,然後計算球的體積。


Program Exercise

1.Write a program which can translate the degree of Centigrade thermometer to the degree of Fahrenheit thermometer.

Hint: (the degree of Fahrenheit thermometer) = (the degree of Centigrade thermometer) * 5 / 9 + 32

[ show me the answer ]

2.According to the formula of last question, write a program to tranlate the degree of Fahrenheit thermometer to the degree of Centigrade thermometer.

[ show me the answer ]

3.Write a program which can translate the length measured by miles to kilo-meters.

Hint: 1 mile = 1.6 kilo-meters

[ show me the answer ]

4.According to above formula, write a program to translate the length measured by kilo-meters to miles.

[ show me the answer ]

5.Write a program to calculate the area of a given trapezoid. The program receives three inputs (the upper, base, and height of trapezoid), and output the area of the trapezoid.

[ show me the answer ]

6.Write a program to input the radius of a ball, and output the volume of the ball.

[ show me the answer ]


10.函數 (Functions)

Consider follwing C program which summarizes parameters a and b :

#include <stdio.h>

int sum(int a, int b);

void main(void)
{

int a, b;

printf("Please input 'a' :");
scanf("%d", &a);

printf("Please input 'b' ");
scanf("%d", &b);

printf("Summation of %d and %d is %d", a, b, sum(a, b));

}

/* Function sum(a, b) will return the summation of a and b. */
int sum(int a, int b)
{

int c;

c = a + b;
return (c);

}

New concepts:

1.Call and return
2.Returned type
3.Local variables and scope of names
4.Returned value
5.Specification and manual page
6.Arguments


11.左值, 右值, 遞增, 遞減 (Lvalue, Rvalue, Increment, Decrement)

(1) Lvalue and Rvalue

Consider following assignment statement,

x = y;

the left-hand side of the assignment muse be an lvalue (an expression that references storage in the machine), the right-hand side of the assignment may be an lvalue of rvalue. For example,

x = 10; x is lvalue, 10 is rvalue
x = y + 2; x is lvalue, y is lvalue, 2 is rvalue
2 = x; illegal, 2 is rvalue and it can not be the left-hand side of assignment

(2)Increment

Consider following statement,

x = ++y;

the value of ++y is one greater than the original value of y. In additon, there is a side-effect: the storage of y is incremented sometime during the execution of the statement. The operator ++, when applied to the left of its operand, is known as a prefix operator.

Conversely, when ++ is written to the right of its operand, it is known as a postfix operator. The postfix form is defferent in that the value of y++ is the value of y prior to the incrementation. For example,

if y initially contains the value 99, the statement

x = ++y;

will produce 100 in both x and y. If y now contains 100, the statement

x = y++;

will produce 100 in x and 101 in y.

(3)Decrement

Everythign said about ++ aaplies to --, except that it decrements instead of incrementing.


12.Assignment Operators

"Add 2 to x" can be written as

x = x + 2

or as

x += 2

The assignment operator += is a combination of the + (add) and = (assignment) operators. All the arithmetic and bitwise operators have combined forms link this:

x += n Add n to x
x -= n Subtract n from x
x *= n Multiply x by n
x /= n Divide x by n
x %= n x gets the remainder of dividing x by n
x <<= n Shift x left by n
x >>= n Shift x right by n
x &= n x gets x bit-and'ed with n
x != n x gets x bit-or'ed with n
x ^= n x gets the exclusive-or of x and n

13.Nesting of Operators

Each of the C operators produces a result that can be nested inside a larger expression. This is because C language accepts an expression anywhere a value is allowed. For example, we have often seen loops that start like this:

while ((c = getchar()) != EOF)

The assignment c = getchar() is nested inside the comparison. This is the sequence of events in evaluating the expression

(c = getchar()) != EOF

Call getchar(), producing an int result.

store the result in c.

Compare c with EOF, producing true of false.


Last update : Wednesday, 2000-11-15