找回密码
 注册
查看: 439|回复: 1
打印 上一主题 下一主题

C语言经典算法50-67

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2017-10-9 16:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
【程序50
6 b; S( e9 `3 B& G) R4 Q! u& U题目:#include 的应用练习 5 o$ p) e6 q; W
1.程序分析: + Y# j; r  b7 u# I! S9 V1 P
2.程序源代码: 4 h. \4 G) g3 b+ w9 o5 f- w
test.h 文件如下: 6 g: d' b6 |0 C' q3 K9 g
#define LAG > . j' ]; M9 H: R1 a  _6 i; M7 o, G
#define SMA < . J( y$ B4 j3 m4 h
#define EQ ==
: C" ?, h3 q0 d/ ~#include "test.h" /*一个新文件50.c,包含test.h*/
  @8 }9 w9 N, h" n! `& U5 `#include "stdio.h"
9 o) M4 r  {! U2 l  ivoid main()
6 y' x) N' G: [% n{ int i=10;
. ~# n; Y1 V: S! ?  pint j=20;
  F2 p/ I! B% b5 }3 r! uif(i LAG j) ( B# a) }" N- s3 [+ y
printf("\40: %d larger than %d \n",i,j);
! X" z+ x; G9 `. B5 T5 `else if(i EQ j) . u( m" Y& M9 d# s4 I5 D3 f
printf("\40: %d equal to %d \n",i,j);
4 y, G+ t( a; ^4 m3 {+ y$ }& \" b- s# {else if(i SMA j)
# l1 z5 m# P& U# Q& C  y& F1 `7 o" v9 X# Zprintf("\40:%d smaller than %d \n",i,j);
: {3 E  D) W9 y5 N1 ~, g( m! y$ belse 4 ]" D+ U$ D" N+ s8 Z( i7 g
printf("\40: No such value.\n"); ; Z7 a4 G$ o" {' r' \
} - M7 q3 ~* b" u* f, Q: t
【程序51( f& k- J& `+ O7 h' C
题目:学习使用按位与 &
' q4 g% q: p1 M) _" L1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
6 X7 ^/ O/ A; s8 k( x2.程序源代码: & k2 U' F2 k0 y& {
#include "stdio.h"
2 D/ E, q! f7 ]! q2 p  Bmain() , x  M$ S) C& v  M
{ : k% s: K* F9 m0 ]: E* E4 @. E
int a,b; ) \. G5 _2 m) a  q
a=077;   c- I, ]+ z! L1 C+ ]# |; B
b=a&3;
. V/ b" e  E. Nprintf("\40: The a & b(decimal) is %d \n",b); ( U7 Z% z, u* w& P
b&=7; ( T5 K6 z: O7 Q* u
printf("\40: The a & b(decimal) is %d \n",b);
! r/ a5 `8 v+ b$ ~5 R) v5 }% ?} + B  d8 y1 |' e8 v! g* H
==============================================================
) f' B: m; S1 ?1 I5 g; |【程序52
$ e0 i. E2 @; {3 J$ b7 D题目:学习使用按位或 | 3 N7 m- u) j! B
1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1
7 N; D( p/ x8 y2.程序源代码: 3 H8 F2 E" c' L, c8 y8 `
#include "stdio.h"
, c; a& H0 u0 f8 r8 O& z9 D6 Emain()
6 K) H& L7 _+ @3 n/ M  ^{
7 p; f9 ~+ m$ d9 f8 E( C: ]( qint a,b;
! O. W' ~6 ~! e0 x' ga=077;
1 F$ j/ O9 Y: K) xb=a|3; % C; C% C& w; I& Q& V% B9 \
printf("\40: The a & b(decimal) is %d \n",b); 9 N9 N* q* B# }6 ?& N+ _% x. J- c
b|=7; + J: |& I$ Q# o) {; ^
printf("\40: The a & b(decimal) is %d \n",b);
4 U7 S- s8 R# h$ g4 S# o( b- A# H} 9 q1 ~+ \+ H7 i' B
============================================================== + O3 N3 i) I! q5 {9 q
【程序53嵌入式信盈达企鹅要妖气呜呜吧久零纪要 6 `( i0 [' X' g' k5 A
题目:学习使用按位异或 ^
  k+ Z/ c0 ?9 t- y8 W1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0 . C2 L5 V2 }, `  t6 h, V
2.程序源代码: 1 j8 x" `  t3 U; L& W; }3 q! q
#include "stdio.h"
' A9 z$ N& Y- }* H% P0 vmain()
8 T+ u  k2 v! d' p{ 9 _: ?# S1 K; o
int a,b; & Z7 u/ P. q2 q7 U+ l+ E. ?5 U! p
a=077;
+ Z0 z7 c! s4 w' ab=a^3;
- d) d3 O( W1 _$ Gprintf("\40: The a & b(decimal) is %d \n",b);
/ n( M0 W$ a9 {/ r6 S( D% |; db^=7; " C5 N9 U1 a% R2 z. ^6 v
printf("\40: The a & b(decimal) is %d \n",b); 6 X" L* S) H; V5 W# H
} ( [( Z" u4 L. o
============================================================== . s+ S7 n$ R. _& J) J
【程序54! A/ r6 H: D2 _& u
题目:取一个整数a从右端开始的47位。
7 F1 u& I4 J' G7 z  d5 l程序分析:可以这样考虑:
6 ~4 ]" e0 L* ~1 t7 m+ u3 X! P(1)先使a右移4位。 7 I( I+ t; ]6 b2 E5 W
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
9 M+ R& }( f1 L* |2 o/ r(3)将上面二者进行&运算。
1 _2 R% l0 T4 P9 _2 W6 W2.程序源代码:
+ v  {! \6 K  l$ P1 ?. Emain() 7 F/ F0 z* T0 V" F
{ 9 W7 Z/ h2 Y/ y$ t
unsigned a,b,c,d;
& I. X# |; o, L, E  h3 Q& |! oscanf("%o",&a); ! L4 S; ]) J: V* j+ G
b=a>>4;
# D) G( N/ [4 c  L1 M- n( F# s; Vc=~(~0<<4); 0 ^6 o' |# p" A. X  H
d=b&c; % Q7 t$ t3 P/ w9 K" W
printf("%o\n%o\n",a,d); " e. B; V- p4 t
} . q' d9 s& \: d6 G4 M0 [! D
==============================================================
. B" N0 K- M  @; F# s& T4 b【程序55
) n) I8 u  w% i% w: t题目:学习使用按位取反~
& \: W# ?6 I! e5 k1.程序分析:~0=1; ~1=0; . m6 {( l( s3 a
2.程序源代码: & w0 y5 D' ?* w/ F
#include "stdio.h" 9 f; `" C) w4 m3 ~" I/ d
main()
* W. |% W& u+ ]) b3 B0 Y/ S5 U{ % a1 z1 t! k" l
int a,b; $ H' @1 K6 b  |+ s% }" o$ X3 B
a=234;
! E( T( Y/ y, I1 r- P) Pb=~a;
8 j9 H6 D2 C) ]; N- j/ z. qprintf("\40: The a's 1 complement(decimal) is %d \n",b);
) d6 x' ^7 a0 J5 Ya=~a;
5 e3 K* A9 A- o! i/ Xprintf("\40: The a's 1 complement(hexidecimal) is %x \n",a); 1 L  K. s; x- z
} + K6 O( T% t7 h7 z
==============================================================
! k3 m4 t0 ~% ]4 B【程序56
3 w* W* N9 G8 `) U5 P$ s7 T题目:画图,学用circle画圆形。 5 D6 Z: K* {+ J( e) Z+ o* S
1.程序分析: 7 P  E5 p" u( b" w: F4 a6 i3 _
2.程序源代码: " V; s% W1 x  D3 u7 F7 I1 `
/*circle*/ ; {! C) ]" Y7 Y' g" X
#include "graphics.h"
9 X- T- ]7 ?8 p" Ymain() % D( [' D7 R' O$ G% \
{int driver,mode,i;
% y2 ?0 T; F1 ?( ofloat j=1,k=1;
# b3 k. H* Q% X5 m% |driver=VGA;mode=VGAHI;
5 [/ i7 R+ R7 X( ?initgraph(&driver,&mode,""); ! ]/ g0 ~- e# j7 q& U5 e6 p
setbkcolor(YELLOW); 2 k& y3 x5 A3 W: F
for(i=0;i<=25;i++)
1 l: V' h/ |- d( z6 F8 w8 N" i{
+ G: f/ v( _% l6 \, Qsetcolor(8);
# }' ~' J' ^) Kcircle(310,250,k); 3 P7 A' _) A6 \$ W+ |$ G  y! _
k=k+j; * r* `9 O  e' h- I  T$ J4 \
j=j+0.3;
( y8 f8 m" H  R} # n' z/ m+ v1 I* y1 T
}
: S$ `( ?6 o5 k, Z2 k6 D==============================================================
' d: e9 ^- c0 o# M# S+ m【程序57
1 s. `+ o+ z/ x! K: S9 D题目:画图,学用line画直线。
, s. U3 v) ]) c! b2 L1.程序分析:
. [1 e/ i# N  T% r% p2.程序源代码: . ^1 R) k! B# \1 G
#include "graphics.h"
9 d+ r; ^! b# Y( V/ A) i) Kmain()
. a; l, A- W# T, D0 Y) Q7 Q{int driver,mode,i;
! g0 N* f! Y: [' ^8 Ifloat x0,y0,y1,x1; ' U3 j. {6 p+ Q( B2 @+ E
float j=12,k; 1 R' x3 I( t0 y/ g  t
driver=VGA;mode=VGAHI;
, s* i; S+ U$ Q- i7 G# Iinitgraph(&driver,&mode,""); % w* z$ \! v+ D# L- F6 [( i
setbkcolor(GREEN); + i( J1 O" Q3 y2 p5 j4 ~2 t4 d
x0=263;y0=263;y1=275;x1=275; 3 ]" B; H/ s, j: @$ r2 n* k* c
for(i=0;i<=18;i++) 4 T. L! \! ~# B! q
{
, z- f, y; h/ X% e* c& j7 {setcolor(5);
% B) N$ t- K4 B+ Cline(x0,y0,x0,y1);
# Z' C- G/ @, |0 P( E: Fx0=x0-5; - ~+ m* B7 ^- O, c
y0=y0-5; 5 G9 `' e( ^2 |" @" b
x1=x1+5;   M# Z. z7 S, B5 k2 H. `
y1=y1+5; ! H6 E  U9 g7 Y3 v- a$ S
j=j+10;
/ v% d" u7 X$ ?# z7 }}
* a+ s. H, L+ E9 h( |. J4 Cx0=263;y1=275;y0=263; 1 P. i3 ]! _& u/ {* b; R6 R# p
for(i=0;i<=20;i++)
8 I1 {3 i# f$ ^{
, I$ T1 Z9 k( Q/ |setcolor(5);   Z2 Z; s! j' B3 z8 e8 x
line(x0,y0,x0,y1);
& v( t. z  z2 M) }) lx0=x0+5; 5 Q* h& V: ?' ?& V
y0=y0+5;
% ]4 |- r! Z- by1=y1-5; 9 X. x! }! ~( L* b; K3 P# s
}
4 X0 }3 x% g$ Y+ ^+ i1 C}
' I$ u6 o+ y, X; g==============================================================
6 y/ t# V/ V* z) a8 o【程序58
3 e4 S4 M2 z: Q+ ^- e. ?题目:画图,学用rectangle画方形。 1 y% ^* p" F4 Y+ D9 [
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 " H0 D& n0 @1 r( V/ c7 G$ a
2.程序源代码:
0 m$ V- p$ r  y# q#include "graphics.h" 0 E  G  Y& t% b. @/ x+ o
main()
0 v3 n0 v) V6 j3 w- u{int x0,y0,y1,x1,driver,mode,i;
2 B  W+ P/ s/ E9 j! J' ^( u% zdriver=VGA;mode=VGAHI;
4 v: g8 z+ v+ G1 |& Z! y6 C3 `) N  linitgraph(&driver,&mode,"");
* B" x5 x( K7 ]: t0 N/ Asetbkcolor(YELLOW); & b% G4 W( k9 a
x0=263;y0=263;y1=275;x1=275; - x/ E5 P& ]7 w, p1 p/ ?
for(i=0;i<=18;i++)
+ Z4 Y" D8 @* B5 c' T% o; r{ 5 f5 s4 v0 P" Z6 n* B* A
setcolor(1);
8 h* |1 y& `- y1 u( J7 Irectangle(x0,y0,x1,y1); 0 c0 Z2 f8 ]+ r& j6 [
x0=x0-5; " x8 ]+ e  c" r5 j4 M1 b1 T
y0=y0-5;
% E5 p" u/ W$ m5 C1 F& Hx1=x1+5; 9 l- U: _1 s5 C/ ]
y1=y1+5;
. V* R  ?+ s9 S8 C  |; E) h. X}
, S, b5 `) |- ^9 u6 v7 I# ^settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
& Q8 |7 Z; t+ ~8 gouttextxy(150,40,"How beautiful it is!");
/ g! y, K5 n* Wline(130,60,480,60); 7 y* C( V; i  z
setcolor(2);
- R- F; |( a" X$ B0 Y$ K( O. G1 qcircle(269,269,137);
3 |# H$ L& t2 w, b9 S9 r2 b4 u}
" t1 Y( Q" j4 q* F/ H==============================================================
& a8 U: Q6 I# j1 w0 l9 M3 M【程序598 _8 `, i) b1 I
题目:画图,综合例子。 0 z- b5 ]5 K  s! }
1.程序分析:
- [) [. |) x/ R7 [: |+ a% B2.程序源代码:
8 Q. }: D0 U! E1 I) I  {# define PAI 3.1415926 " V/ g# z" u; A( o" b9 ^  X6 e
# define B 0.809
+ }2 \; e! P) f" m; w& Z  @5 M# u# include "graphics.h" " o% I/ [8 }- ?, O
#include "math.h" # K9 P3 O6 w- |
main() $ J* {4 ^. b: ^
{
/ A. Y" J) f6 O5 |  Yint i,j,k,x0,y0,x,y,driver,mode;
; _! M+ d$ j. a7 Y- efloat a; & |0 ?8 C+ j" y9 |% {# l2 ^$ k
driver=CGA;mode=CGAC0;
7 K& H' K8 {& F6 j" iinitgraph(&driver,&mode,""); ( t- u, c) a- g- B
setcolor(3); 2 O$ K* Q8 k: A8 `1 A+ i
setbkcolor(GREEN);
8 c, i) X! A$ R1 i) c7 t- A) \x0=150;y0=100; ! o$ R# \* L9 c% f: y
circle(x0,y0,10); 3 ?2 Z' p- O4 P
circle(x0,y0,20);
( l* x+ n$ a9 q! C0 s! pcircle(x0,y0,50);
& p/ w8 x1 _+ A2 kfor(i=0;i<16;i++)
/ \; _  @. a8 {{ : g& E  J5 h2 x+ \
a=(2*PAI/16)*i;
- P1 V( ^+ M9 }+ D) A4 p/ Jx=ceil(x0+48*cos(a)); 7 w/ P8 d1 t: X% N4 f
y=ceil(y0+48*sin(a)*B);
" h& {5 M* j$ c( N- esetcolor(2); line(x0,y0,x,y);}
. |3 c7 e/ G* q; L$ p, F+ |/ Asetcolor(3);circle(x0,y0,60);
) r4 y6 Z. a2 B5 x/* Make 0 time normal size letters */
4 E- c9 B; i! r% M3 }- V. rsettextstyle(DEFAULT_FONT,HORIZ_DIR,0);
9 W5 K9 J1 y) F5 U. @* ^outtextxy(10,170,"press a key");
9 f4 a, N: O$ \4 k& ?$ e* Lgetch(); $ T& c8 Z9 S. Z, P8 R2 d7 ~
setfillstyle(HATCH_FILL,YELLOW);
3 C# t4 N/ W+ f" ~; Y4 rfloodfill(202,100,WHITE); $ ^3 d4 e& ~  q) q! g" m, i, w
getch(); , `5 k& W5 {: ]- t3 k0 Z
for(k=0;k<=500;k++) 0 |, u3 w9 X1 c* g7 t
{ , ^; U/ t% ]9 k8 T6 m. \" M$ f
setcolor(3); 6 q# N( D" C( P+ v8 A
for(i=0;i<=16;i++)
, t5 I5 N2 h$ x+ Q% Y. ?2 F$ W{ 9 l  p1 Y# k( h/ f
a=(2*PAI/16)*i+(2*PAI/180)*k; ' ?# w5 v4 f7 V0 J& K" A$ G
x=ceil(x0+48*cos(a));
( [' m- f' ?% I- u! }y=ceil(y0+48+sin(a)*B);
5 E. w+ m5 y+ i7 N; x+ K1 D' {& ^setcolor(2); line(x0,y0,x,y);
5 ^, _# h& e, C6 m} 5 u3 ~! |/ \7 j- r1 p6 Q
for(j=1;j<=50;j++) + O: t6 N$ ]) x2 }
{
# O  U2 i. r; c5 j& M; Qa=(2*PAI/16)*i+(2*PAI/180)*k-1;
# U: Y" k  k! @- v" B- \x=ceil(x0+48*cos(a));
! k  |/ _9 o* Z' V1 ]* S  dy=ceil(y0+48*sin(a)*B); 2 e; L5 c  I' V6 Y, R
line(x0,y0,x,y); 7 D& y# q; L" O' _# |) a. ~
}
( j, ?; L# N' l2 m' [$ k}
6 t2 V. x" M3 M: v0 Q8 {& n+ erestorecrtmode(); 9 R* ^; w- S8 J7 l: R/ G& B, J) }
}
1 F9 H4 _/ Q5 \5 f: H9 S% S* F& P==============================================================
9 [1 v5 r7 k# B# I" w: n6 l【程序60
$ O+ k; e; o7 V  |1 `- I: Z9 x题目:画图,综合例子。 ; k" A+ f4 P2 h  h. S
1.程序分析:
' ]) K; F$ L6 p6 G1 S2.程序源代码: 2 m; X  D1 U) o6 d  m' h7 K) W
#include "graphics.h" - `0 O+ d; ^$ R4 R* S0 C
#define LEFT 0 / B( d7 l# I5 |% K# x9 ]6 o! ?
#define TOP 0
" D8 u/ U* ]8 F/ G% {$ x6 r3 q5 ]#define RIGHT 639 " P! _; ^5 L3 D0 g# n4 N3 b7 G
#define BOTTOM 479
, W' y% F, J" e* R5 p3 S* q$ Y#define LINES 400 , `% @9 F5 e% `. I( M2 R9 {
#define MAXCOLOR 15
. K: b$ h# m; s5 ]0 W- z8 J9 qmain()
& l' c( l6 c& J; f7 |- F+ S{
  D' v, V( x: L. rint driver,mode,error;
$ }! o! g6 T4 J7 a2 Bint x1,y1;
8 m& {5 B+ O( h: f' @int x2,y2;
, v: v: x7 o) ^int dx1,dy1,dx2,dy2,i=1;
" l0 U  w9 C' O, f- R$ nint count=0;
% T# y  q2 d1 N- J  f) o" mint color=0;
( |1 e& C3 K) H& |; `driver=VGA; : r4 \# p9 w5 V' v
mode=VGAHI; " z. b7 d& w. _  j7 g
initgraph(&driver,&mode,"");
5 W+ y) j/ u& Q- A5 e, _x1=x2=y1=y2=10; ! o* a* n$ u8 D# V$ }% l" C8 v2 V
dx1=dy1=2;   R% b# _" R- a; z
dx2=dy2=3;
& t; ^2 [( L0 Q9 V, q6 L$ A7 J! ]while(!kbhit()) . s- y( N" s3 K) R; @
{
$ i. q$ F  X* f. q/ S0 Oline(x1,y1,x2,y2);
  s; ]/ A* N0 U! Ux1+=dx1;y1+=dy1;
$ s1 s7 c4 T8 y! d. l3 X, Ex2+=dx2;y2+dy2; 2 N( O+ x4 n: ?' `; @8 r$ e/ S3 J5 t
if(x1<=LEFT||x1>=RIGHT) 3 K, V# u' H& C# Y. F& Q2 j- y3 C5 z
dx1=-dx1; * u, @% V& R( h2 T0 O% z0 X
if(y1<=TOP||y1>=BOTTOM)
( @7 L3 H) x; [2 U0 zdy1=-dy1; / E( {$ F3 I7 i: x$ t
if(x2<=LEFT||x2>=RIGHT)
* `- {8 `  p6 adx2=-dx2;
/ o1 H. Z- f. ^4 h- Gif(y2<=TOP||y2>=BOTTOM)
) j1 W. b) D- f  Tdy2=-dy2;
: k8 ^3 y8 t  {6 W! |4 Wif(++count>LINES) 0 g5 x# H& E+ a. O4 [" _5 K
{ , g2 J1 l( H! [$ l: g
setcolor(color);
. ^2 N8 \4 K. _7 }: b) fcolor=(color>=MAXCOLOR)?0:++color;
) I* r4 @& T8 w5 j- o: {} ( V" e- m* c6 x/ a. Q7 L
} % b0 p) O* b4 g5 G1 h
closegraph();
3 T' G7 }4 l5 w' z; `) O3 Q}
【程序61
- y, L& k) s* K8 T+ t题目:打印出杨辉三角形(要求打印出10行如下图) # P1 b" @2 w" e  v
1.程序分析:
+ X; t% _/ H7 I7 P; ^$ j1
1 ?! x- P: ~& w* G1 1 2 o; P9 B$ f- s. d+ |# [
1 2 1
* P5 b% o$ I/ r7 z# Q1 3 3 1 1 j7 `* S" J; g
1 4 6 4 1
5 O! p- ]7 h; F7 N1 5 10 10 5 1 # ^8 h; |7 L2 [/ R) d
2.程序源代码: ; A5 m$ s' |* K1 H* R( G+ I5 @% E5 o
main() 9 d+ c3 j; X4 N" M0 X& Q( z7 z/ C
{int i,j;
. T. Q& N: m% Z9 W3 Z. Jint a[10][10]; & T( ?- ?, N% a; c; ~$ h. Y0 `* C
printf("\n");
. t3 e; i# P& W# L- dfor(i=0;i<10;i++)
( M* w" j3 s9 j  g, b0 s, k{a[0]=1;
7 A/ X2 I. y3 ~, q6 j. @$ S" e' Wa=1;}
, h* U: I# Q. g; J8 Q! ?& g: Rfor(i=2;i<10;i++)
* [" I) v7 O9 c' cfor(j=1;j a[j]=a[i-1][j-1]+a[i-1][j];
' [2 u, p0 m' D# u* wfor(i=0;i<10;i++)
5 v) W' P9 Y* v" T) ?0 z{for(j=0;j<=i;j++)
% e# i# \/ X) u, U* S) Rprintf("]",a[j]); ( [! \# X8 C3 g' @7 ^
printf("\n"); ( C' u. b& |' P3 L
} 0 m6 u3 ~2 T5 s+ }2 \+ m
}
3 B( {: p9 O* B5 ]1 v4 Q/ ^==============================================================
* H- w) J2 N4 e0 _【程序623 f( B7 c# u  ^/ h! \
题目:学习putpixel画点。
0 _! s9 }+ I. B1.程序分析:
( \. P6 B' i  T/ J9 C) _8 r2.程序源代码: ; I* k9 a6 i) u
#include "stdio.h" $ [' z& w0 U/ R0 m# `
#include "graphics.h" % {2 H9 h: y" P/ q/ Z% b  O
main() ! j! I6 u# |! M
{ 8 Q2 _  b2 {0 w; Y2 e
int i,j,driver=VGA,mode=VGAHI;
. G( Q. ~1 m* m/ {, Zinitgraph(&driver,&mode,"");   t( V* g/ c/ ~5 N( I. _1 Y
setbkcolor(YELLOW);
) G2 t: S  o2 @- @for(i=50;i<=230;i+=20)
, M: O3 \1 @2 u2 a0 y5 M& ]for(j=50;j<=230;j++)
# F/ N1 b7 d* U! S, }. ?putpixel(i,j,1);
* c  C0 B% s1 B/ }3 w8 Afor(j=50;j<=230;j+=20) . l, h& W9 c3 e, O
for(i=50;i<=230;i++) 6 p7 M9 P- S5 n; @3 {# a4 A! B
putpixel(i,j,1);
* Q" [5 z+ u& E- Z}
0 F8 d( O* |4 _; d8 J==============================================================
; e' B7 j  |8 X2 \【程序63
3 B( h& A- A' J5 E5 [题目:画椭圆ellipse
0 e+ O2 P1 a" M# Z& Z7 x/ N1.程序分析:
4 l: |! B# H$ C: p1 W6 k) e! Z2.程序源代码:
! V- h& c' p- A( t7 q- k#include "stdio.h"
. c8 r6 @7 a" v$ Y' [( I$ x( A5 D: r#include "graphics.h"
6 j7 _* @  B% a% D4 O#include "conio.h"
, x( ]1 X: L9 s5 |( E0 h/ t) ~main()
4 `( Q6 A% X. Y/ ^5 n$ t{ ! u9 h6 l  B% V
int x=360,y=160,driver=VGA,mode=VGAHI; ( R/ t" |5 d( b, W! N
int num=20,i; : C3 x# b) d. E) d  p4 J8 B9 P
int top,bottom;
4 x0 b( s9 z2 |* q6 ~( \* u) Einitgraph(&driver,&mode,""); ) g" T4 W; G2 x$ ^: W
top=y-30;
' c& M0 |! ]* K' Tbottom=y-30;
; i5 G7 _3 F$ K* l! b/ A; zfor(i=0;i{ , V+ ?; r5 K3 A
ellipse(250,250,0,360,top,bottom); , D3 B6 X/ w' s% z
top-=5;
. Y1 G- w6 N. O6 ^9 abottom+=5;
* |6 \% N* w* r: F- ?7 w. k}
  o, X4 C' C1 H$ [/ ngetch(); : W# f, c7 J3 U2 p; G
} ) \' h2 C1 c- u2 _( K; M6 @8 b6 ]
==============================================================
+ L2 R% |/ {* p& S% [【程序64' d) ?$ K  d6 E1 ]! v8 |/ j; G
题目:利用ellipse and rectangle 画图。 + F% A3 r  p2 i- {/ Q( s5 P) t
1.程序分析: 7 i; a' x2 O5 Z( J) f: X
2.程序源代码:
( I& N8 |: H( V5 C& k3 T9 S' o. Z#include "stdio.h"
1 Y  h) Q' H* K  z3 H! K#include "graphics.h" , w7 Q$ K: F- ~7 A- Y  i
#include "conio.h" & X; x" s2 _2 s2 f; i
main()
0 t; [1 N3 G) Y/ P& w{
+ n% g1 Q8 {$ Z8 |5 n3 {( @int driver=VGA,mode=VGAHI;
9 ]: m+ R% b) Iint i,num=15,top=50;
' g8 `: k9 @* T* R2 I& Hint left=20,right=50; , U8 }: f; r  \8 n& _# N+ Z
initgraph(&driver,&mode,""); ; r- c* i  T: D# n7 E4 d
for(i=0;i{
/ P1 `. k$ v1 Yellipse(250,250,0,360,right,left); % b% o9 x6 h. I
ellipse(250,250,0,360,20,top); " p% t$ I, M; Y: p
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2)); 6 v( ^4 k4 O) |' F
right+=5; 1 `& N: c: ?. e
left+=5; ! q2 P8 L6 H3 C: Y
top+=10;
9 w% p& ~" {, d}
0 i/ p; f% k- K+ l  B5 G; Zgetch();
3 [: s4 f, w1 T9 S: {9 {}
+ y8 R% `7 K1 G( Y1 U' e; d============================================================== $ V0 J% ]3 v7 U3 W. |
【程序65/ H( v" }5 ^/ S; s: K% N, T
题目:一个最优美的图案。 + E! b- p, J) b' M2 c& s! O
1.程序分析: + U* G7 h1 q# q8 I. D" G; C# G+ U
2.程序源代码:
) f3 d! `: R% a! m. R7 f#include "graphics.h"
4 Z0 E! P% ^, r6 z7 k#include "math.h" ( v6 l6 C+ M/ F, y
#include "dos.h"
0 o6 X) Z! N- D, j5 |9 {0 T% y#include "conio.h"
2 s. U4 J5 T0 k" B4 |% g5 `#include "stdlib.h" 6 ]. o6 W+ F4 j* k/ z
#include "stdio.h" # i4 R. }. M) d, q7 V0 O
#include "stdarg.h"
9 N$ b6 v' N2 ?/ b, d% b3 t) Y4 o#define MAXPTS 15   g: }: q+ F. H
#define PI 3.1415926 ) A) L4 Y+ T/ n, r; ]  Z+ U
struct PTS {
# M3 _4 H1 h- f2 J3 kint x,y;
' \) R4 A6 r3 }# C};
  f' f0 [/ Q+ d/ W  ]double AspectRatio=0.85; ! P- G- p" E- @4 D% t4 v
void LineToDemo(void) " g; g, ~1 p9 d- H* z
{ + M7 T$ F- s8 y* D
struct viewporttype vp;
5 d' R2 z3 Y( O7 m8 ^7 M( gstruct PTS points[MAXPTS]; ! a- Y6 b) M; X+ o2 |' [
int i, j, h, w, xcenter, ycenter; : V% J8 C/ N5 r  r. ~9 ?( o
int radius, angle, step; # s. \) K# B; j) z
double rads; , m/ b9 _9 b8 u
printf(" MoveTo / LineTo Demonstration" );
+ `* o. p3 I$ {$ |  p6 _getviewsettings( &vp ); . Q5 }* J& {! B
h = vp.bottom - vp.top;
" D6 j) l/ o7 Z2 X9 R4 a' mw = vp.right - vp.left;
. B9 ^4 w  u% b$ P) W* O% q# U5 gxcenter = w / 2; /* Determine the center of circle */
0 C$ {$ h: |  f  [4 c& L# [4 \) [  }) cycenter = h / 2;
2 l, C  i. f% a* B, B& Wradius = (h - 30) / (AspectRatio * 2); 3 E+ `) A% o5 t0 b* q
step = 360 / MAXPTS; /* Determine # of increments */ 7 s" @1 U: a8 s- K. j* f$ O9 p
angle = 0; /* Begin at zero degrees */
9 N6 O! \! ^9 k4 s' Pfor( i=0 ; irads = (double)angle * PI / 180.0; /* Convert angle to radians */
5 V7 E/ q7 p  f' [& o3 [: E+ wpoints.x = xcenter + (int)( cos(rads) * radius ); : A3 `& [# q/ q& E- g
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio ); 8 R! D5 I2 t4 o5 g4 ~) ]8 O& @
angle += step; /* Move to next increment */
9 @- b/ y5 ?. |/ I/ `: n}
/ |0 h4 g( t1 D% Q' I. Q: ncircle( xcenter, ycenter, radius ); /* Draw bounding circle */
) Y0 @5 c# {, I6 a. k/ Ffor( i=0 ; ifor( j=i ; jmoveto(points.x, points.y); /* Move to beginning of cord */ / u$ s" Q) Y, d, P0 l2 J; g
lineto(points[j].x, points[j].y); /* Draw the cord */ 9 x& H( z- p- f
} } } ) P1 d3 ^2 J3 o- ]3 a* M" Z7 h
main() ( G/ H' B& w7 D# ~; t7 H
{int driver,mode; % N/ y2 @0 X$ a2 }( x( G
driver=CGA;mode=CGAC0;
; ^: T, [$ z9 Z& O: jinitgraph(&driver,&mode,"");
: F# C2 d* r; B7 E$ S' @setcolor(3); 1 f" o/ W0 Q! h9 j' x4 D0 p5 [
setbkcolor(GREEN); 8 J. a. F: p, l/ i) f" P+ r
LineToDemo();}
$ X/ Y' q4 k4 `( K* F============================================================== 6 \( }+ U9 w5 {+ A% W" r
【程序66
3 \, `; Z* z' ^5 }; O# N题目:输入3个数a,b,c,按大小顺序输出。 # k# [2 l) y, |. R+ L
1.程序分析:利用指针方法。 ( F5 Z' X  a4 [9 j3 x9 B
2.程序源代码:
1 i0 n" [( E) f/*pointer*/
" [1 O& `) U- _3 `0 ?7 ?9 [7 c* k2 smain()
; s; r6 F: ~) H0 M, k5 e{ 1 i3 g/ q& l: y0 O1 x3 Q2 q: ], `
int n1,n2,n3;
. X; Z: I& d; U  r7 B& G) u1 Yint *pointer1,*pointer2,*pointer3; . V& }& R8 S" U4 T  w" Z! ^
printf("please input 3 number:n1,n2,n3:"); 1 A6 A0 M0 \) J! F6 B( b0 w+ `
scanf("%d,%d,%d",&n1,&n2,&n3);
  `3 G  k) Z+ A$ i% ^$ F6 ^pointer1=&n1; $ F. j* o: C" m+ H9 C* r
pointer2=&n2; 7 q" {! m- A, H
pointer3=&n3;
* i# A7 H( J: L) A* ?4 {' lif(n1>n2) swap(pointer1,pointer2);
. W  k/ ^' w) E' W6 W; C  Xif(n1>n3) swap(pointer1,pointer3);
  N0 Y! u8 x% m: L" ~, Qif(n2>n3) swap(pointer2,pointer3); ! t- D. V- g8 \1 a0 [, a( `1 ~( L
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3); 4 e  J. H3 H0 u, g
}
' H4 _+ n) ?2 C" Uswap(p1,p2) % F. V( A+ x1 g0 _
int *p1,*p2; 5 p% k- h' T+ _; X
{int p;
9 X" _9 Y: G% A4 hp=*p1;*p1=*p2;*p2=p; ! }. \, a; n0 J) M- @( M8 J! Y' r; n, [
}
+ o& \, s5 D7 S6 ~+ @0 [==============================================================
1 U+ ?9 Z) R# ~: E5 v( f0 E9 X【程序67
2 u1 x  S- v0 G$ V6 V1 x5 L题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 ( Z; P' k  k* J* z  o/ C1 U$ I/ y
1.程序分析:谭浩强的书中答案有问题。 8 b* d4 d' `$ c1 I5 a- j
2.程序源代码:
  t6 e+ x! F( v/ q  G$ amain()   n8 Y% a7 R& v- I2 f
{ $ E. h- C' C# o! n1 ?6 {! z
int number[10]; 4 m8 j) W. X$ E. B
input(number);
6 R/ h( @. `, h1 h: O* k' l4 Ymax_min(number);
" M# C) X- O# n' Woutput(number);   z. h. X% A1 G
} - k& C1 s- T5 c6 j2 e, v7 U
input(number)
) H- C0 R$ C/ c# O8 aint number[10];
- e) V9 p3 A: d; `  a/ d% u{int i; . N; q6 k4 F5 \6 j  n# b5 ]
for(i=0;i<9;i++) * Y! K0 p4 ^3 c+ ]( j2 v/ Q) \
scanf("%d,",&number); / }8 `: q) h% f) h  }
scanf("%d",&number[9]);
" b* d* @/ A- k9 |9 t- h; \* A} 6 C/ l0 Z/ P( B- R2 R. n& V
max_min(array)
" e! ~3 Y( h+ t& A3 @, Xint array[10]; ) L; u  j7 R- r! [5 h
{int *max,*min,k,l;
% m+ N9 C% i: @9 j9 X3 P" {7 gint *p,*arr_end;
+ J. P" {4 O- A( earr_end=array+10;
% v4 u+ e: T3 Z% u2 Fmax=min=array;
2 A! C2 d- f' p; I/ l  Yfor(p=array+1;p if(*p>*max) max=p; 4 D& n  ]& n+ b) X! D1 L% h
else if(*p<*min) min=p; ) @3 A: V  _7 J* ~6 I4 c$ M( e" U7 y
k=*max; 0 x) ^% s% v3 T* o& O
l=*min; + F% u7 C- M9 l8 Z: i: a7 [
*p=array[0];array[0]=l;l=*p; % X+ ~. K9 p+ y  n7 P8 A, o$ t
*p=array[9];array[9]=k;k=*p;
, |+ f0 i. A' V) c  oreturn;
+ V2 ]. s" I# L, k4 d$ ?}
2 ]* s" Z4 Z6 ?# }. k2 @output(array) . P) z2 r, ?  F2 y0 p
int array[10];
. {$ v" ?" S/ q0 N{ int *p; 4 z8 E/ o( X4 J- ^$ l4 E
for(p=array;p printf("%d,",*p);
# H: j! d& ^8 H, m2 O  R( _0 Fprintf("%d\n",array[9]);
/ N! G4 `# a" P" V1 B% w}
6 ?2 J5 @$ \2 b) c  D) i==============================================================
4 A) ~! i3 n# |2 j& H
$ `. O) g0 l7 Z) x' Y- k( o, B

该用户从未签到

2#
发表于 2017-11-25 19:59 | 只看该作者
我开始考虑从google的影响了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-5-31 08:21 , Processed in 0.093750 second(s), 23 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表