找回密码
 注册
关于网站域名变更的通知
查看: 461|回复: 1
打印 上一主题 下一主题

C语言经典算法50-67

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
【程序50
1 Q  ~* W- o) B! u! q6 L题目:#include 的应用练习 1 a+ D" T+ t9 _+ f* R% Q) s4 ]
1.程序分析:
0 g. t4 P% |$ c" b2.程序源代码: 6 U. R# ^. [# `9 P7 m* k1 F
test.h 文件如下:
6 b- i# b5 Q4 @# V+ c/ U/ Y1 p#define LAG >
4 x! u0 z8 s" q7 ?) {' F#define SMA < 2 b/ h0 H5 }( ?! r% r
#define EQ ==
5 i: d1 n$ o- b' s& U4 v% K$ N#include "test.h" /*一个新文件50.c,包含test.h*/
. L! c/ `- |* j0 c#include "stdio.h"
4 d; I/ U6 L( j6 Nvoid main()
7 |. B1 Z# d8 |{ int i=10;
5 I# L2 k1 A+ d- z) q* ^8 Oint j=20; 8 T+ i9 U; d- T. X3 g; e9 [5 w9 w
if(i LAG j)
/ Q* L& p# }& Uprintf("\40: %d larger than %d \n",i,j);
, s& H1 n& f5 ?: P* O) F' gelse if(i EQ j)
; R5 Z' Z# A: U# s9 g" a1 ?5 D5 lprintf("\40: %d equal to %d \n",i,j);
( Q! G% U2 ]' A1 t3 lelse if(i SMA j) " Y! t3 d4 A% X  h5 x
printf("\40:%d smaller than %d \n",i,j);
4 a# ^/ \4 r2 Relse ; l1 B% b( p; V/ j( p
printf("\40: No such value.\n"); 2 V3 I6 E5 N( g" \2 [
}
" e, H' x* o' J! }' Y【程序51
7 z; _$ }% t! E: X6 q6 P题目:学习使用按位与 & ( z7 g& H3 I- o
1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
4 L5 n$ Z" J7 w5 X4 A9 f2.程序源代码: ; ^8 M  {% r5 e3 B: _8 u
#include "stdio.h" ; ~5 r0 V7 z% a" Y, H/ ]/ N
main()
% f) M) N0 _; H& Y: }9 ^{ 4 X: v6 e) H: X. _: P; |
int a,b; , O& E( N: \4 _
a=077;
( i0 \8 c7 u: x, @& Wb=a&3; ' C9 {/ B1 ?! b8 |' c4 j1 e' j
printf("\40: The a & b(decimal) is %d \n",b); . P# I- s- k: M- G, [' H' w
b&=7; % D7 S3 |) w9 O' ]
printf("\40: The a & b(decimal) is %d \n",b);
- A8 C9 M+ d! G4 ]3 |8 A1 E# L6 v} 0 z6 a: a& I0 E  [  k/ X
==============================================================
& w7 j9 M( F* _" E( ?$ m【程序522 \, g+ N! J& C3 {: v, h) U
题目:学习使用按位或 |
) o" h) D! N; m% d! C( W4 _/ {1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1 & W  U6 d) b! U* H. z9 F0 V' [
2.程序源代码:
9 L0 z. g+ z0 {' O6 _#include "stdio.h"
- P9 Y0 x$ i8 hmain() % ^& N3 p" r, W! ]6 ~
{ ) s' |1 m  {4 w
int a,b; $ _7 \# i; I4 ]; g3 c
a=077;
6 d7 U* C, z- w7 j/ C/ j5 wb=a|3; % q# o' s' W9 C' x9 Y, E
printf("\40: The a & b(decimal) is %d \n",b);
" J2 v' ^8 ]& f/ r- v& cb|=7;
* R1 u1 q9 u" ]/ l0 M: T1 xprintf("\40: The a & b(decimal) is %d \n",b); 2 ^/ l) _0 w; R# }! F
}
+ s$ b7 f2 e* ~==============================================================
0 B: q! @/ V9 y& l【程序53嵌入式信盈达企鹅要妖气呜呜吧久零纪要 & B4 \4 k$ Z. T
题目:学习使用按位异或 ^
- e8 v$ `& x5 Q: E1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0 & {/ v. b2 P0 S4 C& G* ^& `8 G, b
2.程序源代码: ; A. h, y  T& g& u  H& M
#include "stdio.h"
) x! B. r! T3 rmain() , V0 i! \- I3 K2 \7 x3 N% E
{
6 R5 k1 s& x* R* K) Mint a,b; # k& ^  U8 ^; q2 i6 r; {, A: n
a=077;
% S/ ?0 T6 m( O5 ~3 M( c/ ^3 Tb=a^3; + i' ^: q3 O+ j7 ~
printf("\40: The a & b(decimal) is %d \n",b); : t5 [2 v. W0 i
b^=7;
6 x* B: i/ s! K; xprintf("\40: The a & b(decimal) is %d \n",b); 8 t8 Q+ w# U6 P& n. j, G
} 6 P. B! n8 ?, ~. H
==============================================================
* S1 }: x* B* j2 J8 i9 e9 m: x: ?【程序54
& A3 \. o2 }* ]5 }: H题目:取一个整数a从右端开始的47位。 8 l* @1 w( C  S  {8 F3 ?
程序分析:可以这样考虑: ! x0 Z- f4 l0 u% e/ D1 O
(1)先使a右移4位。 ) E6 i4 |# L1 ?0 Z
(2)设置一个低4位全为1,其余全为0的数。可用~(~0<<4)   }, E+ U5 O+ l8 w4 t
(3)将上面二者进行&运算。 4 C! u8 w& `% j6 K, K% I# \" |
2.程序源代码:
) C: ]; c  E7 p  X' Cmain() : O' a8 Y  ?; u7 L1 G6 a2 ]
{
7 ^6 i9 k, {/ e  V: [4 L- [3 Bunsigned a,b,c,d; % Z3 ~% J& Y5 J, ?
scanf("%o",&a); ! L& M% o3 o" N4 A, C/ h
b=a>>4; . R( S- U$ U) x% u& h! l6 M$ @
c=~(~0<<4);
) {, c& ?; ~4 N' Q: vd=b&c; ; [/ u) Z7 h2 Y$ w+ G  S1 g
printf("%o\n%o\n",a,d); " p7 o, S# w( ?" L  i1 Q4 Z8 b
}
4 r5 x9 I: n' ~8 B2 C3 h& p7 n==============================================================
* D5 k6 E! _8 a【程序55( R* q, e' k! Z; m# Z& d
题目:学习使用按位取反~1 @) o2 r: O( p; K4 H
1.程序分析:~0=1; ~1=0; , S( G; P3 Z6 b  L9 u: R
2.程序源代码: & d# S! @- C7 y5 V8 `7 @8 z
#include "stdio.h"
& j$ b& j% [  X7 x1 K* ?0 amain()
) L" K; N1 L% @3 |- b$ P' w{ - Y  k" p, {; g: o0 a2 h
int a,b; $ K2 N! k. i+ I7 v- g' |
a=234; 2 C) Z5 Y6 O8 k- u, {7 ]3 E
b=~a;
# D! N1 E  p9 oprintf("\40: The a's 1 complement(decimal) is %d \n",b);
6 ?; m0 D( C. T1 T  Ka=~a; 2 Z5 z/ f+ i# ]% S5 |2 w* G
printf("\40: The a's 1 complement(hexidecimal) is %x \n",a);
7 a/ A5 o* p' r} ! _# C8 g3 o" e5 N$ N) K
============================================================== : n) J  v$ w$ o0 t8 I( N* _
【程序56" j' \* @8 q/ {$ K- ]
题目:画图,学用circle画圆形。 1 p4 H3 K) d$ k4 m* w
1.程序分析: 9 N. I2 y( ~2 h# @
2.程序源代码:
; w% ?' W& c" ?' i/ X( S  r$ q: G/*circle*/
4 z1 E7 p! @4 l8 e- L# U3 U#include "graphics.h"
/ f$ {7 ^8 H& \main() " q/ }* }; z0 c/ d
{int driver,mode,i;
" l4 g' H' C8 x) A) m( Wfloat j=1,k=1;
2 D" G% P5 W/ _  l: m* {( jdriver=VGA;mode=VGAHI;
; b/ i  L# \% L1 `$ X( h& [initgraph(&driver,&mode,"");
/ x( w- b; ]; }' tsetbkcolor(YELLOW);
5 T* ?& f$ R: A/ K  a. Ifor(i=0;i<=25;i++) 8 l- J1 w' o# d8 H0 s9 t3 J
{
" u6 q# |& w$ h3 l$ Asetcolor(8); / J+ K. ?! R9 \( I1 @, J
circle(310,250,k);
8 A' Z& h% P4 j. I) X6 jk=k+j; $ V0 K5 x  [/ K" T/ Y4 g' B
j=j+0.3;
2 r( {  B# `5 @/ \1 K} ; h" X, Z- \/ G( {: L% f1 J
} % g1 s3 O" l1 @
============================================================== ' l; M  G% @) ~
【程序57
+ H+ K- T  S$ S% ]6 c题目:画图,学用line画直线。   }$ m8 K8 {* I  W1 B# c$ j
1.程序分析:
8 M) Y8 y) N: G+ w" `6 ?2.程序源代码: ) t8 Q3 Q, E% S" `1 ^
#include "graphics.h" , F. ]" l- `6 I0 M# \
main() 7 v. z& S" `1 ~1 H. l/ j* S
{int driver,mode,i;
# E/ S2 d3 A. {: J' Q( L5 R7 Wfloat x0,y0,y1,x1; + Q" N  _+ {; V& H( w" B
float j=12,k; # d  [; z& n6 B
driver=VGA;mode=VGAHI;
) `& o1 ]3 p- g/ r9 [4 q$ a% Ginitgraph(&driver,&mode,"");   ?0 Y0 S9 H; d! D
setbkcolor(GREEN);
0 P5 V( e) l- W* q/ M5 sx0=263;y0=263;y1=275;x1=275;
; F, B4 R/ y0 a5 m, l9 [for(i=0;i<=18;i++)
6 q: Z6 t  {3 H5 L' A{
5 g  n2 M2 H2 |, |setcolor(5); / ?' E8 H- j& ?% X: ]7 o* z9 q* G1 `
line(x0,y0,x0,y1);
+ V+ g+ L" `$ ~. d, fx0=x0-5;
& }) K3 @- ^: }, ?y0=y0-5;
& a& @- _# M0 ~" G5 r: q' Ox1=x1+5; 8 Z" {/ W# @6 m" ^
y1=y1+5; 1 h& e8 G" j% n. L- p7 }2 a
j=j+10;
6 s9 t& ~0 ^2 R& v; z- H7 U}
; z6 K. Z5 o7 lx0=263;y1=275;y0=263; 2 P6 q0 |9 R+ J2 W
for(i=0;i<=20;i++)
5 H8 v+ {5 e. B  s, x{ ) X- j3 J0 S! W) @8 T
setcolor(5); ) X' ^0 C0 q: B7 A
line(x0,y0,x0,y1); ' W2 P1 t! f2 \, a# r# t% ^9 h
x0=x0+5;
% ]3 p# n4 J2 F. r; g' \( Y' ty0=y0+5; ' E3 t  M& G  |4 d9 y6 l* I
y1=y1-5;
* h; P; Q! ~7 |% O0 X* }* N} 3 [7 J( m. H# C$ F2 a4 {
}
( `& w$ a+ z& Z, N; d4 s. C============================================================== 1 @7 F, i+ c8 r' c* S7 K3 n
【程序58+ K7 P0 k5 F/ M4 m2 ]
题目:画图,学用rectangle画方形。 7 [' V3 A  H( X/ H( O3 t' F3 S- k
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
! u8 p' T$ O0 L. S' H2.程序源代码:
4 Y% v( V3 m" p8 I3 e#include "graphics.h"
% d: l& C3 M5 b/ J8 C% Omain()
  x) q+ n" A( E$ x{int x0,y0,y1,x1,driver,mode,i; * f" R+ Q) b; |8 z# b
driver=VGA;mode=VGAHI; 1 P  P0 D0 N, _! X) ?; H7 }
initgraph(&driver,&mode,""); 1 V7 k4 p0 G( g( b& m# p0 T
setbkcolor(YELLOW); & G7 \' F0 \; A7 o2 }
x0=263;y0=263;y1=275;x1=275;   o# D" \* L; D; N+ \
for(i=0;i<=18;i++)
  u; X" B5 U8 J9 D{ : d) {* @& i6 W* f7 Y! G5 I: W
setcolor(1); 5 v# c+ ^) V- o$ V7 @/ k. k! q8 x
rectangle(x0,y0,x1,y1);
5 S5 b4 R$ x; C" ex0=x0-5;
. S1 P4 s* V) f1 E* q9 ky0=y0-5; ) p$ a! o9 \7 Z8 v( Z
x1=x1+5; 7 M  z" g* f2 [7 ^: w! P
y1=y1+5;
6 n5 k1 t4 w. e2 U9 B7 k! v1 g}
$ N4 }6 A' N% D2 Psettextstyle(DEFAULT_FONT,HORIZ_DIR,2); : I* x" c  ^$ G8 g
outtextxy(150,40,"How beautiful it is!");
2 n" ~- S! |! p! q$ Z7 t1 i2 aline(130,60,480,60);
0 }; G3 k. V6 b# C4 Ysetcolor(2); ) \" G/ d6 I0 H+ i% d  R+ h
circle(269,269,137); 8 j! S( _3 U/ Z8 q+ B" B
}
6 G# c& h* a. f( Z==============================================================
/ I/ m7 W  x( Q. h9 l; c- i+ j* O【程序59
. A) E8 n1 e7 D题目:画图,综合例子。
0 A  `, ]' e, B8 U" d1.程序分析:
5 M% n* p3 u- p2.程序源代码:
6 A: g. P% Z$ }: d1 [% w/ P# define PAI 3.1415926
. l4 g: H  X+ ]# G) }2 T0 H" U" U# define B 0.809
9 |8 _9 |8 q' V0 n& X; |4 z# include "graphics.h"
0 e0 T: }% K  s7 A, Q& o#include "math.h"
6 z" ?* T* J* A. Qmain() . L6 \( m7 _* \1 \- S
{ . d1 f- q$ }' m' H7 x
int i,j,k,x0,y0,x,y,driver,mode;
  n" C$ B1 ~2 d6 _- L& S3 F* sfloat a;
- X3 y. C" u8 Z# adriver=CGA;mode=CGAC0; $ O* |: F4 y3 A" A
initgraph(&driver,&mode,"");
& u1 D8 T( M/ |( W! t8 g; nsetcolor(3);
$ y) e6 v' R" G! W4 T+ w9 C* lsetbkcolor(GREEN); % k2 Q' ^: h7 a5 G) F6 @' U
x0=150;y0=100; ) W7 q8 D7 o9 O7 f2 S1 P
circle(x0,y0,10);
  u5 \- g/ `3 [- A+ C8 Ucircle(x0,y0,20); % B2 A- R( w' U
circle(x0,y0,50); " O; w* C# M2 ?2 i0 `
for(i=0;i<16;i++)
, \  E8 _* d1 u7 I; I3 u' Y2 u/ s{
% A  J. w) d6 y+ l4 |" q) Ua=(2*PAI/16)*i;
# q7 r- F. P# s" Z( yx=ceil(x0+48*cos(a));
1 s6 f7 `+ u' Dy=ceil(y0+48*sin(a)*B);
' w7 f+ ~4 u+ T# psetcolor(2); line(x0,y0,x,y);}
. }7 E( t; U/ K0 p3 @* c; csetcolor(3);circle(x0,y0,60); % d% u  `, Z* Z& E: k
/* Make 0 time normal size letters */
: X5 a9 j3 b6 C6 ~8 Gsettextstyle(DEFAULT_FONT,HORIZ_DIR,0);
$ h% o: N5 s& S% h( S0 A7 {4 Houttextxy(10,170,"press a key"); " ?8 S5 z/ v4 x$ }$ u( e9 m
getch();
( Y& J( k% M/ [$ vsetfillstyle(HATCH_FILL,YELLOW);
9 \2 |2 G3 {# n. b, ^, ufloodfill(202,100,WHITE);
/ C; K/ s$ g8 B7 U0 }2 f- r/ H+ ggetch(); : ]& t: z6 V$ R' p4 ^( ^
for(k=0;k<=500;k++) 3 j8 z7 v: F7 k. O6 J1 s
{
  g6 j& c) h; u4 ~% L& {3 Wsetcolor(3);
( j7 Z" X' Y# d5 T- g; Sfor(i=0;i<=16;i++)
9 A- n8 L' i4 {{ 6 C" `' ^- X2 y# U% n
a=(2*PAI/16)*i+(2*PAI/180)*k; 8 k4 M6 g$ p. n  f
x=ceil(x0+48*cos(a)); , {3 ], `, W% d6 w/ k- b6 N; ]" C
y=ceil(y0+48+sin(a)*B);
# O. Q) {6 O7 u/ F5 O5 l3 m) Wsetcolor(2); line(x0,y0,x,y); 4 R5 L0 M1 @) h9 R7 n1 |
}   G0 {- t) v& h  a# R0 Z
for(j=1;j<=50;j++) 3 W8 A. |/ I4 W2 J$ R
{ * S6 q) Y& e; [
a=(2*PAI/16)*i+(2*PAI/180)*k-1; % t1 R- k+ s: b( B: c
x=ceil(x0+48*cos(a));
% H5 y7 p, S+ ]1 Cy=ceil(y0+48*sin(a)*B);
& A6 t" C0 T& z* _- kline(x0,y0,x,y); - R$ p- ]$ E+ V0 f* t
} - _7 A  v3 ?1 W, E9 t, b
} 2 |9 a7 k& n5 w
restorecrtmode(); ) I3 s, |4 s( K! A
} , m, q: c; H8 ?/ `: z
============================================================== , i0 h5 X. D8 V/ M
【程序60
/ u+ [3 l) U/ w4 N, J% f% U题目:画图,综合例子。
# q4 M$ ]/ n3 A. E8 }6 g& ?1.程序分析: 5 _: `- O" d% ?0 m: J" g
2.程序源代码: & S! [+ k$ P7 @- Q1 @; m; S5 N2 r
#include "graphics.h"
% k7 h; ~9 ~" q, c! Y+ R( F#define LEFT 0
6 ~- P8 H# t% o#define TOP 0 5 t1 J( g( u) A( D/ H
#define RIGHT 639
6 K# ~# N9 c0 D, i% y#define BOTTOM 479 ; m: d4 S9 O1 B
#define LINES 400
+ Y1 P) t% a6 y! i+ z) p  D#define MAXCOLOR 15 - u. B, O6 s0 R
main() $ {2 F/ Y5 w$ i/ o
{ : b- [' x* Z7 \* w# d" ~
int driver,mode,error; + W  _7 t$ x2 m' f# h" \) O
int x1,y1;
! p+ o+ h+ n2 W+ V! }) y# Qint x2,y2;
1 W% g5 ~1 p5 E% M4 gint dx1,dy1,dx2,dy2,i=1; 7 X; u; k$ G9 {8 q4 I$ v4 F/ }
int count=0;   X$ x) f" \: q* L
int color=0;
4 [. h. @+ X1 J% Vdriver=VGA;
( m3 F  h8 U, y6 C; \( R' v* bmode=VGAHI;
7 o7 x2 ?) X0 Einitgraph(&driver,&mode,""); 0 j5 l" `. M, N0 F
x1=x2=y1=y2=10; ' v& F* h; S" S
dx1=dy1=2; 4 k5 M+ Q& R! \& {/ C
dx2=dy2=3;
& U/ X3 C' N$ L# bwhile(!kbhit()) 9 @# e# u/ N, p* }2 O
{ 1 R. F) D5 u( v4 B3 `  P- h
line(x1,y1,x2,y2); 8 Q+ i$ W, v) W8 d& Y2 q
x1+=dx1;y1+=dy1;
# V6 }% a) n; Ux2+=dx2;y2+dy2; ; ~( D3 H. M4 C9 I; [0 v6 h6 N4 p
if(x1<=LEFT||x1>=RIGHT)
  D$ Y- ~# A- qdx1=-dx1;
4 F1 i+ S7 `& |2 m- Aif(y1<=TOP||y1>=BOTTOM) $ C( q& H. H# |1 N) c
dy1=-dy1;
" `9 e0 U) p" h' fif(x2<=LEFT||x2>=RIGHT) . v: M$ |* P# o' B2 X4 g* k, i
dx2=-dx2; ' ]; h5 E0 v) @4 F! @) {7 b
if(y2<=TOP||y2>=BOTTOM) # e4 |# J! K8 W# H2 y, |- G" _; ~
dy2=-dy2;
8 f4 i) [2 I2 J: h4 t# Q8 yif(++count>LINES) 0 H% n! b: I$ y3 D( M8 l: J
{ ' P# v% i" ~& i' \
setcolor(color);
: Z0 E+ {4 \! Q5 t9 k/ j$ ccolor=(color>=MAXCOLOR)?0:++color; - E! a% n* E- Q" k8 x! `! p# `! O% R
}
# z2 s: `( N9 V5 k* b; c7 o}
6 V* e/ _- Q) _7 l! x0 ]8 bclosegraph(); & q  I% Q. O) N1 a% J
}
【程序61
; C8 o# Y% {$ ]; s4 H% n) W题目:打印出杨辉三角形(要求打印出10行如下图)
1 M7 C5 k: G/ z1 E0 j! z- R1.程序分析:
3 r6 M$ z' p# u1 # j7 `& X6 O- R( D  ?1 r
1 1
5 {: _9 i7 n1 {3 [" U5 L9 y1 2 1
+ _. r: h* V0 |9 H. C1 3 3 1
( O' F% r( L. s) y7 h  }6 k1 4 6 4 1 ' n  S2 ^/ a, Q+ Y
1 5 10 10 5 1
# O7 K: Q7 W5 Z2.程序源代码:
$ U) \7 T; U& I+ t  T* h9 Rmain()
. i  r& }0 ]1 ]2 y, f: d' H4 N{int i,j; 3 I3 _) U4 z. t
int a[10][10]; ) W9 j1 y/ x: K2 c; m) Z
printf("\n");
" G+ p. J* O: Z) Y3 Nfor(i=0;i<10;i++) 8 |' |: {, Z2 S/ R4 S, T6 `
{a[0]=1;
/ [. ^) M* }1 ~7 `( Qa=1;}
7 P* f* P5 \9 K: v# }1 ]8 xfor(i=2;i<10;i++) - i) |. R0 A& l2 P1 e( c1 d7 I: ^
for(j=1;j a[j]=a[i-1][j-1]+a[i-1][j];
# O7 z- u. C6 H( `6 yfor(i=0;i<10;i++) % t- t+ Q6 Q# i3 d: ?* Z
{for(j=0;j<=i;j++) & O7 o( N2 j  ]4 D) c9 d
printf("]",a[j]); ( x$ U, m3 o2 B( ]( m
printf("\n"); , u4 V7 r5 E4 k' I( }
} 3 R3 K1 n/ s, ?: ?  G* H
} 2 g0 R: k, s: u3 C$ l$ q2 U3 T% {
============================================================== " j9 G- S  a( r$ K
【程序62% T# U9 O7 [0 o- t* Z6 P0 e2 L  p
题目:学习putpixel画点。 : _% ~) e% ]. `: X& {" J; H" p0 K9 w
1.程序分析: 8 K% H, g; z0 ~) B8 ~
2.程序源代码:
7 a" V( C( T6 Q. J; q$ A9 ~#include "stdio.h"
4 N: F4 m* s0 F9 ~# Q#include "graphics.h"
- g: ^  S6 G" B% {5 m& wmain() 1 V0 u5 [/ m4 r
{
& x7 ^  C3 q5 M5 t) }4 h% C. Y3 Bint i,j,driver=VGA,mode=VGAHI;
: m2 C0 O, J* _3 w4 C$ E; sinitgraph(&driver,&mode,""); 4 Q" @, q6 x+ m8 W3 W: E/ E! P$ F
setbkcolor(YELLOW);
4 u% r5 C* k0 ^* Cfor(i=50;i<=230;i+=20) ) b3 S8 j5 x/ h0 c" T, u
for(j=50;j<=230;j++)
/ G- s. g. w0 G, o4 x+ ]putpixel(i,j,1); 0 s( [8 o0 l. e
for(j=50;j<=230;j+=20) 7 P3 b2 {$ @+ O4 e3 n0 O' [
for(i=50;i<=230;i++) " H3 r/ |, S0 Z/ F$ |
putpixel(i,j,1);
- L! x5 p( `+ e9 R/ [5 ^} 6 L, ~* @( q3 X' ]2 ~) j% f
==============================================================
& N% {1 ~/ O: d& C$ q( f【程序63
& }, U* S5 h- w6 r题目:画椭圆ellipse ) D4 G5 d7 I5 R, a( C
1.程序分析: . B3 t6 F; r4 {" }9 h: n
2.程序源代码: & J4 w. e1 _. R. w: Q9 v7 C' \# T4 ^
#include "stdio.h"
! a% G6 v/ S$ Y4 j2 @8 b* }* P#include "graphics.h" 7 X; q* j0 M8 Q( P( U1 b
#include "conio.h"
8 e7 @" l2 e  [' _main() 9 W% l, V& S6 `4 ?
{
2 [5 }. @- ?2 Wint x=360,y=160,driver=VGA,mode=VGAHI;
6 R3 \+ z# W' i  W: A2 Wint num=20,i;
) O& o- r3 Z5 B: c3 pint top,bottom; 0 x4 C& N$ z& j2 Y& j
initgraph(&driver,&mode,""); ! U7 G7 s3 N. D; F- C9 @( d
top=y-30; # k' V( D$ q0 e3 p/ K* U
bottom=y-30;
3 u* E, o( \7 `, e% Vfor(i=0;i{
0 H, c, t1 x9 Q- |& A) Pellipse(250,250,0,360,top,bottom);
) y$ i8 l. t1 l0 P0 P8 D$ p. ftop-=5; 8 |, x# W' t4 V2 Z, x
bottom+=5;
, n% R9 @  ]* {" Z6 [# B! o}
( K% J" M7 s& O7 o! U% ]! Pgetch(); ! Q, L9 k3 O6 o; ?
} $ D1 O" Y9 g( }* n( y1 E5 V5 ?# Q
==============================================================
4 D+ u0 Z' {5 t2 q, D) C% Y【程序64
8 Z# b( L( v( F  @; @# A  {题目:利用ellipse and rectangle 画图。
% f' K" h5 y) C1.程序分析: ' U, w. A8 E) N, g
2.程序源代码:
  e3 H1 N2 u7 W# a: ~#include "stdio.h"
( a6 [, G- e6 ^4 Q#include "graphics.h" ) t) J% i" p/ f$ n; D5 H
#include "conio.h" : F) |! y/ f. t! t7 R* P5 `
main() 0 ]! [6 j8 [  ]# y7 ~0 r
{   H1 ~" d  \: x$ ?6 [0 _! |
int driver=VGA,mode=VGAHI; 0 V% f( H* N, G2 b; a
int i,num=15,top=50;
2 F2 o/ B$ O3 u' Z, s, ~3 ^int left=20,right=50; % C) C9 @4 {; Q
initgraph(&driver,&mode,"");
2 r8 V- G  N, N2 U7 Bfor(i=0;i{
' E0 ~6 Z- R& G0 a  Wellipse(250,250,0,360,right,left);
% o! X* K( e% h: B% ^ellipse(250,250,0,360,20,top);
& y* l/ e5 t& a2 Erectangle(20-2*i,20-2*i,10*(i+2),10*(i+2)); 4 P, {' l# i3 A( t
right+=5;
2 ^% N, `! n' J/ }& C$ dleft+=5;
* @8 F# i* `3 E; _$ Otop+=10; 9 y! }: [% S! Q: X9 e
}   j3 E4 Z( ^0 g5 S/ F9 N. L/ G4 s' Q. N
getch();
  v* y. h9 d) ~8 A, r7 I}
% s- y: H6 G$ y0 b9 _============================================================== ! ~* q7 k; y7 D# ^+ C; L+ L7 j
【程序65
, P; i) p4 d1 J3 r6 X- {' K题目:一个最优美的图案。   V# n# {7 \) Z5 w2 R0 x4 `
1.程序分析: & \1 b* e8 p) `! }# l
2.程序源代码: 3 {6 b6 R4 e' E' w
#include "graphics.h" 8 @9 @3 q' u+ L+ N4 M' p
#include "math.h" " O; v* {# u, d% P0 r* a
#include "dos.h" % _& x. n3 n# T8 B
#include "conio.h" ) G3 [5 C8 B$ U& ~$ Z4 x
#include "stdlib.h"
) e8 t* _! L; N' h#include "stdio.h"
) ^- J1 [& t/ j; D& \8 G7 m3 s3 j#include "stdarg.h"
! Q, V7 o2 C1 h9 a#define MAXPTS 15
) v$ ^* w6 F* ^4 i9 m+ S#define PI 3.1415926
' s8 t  G, k/ t) D  T/ ~! Qstruct PTS {
8 T5 v: F9 z, I" Q/ A+ @" r/ |int x,y;
! }5 @9 R- |' ^- p7 E3 ?}; 4 a) E. O4 k, T8 p! q! L
double AspectRatio=0.85; 2 B6 q% i' K# d. z* }1 s2 X
void LineToDemo(void)
! n. m9 k% P( ?2 ?9 c; |{ : L1 q2 [+ V) T: z$ K
struct viewporttype vp;
4 z& |8 R( W/ \% @8 \1 x# l4 \, ]struct PTS points[MAXPTS]; 7 L( _% u2 v% }$ L% |
int i, j, h, w, xcenter, ycenter; 6 B2 c  K9 J1 @
int radius, angle, step;
, D2 a) [- a9 K8 [double rads;
/ L- s- Y8 a8 z2 {* eprintf(" MoveTo / LineTo Demonstration" ); 4 j& X" s4 n- x7 T
getviewsettings( &vp );
- r3 y8 q! e5 Jh = vp.bottom - vp.top; 7 h* R0 u! P! P# C4 H
w = vp.right - vp.left;
$ g$ Z1 r: t4 Vxcenter = w / 2; /* Determine the center of circle */
* y" N. Z9 i& @3 aycenter = h / 2; 5 t( e; b2 o9 _( N
radius = (h - 30) / (AspectRatio * 2);
3 A8 f5 X& {: K- H' P$ n1 Z4 H2 S; lstep = 360 / MAXPTS; /* Determine # of increments */
- }) L' R$ `; D) r* dangle = 0; /* Begin at zero degrees */
# X6 J8 `' M4 ^+ e  I. N# i: x& Ffor( i=0 ; irads = (double)angle * PI / 180.0; /* Convert angle to radians */
6 _- j- h8 p  D0 qpoints.x = xcenter + (int)( cos(rads) * radius );
- ?6 u9 V2 \- ^points.y = ycenter - (int)( sin(rads) * radius * AspectRatio );
6 @2 l# n( M2 B. z# K6 sangle += step; /* Move to next increment */ - ^  D( \6 q4 Z2 W; d9 R8 s
}
7 @! I2 u2 J# Q0 K6 V4 z, W% D* Scircle( xcenter, ycenter, radius ); /* Draw bounding circle */ 2 J* ?2 I: y3 M3 h- ?0 F
for( i=0 ; ifor( j=i ; jmoveto(points.x, points.y); /* Move to beginning of cord */
4 I8 r1 ~/ R( b) Olineto(points[j].x, points[j].y); /* Draw the cord */ : J' t' S' p& U, A
} } }
: i; |0 R; c* N0 M1 R. _& Kmain() " g0 @% |" Y; r* }. x( M
{int driver,mode;   F3 B: G* H* j8 C9 e
driver=CGA;mode=CGAC0; 7 u% c; k2 H6 V/ `) y- N" @
initgraph(&driver,&mode,"");
+ O* Z$ }4 V8 [; \7 J! `setcolor(3);
2 x+ ]. g$ L& `5 M8 y$ W7 X& ]setbkcolor(GREEN); ; j; r! o0 C: M. z6 {' E
LineToDemo();}
! S5 e- S3 i+ E7 E1 k4 i# i# L============================================================== 5 R6 D5 C) J4 {  ^8 @) z5 i: A
【程序66
/ F! q/ s; ?+ d/ l# [7 _. b) Y0 @题目:输入3个数a,b,c,按大小顺序输出。
' m6 H- B+ |0 I' @0 p: H+ y) x, Z0 X' a1.程序分析:利用指针方法。 3 N  S, v8 O0 D/ Q% ?
2.程序源代码:
- [3 g& U" ^0 Y1 T2 u/*pointer*/ 0 {/ L( p9 F7 H
main() : J5 D$ O2 |7 X( Q
{ % _3 v/ d6 f) T3 B( c7 {9 p
int n1,n2,n3;
$ F# Q& l4 s  F0 |6 \1 Nint *pointer1,*pointer2,*pointer3; 8 o" p* P5 ^, E* k0 Q% `' D% R
printf("please input 3 number:n1,n2,n3:");
" H  \) p1 u/ K/ _1 fscanf("%d,%d,%d",&n1,&n2,&n3); * I) h5 S. f: F6 Z/ @1 N4 v- m( b: ~
pointer1=&n1; % `. `& b7 [$ Q1 u0 `1 S
pointer2=&n2; 8 L4 ]1 }  u- M/ }. }& H
pointer3=&n3; ! a4 }& Z- S7 A. t
if(n1>n2) swap(pointer1,pointer2);
8 t5 B* z1 Y) g8 y  j( @7 |% j4 ?if(n1>n3) swap(pointer1,pointer3); : c2 D* B0 M" h+ ?$ A  S* C. I
if(n2>n3) swap(pointer2,pointer3);
/ X& k9 @- D: M6 D) f" T  ?5 J& sprintf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
' L. E* m8 B1 e& G$ n9 _} ( X, W. y: A; E
swap(p1,p2)
$ G4 F: H) M0 y8 ^, l7 y) Mint *p1,*p2;
5 O+ A: b% ^8 N' z/ o# |7 {& |{int p; % M6 B. f' A/ T# I, [* e9 K2 I
p=*p1;*p1=*p2;*p2=p;
+ F% _4 j: C- g# G' w9 ~; `}
) H  b7 C3 }( V4 Q9 G  O============================================================== & |) x- {; y0 b. O4 M, ~* Z
【程序67; d3 m0 O+ S8 T" y
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。 5 j. [# y2 E; t7 N2 ?: l  `
1.程序分析:谭浩强的书中答案有问题。
5 ]: g. U8 }1 ]5 {2.程序源代码:
5 |6 l1 [  V; z9 [main() , e  a. v% }; a
{ 5 U* a" e# [1 @
int number[10]; 8 y. M* Y$ l+ ]
input(number);
- B* Y* C& x  Imax_min(number);
6 Y, F2 C+ G8 d8 ?output(number); # j9 b. Z1 T& h8 D5 F1 {5 X
}
% B/ n% w4 n$ ^7 ~$ N( Iinput(number) 4 }* D0 U' A* X$ ?6 p
int number[10]; 4 \1 x2 r# E; p! n. n% F, Q' j
{int i; 8 n4 \+ M  P% x+ W" e6 W
for(i=0;i<9;i++) 5 f- B& ^1 V7 v# t9 p, l
scanf("%d,",&number);
& q5 E9 S! G0 e- T+ X' S% G  p* kscanf("%d",&number[9]); 8 ?' k# ]: A+ W2 @' d6 ]
} : Y6 d6 H3 I: v/ x, G
max_min(array)
' w- F, D) n0 P2 V2 [7 a* oint array[10];
( a# w7 {6 T+ |' B{int *max,*min,k,l; ! V- I- M$ T" u# s5 X4 [8 j6 e1 l
int *p,*arr_end;
4 w! _; ~- r( H2 z2 h! [% J) Darr_end=array+10;
7 L1 h" j2 N5 F6 e) `* Emax=min=array;
6 Q; ?: v; b1 b% `; Y6 a: R" Cfor(p=array+1;p if(*p>*max) max=p;
" H. {/ y  h! w6 S. helse if(*p<*min) min=p;
5 o6 }; J* ~+ e4 G9 I& k% C) ?- [) Ik=*max;
8 Z) F* j+ l; W) r, dl=*min; ) o+ v. F) ^* @5 K9 |
*p=array[0];array[0]=l;l=*p;
/ g2 S- r2 f; r( ?*p=array[9];array[9]=k;k=*p; ; w& |" q4 x2 {9 j' t7 P; J# h
return; 4 F( E5 ~$ Z8 l2 z: y  R* N
} 7 P! \$ f& D/ k- }2 Z- A7 `
output(array) 1 ]% z' S7 l; \. U; M! S5 a
int array[10];   g( b7 B5 K1 ~: C9 o3 U' f
{ int *p;
/ D6 F4 W  O4 R3 B6 z6 \for(p=array;p printf("%d,",*p);   _* A6 [6 B% P$ @% K+ l# ^
printf("%d\n",array[9]); ! R! |8 B% h: f* x! I
}
5 z! v/ u+ t+ G==============================================================
& o4 ?6 w# w* L# A

! Y+ _( Z6 b, [  q( s

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-8-18 00:06 , Processed in 0.125000 second(s), 23 queries , Gzip On.

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

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

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