1. Trang chủ
  2. » Giáo án - Bài giảng

Lập trinh C cơ bản

91 388 3
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 91
Dung lượng 672 KB

Nội dung

 !"# $%&&&&$'()*+,&' -. /0,&1"#2#$3$& &"4& !&"#.5&*%& #&#4&!&-  &6&1*5&*7/++8 95&:;)*1."#<*/!=/8 §1. C và C++ >-.-";?2&#&@.)*8+--&A*@B&  @#C&+-&8 >D&"E$F&&=2#@#C 0&A* #@.$4 C #5C #!#(&F@'* /."E$F& G2' 8 >D";?2&)*/*&H2& 89//$09/=I&&+ @*#&/J6&CK$ &@+@(&G 2!L/$0)*98 9&-.$&2I71*CK$.@*# &/J)*"E$F&8+@(&#M&-.$J @1"*@'&8 DN$FO@/$0&"*&(,&"P@(&C/$Q& #RS*&T!(&#MRT8U&@'"P@1"*8 $O #include<stdio.h> void main() { float a,b,c,p,s; printf(\n nhap a,b,c); scanf(%f%f&f,&a&b&c); p=(a+b+c)/2; s=sqrt(p*(p-a)*(p-b)*(p-c)); printf(\n dien tich=%0.2f,s); getch(); } U/$0&&9"P#(&#M"*O VOWX"1X"$*!* VOWX&X"$*!* .&/&=O >Y(C&ZZ >/[@*#OK$\8]!K$\8] §2. LẬP TRÌNH CẤU TRÚC VÀ LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG 2.1)Phương pháp lập trình cấu trúc >9?&N^:&#&89&*"*- [@.&-)F!89&-_'& -8 `2&2$&.;C2=!C-O "C @1*;C2"N#8`=-!# & F2$&/&8 DC*^$4C&4*#;C(&1*#!# F8 #&(&4 Zabac WdeZfd#&(&4g@*.& #58 h2&5&i5$4CR & &T!# ) F8 UC!FN)*!C^: @&5^:& # )F8&"P*&i# )F8 DN$FOD&'2R< T)*2$H#. "*-2Y .#<**8 9?&)*5-.^:&"*O >bE$F&[&";2<!.:**2$H. >e$;&[O T`*".'(. -_&/.! @*#"*O !$*"RTj T`$k$*$&.N2$'l&1*[.-'2!m! @*#"*O !$$k$*R mTj 9?&#O >n#&#0)*$*<$k$*R.: .:[T >9N2$o&Y. >U2$L&#&#0-$*<i&,!0N)*Y. -8 &;C#/!"*O $O #include<stdio.h> #include<conio.h> #include<math.h> float x[100],y[100]; float do_dai(int i,int j); { return sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2)); } void nhapsl(int n) { int i; for(i=1;i<=n;i++) { printf(“\n Nhap toa do cua diem thu %d”,i); scanf(“%f %f”,&x[i]&y[i]); } } void main() int n,i,j,imax,jmax; float d,dmax; printf(“\n So diem: “); scanf(“%d”,&n); nhapsl(n); dmax=do_dai(1,2); imax=1;jmax=2; for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) { d=do_dai(i,j); if(d>dmax) { dmax=d; imax=i; jmax=j; } } printf(“\n\n doan thang lon nhat co do dai bang:%0.2f,dmax); printf(\n\n di qua 2 diem co chi so la %d va %d ,imax,jmax); getch(); } 2.2)Phưong pháp lập trình hướng đối tượngO >p#C&)*L&&LR*""T8-.<L ";@&4*#=$4C!#8q&-.<L";? 2&5&R"T6&#*/!#&:R$T *Q&#!/RrT8h2L0&A* "*O *""9k s tt@*##=$4C tt@*##&: uj >#&:-.!R<$;&T/&Y/&RN*$LT =0&A*L85R#!T&:&;&'o 1G"*Op<$;&2&5/&0&A*L&$Q& =/=$&/L![$OYL/&:.vw&: 2LR<!N$F/$LT8 >bE$F&#=$4C&&:OD&:!# =$4C2%&_L!!&:/x<EN# =$4C /&*)*&:-1+#= $4C2%&_L8 >yLOb*@0&A*_L -.$&/Lx@*##@.L *Q&#&8hM&"P-#=$4C!# &:8c,&&:=:*/&.<#0&: ;C&8 >h2&L&&"P*&i#L1*C!L*8 >DCN @&&#L&&6 @ <$;&#L8 >9o@#CLH"*&'#@#C@#O9=$4 C &: '! ";-&&- ' z ";o*@ L"? L$J < &:&2 &:X >{.)*!C@L&&&<#0#L.( #;.)*#8hML*!#=)*;.!<$;& (#&:.<EN$4C8U!!C@&r<#o #2$& #!+)*#8 >#&(&4=|L&&Rb**@TvM=|#@# C!+L @(&M#@#C!+8 >&(&4* -g"E$F&#@#C!+Lq&!+8 >.*#@#C!o*/!+L&&*<g' #2$L1*[.89&#*&Y_;.$H .8#=)*L$H.*&iO Ty&/".)*$H T7<@.;7!%&L:*$H2 T7@.;7!%&::*$H&2 #&:=*!/=#*&i >U'22. >9N2$'l&1*[.8 }L&!@L&&8.C& LY/CZZ @!&9~8•()*C &Y0ZZ89&&*/2+LO#@*#  &-.!g@M&&3@*#L@"E $F&8 9#H?/$L"P&8 $O #include<stdio.h> #include<conio.h> #include<math.h> #include<alloc.h> class daydiem { public: int n; float *x,*y; float do_dai(int i, int j); { return sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2)); } void nhapsl(void); }; void daydiem::nhapsl(void) { int i; printf(“\n So diem N= “);scanf(“%d”,&n); x=(float*)malloc((n+1)*sizeof(float)); y=(float*)malloc((n+1)*sixeof(float)); for(i=1;i<=n;i++) { printf(“\n\n nhap toa do x,y cua diem thu %d: “,i); scanf(“%f %f”,&x[i],&y[i]); } } void main() { daydiem p; p.nhapsl(); int n,i,j,imax,jmax; float d,dmax; n=p.n; dmax=p.do_dai(1,2);imax=1;jmax=2; for(i=i;i<=n-1;i++) for(j=i+1;j<=n;j++) { d=p.do_dai(i,j); if(d>dmax) { dmax=d; imax=i; jmax=j; } printf(“\n Doan thang lon nhat co do dai bang: %0.2f,dmax); printf(\n di qua 2 diem co chi so la %d va %d,imax,jmax); getch();} €~8h2"?2&&)*"!L 9&F"P2"?2&)* &&' +C8 ~8_TD#$Q&&5O 9&!J-.!#$Q&&5&#$t•!•t&8# g!&5/+$Q&8U&*&Qg!& 5/_$Q&"**$&'g8 DN$FO< jtt@*#*;8 ~8[Tp*#'O 9&#C@*# &F2Y=@8}!( @!0N@*#!!0N"E$F&@#<** &@-@B&!C@."# &&8H@GF;.6&#g# C@*# &?@M&&L@# & -"E$F&8 DN$FO&2$H";i"G<:;B&$=-.! &"*O $O #include<stdio.h> #inclue<conio.h> #include<alloc.h> void main() { int n; printf(“\n so phan tu cua day N= “);scanf(“%d”,&n); float *x=(float*)malloc((n+1)*sixeof(float)); for( int i=1;i<=n;i++) { printf(“\n X[%d] = “),i);scanf(“%f”,x+1); } for(i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(x[i]>x[j]) { float tg=x[i]; x[i]=x[j]; x[j]=tg; } printf(“\n day so sau khi sap xep: “); for(i=1;i<=n;i++) printf(%0.2f,x[i]); getch(); } ~8~T9#Eg@.O 9&#E!"*O R@.T.: 9&!J-.!8U&*g!2#@#C "*O p/R.:T DN$F&N(&:O b‚[t_~t[XR_Tt !L2"&/$&oN -.!"*O $O #include<stdio.h> #include<conio.h> void main() { int n; printf(\n So phan tu cua day N= ); scanf(%d,&n); float s=0; for(int i=1;i<=n;i++) s+=float(i+1/float(i); printf(\n S= %0.2f,s); getch(); } ~8ƒT`6&-@. /'*26&-@. *"E$F&o@#"YL2@*#@?&# &#08b*2"!N$FO `6&&/O "*<"„‚_•••j *…*<"„†j 56&O $r" s < j *j u}‡Vhj "}‡Vh$‚s~[• [ƒ• _ˆuj &$L'#$&6&-@.8&'2 56&R@.}‡VhT(&4*i'!G&8.. 0/i'8 $O #include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> typedef struct { int x,y; int mau; }DIEM; void main() { int mh=0,mode=0; inỉtgaph(&mh,&mode,); int loi=graphresult(); if(loi) { printf(\n Loi do hoa: %s,grapherrormsg(loi)); getch();exit(); } const DIEM gmh= {getmaxx()/2,getmaxxy()/2,WHITE}; putpixel(gmh.x,gmh.y,gmh.mau); getch(); closegraph(); } 5|O *8-.$%&#.&#&#0#6&"-@.R&&/ $%&#&*<<!&*<T8 8hC6*^&#0)*6&-@.+0#M@/$0 &8DN$F&&*!&&8<‚[••j@$ &"P(&#M"*O *$r*"m ~8ˆ#@.*! 9&26&@N;&/$---@NL[ !N$F& O"„rRXaXT‚"„rRT‚[8 Q&26&@N;<&#0@.*!-@NL_8 U!&O"„rRXaXT‚"„rR*T‚_ ~8‰c0v#=E&[+ 9&[8•@(&g$&gŠ.0*v#=E&[ +8D!@2*;R$&"*rT*1*2& &*"*-L&##=E&8 9&9~8•g0*v#=E&;[+8}--.$& "*r.;#=E)*&8 &"*"P'+8&2*; <!<#0=E-&#0L8 $O #include<stdio.h> #include<conio.h> void main() { float a[20][20], smax; int m,n,i,j,imax,jmax; clrscr(); puts(“\n Cho biet so hang va so cot cua ma tran: “); scanf(“%d %d”,&m,&n); for(i=1;i<=m;i++) for(j=1;j<=n;j++) { printf(“\n a[%d][%d]”,i,j); scanf(%f,&a[i][j]); //lấy địa chỉ phần tử mảng thực 2 //chiều } smax=a[1][1];imax=1;jmax=1; for(i=1;1<=m;i++) for(j=1;j<=n;j++) if(smax<a[i][j]) { smax=a[i][j]; imax=i; jmax=j; } puts(“\n\n Ma tran”); for(i=1;i<=m;i++) for(j=1;j<=n;j++) { if(j==1) puts(“”); printf(“%6.1f”,a[i][j]); } puts(“\n\n Phan tu max: “); printf(“\n\n co gia tri= %6.1f”,smax); printf(“\n Tai hang %d cot %d “,imax,jmax); getch(); } '"! &,)&2F*/ *=/ *+C.G8U&C!&)*@&,&/:5& *"PY*#7O '"* @(&"P‹. 5&*7*+ . …€ƒ8D*& ƒ8_T##E!&:< .$4C*!$4CoN &!J-.$&# r!"*rR&v&#&?#F/T8 U&*&Q$&##E<O \\.:\\X\\.:j .*&#0#.:*$%&##EO ]]]]X]]j .#&#0"R&/ ;ToN!&##8 .2$H@(&1#@N;!:*&R@.*T-.$&& :8&"*O 8&R Tj 5|_O9#E]]"P.'@N;.$Q&XŒX&2C @N;- .(&:8&8.@GF'&/=$&&: 8&.71*_@N;.$Q&"*O 8*&R_Tj 5|[O."E$F&##E!#&:-/=@*#C /+OK$\"*8] +5!0O!L*@(&=1*L#Y)* !@$0 &$0H;2&*-'&@&YC@*# &"*"P*!C"E$F&#(&F!*L)*._ $*"#N"8}4C)*MN"&i/ #.# N #8b*- $*"#N":;&)*^&.8 $O #include<iostream.h> #include<conio.h> void main() { struct { char ht[25]; float t,l,h,td; }ts[50],tg; int n,i,j; clrscr(); cout<<” so thi sinh: “; cin>>n; for”(i=1;i<=n;i++) { cout<<”\n\n thi sinh thu “<<i; cout<<”\n\n ho ten: “; cin.ignore(1); cin.get(ts[i].ht,25); cout<<”\n\n diem toan ,li, hoa”; cin>>ts[i].t>>ts[i].l>>ts[i].h; ts[i].td=ts[i].t+ts[i].l+ts[i].h; } for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) if(ts[i].td<ts[j].td) { tg=ts[i]; ts[i]=ts[j]; ts[j]=tg; } cout<<\n\n Danh sach thi sinh sau khi sap xep; for(i=1;i<=n;i++) { cout<<\n\n ho ten<<ts[i].ht; cout<<\n\n Tong diem: <<ts[i].td; } getch(); } ƒ8[80$'&@* .10";Rr* $T*5&4""*$  *"E$F&i&,#"*O ""r*&"RNOO"•Tjtty,C"• ""RTj #=Y&#E<"*O \\""r*&"R"OO"•T\\""RTj C/"P-C;!L##E<@&Y 2C0$'&L8 .102$2&.•!0N&#0R&/ ; MT& ##E< *$& bR•T `=Y&#E<!-v-C;_&#0&= 8#&#0*"P-22&•8U!CO \\"R~T\\XayX\\X}X bP*ˆ@N;O2$#!ƒ4#a y !}8 h"E$F&#/=**CK$"*O K$\*8] 9?'&/*$*"#N"*"P@(&l&28.@G F+=!''&"*O $O cout<<\n Danh sach thi sinh sau khi sap xep; cout<<setiosflags(ios::showpoint)<<setprecision(1); for(i=1;i<=n;i++) { cout<<\n Ho ten: <<setw(25)<<ts[i].ht; cout<<\n Tong diem: <<setw(5)<<ts[i].td; } getch(); &$L2'@#!+!C"E$F&##E <! #0$'&&8&_*;<8b*-* $L$'&&!2=EL8 $O #include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { float a[20][20],smax; int m,n,I,j,imax,jmax; clrscr(); cout<<”\n cho biet so hang va so cot cua ma tran: “; cin>>m>>n; for(i=1;i<=m;i++) for(j=1;j<=n;j++) { cout<<”a[“<<i<<”,”<<j<<”]=”; cin>>a[i][j]; } smax=a[1][1]; imax=1; jmax=1; for(i=1;i<=m;i++) for(j=1;j<=n;j++) if(smax<a[i][j]) { smax=a[i][j]; imax=i; jmax=j; } } cout<<”\n\n ma tran”; for(i=1;i<=m;i++) for(j=1;j<=n;j++) { if(j==1) cout<<’\n’; cout<<setw(6)<<a[i][j]; } cout<<”\n\n”<<” phan tu max: “<<’\n’; cout<<”co gia tri= “<<setw(6)<<smax;; cout<<”\n tai hang “<<imax<<”cot “<<jamx; getch”(); [...]... theo: §5 .C u tr c, hợp và kiểu liệt kê 5.1 Tên sau từ khoá struct đư c xem như tên kiểu c u tr c Trong C+ + một kiểu c u tr c cũng đư c định nghĩa như C theo mẫu: struct tên_kiểu_ct { // khai báo c c thành phần c a c u tr c }; Sau đó c thể khai báo c c biến,mảng c u tr c trong c dung như sau: struct Ten_kieu_ct danh sách c c biến,mảng c u tr c; Như vậy trong C, tên viết sau từ khoá struct chưa phải... đư c Vâng hôm nay em xin gửi đến mọi người bài tiếp theo c a chúng ta §2 TRUYỀN GIÁ TRỊ CHO HÀM THEO THAM CHIẾU 2.1 Hàm trong C Trong C chỉ c một c ch truyền dữ liệu cho hàm theo giá trị: + C p phát vùng nhớ cho c c đối + Gán giá trị c c tham số trong lời gọi hàm cho c c đối sau đó hàm làm vi c trên vùng nhớ c a c c đối chứ không lien quan gì đến c c tham số Như vậy chương trình sẽ tạo ra c c bản. .. sao (c c đối) c a c c tham số và hàm sẽ thao t c trên c c bản sao này, chứ không làm vi c tr c tiếp với c c tham số Phương pháp này c 2 như c điểm chính: Tốn kém về thời gian và bộ nhớ vì phải tạo ra c c bản sao Không thao t c tr c tiếp trên tham số, vì vậy không làm thay đổi giá trị c c tham số 2.2 Truyền giá trị hàm theo tham chiếu Trong C+ + cung c p them c ch truyền dữ liệu cho hàm theo tham chiếu... main() { clrscr(); cout . a,b ,c, p,s; printf( nhap a,b ,c ); scanf(%f%f&f,&a&b& ;c) ; p=(a+b +c) /2; s=sqrt(p*(p-a)*(p-b)*(p -c) ); printf( dien tich=%0.2f,s); getch();. 9#H?/$L"P&8 $O #include<stdio.h> #include<conio.h> #include<math.h> #include<alloc.h> class daydiem { public: int n; float *x,*y;

Ngày đăng: 28/06/2013, 01:25

TỪ KHÓA LIÊN QUAN

w