找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

巢课
电巢直播8月计划
查看: 65|回复: 1
打印 上一主题 下一主题

C语言经典算法50-67

[复制链接]

114

主题

136

帖子

1000

积分

四级会员(40)

Rank: 4Rank: 4Rank: 4Rank: 4

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

EDA365欢迎您!

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

x
【程序50
5 d0 a3 b8 H# w# G$ A题目:#include 的应用练习 7 c8 y1 j" c  @
1.程序分析:   b; w1 L4 l: |  q. m8 u4 |, n
2.程序源代码:
) w" G0 }6 p' [( O- ], \' ^" Ktest.h 文件如下:
5 S; |4 ]8 k5 T9 L3 Z" X4 K#define LAG >
( p" H. y. s' a6 P#define SMA < . l# U3 T0 O1 |! I$ k4 ~5 S& ]0 l( B
#define EQ ==
3 _. a% K! z" Y2 r" b0 N: _3 @( u+ s#include "test.h" /*一个新文件50.c,包含test.h*/
1 y& z6 X6 ^% D#include "stdio.h" + j+ B: g+ X1 Y4 ~
void main()
5 ^/ b$ b: T8 I{ int i=10; 9 e+ \4 U4 z9 o
int j=20; 3 ^3 Q/ @/ w0 O4 D5 K7 b
if(i LAG j)
9 {6 O0 @- d- L) ]5 ?8 V3 l! Tprintf("\40: %d larger than %d \n",i,j);
& ?2 x0 D2 n" y0 g% ~$ C9 Oelse if(i EQ j) 1 g9 v* u* W3 M& p+ O$ x% c, D, B
printf("\40: %d equal to %d \n",i,j);
* s# i* G# J/ R0 `% U7 ~) helse if(i SMA j) & U1 \! v+ F: q( K8 J
printf("\40:%d smaller than %d \n",i,j); $ S  P" D* [3 {0 p0 @% @% E" s
else ! H& y6 Q! p7 ]! U  [( U4 @
printf("\40: No such value.\n");
4 h" t* {4 v" T0 s% d. m9 @} # G. A$ y4 Q6 H8 p
【程序51; J1 d9 x: U: J0 a% u
题目:学习使用按位与 &
# X4 e" n' m- ]7 B/ z1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1 6 q. s9 Z6 g7 i2 [; ]: Y, m: l
2.程序源代码:
, j8 g+ s0 {0 ^- n  P& u7 T$ D#include "stdio.h" ' E' I+ j& F$ W& s6 M
main()
+ m% F# W! Y* F# t{ + [8 ?* o4 k3 `" Z: w: a1 ~& ^. j
int a,b;
0 W1 o, O9 x1 G' J# {a=077;
; r: y9 G; b! X5 Xb=a&3; - }; [* X1 l! j5 @
printf("\40: The a & b(decimal) is %d \n",b);
" J1 |. c# G! }' I& r$ n$ e  Y5 u% jb&=7;
8 h0 I$ c, Q4 fprintf("\40: The a & b(decimal) is %d \n",b); ' M$ a  p( t- U9 R5 K: d
} 5 T) s! H9 F' l+ a0 A
============================================================== , Q) a( L1 J, T9 w/ B
【程序52
0 t. [2 o+ v5 `1 _题目:学习使用按位或 | 8 O+ }& W; b& b# C' b
1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1
2 i# w# {4 y8 I/ I2.程序源代码: " k; x+ _% \( _; V7 r0 h3 E) |# i6 b
#include "stdio.h"
9 g5 t& t  B4 ]4 Fmain()
- R2 g4 n; U, m! M% u{ + a! a+ Y# y7 c
int a,b;
6 `% p( d  x' P2 T/ S+ U0 j1 E6 e. Ba=077; 2 s2 K5 Z! R' m8 J% f8 d3 S
b=a|3;
0 [" b6 V/ c% W7 p$ Tprintf("\40: The a & b(decimal) is %d \n",b); # B6 E0 @0 e( o0 y
b|=7; : C9 V0 D; W7 }6 U2 z- U3 Y
printf("\40: The a & b(decimal) is %d \n",b);
$ q% z! p3 ?; c/ @9 C6 I/ L9 E9 D1 T1 F} ; l" n9 h" h0 x! ~$ {0 q
============================================================== " L: B0 s% _% P2 B, }9 K
【程序53嵌入式信盈达企鹅要妖气呜呜吧久零纪要 ! _9 R9 s4 w0 b
题目:学习使用按位异或 ^ 5 ~- n7 S. f( P7 r( ]6 S
1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
* T2 x8 L2 r7 i5 Z- D2.程序源代码:
% b9 M" v- ?8 ~! I5 g6 ?  M#include "stdio.h"
! h' Q9 m4 x  x0 o8 B2 @. vmain()
5 N1 z8 U4 Z9 W: ]{
3 E6 N  @+ D. Bint a,b;
$ ]. N& ~( E3 n4 D+ F& \a=077;
- v/ |3 P9 d: D2 Ib=a^3;
$ c' A0 V: h2 z/ `6 fprintf("\40: The a & b(decimal) is %d \n",b); 2 ?1 P9 e8 t4 G0 F1 z" B4 ^% W8 G
b^=7;
' K! ]2 m1 X3 s/ Y1 |printf("\40: The a & b(decimal) is %d \n",b);
/ o9 N; D: N8 ]% R+ B, c: e/ `} - I) v: O8 s$ S7 l3 X
==============================================================
, [0 m$ {3 j, R5 o【程序54
5 P) y5 o* ~) t. _题目:取一个整数a从右端开始的47位。 3 E$ X& t6 n- k9 P* g) M. y
程序分析:可以这样考虑:
9 }# }! ?* v, N6 T! d(1)先使a右移4位。
) W, p  G5 I# f3 S, |(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
% [' V% L4 u7 E* y+ z( K9 E$ C(3)将上面二者进行&运算。 6 O3 s4 E: G$ c+ `9 J7 f' A& x  T9 b/ c
2.程序源代码: % F9 x5 @2 a  \( |
main()
$ V+ `& E( O3 x! w{
, I& A5 c6 s- U1 junsigned a,b,c,d; $ ~9 K1 k9 h" h. X. o) P- H; @
scanf("%o",&a); 2 Z3 G5 K3 e/ S! b: m
b=a>>4;
+ T# a, F* r$ wc=~(~0<<4); 9 m' E4 H1 m. E2 q" `
d=b&c;
* H' v0 V  D9 _  @7 N9 R* s* Sprintf("%o\n%o\n",a,d); " W" P6 J/ L$ ~9 d: O8 p
} % r8 i' T9 L# u& h" G& Y- j
==============================================================
  P3 v8 {* S/ N( u0 i【程序55
& o% o" {+ ~0 I, n1 ?, B0 c# s题目:学习使用按位取反~
. p; b( i4 h& Z* ?6 p: n4 r1.程序分析:~0=1; ~1=0;
7 O8 `' j  ]) s  K5 F( t5 ^/ U2.程序源代码:
3 P% x+ p& L- l. h) y, N2 {" B#include "stdio.h"
+ K" W! _4 U* U" `main()
; \# X7 S6 m& j- R: i% O* y* u{ # A' h0 B: d8 w1 }0 P
int a,b; 5 W, l( m* S( x, ?. P# {
a=234; 4 Y, Z0 _2 ?/ `; K! L
b=~a; + B3 d: H/ S, b
printf("\40: The a's 1 complement(decimal) is %d \n",b);
% M' l' }4 ]0 R5 M$ T! j: u5 ua=~a;
: a9 x( ^' V; w: p& {; Iprintf("\40: The a's 1 complement(hexidecimal) is %x \n",a);
4 S- X/ x( Y, _! O4 e} 6 S0 J+ b9 P9 c7 [7 i8 D. Z
==============================================================
  m' R( e* ^, I3 b【程序56+ S% q# c, r4 F# ]% f$ ~
题目:画图,学用circle画圆形。
) _/ L' m& a* }" V, B* R1.程序分析: 0 ?5 w4 c7 F8 A# e9 B: |, y
2.程序源代码: 3 V7 p, \  @7 b0 ?0 h5 R* U
/*circle*/
" h) m3 P8 j- Q* M& E#include "graphics.h" $ v% L; K% B/ Z& g! L1 {
main()
$ r( E6 s# s9 m2 c# y! P" ?7 R{int driver,mode,i;
1 n- t4 \; ?3 M6 l9 hfloat j=1,k=1; / a+ F" c8 D/ V. v. k9 V
driver=VGA;mode=VGAHI;
0 I$ u# ]1 `8 Q# W7 F" Z. |initgraph(&driver,&mode,"");
- b# H# W* M) A9 M! Y' Usetbkcolor(YELLOW);
  ?+ K! w9 [5 @) Bfor(i=0;i<=25;i++) 3 T- l! P' v  o" k) z
{ 2 g8 S; k+ R( Y# k. T
setcolor(8);
0 R- w$ Q) }. B( W; z- U3 c4 }circle(310,250,k); & O& F, |# l: U. f1 r8 h: d
k=k+j;
/ c6 V" g* G! O6 ?' A8 Aj=j+0.3;
$ }* k( c9 A  t' r8 H}
2 @. e) q5 P% N) I5 Z}   ]1 R5 C4 v: b; P. ?$ B6 K
============================================================== ) a) x5 Y# I. c5 E! d4 T  U
【程序57
5 o3 A6 F5 a  @% _& i$ ?1 w' W3 N题目:画图,学用line画直线。 ) H: A9 V1 D1 b& ]7 _- E& Y
1.程序分析:
. @+ r1 G+ K5 T& s2.程序源代码:
$ o* M) z, P7 }  `- t/ M#include "graphics.h" & F  B1 a. l* [" z
main() / i8 T3 F! S; a/ \
{int driver,mode,i;
, G! m/ u9 }6 ]. b" N; M. Nfloat x0,y0,y1,x1; 9 r  j! s1 Z6 t7 W1 |  d3 Y
float j=12,k; - q2 ?( l  n* F, k8 m- T" G
driver=VGA;mode=VGAHI;
% D4 A! c# O; T! {  einitgraph(&driver,&mode,"");
) D; J7 B" E1 b5 f# Q/ Psetbkcolor(GREEN); : H3 D% k$ B( ~8 o/ X% _
x0=263;y0=263;y1=275;x1=275;
  D6 X0 v# h! w) q7 g( @! Cfor(i=0;i<=18;i++) 5 e: i+ B' w2 M2 k7 f+ q
{
2 j1 g) ]8 F9 |- ?9 Ksetcolor(5); # K( L1 a: S" g
line(x0,y0,x0,y1); ! i2 I  F6 ~* ]5 R5 E; U
x0=x0-5; 5 _3 `' b3 k- g6 e# m- C
y0=y0-5;
# k7 H2 e3 u- U  Cx1=x1+5;
# j. |+ ~' N$ j9 u) k3 xy1=y1+5; 8 o7 [& ~0 k2 W; ]8 d3 Q( n
j=j+10; % j5 O5 v$ X0 H$ v) k
} # w) U' {  E( ]) G' _! @8 n
x0=263;y1=275;y0=263;
# _4 u/ n/ P/ v& @for(i=0;i<=20;i++) 7 x/ v8 \0 t4 g: E  m
{ 7 A$ d! U5 x3 s2 s3 k1 d- C: k+ {7 p
setcolor(5); / i2 i: d! x( B; H
line(x0,y0,x0,y1);
. q4 M8 u9 T% U* F# ox0=x0+5; ; P, Z- z  G7 r) [. Z9 t. P
y0=y0+5;
9 j0 v% @; ]  P) g  ]1 S# W. o5 Ky1=y1-5;
* o! V) c1 [" I$ {} & X; I( y: \  X( Z" [% x
}
2 c' ]5 s3 e& E( P* i3 ^" L==============================================================
# u) w+ v+ [% A【程序58- D7 T3 C# M* ]' y% |  H
题目:画图,学用rectangle画方形。 4 b' V* X& ^6 j! ^+ x. I& V+ C6 M; M
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 3 o! z3 w/ L; w) b2 K
2.程序源代码:
, o$ y. m" P" N+ b6 c$ D#include "graphics.h"
  X, J% {! P, m) m0 fmain() 9 y1 x0 b- N+ O5 P2 V. l+ w
{int x0,y0,y1,x1,driver,mode,i;
# B7 y2 N% a5 P/ a) `6 u; `- hdriver=VGA;mode=VGAHI;
& Y+ U9 B0 ^7 Zinitgraph(&driver,&mode,""); : f: p- t) z  B5 g
setbkcolor(YELLOW); 0 X1 {4 j, g; V( ~: S8 I( }
x0=263;y0=263;y1=275;x1=275; $ ]2 K  R6 F' ^6 V% F5 T: B# Z
for(i=0;i<=18;i++) 6 B: U2 F9 y" k( X( l+ K; b0 M
{ 9 a- {. \5 k. @3 S
setcolor(1); * i+ [# N" ^. H
rectangle(x0,y0,x1,y1);
& N- e- [! |0 x+ V& U2 U- b+ Fx0=x0-5;
4 L# X1 h# R6 i4 ry0=y0-5;
3 V( ~, `0 k% N! qx1=x1+5; / X  U9 _: k4 T
y1=y1+5;
" s" ~- }$ I& [& D& z5 a  s9 j6 @}
2 h+ v  ]$ K* P% q7 M2 x+ n1 Asettextstyle(DEFAULT_FONT,HORIZ_DIR,2);
' m, d' z) y# Youttextxy(150,40,"How beautiful it is!");
' q: x: [) F. W6 x/ Eline(130,60,480,60); 9 ~) o8 R3 D8 ~' p
setcolor(2);
  i( l2 Z. `; F0 K5 X; p. dcircle(269,269,137); / x. a/ ]' X3 `: k# f
}
- d" l3 I4 D: }/ e5 |==============================================================
8 \; M% W- N, P# U# `8 e. F+ ^: @【程序59
0 K) T! A3 Z+ a7 z题目:画图,综合例子。
( \! s: L- C! j2 g2 r6 n$ I1.程序分析: 9 d4 ?: e5 g, y4 P
2.程序源代码:
6 E8 u9 U5 R1 q( F# define PAI 3.1415926 . V  a; T. C/ O9 a9 k
# define B 0.809 1 p( k% J0 I  x; O9 e
# include "graphics.h"
8 p% s5 N/ F% g$ ^#include "math.h" 9 R& J* e6 L, v
main()
3 c" J  H) Q( p2 f, ]7 O0 G. F{
( n1 M! A8 c2 l* gint i,j,k,x0,y0,x,y,driver,mode; + i( a% u( V- l2 ~
float a;
- [- m: P, T) ^' bdriver=CGA;mode=CGAC0; 7 s* i! n( h1 H0 ?: o  q; ~
initgraph(&driver,&mode,"");
% J: D- M( \# m+ R; D; {setcolor(3); 1 P5 u/ L6 F& O) X, }, E% Z' {
setbkcolor(GREEN);
# d7 v4 W' J. sx0=150;y0=100;
# L: z1 \' ~3 L) icircle(x0,y0,10); " h& T8 ]8 k# b, S0 T
circle(x0,y0,20);
0 q. |  ~+ H' c9 v4 Xcircle(x0,y0,50);
8 [( e" M; [/ }; Q8 W0 \for(i=0;i<16;i++)
7 l1 w! G4 K3 ]8 Y{
  v9 R+ L- @7 c9 i% ua=(2*PAI/16)*i;
4 W" z+ T( E7 \& `2 \2 Wx=ceil(x0+48*cos(a));
- E! I  }) L( Y. V7 i; Jy=ceil(y0+48*sin(a)*B);
) r& C4 o, w+ G+ d, i# Z9 ]- ]' [setcolor(2); line(x0,y0,x,y);} # Y; o" \! f$ x. ]8 }
setcolor(3);circle(x0,y0,60);
' }' c. u, n7 i. Q/* Make 0 time normal size letters */
# ^9 ~1 ]! Q+ d1 p: q6 _settextstyle(DEFAULT_FONT,HORIZ_DIR,0); ' E! \; m  o( g
outtextxy(10,170,"press a key"); ; n8 U# {$ V( x
getch();
- t" T8 y; I3 @4 ^setfillstyle(HATCH_FILL,YELLOW); . H0 w) P+ ?1 N! i7 e
floodfill(202,100,WHITE);
* r( p9 I7 ]3 ?4 J& ], }getch(); & `; h; [; w1 M. q1 @$ i' D; b  T
for(k=0;k<=500;k++)
# g9 a* V6 z/ K; E  b7 Q{
! M, t# O1 t1 _# J8 e/ W3 A- Dsetcolor(3);
  g' `4 Z* Z* E2 Y/ O- xfor(i=0;i<=16;i++)
6 L. x& p3 K% s& q4 _{ , P$ r" @) R* F. }1 E  Z  {
a=(2*PAI/16)*i+(2*PAI/180)*k; 1 O; I$ d) S; `( P
x=ceil(x0+48*cos(a)); & R7 n  h: c. x
y=ceil(y0+48+sin(a)*B); 4 r/ D' N& j, U& e  o: R" g) ~1 [- x
setcolor(2); line(x0,y0,x,y);
: k2 k4 v' n$ [6 i! Y2 u: F}
3 l/ h& {) i3 B6 b& ]4 ufor(j=1;j<=50;j++) 6 L8 [% w7 X& s' Z
{
$ J) {, @# g* J5 @* \' z& ?a=(2*PAI/16)*i+(2*PAI/180)*k-1;
; W0 X% y0 T; Z' [x=ceil(x0+48*cos(a));   u8 k- V; P8 X7 `
y=ceil(y0+48*sin(a)*B); 5 I# L0 x; H; S4 W) p7 I& O+ b: f
line(x0,y0,x,y);   o! ]2 Q3 v7 M- K' o/ ~
} - A4 D+ G  `6 j, {
}
6 {, a' f& K) w# f0 j" s' ^+ ^restorecrtmode(); $ f# c+ t7 j. X! l' X( z
} ) \8 ^8 S0 H4 n4 o4 L" Y' j
==============================================================
: m3 u5 e2 ^: C* Q. J6 d+ \【程序60
4 v# j) ]  h; X* H2 b: ]* w题目:画图,综合例子。
  F* D# P# u. Z" j* o% k" x1.程序分析:
. L) O$ ]( V9 @. Z2.程序源代码:
; l; U* l7 @  O; D#include "graphics.h"
  J9 s6 \9 U$ |( h5 P7 B  O0 ?#define LEFT 0 ! w* i* l9 v, ^: i/ ~- y
#define TOP 0
6 k' J! T- \  R1 T/ o2 R9 ]#define RIGHT 639 ! S4 Y1 I6 O$ ^2 q0 I
#define BOTTOM 479 % [, z' [: b  B) Y% |' m
#define LINES 400
* n, f1 e  x- c; \, _% B  E7 t#define MAXCOLOR 15 ! U+ i/ R$ _% U
main()
$ A4 w7 k9 v& \) ]{ * p4 c! |7 q5 S- _- n+ U
int driver,mode,error; " d! X0 U, \6 ?0 m* h1 }4 j
int x1,y1;
5 l! t7 [1 O7 A" P8 Z$ r* eint x2,y2; & ]) N" m$ O% S  j+ ?' }, t
int dx1,dy1,dx2,dy2,i=1; ; j4 p  m5 Y4 Y: K7 e+ s7 t9 K: i- K# Q
int count=0; 5 L( J9 p) ?) @2 a( J3 d
int color=0;
0 v+ |& h' A* W: {8 j5 N& u5 [driver=VGA;
) d  D$ T+ ?: D! _mode=VGAHI; + z7 E/ E& F1 C. \
initgraph(&driver,&mode,"");
5 Y/ j( \1 {% ?" l, C$ _x1=x2=y1=y2=10; 4 U5 `: F* w2 k% Z4 K* x
dx1=dy1=2; ! u. G, \( f. f. |2 T
dx2=dy2=3; : X& j/ e' A, \4 g# T
while(!kbhit()) ) y5 x3 M+ M" \- O4 D; k, ~
{ 4 }- i8 A0 q3 Q
line(x1,y1,x2,y2);
2 m, i5 \& d& c% vx1+=dx1;y1+=dy1; ) L  T! K, h2 f8 ^! O$ z6 @
x2+=dx2;y2+dy2; 7 s. ^3 b  F4 _5 `1 z3 D) E! X) m
if(x1<=LEFT||x1>=RIGHT) 0 A" `1 d! T+ p. s$ z- S' Q
dx1=-dx1;
9 U% o; o! D4 E4 z9 G+ Jif(y1<=TOP||y1>=BOTTOM)
5 _- ]! U1 J4 D/ D0 l6 S. {dy1=-dy1; - }4 K/ e5 o( R4 E, d6 x+ g
if(x2<=LEFT||x2>=RIGHT) $ F: b3 W2 w8 g9 p- e
dx2=-dx2;
8 U4 P. p) N" R  p  o4 d, ^0 H7 L2 I' d/ dif(y2<=TOP||y2>=BOTTOM) ' T1 o7 z: b. I( H' B
dy2=-dy2; 9 p1 p* P/ d8 ~7 |$ ^
if(++count>LINES) 6 W1 h0 b3 R" {: i! \3 a
{   W2 p% h' ]' Y2 b1 n% s% F+ M
setcolor(color); 1 }" o; Z% }% p  H
color=(color>=MAXCOLOR)?0:++color; $ l' c/ a& T; F, t
}
; @% U" U6 [( r. v0 F! j5 }% p: H} ) c. v# d; t! f- E2 l& f
closegraph();
! @( @* \0 F) h; A9 o8 D1 B  Z}
【程序61: O  p! D/ z8 p* _: C
题目:打印出杨辉三角形(要求打印出10行如下图) 7 x$ [* ~( s3 ?, [  c% p
1.程序分析:
; L- V8 P# q) P  R! X: w1 * ]5 K; E, l7 H: D
1 1
$ h. \( R0 h2 }, P1 2 1 & g- z3 T& O  y/ X9 r
1 3 3 1 : d& `0 o! G5 C4 q% Y
1 4 6 4 1
+ D/ |7 n; p& v8 F1 5 10 10 5 1
% |. C: X% r$ v. X6 |2.程序源代码: 0 s1 N3 N8 X8 M0 d# ]! Y
main() / S9 t5 s- T; K* c
{int i,j;
  {7 G' h, H) a! T! @) Bint a[10][10]; 8 B! U/ t! g  z. v- T3 @: I+ `+ G
printf("\n");
& q2 j4 Y1 o5 C$ D; bfor(i=0;i<10;i++) ' m# A/ f. @/ B# R: M4 R
{a[0]=1;
5 P4 r( ^2 |! k3 [0 k; W/ ma=1;}
9 s3 Q5 X/ o9 }for(i=2;i<10;i++) 4 ^# v' @: d' n3 C* E1 ~3 h4 P  k
for(j=1;j a[j]=a[i-1][j-1]+a[i-1][j]; 8 U' A5 r4 g! n' r& M
for(i=0;i<10;i++)
7 `- I5 I5 B4 \; X9 s; A{for(j=0;j<=i;j++)
7 j0 O. d& t  d- G) z5 z# qprintf("]",a[j]); : k6 C- k2 d1 H2 q( M5 C. `
printf("\n"); 7 l$ \8 a) ^) l* s
}
7 J( a2 t$ ]$ F# ^, H}
$ c# S% o0 M7 H: v; V============================================================== 9 E) i, @$ u+ G# j' B2 O/ H5 `
【程序62
3 a' |+ `' {1 m9 _题目:学习putpixel画点。 % @8 s% l1 t, Z$ i( m
1.程序分析:   f3 }0 T. \& O( |1 l
2.程序源代码: + u( q" l/ ]' G# h4 T( \/ \
#include "stdio.h" # W! ^9 z6 G1 x/ K: o
#include "graphics.h" 7 t4 u/ t8 |/ V6 n& i) y: v7 N
main() . _# s* [& Y; F9 h( s* @+ L$ D
{
( j' f: E) \% Eint i,j,driver=VGA,mode=VGAHI;
4 W. B# i) z: B2 O5 |& yinitgraph(&driver,&mode,""); ) J3 Y, o; H/ _. l- c- L/ w
setbkcolor(YELLOW);
1 [5 r9 E2 Q% C" s8 B6 x$ p# H/ u) Zfor(i=50;i<=230;i+=20)
. O; S# u* X! d+ c0 k# @" ofor(j=50;j<=230;j++) " @4 S8 y; m+ {% J5 D
putpixel(i,j,1); ( ?* P3 g5 C: H. D
for(j=50;j<=230;j+=20)
- }- i! T$ x2 @$ i0 z  Jfor(i=50;i<=230;i++) . B* m% K! K% x* A5 R/ ^5 x
putpixel(i,j,1);
$ e9 f7 I8 Y. b1 q}
0 V3 M/ W$ [+ s/ Z0 B==============================================================
& v5 x1 D) y( b【程序639 `# @. d7 j$ F6 b) q- x
题目:画椭圆ellipse
4 x( j$ n) J- ?- L4 s. Y1.程序分析:
; z& ^, F7 C6 T$ e2.程序源代码:
! s+ l% d  a. B3 S9 u, D4 }#include "stdio.h" 7 B1 V+ X  b' ^) O/ y( u2 z1 q1 o
#include "graphics.h"
* q5 _# u- o  `8 @6 L#include "conio.h" ) u! z3 N* A1 E* i8 Z8 a  o! v' N7 u0 ^
main()
5 N5 b$ t" w, s( U. C{
" |5 H( o* h6 X% I0 g; N% m! {/ w! cint x=360,y=160,driver=VGA,mode=VGAHI; , l: \$ O( j6 z1 y- J' ~$ f" G
int num=20,i; % K/ U# Y( `) k
int top,bottom;
2 w* G$ F! M, A4 ^initgraph(&driver,&mode,""); 6 Z3 Q& H7 F0 Z2 m, S
top=y-30;
6 X: N7 m# R* ybottom=y-30; 6 Q4 g" t) s: g& o$ H
for(i=0;i{
, E4 P: i9 f2 p$ Q" Dellipse(250,250,0,360,top,bottom); 8 c: n6 `9 a! D8 m" l! ]0 _% `
top-=5; ( a, f" m" u% I4 W* o, ~1 e3 M% u
bottom+=5; / l- J8 J0 \' x
} , }: e6 Y) n( v- n
getch(); 4 [2 x2 J$ D7 i2 m$ Q7 [  R, y9 y9 ^
}
* [) v! Q+ y: W' W============================================================== 2 N% d# B$ K3 X3 J$ J* [
【程序644 i" f: q1 u, o( X6 Q' Y
题目:利用ellipse and rectangle 画图。 ; R& i, c- @$ I# l! Q
1.程序分析: & H8 A9 C( V3 B2 d9 S; b$ k% F; l+ a
2.程序源代码: 7 X" [  E6 `2 R, a0 ]5 Y
#include "stdio.h" 9 u0 Y" O) @% G' r3 n5 m
#include "graphics.h" 1 m9 K& D2 ]$ S# ~6 o+ K0 k0 I
#include "conio.h"
' |( g1 s. i" Cmain() 0 Y9 B: S# {+ N/ ~
{ " O/ ?) k& X# _: k; R, z
int driver=VGA,mode=VGAHI;
, c5 O& N+ l0 U% l6 ^8 Gint i,num=15,top=50;
/ h* K2 X( v# y! |& Pint left=20,right=50;
9 U0 Y7 {2 `$ l5 ^/ g' V9 rinitgraph(&driver,&mode,""); # i/ p5 ~1 I8 c
for(i=0;i{
: F; ~/ d- E% d0 pellipse(250,250,0,360,right,left);
) b4 A: e- r" d& ^  @9 k+ Lellipse(250,250,0,360,20,top);   |( T* {* c6 t2 ~, {  t- R6 O+ R
rectangle(20-2*i,20-2*i,10*(i+2),10*(i+2)); 9 `0 x# T+ C! t0 I! B$ x
right+=5;
! U& ]' r8 l0 {+ I3 `$ Mleft+=5; 6 j- n+ H  l1 ?5 Z7 o2 r
top+=10;
+ f3 S9 F! l, K7 L' x( L! E}
0 W+ c- X  {( N% S% A) O$ Bgetch();   |' D6 y6 K' g
} ' j0 @: Q1 e7 [- _  w
============================================================== 5 B2 W; q1 Z9 z6 ~0 b0 n0 ]% I) l
【程序65
8 Z7 J) X/ G7 y2 d  I0 U题目:一个最优美的图案。 - c4 x$ R2 a' d) |. R/ k
1.程序分析:
7 X& _& _( s" E8 @( ?2.程序源代码:
# F: J  S, T' u/ A6 U7 |#include "graphics.h"
7 Z: M4 J4 ]; v% l7 [, ]#include "math.h" ' @) o2 |1 V8 }6 c( q
#include "dos.h" 3 n, y8 {2 E8 ]2 d( S' \# t  D/ H
#include "conio.h" / G& d8 |; W. h  ^! x1 W' T4 t
#include "stdlib.h"
/ \; q6 S" w( G#include "stdio.h"
# x2 n  K- }& ?& u#include "stdarg.h"
$ N! m" V- H& Y2 I#define MAXPTS 15 7 b" H0 J6 ]* t9 {( \: ]0 v
#define PI 3.1415926 # i0 B1 S2 O6 }' X
struct PTS {
" _, a. M+ _5 l  [4 L" @int x,y; % z$ Y# _9 q! I
};
4 y+ B7 s+ Q/ w6 jdouble AspectRatio=0.85;
# V7 x" k2 `" ?# [2 I  s& S0 Mvoid LineToDemo(void)
# [& t0 O+ h/ P3 A) }& N{
; t5 [* p5 z6 C! P& c- Sstruct viewporttype vp;
6 `0 T8 q9 q2 d4 K2 E" |3 |% q7 r1 }struct PTS points[MAXPTS]; ' k! U5 n, T. [& s. t4 W$ P
int i, j, h, w, xcenter, ycenter;
& k8 ?1 S7 W/ K% |int radius, angle, step; 3 Y3 [! u0 j& v9 M" X, k( u
double rads;
0 Q' o+ T5 D, ~  iprintf(" MoveTo / LineTo Demonstration" ); + y; B* S+ e+ b, f; S. r
getviewsettings( &vp );
; T# `% L: N& Z2 b$ b4 U' Qh = vp.bottom - vp.top; 0 L+ R1 w/ ?  F7 ?
w = vp.right - vp.left;
6 w( P  o) {9 m2 |- nxcenter = w / 2; /* Determine the center of circle */
8 M/ l  i6 J- N) ~9 Gycenter = h / 2;
+ i8 N1 B+ `4 K) q- ^) ^radius = (h - 30) / (AspectRatio * 2);
$ n0 n8 b7 D" X! f. |* Ostep = 360 / MAXPTS; /* Determine # of increments */ 0 v6 ]% k1 Z% O6 u
angle = 0; /* Begin at zero degrees */ # A1 F8 s  A( e
for( i=0 ; irads = (double)angle * PI / 180.0; /* Convert angle to radians */ & g4 X% P6 r6 N8 Y1 }  ]/ y* X3 y
points.x = xcenter + (int)( cos(rads) * radius ); " c' j/ q' @  i( I2 e
points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
' c% {7 q0 `# `; ?- ?- z2 Bangle += step; /* Move to next increment */
1 p/ R- v7 J) I: B- O} 2 C  P* W' d0 l; v0 m3 n
circle( xcenter, ycenter, radius ); /* Draw bounding circle */   W; n  }5 R' A& l0 v. l
for( i=0 ; ifor( j=i ; jmoveto(points.x, points.y); /* Move to beginning of cord */
; v6 h% B7 {. ^/ D9 ~lineto(points[j].x, points[j].y); /* Draw the cord */ + l3 e- o/ D5 e; P# k# A5 Q
} } }
  G+ L3 g+ `* A* H& \main()
/ n& o7 o0 V5 r" o2 c. a{int driver,mode;
2 U6 R& W; l9 Y7 {1 }" qdriver=CGA;mode=CGAC0; . y% k3 o! y- s- C. M1 V
initgraph(&driver,&mode,""); 8 S1 P+ G# k+ c, q+ C
setcolor(3); ' X- _( _) _! s% U' I9 r, P0 o' B
setbkcolor(GREEN); ' ~4 z! a# ]+ v& I' @
LineToDemo();}
; w8 Z5 ]. r0 e9 C==============================================================
$ K2 e! c8 w2 d6 @. V  B【程序66, J' x  ]! M" j
题目:输入3个数a,b,c,按大小顺序输出。 * I+ z; `: d. u/ |& _+ w
1.程序分析:利用指针方法。
9 y; ?. u4 V/ X+ e8 X3 Z2.程序源代码:
% b. P/ o8 f% _/*pointer*/
, \1 N9 L$ M2 H& Q7 ~main() ' p; i' C: X6 B! V9 p6 P9 V  X" l
{
1 S1 V- s1 ~  g" l+ cint n1,n2,n3; ( s' @% K5 y) L' c* t
int *pointer1,*pointer2,*pointer3; + l1 Q, x% ^! i- j9 _' R$ v
printf("please input 3 number:n1,n2,n3:"); / Q! o# Z* W, }" h, Z$ ]' Y0 F. R
scanf("%d,%d,%d",&n1,&n2,&n3); , d$ S$ q, E, D
pointer1=&n1;
# ]7 Y4 Z) d; J; F% a  Dpointer2=&n2; ' T% U: {2 U! v9 Q4 Y! i+ i$ M
pointer3=&n3; ( J, d# a0 S6 Q5 ?3 J
if(n1>n2) swap(pointer1,pointer2); + \, w" c, ?! y9 ]$ G
if(n1>n3) swap(pointer1,pointer3);
% A$ D" [4 A, K& \9 rif(n2>n3) swap(pointer2,pointer3);
* y) j" r  m/ G, \' O& xprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3); " w4 g. U- ~2 f) f
} # m* L6 b  W( O0 E0 H
swap(p1,p2) % ?3 l$ v  \5 `: A! {- A6 C/ w
int *p1,*p2; : x' {% p9 n% z* d( |& D
{int p;
2 C4 ?7 Q  W6 M7 }& F: B  Qp=*p1;*p1=*p2;*p2=p; 1 }' {3 A& n( p, b" ~8 k
}
" m7 k7 c: v' n- X============================================================== ! E3 n6 o9 v5 e
【程序67
8 Q6 \- Q9 l0 P+ \. c题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
% [6 H+ y8 G  Z: o  J3 ]" m1.程序分析:谭浩强的书中答案有问题。
% K# _; S2 H% P8 p9 p8 J2.程序源代码:
/ f( }% B7 U7 O/ K0 N3 Lmain() 3 d' R5 y  O5 f: e
{
- J1 c* T# T0 s, U& Uint number[10];
7 ]3 {: n- V% dinput(number); ( s( _2 O' j/ `# w
max_min(number); 9 N9 I. `+ g- r. y# [5 |; d; ?
output(number); 5 a8 E0 X/ |4 ^! E8 i! C, }
}
8 [+ N. F7 I" J. l4 [4 minput(number)
1 V0 z, ~" H( {( f% K: ~) wint number[10];
" k3 l8 O2 m5 B  @+ c2 M{int i;
9 S2 f% S+ n/ d7 o1 J8 rfor(i=0;i<9;i++) $ l. o; T0 E; Q- Z" Z5 U) \
scanf("%d,",&number); * e+ j8 J5 m9 l# C/ l! R3 Y: g
scanf("%d",&number[9]); * v7 d4 _" Y/ d
}
9 s3 c( f7 Z# r* V, B. {( B7 Pmax_min(array)
/ L- y- x* Q* n  |int array[10];
  L- d3 U& T$ X{int *max,*min,k,l; 0 S1 ?; V% Z5 f( [! C) u
int *p,*arr_end;
9 T& d$ J0 U) m$ L: C% tarr_end=array+10; # A3 q. P3 G; r! J5 T( s9 p& m& Q% E
max=min=array;
& z" l" J' E/ bfor(p=array+1;p if(*p>*max) max=p; 0 s7 T% y6 C. u3 c
else if(*p<*min) min=p; , x8 \" O4 c5 T. k2 B6 X: ?% L- p( ]
k=*max; ' o: R2 S% k' G1 E: Q
l=*min; 4 z3 R$ l) U' E
*p=array[0];array[0]=l;l=*p; ( u) _, T' V+ O% w8 ^4 x
*p=array[9];array[9]=k;k=*p; , H! n! z; J6 Z4 |) S9 H, c
return;
' L- g4 {$ I6 [+ D) C) N6 q/ H}
3 W8 V' {6 V  x! A; @output(array) 2 c  O: D3 D, H6 x0 \# F. ]1 X
int array[10]; / k  ^" u- j' _3 \* H8 P1 V
{ int *p;
, ^( }. b# i, s3 M- g! X# q8 U0 vfor(p=array;p printf("%d,",*p); * {' v9 r" Z8 U8 Q$ r
printf("%d\n",array[9]);
4 }9 e/ G' j. t( u; f} / I4 q! y+ }  w
==============================================================
4 o* L) ?) T& j# [
) L& w) X/ @3 x+ ]0 x% [# A. A+ k
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 支持!支持! 反对!反对!

0

主题

82

帖子

93

积分

二级会员(20)

Rank: 2Rank: 2

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

本版积分规则

关闭

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

巢课

技术风云榜

关于我们|手机版|EDA365 ( 粤ICP备18020198号 )

GMT+8, 2025-2-2 18:08 , Processed in 0.060099 second(s), 31 queries , Gzip On.

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

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

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