You are on page 1of 38

CC PHP TON CA C

TRN UYN TRANG Khoa Tin hc Trng i hc S phm, HN


October 13, 2012 C programming

Ton t s hc Ton t quan h Ton t logic Ton t trn bit Ton t tng, gim Ton t gn Ton t phy, biu thc phy Ton t iu kin, biu thc iu kin Ton t sizeof u tin ca cc ton t Cu trc tng qut ca mt chng trnh C
October 13, 2012 C programming

TON T S HC

+, -, *, /: char, int, long, float, double %: char, int, long, unsigned Th t kt hp theo nguyn tc ton hc: *, /, % trc; +, - sau; nu ngang cp th kt hp t tri sang phi. C th dng php ton n hng i vi +, *, / s nguyn -> cho kt qu nguyn

October 13, 2012

C programming

Php chia hai ton hng nguyn cho kt qu l s nguyn (tng t php DIV trong Pascal). V d, 7/4 cho kt qu l 1, cn 7.0/4 hoc 7/4.0 hoc 7.0/4.0 cho kt qu l 1.75.

October 13, 2012

C programming

Vn

Nu c nhiu ton hng khc kiu nhau tham gia vo biu thc -> tnh ton ntn? Gii quyt: Chuyn kiu t ng

Kiu c tr nh -> c chuyn sang kiu c tr ln hn

Vd: float -> double

October 13, 2012

C programming

Quy cch i

signed char -> int unsigned char -> int short -> int unsigned short -> unsigned int -> long unsigned -> unsigned long int -> unsigned long -> unsigned long float -> double
October 13, 2012 C programming

V d

Vd: double a = 10; int b = 3; a/b?

October 13, 2012

C programming

TON T QUAN H
operator
> >= < <=
October 13, 2012

meaning
so snh ln hn. so snh ln hn hoc bng. so snh nh hn.

Ex
3>7 -> 0 a>b 7>=7 -> 1 a>=b 6<7 -> 1

so snh nh hn 10<=5 -> 0 hoc bng. a<=b


C programming

TON T QUAN H
== so snh bng nhau. so snh khng bng nhau. 7= =7 -> 1 7= = 6 -> 0 a= = b 7!=7 -> 0 a!=b

!=

October 13, 2012

C programming

V d
Vd: int a, b, c; char check; a=1; b=2; c=-3; check=(a>=4)*2+(b<3)-c;

October 13, 2012 C programming

LU == v = Vd: Th1: a=(b==5);

Th2: a=(b=5);
October 13, 2012 C programming

Cc php ton quan h c u tin thp hn so vi cc php ton s hc, nn biu thc i<n+1 c hiu l i<(n+1) .

October 13, 2012

C programming

TON T LOGIC
operand1 operand2 A 0 0 B 0 0 !A 1 1 Result A && B 0 0 A || B 0 1

0
0
October 13, 2012

0
0

0
0
C programming

0
1

1
1

u tin
not, and, or Vd: !x !0 ? !12 ? a=3; b=5; -> (a>5) && (b<3) ? 7<=3 || 8>=3 ?

October 13, 2012 C programming

Kt qu ca cc biu thc c cc ton t logic c th dng tnh ton? Hai php ton && v || c u tin thp hn so vi cc php ton quan h. Tt c cc php ton ny li c u tin thp hn php ph nh mt ngi.

October 13, 2012

C programming

LU

&& v || c thc hin t tri sang phi. Vd: tnh gi tr biu thc Rt ra kt lun g qua vic tnh ton bt trn -> S ti u ho ca C Vd: scanf(%d, &x);
if ((x>=10) && (x<=(y=99)) {}
October 13, 2012 C programming

(c>=A) && (c<=Z) || (c>=a) && (c<=z)



TON T TRN BIT


operator
~ & | ^ V nh phn HOC nh phn HOC LOI TR nh phn

meaning
o bit, t 0->1 v ngc li

<<, >>

Dch tri, dch phi

October 13, 2012

C programming

Bng hot ng
bit A 0 0 1 1 bit B 0 1 0 1
October 13, 2012

operation ~A 1 1 0 0 A&B 0 0 0 1
C programming

A|B 0 1 1 1

A^B 0 1 1 0

TON T TNG GIM


C php: ++ bin bin ++ -- bin bin -Bin c th thuc loi bt k hoc pointer Tin t hoc hu t
October 13, 2012 C programming

++ a; a++; a = a + 1; - Khc nhau : ny sinh khi php ton nm trong mt biu thc. - Tin t : . Thay i gi tr bin trc khi s dng . Tnh ton cc biu thc bao quanh - Hu t : . Tnh ton biu thc bao . Thay i gi tr bin.
October 13, 2012 C programming

i = 3 v j = 15

i = ++j; i = j++ i++; j = ++i + 5; j = i++ + 5;

October 13, 2012

C programming

Ch
a++ + b a + ++b Vd: Vit chng trnh tnh tng t 1 n n (s dng vng while) while(bt iu kin) { lnh; }

October 13, 2012

C programming

TON T GN

Gn n gin <bin> = <tr> Gn phc tp <bin> <op> = <tr> - <tr>: hng, bin, biu thc - op : * / % + << >> & ^ |
C programming

October 13, 2012

Vd: int a, b = 2; a=4; b*=a*3; Lu : Trong php gn n, nu 2 ton hng c cng kiu th ton hng bn phi s c gn vo ton hng bn tri. Nu khng cng kiu, trc khi gn ton hng bn phi s c chuyn theo kiu ca ton hng bn tri iu ny c th gy ra kq sai nu kiu ca ton hng bn tri thp hn kiu ca ton hng bn phi.
October 13, 2012 C programming

Lu

i vi php gn phc, chuyn kiu t ng trong khi thc hin vic tnh ton biu thc v vic chuyn kiu ca php gn n gin. Vd: n=n*(x+5)+n*(a+8); n*=? int a=4, b=1; b+=(a=2*b)+(a*=b); /*b=?*/
C programming

October 13, 2012

TON T PHY-BIU THC PHY

C php
bt1, btkt_qu; Kt qu ca bt phy l gi tr v kiu ca kq ca bt bn phi Khi c nhiu biu thc c vit lin tip nhau v chng c cch nhau bi du phy th my s thc hin ln lt cc biu thc theo th t t tri sang phi. Vd: m=(a=2, t=a+3); /*a=?, t=?, m=?*/
October 13, 2012 C programming

TON T IU KIN BIU THC IU KIN


C php: condition? expr1: expr2 condition: l biu thc bt k Nu condition khc 0, kt qu s l kt qu ca expr1. Ngc li kt qu s l kt qu ca expr2. Vd: if (i>0) n=1; else n = 0;
October 13, 2012 C programming

n = (i>0)?1:0; cu lnh sau y xc nh gi tr ln nht trong ba s a, b, c: s = (a > b)?(a > c? a : c) : (b > c?b : c); Tn dng lm gn chng trnh

October 13, 2012

C programming

TON T SIZEOF

Cho kch thc ca mt bin hoc mt kiu d liu no . C php sizeof (bin) sizeof bin sizeof (kiu) Kt qu : gi tr nguyn ch kch thc (byte/char) ca kiu d liu hoc ca bin .
October 13, 2012 C programming

TON T SIZEOF

Vd: double f; sizeof (f) sizeof f sizeof (double) Cho kch thc ca bin f hoc ca mt s double ni chung, tr s l 8 (byte) S dng kt hp vi cc hm malloc(), hoc calloc() cp pht bin ng
C programming

October 13, 2012

Vd: int *p; p = (int*)malloc(10 * sizeof(int)); Kch thc ca 10 s int.

October 13, 2012

C programming

U TIN CA CC TON T
level 1 2 3 4 5 6 ( ) [] -> ! ~ ++ -- - + (type) * & sizeof * (nhn) / % + << >> < <= > >=
October 13, 2012 C programming

operator

combined order --------> <---------------> --------> --------> -------->

7 8 9 10 11 12 13 14 15

== & ^ | && ||

!=

--------> --------> --------> --------> --------> -------->

October 13, 2012

?: <-------= += -= *= /= %= <<= >>= <-------&= |= ^= , -------->


C programming

CU TRC TNG QUT CA MT CHNG TRNH C

Gm 2 phn Header Function Header: Lnh tin x l: include, define Khai bo hng, bin ngoi Prototype Function: nh ngha cc hm s dng trong ct, main().
October 13, 2012 C programming

P DNG

Nhp vo mt s, kim tra chn, l.

October 13, 2012

C programming

#include<stdio.h> #include<conio.h> int check(int n);/*0:chn, 1:l*/ main() { int n; printf(Input:); scanf(%d, &n); if (check(i)) printf(So da nhap la chan \n); else printf(So da nhap la le \n); getch(); }
October 13, 2012 C programming

int check(int n) { return (n%2==0)?0:1; }

October 13, 2012

C programming

BI TP
1. 2.

Nhp 3 s, tm s ln nht, nh nht Nhp 4 s, sx theo th t : a. T ln ti nh b. T nh ti ln c. Kt thc Mi chn thao tc (1..3)


C programming

October 13, 2012