Moät soá kieán thöùc cô sôû veà laäp trình baèng ngoân ngöõ C

Phaâàn tröôùc | Muïc luïc | Phaâàn sau

VI. Thao taùc döõ lieäu vôùi caùc toaùn töû

Noäi dung:

Vôùi phaàn naøy ta tìm hieåu kyõ hôn veà caùc toaùn töû:

 

Toùan töû gaùn soá hoïc

Toùan töû gaùn (=)

Bieán = bieåu thöùc;

Baèng toaùn töû (=), giaù trò keát quaû cuûa veá traùi seõ ñöôïc gaén vaøo boä nhôù daønh cho bieán ôû veá phaûi..

Caûnh baùo

Ñöøng nhaàm laãn (=) with the bieåu thöùc quan heä (= =). 

Moät soá caùch vieát ñôn giaûn hoùa trong C

x = x + y     à     x += y
x = x - y    
à     x -= y
x = x * y    
à     x *= y
x = x / y    
à     x /= y
x = x % y    
à     x %= y

Chuù yù :   x = x*y + z      khaùc vôùi   x*=y +z   ó  x = x*(y + z)             

Ví duï:  

1:  /* Using arithmetic assignment operators */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int x, y, z;
7:
8:     x = 1;   /* initialize x */
9:     y = 3;   /* initialize y */
10:    z = 10;  /* initialize z */
11:    printf("Given x = %d, y = %d, and z = %d,\n", x, y, z);
12:
13:    x = x + y;
14:    printf("x = x + y  assigns %d to x;\n", x);
15:
16:    x = 1;  /* reset x */
17:    x += y;
18:    printf("x += y  assigns %d to x;\n", x);
19:
20:    x = 1;  /* reset x */
21:    z = z * x + y;
22:    printf("z = z * x + y  assigns %d to z;\n", z);
23:
24:    z = 10;  /* reset z */
25:    z = z * (x + y);
26:    printf("z = z * (x + y) assigns %d to z;\n", z);
27:
28:    z = 10;  /* reset z */
29:    z *= x + y;
30:    printf("z *= x + y assigns %d to z.\n", z);
31:
32:    return 0;
33: }
Ñaùp soá:
 
Given x = 1, y = 3, and z = 10,
x = x + y  assigns 4 to x;
x += y  assigns 4 to x;
z = z * x + y  assigns 13 to z;
z = z * (x + y) assigns 40 to z;
z *= x + y assigns 40 to z.

Toaùn töû taêng giaûm

Toaùn töû taêng: ++  (coäng theâm 1)               x = x+1  ó  ++x
Toaùn töû giaûm: --  (tröø ñi 1)                        x = x-1 
ó   --x       

 Phaân bieät:   y = x++   nghóa laø sau khi x coäng theâm 1 (x=x+1), môùi gaùn cho y (y=x+1)
                      y = ++x           nghóa laø y laáy giaù trò cuûa x ban ñaàu (y=x), sau ñoù x môùi coäng theâm 1 (x=x+1)

Ví duï:

1:  /* pre- or post-increment(decrement) operators */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int w, x, y, z, result;
7:
8:     w = x = y = z = 1;   /* initialize x and y */
9:     printf("Given w = %d, x = %d, y = %d, and z = %d,\n", w, x, y, z);
10:
11:    result = ++w;
12:    printf("++w gives: %d\n", result);
13:    result = x++;
14:    printf("x++ gives: %d\n", result);
15:    result = --y;
16:    printf("--y gives: %d\n", result);
17:    result = z--;
18:    printf("z-- gives: %d\n", result);
19:    return 0;
20: }
Keát quaû: 
 
Given w = 1, x = 1, y = 1, and z = 1,
++w gives: 2
x++ gives: 1
--y gives: 0
z-- gives: 1

Toùan töû quan heä

Keát quaû caùc bieåu thöùc coù toaùn töû quan heä thu ñöôïc theo nguyeân taéc ñaïi soá Boole.

Toaùn töû

Moâ taû

Toaùn töû

Moâ taû

= =

Baèng

<

Nhoû hôn

!=

Khoâng baèng

>=

Lôùn hôn hoaëc baèng

>

Lôùn hôn

<=

Nhoû hôn hoaëc baèng

Ví duï:

1:  /* Using relational operators */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int x, y;
7:     double z;
8:
9:     x = 7;
10:    y = 25;
11:    z = 24.46;
12:    printf("Given x = %d, y = %d, and z = %.2f,\n", x, y, z);
13:    printf("x >= y  produces: %d\n", x >= y);
14:    printf("x == y  produces: %d\n", x == y);
15:    printf("x < z   produces: %d\n", x < z);
16:    printf("y > z   produces: %d\n", y > z);
17:    printf("x != y - 18  produces: %d\n", x != y - 18);
18:    printf("x + y != z   produces: %d\n", x + y != z);
19:    return 0;
20: }

Ñaùp soá:

Given x = 7, y = 25, and z = 24.46,
x >= y  produces: 0
x == y  produces: 0
x < z   produces: 1
y > z   produces: 1
x != y - 18  produces: 0
x + y != z   produces: 1

Toùan töû chuyeån ñoåi kieåu

 (data-type) x

Ví duï:  (float)5 coù taùc duïng chuyeån integer 5 thaønh soá float 5.0.

Ví duï:

1:  /* Using the cast operator */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int x, y;
7:
8:     x = 7;
9:     y = 5;
10:    printf("Given x = %d, y = %d\n", x, y);
11:    printf("x / y produces: %d\n",  x / y);
12:    printf("(float)x / y produces: %f\n",  (float)x / y);
13:    return 0;
14: }

Keát quaû:

Given x = 7, y = 5
x / y produces: 1
(float)x / y produces: 1.400000

Baøi taäp

  1. Vôùi x = 3 vaø y = 6, z = x * y = = 18 baèng bao nhieâu?
  1. Phaân bieät:
printf("x++ produces:   %d\n", x++);
printf("Now x contains: %d\n", x);
 
printf("x = x++ produces: %d\n", x = x++);
printf("Now x contains:   %d\n", x);
  1. Trong chöông trình döôùi ñaây, coù caùi gì sai ?
#include <stdio.h>
 
main()
{
   int x, y;
 
   x = y = 0;
   printf("The comparison result is: %d\n",  x = y);
   return 0;
}
 

Phaâàn tröôùc | Muïc luïc | Phaâàn sau