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

C语言经典算法50-67

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
【程序50- }3 T3 ]+ b( _9 m$ a
题目:#include 的应用练习
$ \; L, B2 @. \- q1.程序分析:
2 e5 J3 X) _- t( Y2.程序源代码: % p$ `5 ~( [8 O3 S3 F/ \) i: L) r
test.h 文件如下:
0 ?9 K  Z' a  n+ l5 \! |#define LAG >
4 D: Y$ _. _' B7 r  D; ]% t$ S#define SMA <
3 z5 t2 W- L0 N#define EQ == % c( I6 n) ]6 k7 @: b$ i7 S
#include "test.h" /*一个新文件50.c,包含test.h*/ 0 W7 @8 K, ?% L
#include "stdio.h" + ]- _& t9 U; |3 H
void main()
# l4 z" w& w( k# q1 U9 A{ int i=10;
+ V/ o$ w% g3 P; P% Z8 S8 iint j=20; $ ?. z' A% x# {) K
if(i LAG j) / s7 a5 f2 I7 L* W' s" W
printf("\40: %d larger than %d \n",i,j); ) G7 D1 M- ?$ r! K
else if(i EQ j)
. D9 k0 J: B) [/ zprintf("\40: %d equal to %d \n",i,j); + K% r0 L  @3 {, \5 U# H
else if(i SMA j) 0 t$ t' |+ M0 E  L
printf("\40:%d smaller than %d \n",i,j); % L1 V; k8 k* V( O* R7 B
else
; b& {; y' }& tprintf("\40: No such value.\n");   p/ h5 v% _4 X+ c) _; u- }
}
, N; L- |  ^* h【程序51
3 k: ~; D! A. G5 e: c题目:学习使用按位与 & 0 |& G( {6 ^7 o& p; g  V, [
1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
/ S0 E5 ?5 s1 c7 p7 C) O. |: G2.程序源代码:
# H9 B% W, i" Q, T, N#include "stdio.h" 3 n# w. ~2 ?3 P. |
main() " Q3 |, I" W8 X( r' Q
{ $ k0 i! ^0 J- w3 J3 W! G: o2 i
int a,b;
- |( T+ W" u3 X, S' [1 ia=077;
0 v4 j7 c" r# F# Ub=a&3; . `3 Z5 [5 j7 S/ B
printf("\40: The a & b(decimal) is %d \n",b);
# X! y# ?9 T, Lb&=7;
+ H* R+ E  \) w. j* Yprintf("\40: The a & b(decimal) is %d \n",b); 8 G. W( I3 P7 f, l
}
2 J9 o. c' H& A( }& k6 ~0 E% U==============================================================
4 v& H; a9 w: g: H9 @【程序521 ^  u- B' a9 K
题目:学习使用按位或 |
( \# `+ E# Y- X6 O0 @1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1
2 G: j1 `" @! q% Y2.程序源代码: 8 ?5 O1 w. h% i# ^2 C  t1 n
#include "stdio.h" ) |3 K7 I- _3 |3 v6 K  K0 Q
main() $ f, ~1 O$ G6 a4 k6 G6 _& H5 `/ L+ H
{
' b" C, y8 t4 z! Pint a,b; 4 R: o7 w) ^" l
a=077;
' a; o" }" {# S: s: d% p' Ab=a|3; 5 ?6 I" P9 Y5 i9 U) s( Y/ y3 i
printf("\40: The a & b(decimal) is %d \n",b); 4 v' U* G' l# J' Q  P
b|=7;
7 @" t& x6 o- X  d( S1 ~printf("\40: The a & b(decimal) is %d \n",b); 7 t8 S" U: M3 |: i1 n4 J
}
9 r9 Y8 a. h: p8 u, S9 H5 c' C==============================================================
' r. m" ^" D: W! V$ l【程序53嵌入式信盈达企鹅要妖气呜呜吧久零纪要 & X& R# ~4 n. A- n+ R
题目:学习使用按位异或 ^ 5 G3 L+ O  h: P0 q2 L& s
1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
0 j( K$ `, c) X. ~8 o+ {2.程序源代码:
  j% L6 k1 P/ h" Z#include "stdio.h" ! p0 _: m, B8 `, ~* u6 K
main()
6 e' ?# l  Z( k5 W* O$ ?- j{
/ ^' W3 j# {4 p/ P( {1 J3 Aint a,b; # i: d1 J' b9 |6 C# c
a=077;
. x7 U' c6 c; O, D. w& jb=a^3;
( P5 G  N% F! |8 G3 ?- zprintf("\40: The a & b(decimal) is %d \n",b); & I; R4 f$ R* E" _% U
b^=7;
6 V5 u( d4 d7 d5 _* Oprintf("\40: The a & b(decimal) is %d \n",b);
% ?. ^# y4 }% m8 u; K}
% ~5 _2 {; Y7 Z. Y7 R) G: o, a4 |==============================================================
& U) M9 R8 H& c% _+ B4 z【程序54
5 A# F9 D  x1 r6 a题目:取一个整数a从右端开始的47位。
, F! p- o7 Y$ E0 [( v9 l程序分析:可以这样考虑: 6 d# T2 x  v- n
(1)先使a右移4位。
0 Q. K1 p" ^4 _(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
3 e% B9 M3 U: N(3)将上面二者进行&运算。 5 N- ^0 C% r' j9 H
2.程序源代码: # x! f% c: c- K2 `
main()
0 ]% s; v' x$ [; Y2 z9 b7 k{ / C" b% S2 j1 E" f: C2 F/ w
unsigned a,b,c,d;
. W$ m+ B1 h8 b8 K2 Mscanf("%o",&a);
! q) x/ p( A4 {* J- O1 tb=a>>4;
9 N) _. E$ H( yc=~(~0<<4); ( u+ J' }9 \3 a4 s! D8 i2 W
d=b&c;
3 M$ @( M$ p$ p* b0 |* n! yprintf("%o\n%o\n",a,d);
! D0 y  d8 m% U% }& V} 7 g9 a, L6 C, ^% R
==============================================================
5 D# m! {- Q% F【程序55% ]/ z7 d, I; V0 d+ ~# s" ~
题目:学习使用按位取反~
/ ^0 ~% O# Q8 A6 y& E1.程序分析:~0=1; ~1=0; ) h. W% G% R/ ^. J# P4 b" X
2.程序源代码:
2 U4 ]4 k* y, N1 K1 v#include "stdio.h" ( r) `* ?8 h2 l/ N+ S  G3 R
main()
* q. q0 C) R4 l% s" l/ D% d0 R{ . e* p$ T1 z; |0 Q0 F+ x3 @/ S
int a,b; ; v6 f4 a  b. X
a=234;
- b# x* K+ P. f* |b=~a; 3 T) r% O9 t2 `% H/ r. N7 {% C! G) D
printf("\40: The a's 1 complement(decimal) is %d \n",b); 9 K, X% f( L! d0 c9 u. u
a=~a;
% O  o) _$ `$ |- c$ l( zprintf("\40: The a's 1 complement(hexidecimal) is %x \n",a);
' Q, `1 C- d" F, V/ O} 5 R: A& f/ u. y  F
============================================================== ) H' H$ d% z: u7 d8 T- j
【程序56
2 O) v. g  P  T' X' @% I" n; l6 w题目:画图,学用circle画圆形。 8 o1 p* Q+ p1 d1 Q( }2 W
1.程序分析: 7 T6 ~8 e' E: F4 c
2.程序源代码:
5 s9 |' Y# z' {6 ?; d7 R/*circle*/ + N" _, B: x& ~6 ^
#include "graphics.h" , ^8 O  B% b* m4 Z9 R; w
main()
$ J: ]2 a. H3 ]; S- U0 R  E6 J{int driver,mode,i; & d$ B" G- J( m9 y+ b% b& c
float j=1,k=1; 8 {! x8 x$ j/ r( u
driver=VGA;mode=VGAHI;
2 J1 O. A  ?2 ]. Rinitgraph(&driver,&mode,""); 8 Y9 s) f3 d' K% H
setbkcolor(YELLOW); $ q$ E6 V& X/ M  Y
for(i=0;i<=25;i++) 4 n1 ]0 Z9 M; z- }! W1 |
{ % S+ Y7 ?2 @4 J8 G4 ?5 H# ^' C& S
setcolor(8); 5 T# ?+ ^# @, L7 H5 H# y% M
circle(310,250,k);
- ?$ S+ `& r$ ?; M2 s/ mk=k+j; 7 Z: p! @9 d( t8 I, u& S. ^! h
j=j+0.3;
' {( u' m: h# b, m" g) K} $ v  z3 R' C7 v, j
} 2 o% m# N9 j$ `6 O" |
============================================================== ' E& l: [; z0 e5 I2 i
【程序57
  K/ E9 z0 X) F$ |题目:画图,学用line画直线。 5 i2 S' a* r/ |7 V
1.程序分析: ( c* q! y5 A7 q6 v
2.程序源代码: ! S9 {% C  g  F  e* ?% w9 p
#include "graphics.h" 9 b* v8 v) U- {. X8 Z3 |
main() 5 M. `' m- D9 p. [
{int driver,mode,i;
# `2 q+ ?7 K8 F. Z% c6 ~float x0,y0,y1,x1;
/ d. C% J. J2 c1 }9 a9 t, K3 [float j=12,k; ( p* ?2 T. E1 c5 l5 u7 d) N
driver=VGA;mode=VGAHI;
& y2 b& p* `' G, C# Dinitgraph(&driver,&mode,"");
5 q- ]+ a- B. m6 y$ Tsetbkcolor(GREEN);
3 [; R' W) y' o# F% ax0=263;y0=263;y1=275;x1=275;
/ o. N5 J. o" L# Y& h  ]for(i=0;i<=18;i++)
5 J7 e+ n  F/ v9 f8 u9 j. q& H: ~{
5 E+ p: `9 X* ~) f1 R) N/ y, J2 fsetcolor(5);
- {0 t7 O6 }. |5 W9 g* X: P0 Tline(x0,y0,x0,y1); : h+ |% t0 `$ {, B2 f$ l
x0=x0-5;
: K4 ^0 S1 f* t: U! o5 |y0=y0-5;
! g4 I3 s( q, y/ ?* H/ A: [x1=x1+5; 3 v- Q2 K: i+ @9 q) I" r
y1=y1+5;
, [6 k8 y' J/ c/ g! ]* Oj=j+10;
  o- L2 w- B( G( q* P$ c. X}
6 B' }. r7 L' m! I. I- Ex0=263;y1=275;y0=263;   Z$ o( H9 J' b
for(i=0;i<=20;i++) - o  N! C% M' d# @
{
  v' S) L# `& J( s% E5 q! ?% ]setcolor(5); 5 U8 X* R' [( ?( J  N2 T* @+ q
line(x0,y0,x0,y1); 4 x& s$ [3 g" T# m: A7 h
x0=x0+5; 8 w2 }4 N0 T# `
y0=y0+5; 1 r" a* N& o4 x; Q
y1=y1-5; + S3 m. Z! d6 A1 `
}
' A+ K" b: m4 A+ D/ j/ y}
& C" j8 q: v6 I  R==============================================================
) R+ k* a/ k/ `5 D/ T9 O【程序58
# `, J$ S4 j' b( \0 }题目:画图,学用rectangle画方形。 4 H/ C+ e) L7 {  n
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
" e+ U$ f: N, O  M2.程序源代码: 3 X  [5 O& w% @% i
#include "graphics.h" 1 Q$ B. ~- Y6 u! a
main()
" T5 F9 d4 T, A* m{int x0,y0,y1,x1,driver,mode,i;
7 K" B' x: |+ a, t4 n( I1 Q( idriver=VGA;mode=VGAHI;
! `9 ^1 l0 T2 k$ t  winitgraph(&driver,&mode,""); 6 x0 ^5 `* Z2 Q: E' h2 A
setbkcolor(YELLOW);
7 \  J! S8 f* b  }( {* ix0=263;y0=263;y1=275;x1=275; ; v. D/ H# y1 \+ b* c9 b5 j, ^  C
for(i=0;i<=18;i++) # G; g1 \, s! L" u. r- g( @9 j
{ . I2 g5 I3 u  ~+ [
setcolor(1);
4 O3 [/ r0 x" s$ U' i2 drectangle(x0,y0,x1,y1); $ T/ }6 O' ~) x. c. G1 O
x0=x0-5; - [$ ]* I0 h4 C+ e! q; u
y0=y0-5;
( N4 H1 \8 ^" i$ M; `9 ^1 Ox1=x1+5;
* Z) c6 Y6 u& U5 {y1=y1+5;
4 E+ M; S( Z$ s/ X} " [" g: l$ b4 O) t& I, k7 ?* f
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
2 w/ H% ^  ?* x3 ?' w, eouttextxy(150,40,"How beautiful it is!"); / A2 V0 B: Z& E+ z- }( f3 F
line(130,60,480,60);   A7 D- K4 N8 i
setcolor(2);
0 @4 @( u8 H! r* D- J* Icircle(269,269,137);
! U. A+ P0 x+ T: \( F. U* E}
. T) I; [& o; ?" H" }==============================================================
3 g" H  U" o* l( ]9 g【程序59
4 J& j8 Q4 W7 |- ]. m) V8 c题目:画图,综合例子。
9 C' ?, u; ?+ n; _. R3 \* V1.程序分析: 9 g/ L9 P$ ?! D0 F' `
2.程序源代码: " t7 ~) \( p# h1 o
# define PAI 3.1415926
& o1 F1 @4 H  s# h8 Q* {! {# define B 0.809 2 |) a0 g/ D6 D' R& N" ^9 {% Q6 G' U
# include "graphics.h"
  a+ t+ M2 |$ z4 z#include "math.h"
! k. w3 q# n& n1 c7 |- V+ Umain() 6 _3 z, H! S/ n: u4 k" f% Y2 F/ k
{ 7 L2 n$ O9 U" ]5 v  A4 d
int i,j,k,x0,y0,x,y,driver,mode;
$ `& \5 Z4 y7 l0 w) e4 }5 C" ^. J7 N. Sfloat a; ) K0 ]: Y* A/ }
driver=CGA;mode=CGAC0;
. K8 l, ^. V; l" m. rinitgraph(&driver,&mode,"");
3 C7 p/ F& M7 d# G( N+ p# X5 Msetcolor(3);
( |6 q; w3 p( S+ E+ X3 w8 ~; vsetbkcolor(GREEN);
* \1 c; h/ B. I! ]7 |" kx0=150;y0=100; 2 n: r4 ^; s2 F/ s0 l1 ?6 e
circle(x0,y0,10); 3 ~6 q/ V' a2 W9 V) v
circle(x0,y0,20); 5 M+ V0 J  i: f( G: w2 ?/ |8 }5 g9 B
circle(x0,y0,50);
& `- B9 r/ {6 K1 q- B7 {% `7 Mfor(i=0;i<16;i++) 9 V, [3 n3 R- i9 l. b  r4 Z* \
{
' R0 x) ~7 x4 [6 c3 @a=(2*PAI/16)*i; ( a7 J2 b3 [3 A  p4 [
x=ceil(x0+48*cos(a));
* D: M- e- V4 K: p9 B: }$ {+ q; vy=ceil(y0+48*sin(a)*B); ; J' J% S. y5 X. M) }9 o9 _
setcolor(2); line(x0,y0,x,y);} 1 b! X8 ^* E& j; f0 x* |% {& T
setcolor(3);circle(x0,y0,60);
/ j8 ^, w! J& P* @7 K  R/ \8 _/* Make 0 time normal size letters */ 5 Z. ~# |" \& M" N9 C
settextstyle(DEFAULT_FONT,HORIZ_DIR,0); - Y4 W- m) X6 z( a
outtextxy(10,170,"press a key");
& r  M# [6 E3 ~getch(); 4 J8 K) s9 J* {
setfillstyle(HATCH_FILL,YELLOW);
) K" x+ e- i4 nfloodfill(202,100,WHITE); ; y. P) B, y! `$ h, |+ y! @
getch(); " n9 h  q5 ~9 ^. _% Y, b
for(k=0;k<=500;k++)
4 T5 q7 \. ]) \$ Y* g2 R: J{ / C6 g  {7 ]* n' M
setcolor(3); ; G! @' u9 T) c, j1 m& L+ Y
for(i=0;i<=16;i++)
) k0 |" H9 ~. R+ z5 W  Q1 e{ + E2 B  f/ s  a" Z
a=(2*PAI/16)*i+(2*PAI/180)*k;   y/ g4 `4 W; u5 w5 V" z, ^
x=ceil(x0+48*cos(a)); : `& V0 \+ M) P6 G7 S6 D
y=ceil(y0+48+sin(a)*B);
- h2 N) J" e) U. ]! o4 K: Hsetcolor(2); line(x0,y0,x,y);
: Z) ^4 A0 @9 h6 k; W$ x} * t* F! A$ u) F1 K. R
for(j=1;j<=50;j++) ) L5 F" Y0 d! a  y  E; B
{ $ _: V! J% V' n& p
a=(2*PAI/16)*i+(2*PAI/180)*k-1; 5 R0 w5 L4 F; m. X* W. y- Z
x=ceil(x0+48*cos(a)); , }- V: {* p7 q! ^0 n* @
y=ceil(y0+48*sin(a)*B); 4 q0 }- G" d/ W3 \7 v# e/ Z& Z
line(x0,y0,x,y); 4 `' _% y4 `( E8 B* b% c/ C% G
} $ s  s% e% w! w7 t# {0 Y
}
8 ~. ]* ?5 [- ^restorecrtmode();
* F; J" f! }8 e& E; d8 M}
. R" w  [5 e$ {! q5 w2 i9 y============================================================== 0 \+ ]* `. ^6 f1 P/ S4 ?2 a9 L1 {
【程序60
; t6 i) y+ f1 ~9 |$ p1 x- b% L9 v题目:画图,综合例子。 2 C% f- T, m$ B0 O* G$ Z; ~
1.程序分析:
2 u) t9 g* ], T% r5 `# N2.程序源代码: $ p& \* G, U& y8 r9 T5 o
#include "graphics.h" ' g5 R* z; z5 A/ t0 `
#define LEFT 0
2 ^3 d* L* X. B#define TOP 0 , Q+ R4 P0 `& ^" l+ s5 Z
#define RIGHT 639 1 n& h2 ?4 @- i2 k
#define BOTTOM 479
. \' I4 @; e' x8 `$ [#define LINES 400 , j' `6 F; R2 A4 E* z
#define MAXCOLOR 15
8 t) C. ^* t% cmain()
+ K# Q2 B1 n# S( K2 l9 k) ?{ % p( N0 x7 p4 n% B" n$ e0 R
int driver,mode,error;
5 o- z) P4 A" \0 _2 L; Yint x1,y1;
6 @- L& G4 p) l" B) X2 ?! Iint x2,y2; , o) D7 f3 \; M7 k& o: ^. e8 t. m9 T
int dx1,dy1,dx2,dy2,i=1; 3 J6 o$ L; F' m8 k: I" i4 u
int count=0; 6 v5 y4 G4 m7 f& F( z9 O: M
int color=0; 1 c' _- c" d( o8 f
driver=VGA;
  a  h9 X( w7 t3 r! Mmode=VGAHI; " j# Z; R# t( u3 y5 d
initgraph(&driver,&mode,""); 3 K) n: b/ h4 G
x1=x2=y1=y2=10; 6 _! K( P4 G, V. U
dx1=dy1=2; 5 s9 o0 T* A4 p' Z
dx2=dy2=3; 1 f: k2 R$ V; {  o- m" L% b( T
while(!kbhit())
% z$ G7 q  `+ N5 q# o) p4 B{
2 C8 i6 S# l5 S  v$ ^: |+ M, w3 ?line(x1,y1,x2,y2);
& r+ b+ U4 C9 _+ r: [x1+=dx1;y1+=dy1; ( q; U" @+ U% Y. A$ A/ @
x2+=dx2;y2+dy2;
$ Y0 Y7 l) ]0 ]4 g. eif(x1<=LEFT||x1>=RIGHT) 7 U- j" e" [) Y, W
dx1=-dx1;
  A1 ?* n- n  F6 @if(y1<=TOP||y1>=BOTTOM) ' Z7 }* @1 F" N  p2 y1 ?
dy1=-dy1; 8 q  K/ X& p, t
if(x2<=LEFT||x2>=RIGHT) & M% d2 J5 K" h9 }" t
dx2=-dx2; 6 b6 A4 B$ h! y% ~4 S+ [! z
if(y2<=TOP||y2>=BOTTOM)
& M3 b. r. h; F+ G) w! D$ D- xdy2=-dy2;
0 |% O* y9 [" S) j0 _" tif(++count>LINES)
) B3 T( W: s/ {  k/ ^{ 9 j. v6 R! F' v4 [6 a5 S
setcolor(color); 9 N5 C# \$ V$ S: U4 x& v
color=(color>=MAXCOLOR)?0:++color; + [4 o  B  x7 N- T6 u
}
* q* _1 F: O; b. v) s* ?$ F}
' B# q- g7 L1 z" jclosegraph();
1 V7 N9 v) W) {% X}
【程序61$ x* r8 x/ ?* ?+ a
题目:打印出杨辉三角形(要求打印出10行如下图)
3 H3 P1 |0 ^9 A0 s  p# L1.程序分析: : x# P) g, z7 y8 a. P3 U. g
1 ( X( N" g  c7 p
1 1
) o6 t. g2 J- d3 o1 2 1
! J# W/ p5 o9 J" b2 [, I1 u! A: j1 3 3 1 6 v) X* e5 y& B; ~$ U: g/ M
1 4 6 4 1 0 ^. Y8 S# U' C' p  I! A% a
1 5 10 10 5 1
" c8 q2 y( |9 W" D& E/ L) F& i2.程序源代码: 4 t% `& \* s7 X6 n, j
main() ( E, ?; k0 `5 o2 z6 q1 X
{int i,j; 2 O# k7 f6 l, w6 }. Q4 G, J
int a[10][10]; . F( t0 g, N: M' e- Z- q# S, f
printf("\n"); + X% `9 y% w) E6 Y6 J4 @6 g; `8 i. u
for(i=0;i<10;i++) 6 B' ]# |: F% g- @
{a[0]=1;
% d, z! o% t0 e; @a=1;} 6 j8 l' r8 [3 f: q4 l4 w9 J
for(i=2;i<10;i++)
' ^  [/ D* T/ V2 Sfor(j=1;j a[j]=a[i-1][j-1]+a[i-1][j];
9 ~. e5 S/ Y6 S+ u- b  h8 V8 afor(i=0;i<10;i++)
; }* x9 `8 ?9 J& C{for(j=0;j<=i;j++) , Q" [+ [2 P1 K) d0 Y* S
printf("]",a[j]);
# t; ]. _# K8 r3 Q$ |; T5 K7 {printf("\n"); ; g( W9 V$ M- J5 V1 @. K( d
} 9 g* B9 ~2 m) M5 I! I2 V2 X, c/ c
} % F6 H# ]7 F6 Q" p, ]3 A
============================================================== + x+ e; r% c4 x
【程序62
+ ^' ~& @9 p' n' g" l题目:学习putpixel画点。 - t$ `* ^' J! j1 ^
1.程序分析:
4 {% _" [2 s6 i+ F% k) a: {2.程序源代码:
8 {0 X$ d1 c- w4 H2 j/ l#include "stdio.h" . v" z) V# M# h$ J6 Z
#include "graphics.h" & P, n. M' B3 c2 ~. x. B3 W; Z& K
main()
' I/ E$ n+ _" h5 L4 |" z{
/ J$ T) T0 j. X' a4 }int i,j,driver=VGA,mode=VGAHI;   \+ R% i+ ^  K
initgraph(&driver,&mode,"");
3 J7 Y1 ~9 v- D) F2 l( V* Y; hsetbkcolor(YELLOW); ) I1 I6 b5 T  ~) X7 I  t
for(i=50;i<=230;i+=20) - M( c/ F0 E6 `8 ~  x- {
for(j=50;j<=230;j++)
" |) m; w  X; R5 Z* ?* Cputpixel(i,j,1); 3 R( ~3 }! |9 A; O
for(j=50;j<=230;j+=20) ! [1 w, Q; T0 X! \$ N7 F8 L
for(i=50;i<=230;i++) 9 U# M4 m& F( Z# X+ `- m
putpixel(i,j,1);
7 V7 R$ Y; Y) S+ N5 }* J} ; B% L' S% b, c2 e) f7 b
==============================================================
; O: l1 ?( c, o' E1 W8 b【程序636 J6 Y* {+ c0 F  y' c
题目:画椭圆ellipse
6 N1 z" B+ C: e( v" T8 r+ z1.程序分析: ) x$ v2 W* s  {: g% m
2.程序源代码:
7 F2 {; L5 [' A+ n4 M" P2 e: J#include "stdio.h" . [, j  e) i9 r- s! [
#include "graphics.h"
0 |# i2 _- \  U& n3 c5 z0 c#include "conio.h" 1 |2 r4 ?/ @5 f. Y( O" Y
main()
- u; {- I2 K2 w{
* U/ h. x$ x) c+ N5 V0 Mint x=360,y=160,driver=VGA,mode=VGAHI; 3 p5 X$ G9 \5 y6 r4 i$ b+ Y1 Z
int num=20,i; * H$ ]& Y7 i8 X& n# J1 t
int top,bottom; % A3 W$ J( G6 {  |' z' i
initgraph(&driver,&mode,""); . }. P) m/ r7 F7 H! u* `
top=y-30; 0 o# j0 q/ U0 j2 M% k
bottom=y-30;
3 |1 i9 w2 I2 V% y' Dfor(i=0;i{
: Z0 W# U' m/ pellipse(250,250,0,360,top,bottom); 8 t- r4 h* X  h
top-=5;
4 i' ^# V5 {0 c) Kbottom+=5; ( S) _$ ?" w  t7 Q
}
8 D" G3 O, X$ P4 U2 Rgetch();
) d5 v  m1 @# H' P8 Y) n} ; W3 `1 C) m0 ^, ?
============================================================== / l8 ?! o# B+ a. Y2 u
【程序64
& \9 D9 c5 o- Z1 |# ~) X2 h题目:利用ellipse and rectangle 画图。
( [4 U, U' P5 ~- j8 x9 h$ e0 m0 d1.程序分析:
8 Q' J4 Y. J( M1 Z4 J8 g# O2 i# E2.程序源代码: ' M7 ?; |4 O- H
#include "stdio.h" # t( L4 q8 W) {  D& O8 C& P
#include "graphics.h"
- G: U' `8 @2 G1 w, H#include "conio.h"
- R) u, i; s' R$ lmain() ) `# X4 a4 I6 O% z8 P( t" i
{ ( C" Y7 L) I3 S
int driver=VGA,mode=VGAHI; 0 Z: R; t! B- f1 C
int i,num=15,top=50;
1 h+ `9 ^9 F2 _7 j" _. m5 o) bint left=20,right=50; # y. X7 m/ b9 T/ n3 p
initgraph(&driver,&mode,"");
! p* T' l# t8 I, Q7 wfor(i=0;i{
' K) d5 \. \5 pellipse(250,250,0,360,right,left); % A. K4 C9 T; \/ i' }
ellipse(250,250,0,360,20,top);
) O: \: a* w+ _  w! D4 y: t2 Lrectangle(20-2*i,20-2*i,10*(i+2),10*(i+2));
( K) S$ J  W/ Q3 S& }right+=5;
% H8 l  [5 f/ g; |left+=5; 3 ^3 n+ R  p* j4 b! f
top+=10;
, P% _, v! M1 \6 R- D, s! I! w  x} 6 O4 ?# K- N3 c; r/ |
getch();
) l/ Q0 \" g' J) r) _2 n} ' `/ o6 h$ g8 q9 c, w3 m
============================================================== . Z1 g( k  S" ^5 t( x: f
【程序65: o# x% B; [8 Y2 H. N
题目:一个最优美的图案。 / A5 T3 g. l7 K9 O  ~- T% s) C. V! j
1.程序分析:
: a# a/ Y/ e! I+ R& U2.程序源代码: 7 N! q8 G- |0 U: D; H4 d
#include "graphics.h" + B6 L1 o) G! K$ y
#include "math.h"
2 h0 q3 g! r' a* F#include "dos.h" , {/ z' T4 |) z3 g+ ~
#include "conio.h"
) W/ R4 t/ h- H#include "stdlib.h"
, R7 j0 i# L- \7 ?) b% T0 X#include "stdio.h" ' [# |+ K" R" E9 i" ~
#include "stdarg.h"
4 M: H. e( l; E- n#define MAXPTS 15
3 o+ S: O& K8 P9 W8 v#define PI 3.1415926 / G( r# m) b5 Q
struct PTS { " T3 M7 |. r8 k
int x,y;
/ P3 @$ G4 x" y- T: h: A}; / A# ?  A# t: a9 f  M/ d' E7 u9 b
double AspectRatio=0.85;
4 \; c! d" R; Cvoid LineToDemo(void) & O- Y# |, s; w+ f
{
* W. ?+ r9 X' c. T8 Cstruct viewporttype vp;
0 c, Q& {" e  s% A; Jstruct PTS points[MAXPTS]; : j: ]/ [' R. z; c6 {9 K' U" n
int i, j, h, w, xcenter, ycenter;
* ]% }- b3 [* ]8 W+ _& Q; X& Lint radius, angle, step;   D$ G& J" \) U8 o" b% _
double rads; & ^' e9 j" t3 w2 z# g: J2 J1 f
printf(" MoveTo / LineTo Demonstration" ); # D& @. ^2 |% A- d3 `
getviewsettings( &vp ); 7 Q0 {: O1 V) S
h = vp.bottom - vp.top; ( ^" U  n# _& \0 O& Q) t# c
w = vp.right - vp.left; " ]" @8 h" I) f2 F' b6 H  z
xcenter = w / 2; /* Determine the center of circle */
8 u2 h; O" j% }9 x. N) O9 d9 pycenter = h / 2;
* o: x2 k) n. X/ v  p7 n8 Jradius = (h - 30) / (AspectRatio * 2);
& Y( x! T+ J6 v9 ?step = 360 / MAXPTS; /* Determine # of increments */
- \$ I  l( W7 v! f# \3 ]angle = 0; /* Begin at zero degrees */
  k: F$ h+ F- @7 j7 P6 F+ Xfor( i=0 ; irads = (double)angle * PI / 180.0; /* Convert angle to radians */
* n! R: m& k+ y. |4 E* B. |; Ypoints.x = xcenter + (int)( cos(rads) * radius );
$ k" c9 P- }! k. n5 I9 Qpoints.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
0 h% r! o* N* {angle += step; /* Move to next increment */ % [" q$ D2 e3 q8 C2 ]9 K& p, Z
}
1 ]& q1 U; n  Acircle( xcenter, ycenter, radius ); /* Draw bounding circle */
- s9 |2 f  U- J0 E) M3 y- Vfor( i=0 ; ifor( j=i ; jmoveto(points.x, points.y); /* Move to beginning of cord */ $ M8 U7 {  j* v$ x5 v
lineto(points[j].x, points[j].y); /* Draw the cord */
5 R1 q/ Z% J2 X+ F} } }
1 C, a( v. U. K, u0 Vmain() # F0 j* E* \( D. O$ d' T
{int driver,mode; 1 X) h# B# r' \* y
driver=CGA;mode=CGAC0; 5 j& K" I$ p6 y& Y; O: t4 x  R
initgraph(&driver,&mode,""); ' T! i8 J$ |7 u+ b: B6 x- D
setcolor(3); , \2 N% b* H! U5 O) ?9 e5 r
setbkcolor(GREEN);
9 z9 |* ?6 `. ALineToDemo();}
  d6 R5 P' T( c. v==============================================================
3 C, n% |3 h6 K" u- I3 n5 ?【程序667 z  {& P7 e+ X8 S+ k
题目:输入3个数a,b,c,按大小顺序输出。
8 O* r1 U! h9 C/ q; T. n2 N8 }1.程序分析:利用指针方法。 5 ]  W* D0 f& {, {: x/ C$ @) s
2.程序源代码:
2 A9 W9 U- w$ ?7 l/*pointer*/   r4 J- }1 D& T) p9 r$ w
main()
2 w4 m& T; ^$ A{ : {& B: Q  M' n, j. N, Y
int n1,n2,n3; , U% ^& D! T- U6 e: W# v: r
int *pointer1,*pointer2,*pointer3;
  S; \; q7 j/ P5 d7 {! aprintf("please input 3 number:n1,n2,n3:");
& W- n8 J4 [* p2 T" v6 Tscanf("%d,%d,%d",&n1,&n2,&n3); + j. y' @1 F2 t/ U/ x
pointer1=&n1; # i& v' `, l! `, R
pointer2=&n2;
1 I8 o& i7 m% C8 m& Qpointer3=&n3;
+ B& _9 ]# |- O4 Bif(n1>n2) swap(pointer1,pointer2);
2 C1 T" B4 B1 L% p7 g( Qif(n1>n3) swap(pointer1,pointer3); : b5 E/ H1 ?0 b5 ]/ X; v' ~, j  [( `
if(n2>n3) swap(pointer2,pointer3); 5 g' W* v" S; i  ~3 Y. u( F
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3); 8 i! y' [/ H4 C2 I. _+ c
}
/ ~0 [8 {! `$ s; @- qswap(p1,p2) ( k% \2 X; @4 A( y
int *p1,*p2; ' H/ B8 U( {  r) S! I
{int p; / Q, Q! H2 _& h( ]5 c
p=*p1;*p1=*p2;*p2=p; % O5 N+ Z$ e, N8 ?, q: S# x
}
3 o6 p( u% b' g6 A==============================================================
) A. h- f1 h5 g; [【程序67- y: @0 g) a9 Z# @% [
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
3 G6 F0 E3 s, u, v% Q7 d! \1 H% _1.程序分析:谭浩强的书中答案有问题。
% E- d; q4 @9 T% V9 m6 b2.程序源代码:
" t% ~* E* G9 tmain() / H0 _5 C% _" ~# _, u9 v. W7 G
{
$ N  e6 l) [6 p$ L7 C. g9 ^int number[10];
: A) L4 C) Q: `" }: m$ c5 Yinput(number); ' \7 V+ K' G+ z1 p! o
max_min(number); ) U7 h, F6 f2 G( [0 r4 C, n3 N/ f
output(number);
& s( n. g7 _& b# q- F} 2 u4 D! Y# h; K5 l1 H7 ~: p
input(number)
: m, f. h2 v/ }. g) J) [int number[10]; * K. v! I- I+ _  N6 A5 |
{int i;
& o$ r: h- x# L+ ufor(i=0;i<9;i++) 0 b- V2 h% R. D8 u- I5 ]& ?% @
scanf("%d,",&number);
; u2 e9 b% I, G2 D; I7 p( c4 b$ g: escanf("%d",&number[9]);
5 P3 B9 }" }( t9 f+ g} & Q7 D, v7 S( q; b( U
max_min(array) ; B! G5 w; p- t  s% M/ p2 j1 j
int array[10]; 1 h$ N/ W9 k1 f2 B0 W* I$ m
{int *max,*min,k,l;
: M# k- \& S: c' Xint *p,*arr_end;
# E' l& U& ^7 z& [6 K8 x& ?6 sarr_end=array+10; 1 {( H$ @# n, U+ u' F
max=min=array;
# [8 R6 s. ~" E; C4 S" N6 a' @, |for(p=array+1;p if(*p>*max) max=p;
8 Y! [9 c& Z- U1 z$ v6 Delse if(*p<*min) min=p; 3 l$ I& X, v, u
k=*max;
8 W9 J" h5 u% Vl=*min; 7 P7 j2 `) j% H7 u2 Y3 {
*p=array[0];array[0]=l;l=*p;
. u" X# {! Q1 l2 I. o*p=array[9];array[9]=k;k=*p;
0 x, G' E/ I# I5 ireturn; , h+ e- c$ z! i9 U9 Q
}
- j2 V- b3 \: z9 joutput(array)
; j  S0 T; E6 x7 l1 c$ o" \$ vint array[10]; / x, H$ H" ~  [
{ int *p; 0 t- F$ f) @( V
for(p=array;p printf("%d,",*p);
& `5 n5 U2 k7 F, `/ q8 h  u; Kprintf("%d\n",array[9]);
2 a- w& z' x2 k% Q7 P} / E4 J* R. ~1 k) V# }& L
============================================================== 6 z+ S" t- F& r8 `! m& P# q

# x. v- f$ o/ ~& n7 t( k2 D) J

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-5-29 05:06 , Processed in 0.093750 second(s), 23 queries , Gzip On.

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

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

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