展望2011

编程练习——c语言编程题精选(21-30题)

上一篇 / 下一篇  2009-03-11 23:11:47 / 个人分类:编程基础

/}"i\H:vp021、定义一个结构体变量(包括年、月、日),计算该日在本年中为第几天?(注意考虑闰年问题),要求写一个函数days,实现上面的计算。由主函数将年月日传递给days函数,计算后将日子传递回主函数输出
D"nA3i`2Wb6Aa0#include <stdio.h>51Testing软件测试网0]iOllD*m's
#include <stdlib.h>
m7\t.x&C`L0#include <time.h>
0qB'`;aI-_!c0#include <math.h>

`5AQd9L/v l051Testing软件测试网#i1~i|8l2@Pvi9Lh

struct TIME51Testing软件测试网 H~8w*C3p
{
])| K1S~TZz y0 int year;
8V:x!V+ez5Ao0 int month;
rR:x(IM3pe3Z&wg0 int day;
)K ve P(ml0Lo0};

&y*L-G/X ? Uy051Testing软件测试网]T&i;{&SK&V8Gb+u

int is_runyear(int year)
8Q fh:J[E0{
4o"KK6kPP pb0 if((year%4==0&&year%100!=0)||(year%400==0))
p'_6UB3xr0  return 1;
?l3Oz*`8K o;H O:g0 else51Testing软件测试网U I*b1[x)GU5p
  return 0;
}J A?4yV eX V0}
51Testing软件测试网iM8pg-@@pr

G%y(`Hh2A"x0int check_data(struct TIME time)
9u `$J]*t.I0D K0{51Testing软件测试网&GWmX q/M
 if(time.year<1)
#e ^&]sG H0 {51Testing软件测试网(pb-L/lz
  printf("输入的年份错误\n");
D,o$BLzf }G,q9K0  return -1;51Testing软件测试网fE.y#lr*?
 }51Testing软件测试网$CI3Tglna5d
 if(time.month<1||time.month>12)
!bjEt!DM0 {51Testing软件测试网1t"c{Zr's*pa
  printf("输入的月份错误\n");
j@'i/?A&~ t0  return -2;
U3gQ6N6t)r0 }51Testing软件测试网c#h.q%Z_QfA
 switch(time.month)51Testing软件测试网Z2P5W N+~C
 {
|(Gp#aLl0 case 1:51Testing软件测试网$K&ec1I$N'pF [5U
 case 3:51Testing软件测试网.J:U N\/} [@c4RR
 case 5:
,B,cc!{ MY0 case 7:
#qM!V"K7M0 case 8:
.?"d*qd.BO:FD0 case 10:51Testing软件测试网|}acwt
 case 12:if(time.day<1||time.day>31)51Testing软件测试网!N u%K'C+\&JM
   {
"\Cr&Jb'|*k E0    printf("输入的日期错误\n");51Testing软件测试网~5px%F!_| }
    return -3;
+wE-iLu\za/A0   }51Testing软件测试网.v B)D2UMQ0Q
      break;
"o#f6W/K!@Hx v0 case 4:51Testing软件测试网|J?bAI
 case 6:51Testing软件测试网t~/L4s!fSRU
 case 9:
b3ow&mC9i ^k0 case 11:if(time.day<1||time.day>30)51Testing软件测试网}'S]T7s6VHB
   {
q8V/vW0R E RNZ0    printf("输入的日期错误\n");51Testing软件测试网:h9R6b/`NlU
    return -3;51Testing软件测试网3s&k E%d!S5C
   }51Testing软件测试网'Qt6e(H5y Y~}
      break;
L+`nu-r0m&o2ix }~0 case 2:if(is_runyear(time.year)==1&&(time.day<1||time.day>29))
+k,mH6Z X0\7O R-n_@0     {51Testing软件测试网6U&Eef(`8I:H2cD'ZRZL
      printf("输入的日期错误\n");
,`tkKGz'g0      return -3;
6ekZ8[ ~0     }51Testing软件测试网Us%cd:s/a
     else if(is_runyear(time.year)!=1&&(time.day<1||time.day>28))51Testing软件测试网4o*@4i^Q!ROBB)\
     {
}IZ#`1b"J8|.d s2x0      printf("输入的日期错误\n");51Testing软件测试网;kvcd z2i
      return -3;51Testing软件测试网-D(FBS:fU~l
     }
M*\2j6d5_ j2d0 }51Testing软件测试网8X1R0P#F6KxPM
 return 0;51Testing软件测试网 K~+c3EIF}5y
}
a7n#isO| H0 51Testing软件测试网7b A-of:o K_%R
int days(struct TIME time)
Igj^mi;c*M0{
%b }I,jx)Xj0 int flag=0;
HyS EPwl;^7g0 int result=0;
}G)?F6zK8p@~{Q7i0 flag=is_runyear(time.year);51Testing软件测试网bK0z"J kvb
    switch(time.month)
s5e#Og^Mp5M?F0 {51Testing软件测试网+GxkY%Jy*SV2V9[
 case 12:result+=30;51Testing软件测试网 q}s7A/y8G
 case 11:result+=31;51Testing软件测试网4BwZ/YT9dz1A/_
 case 10:result+=30;51Testing软件测试网'R8e)DwfZ
 case 9:result+=31;51Testing软件测试网gk;T ~[2J
 case 8:result+=31;
)q5X Re3~0 case 7:result+=30;
#UtB(Y6cW'Vh h0 case 6:result+=31;
Kn?}X?#k0 case 5:result+=30;
(y$mq/]J;H(eu{n0 case 4:result+=31;
?Vz.g(bs}z0 case 3:if(flag==1)
F,~(o]Bm%d0      result+=29;51Testing软件测试网6WGh:s$R
     else51Testing软件测试网FmL L0l"T
      result+=28;
r j o1fSJ`_4`0 case 2:result+=31;
/z)jnl ge0 }51Testing软件测试网1r7M"p0N(Z+ex3s n
 return result+time.day;
2t)A LG ~G`:i A0}
51Testing软件测试网g@N-jT

51Testing软件测试网,{&d(DpX O

void main()51Testing软件测试网wjTR`L;^K_+t6m
{51Testing软件测试网,?T1DjN Q9o
 struct TIME time;51Testing软件测试网"A.TK&RBu
 int recode;51Testing软件测试网1KF(u&_ b
 printf("请输入年月日,以空格分开\n");
6JAe'eH S0 scanf("%d%d%d",&time.year,&time.month,&time.day);51Testing软件测试网5Z8MM6Y e4PN4P
 if((recode=check_data(time))!=0)51Testing软件测试网twwG tl E/C A O
 {
\g3hi"MoyC4@0  printf("输入数据错误,错误码为%d\n",recode);51Testing软件测试网,`s,aU y&KU6T
  exit(0);51Testing软件测试网C)RB3ZC3yn d
 }51Testing软件测试网jNEs6Y(za
 printf("是该年的第%d天",days(time));
dU8oys Fyxa9G0}
51Testing软件测试网4R!avi|3n^

51Testing软件测试网zNcE-^~l a

22、建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中的结点包括该学号,则输出该结点内容后,并将其结点删去。51Testing软件测试网,l.S,c6N9i9w&}em

51Testing软件测试网ccxpG~ H C aO

#include <stdio.h>
%y)Mk.jae*O.D0#include <stdlib.h>51Testing软件测试网2? g a2`8cRG/G-z
#include <time.h>
%S4g3v5ae0#include <math.h>51Testing软件测试网eM[&MQf*WpEpV
#include <string.h>
51Testing软件测试网 wZ&H8R.Nl%l

51Testing软件测试网@n:}R]8M

struct STU51Testing软件测试网 ?,wd%`{y q*k
{
s2dv?/}5BWv5{0 char number[20];
4i&Q!s \0zX2p'j0 char name[20];51Testing软件测试网i `eR4c;?
 int sex; //0:女 1:男51Testing软件测试网T1W F M#U
 int age;
F8u's d6n8_FT3K0 struct STU *next_stu;
a9E9}KeAd0z9n0};

"p@TcTe9[IB051Testing软件测试网M;r X/X7O-S

int n;51Testing软件测试网Y4FkZ:B]'\h)p8|

51Testing软件测试网SR&RD s];Z

struct STU *creat_stu()
/Tj"~ Egpt0{51Testing软件测试网G(B \W x G9v
 struct STU *p,*before,*head;51Testing软件测试网7zn RHvs
 int n=0;51Testing软件测试网F+V;D%e[+| Z
 int temp;
X{3y/A`tR0 p=before=head=NULL;51Testing软件测试网S7HFMLTjb&H
 printf("请输入一个学生,以回车分开\n");
1cc)eN(I0 while(1)51Testing软件测试网-i(C!],j-h5G(P
 {51Testing软件测试网h p:[ [ GD"Y
  p=(struct STU *)malloc(sizeof(struct STU));51Testing软件测试网~vx#V~ |w@N l
  printf("学号:");51Testing软件测试网 fN+_M9co.z
  scanf("%s",p->number);
e{2j.wi'w0  printf("姓名:");51Testing软件测试网"F7iEzK;M
  scanf("%s",p->name);
3eJ ]O6Kx A4l0  printf("性别(女填0,男填1):");51Testing软件测试网nZ"]iZ"C
  scanf("%d",&p->sex);
m*m?'r#k0  printf("年龄:");51Testing软件测试网M$B'A Bzu3Y u#c
  scanf("%d",&p->age);
uN!Pk#a'cPnH b(H7U0  n++;51Testing软件测试网*n!]4Sd)O
  if(before!=NULL)
O~b's,wG0   before->next_stu=p;51Testing软件测试网-WI#Y1jZ8F'Fc
  else
$s-twbT9u#|'I2CW0   head=p;51Testing软件测试网'v7?bT-P7i;t(r8j c
  printf("是否继续输入?(是的话输入1,不是的话输入0):");51Testing软件测试网$q e2J M XK+n:^
  scanf("%d",&temp);
P] ~.N-ykQ-W0  if(temp==0)
2{Nh lylw(J+H,R0  {51Testing软件测试网l/lE"X5YQ/l
   p->next_stu=NULL;51Testing软件测试网{vU"B M`
   break;51Testing软件测试网 U1W1z&s7~f P
  }
!|MlSd}o`#R0  before=p;
t x x&b4PVeF P0 }51Testing软件测试网])r7[&B,Ae$H@M
 return head;
%aP)eYZ;d7D0}

,JS%@6Q0S0

1n \v'] k#A A0void delete_stu(struct STU *stu,struct STU *before,struct STU *temp)51Testing软件测试网"c@UGOu6J
{
Fz\f [t0 if(temp==stu)51Testing软件测试网C^Npk)R H
  stu=stu->next_stu;51Testing软件测试网2\&p1t0CRr:a:b
 else51Testing软件测试网1f*XEW9p%d X
  before->next_stu=temp->next_stu;
+U5D{g#[V0}

@'a0Q2xv#E051Testing软件测试网8Ut;Xp6moH!\

void main()
~W1[y {-iM0{51Testing软件测试网6Y {kJ\k
 int i=0;51Testing软件测试网9Yv9r)z9j.qC3j
 char str[20];51Testing软件测试网Mrl A3i2k
 struct STU *p;51Testing软件测试网`-K^3R7G
 struct STU *temp,*before;51Testing软件测试网!VG4k;Lm2I
    p=creat_stu();51Testing软件测试网rl&Q Mu2I9KG
 printf("需要查找的学号为:");
W,s4@ A @~0 scanf("%s",str);51Testing软件测试网I){T}2c
 before=temp=p;
8W6k7bD z0 while(temp!=NULL)51Testing软件测试网8B:@#ZS'j6q
 {51Testing软件测试网I2SK/JC$Ew;^W
  if(strcmp(temp->number,str)==0)
:A }@u0~0  {51Testing软件测试网9M"@0h(]c*p3?'f-g
   printf("找到该学生,学号:%s,姓名:%s,性别:%s,年龄:%d\n",temp->number,temp->name,temp->sex==0?"女":"男",temp->age);
WoYS+L0O m0   delete_stu(p,before,temp);51Testing软件测试网US-K/s Vk\4?
   printf("删除该学生完成\n");51Testing软件测试网j#n0PE"`9qh+o
   break;
_-H Q*} ['~wPw0  }51Testing软件测试网 s!}\6Z?7q
  before=temp;51Testing软件测试网;W __JH
  temp=temp->next_stu;51Testing软件测试网:M*s)M,G/h'a(d@ |8^
 }
Q zRFD eG0 if(temp==NULL)51Testing软件测试网\9tO!ZH/P
  printf("没有找到该学生\n");
-M(A1T9M3m0 free(p);51Testing软件测试网4p4Z MC M H2z)M] K
}

%g4cA4rv-Z:G5L0

^3?vw8e#B |\V023、给定一个日期,求出该日为星期几(已知2002-3-28为星期四)。

Z$L|6i4cC051Testing软件测试网1t:u,Rh.c4N,h.e

#include <stdio.h>51Testing软件测试网+[:?M/H ]&nq
#include <stdlib.h>51Testing软件测试网(xH u0a8Ox(Q(r
#include <time.h>
'w1fuY5M7Kka%v0#include <math.h>
I{}#d"d7Y[7B0#include <string.h>
-l {+q?-yjdGf;J0struct TIME51Testing软件测试网}1qw FI2j3A
{51Testing软件测试网-j6AS1O0k b
 int year;
{bMCpI!N0 int month;
,u)tf$`L%jH"]0 int day;51Testing软件测试网%a~2``_#wQk
};

1UPw)E.bV'N;HZ0

ov6|~ zA0int is_runyear(int year)51Testing软件测试网9|(@s+B7d6}
{
/u7Gx(C uEb0 if((year%4==0&&year%100!=0)||(year%400==0))51Testing软件测试网.[D)bbi6O0F5@
  return 1;51Testing软件测试网,oPy:S7t*o(H6[
 else
3ng6`8^%? P |0  return 0;
Qr!Ukg |Y3~.A7~OQ0}

tMa7PI jB_(g0

{ Y)k \V7M0int computer_date(struct TIME time)
:O IZ&V^9je%P0{51Testing软件测试网Ls2lCC"I1W,`
 struct TIME t={2002,3,28};51Testing软件测试网%Ia2D c0JR
 int d=4;51Testing软件测试网)u#h!eU[ GF;B2Y
 int temp_year,temp_month;51Testing软件测试网_R j6A9A
 int year_day=0;
\Tr.rx5Yv%C0 int month_day=0;51Testing软件测试网4W8U3FQZ7d2o
 int day_day=0;51Testing软件测试网FI'ei,q,xooc2P
 int temp;
@ } a8?/w;]H(EX+S0 temp_year=time.year;51Testing软件测试网wM:AIX`S1n6f!oI
 while(temp_year!=t.year)
2D8qN6p%^0f0dUq0 {51Testing软件测试网D?6d'D2R"I\@ Q|
  if(temp_year-t.year<0)
rd\x(~a-j0  {
.SkbRZ!c0   if(is_runyear(temp_year)==1)51Testing软件测试网8{m _vYi_&W*{5G
    year_day-=366;
o{oL#Q0   else
?-S J*{Pp L0    year_day-=365;51Testing软件测试网2o$D,d-[&E} x g
   temp_year++;
'@2K] ~7[v }UH0  }51Testing软件测试网4ekz)ad`
  else51Testing软件测试网3f)_;HT AnP~A
  {51Testing软件测试网)zO c"O Y
   if(is_runyear(temp_year-1)==1)51Testing软件测试网dCX8p(f
    year_day+=366;51Testing软件测试网{4~-M(X R-HV
   else
_NWT-yU5p0    year_day+=365;51Testing软件测试网 l5R.W#U x,Lmx$X9\ i
   temp_year--;
|M,V1l4~l0  }
MD*hl5x2d(A"D\ M]6?0 }
}1|'j9uU0L0K Q%{0 temp_month=time.month;51Testing软件测试网b1cZ#^xE/c]
 while(temp_month!=t.month)
PzL/]"E0 {51Testing软件测试网Z]~e*Y2d%H8P
  if(temp_month-t.month<0)
s{r/[r[3i0  {
W(I'MZW9\ ^J*O0   switch(temp_month)51Testing软件测试网F#{b Kp$J
   {51Testing软件测试网'o1q @S o'Ik
   case 1:
/A@bt-c*T3@x0    month_day-=31;
#l;N4uK g:P&C0    break;51Testing软件测试网 M(j"g(o F(vjtm
   case 2:
&RO.E0LF0    month_day-=28;51Testing软件测试网)g |hE4G r6R'd*j DG
    break;51Testing软件测试网 TUF!~M'N
   }51Testing软件测试网J3i!l,g:YVW
   temp_month++;51Testing软件测试网*jqh/pa,Z!_
  }51Testing软件测试网h B_;v0zD+f5Be(qr
  else51Testing软件测试网9P-cK#u!Vt@j
  {51Testing软件测试网0nf5ur3at7X$t
   switch(temp_month-1)51Testing软件测试网&_:XH*pS
   {51Testing软件测试网;}J6R*BpQ
   case 4:51Testing软件测试网9r.QL|:E
   case 6:
a N`[RkX-c F1E0   case 9:51Testing软件测试网1M$S3xQ%^&s
   case 11:
s/G)lY+k'X7y0    month_day+=30;51Testing软件测试网V%qAe V ^
    break;51Testing软件测试网M-e"ffxRC
   case 3:51Testing软件测试网 zcy rl)d3j
   case 5:51Testing软件测试网 Gb8a2l!C7T
   case 7:
9Ls Fi/o&@6H0   case 8:51Testing软件测试网[2[,D^z{&e1b/b{.vG
   case 10:51Testing软件测试网5i:C4@Us)POE
   case 12:
Xu1Q:@'{0    month_day+=31;51Testing软件测试网 r x`xk:Q
   }51Testing软件测试网-d*q"Wq8A
   temp_month--;
-O*a(c `2J5W+Y0  }51Testing软件测试网0z uL}iFc(J(o6{{
 }
*\h6nH~5].YS"}%D0 day_day=time.day-t.day;
m%DG/}5}e7@0    temp=year_day+month_day+day_day;51Testing软件测试网nI!\M+jt
 if(temp<0)51Testing软件测试网 y J,\'qj b9B
 {51Testing软件测试网H6tLws
        temp=(-temp)%7;51Testing软件测试网F*[t;kt2jj/Qn
  if(temp>=4)51Testing软件测试网/}H&O-_0o{5U
   return d+7-temp;
5K?}3Qv'`+nS3joj0  else
5r4F'v.N)Lfi0   return d-temp;
d3| o!J _{~h$E&k0 }51Testing软件测试网"KF9q9o,c
 else51Testing软件测试网#Y.C*h4g c(i*c
 {51Testing软件测试网La%VojI4z-t
  temp=temp%7;51Testing软件测试网(pl:E2Q-Msvuf X
  if(temp>=4)51Testing软件测试网:Ki%mbD+Ag|l
   return temp-d+1;51Testing软件测试网,w_QG}9sS,fR*E*?
  else51Testing软件测试网%]T'H!H#`X
   return temp+d;
pANBB*}0 }
"M1pF2v7x!NPmZh0}
51Testing软件测试网(eyz.Dd1Z"Y2Yz

51Testing软件测试网5S'PB$[)IT*Z T V

void main()51Testing软件测试网W;sJ^?qi'r;U_
{51Testing软件测试网 wCN y6v V9S3n T
 struct TIME time;51Testing软件测试网#MO.Xf D6F%xGr
 printf("请输入年月日,以空格隔开\n");
\P J lO-PM0 scanf("%d%d%d",&time.year,&time.month,&time.day);51Testing软件测试网%X5l|.|&R%S
 printf("是星期%d",computer_date(time));51Testing软件测试网 N,W)p:Dnv-j
}51Testing软件测试网P%C2v5Q.z e

dB!v&Z4t1z'o0

0P/o }zf!Sj024、用递归法将一个整数n转换成字符串(例如输入4679,应输出字符串“4679”),n为不确定数,可以是位数不超过5,且数值在-32768~32767之间的任意整数。51Testing软件测试网oEdR)t"^^cY
#include <stdio.h>
A"wMIt9[:ga6P0#include <stdlib.h>51Testing软件测试网t.y w4p{e^D'x
#include <time.h>
/mhV9E7b`0#include <math.h>
\7^W*ocyo/[Sq3BUb0#include <string.h>

Yg qA Jm f)C0pz%_"ya051Testing软件测试网#ZJD&GD!m0l

void func(int data)51Testing软件测试网^4JU_K:Ml.r
{
,Kp3_?LAmj1c0 int temp;
#_9h wq"x5S0 temp=data/10;
B)NY,bbF,|0 if(temp!=0)
Au0bm.q"q0  func(temp);
;i7wU#Jq V0 putchar(data%10+'0');51Testing软件测试网*jF8J)cxm3v%i5jU
}51Testing软件测试网!J0Bh8w.w

51Testing软件测试网"| H"L'P-j-^

void main()
B;C+c,o(m*P'Y.T`0{51Testing软件测试网c*xS:xos)A
 int data;51Testing软件测试网$h%WII6QT*}NN|-v c
 printf("请输入一个整数(数值在-32768~32767之间):");
e%lc-i!T!~n0 scanf("%d",&data);
}]4C1_6Z$]7D&o0 printf("转换为字符串后为:");51Testing软件测试网f.};X`8_@0b v
 func(data);
#o(O"GS ?&w;F0 printf("\n");
$H9q gV B| Y/\0}

3l{ hrj#r0

K Q)d'ea)em025、有一个字符串,包括n个字符。写一个函数,将此字符串从第m个字符开始的全部字符复制成另一个字符串。要求在主函数输入字符串及m值并输出复制结果。
XH)K:T5Il,aR0#include <stdio.h>
'? j_5lS0#include <stdlib.h>51Testing软件测试网l3hO'Y p
#include <time.h>
w#_j"i8xIY `1Nh0#include <math.h>51Testing软件测试网o[T5h w|!|J
#include <string.h>51Testing软件测试网5C[U{G(?0x

51Testing软件测试网jb T!{0p

void func(char *str1,char *str2,int m)
#H D4E4w ?:g.xr-e#[0{51Testing软件测试网joy'`Zu0FS
 char *p,*q;51Testing软件测试网Q5Ik W;^py(O!N
 p=str2+m-1;51Testing软件测试网w ]PV p_"\
 q=str1;
rBpp2C+C3}4RO0 while(*p!='\0')51Testing软件测试网Ue q G/T&[2Lm.Bz
 {51Testing软件测试网6we x`;f1]
  *q=*p;51Testing软件测试网p3U:QB%c$s[g
  q++;51Testing软件测试网O4N7\3R8H(s
  p++;
j^X4xU#Kb;^0 }
9taucbjSC?0 *q='\0';51Testing软件测试网'Z&wn:P,\jf
}51Testing软件测试网1lRRAx E;z

51Testing软件测试网g"R3i#L^^f

void main()
#k,@6x_*]&E_0{
[Y NS9HB qw c0 char str1[1024];
$}X$Jr(`cr]0    char str2[1024];51Testing软件测试网 g.m4LF9{J-d
 int m;
tOn ~K$I,w)`0 int leng;
G$d2H-? N0 printf("请输入一个字符串:");51Testing软件测试网iaI x'u2mO
 gets(str2);
/r@%Si$NPS D ?0 leng=strlen(str2);
QW@7`3om _ Jb0 printf("字符串长度为%d\n请输入m的值",leng);
g s%N6|_ O5w;o0 scanf("%d",&m);
!Y6B L-@-]"d0 if(leng<=m)51Testing软件测试网u'H6m-Er4xCb a
  printf("error!");
H,[1b'd?N w*? b0 func(str1,str2,m);51Testing软件测试网(G cu#F~x v
 puts(str1);
K pK1N{*F|r0}

0{Aoux0

;@&hn-^fPB2d026、在主函数中输入6个字符串,用另一个函数对他们按从小到大的顺序,然后在主函数中输出这6个已经排好序的字符串。要求使用指针数组进行处理。
_O8ylx0#include <stdio.h>
P:m#L"^Y!{+b0#include <stdlib.h>51Testing软件测试网%GY4k9wW k
#include <time.h>51Testing软件测试网/N ~u d@|'Q4DC
#include <math.h>51Testing软件测试网&J6ydPM^ i
#include <string.h>51Testing软件测试网,Oa6? du

0[}'YI-j0void func(char *str[6])
1]A-EmF?O0{51Testing软件测试网6J.hTr;Xj
 int i,j;51Testing软件测试网q&P!o6MU|
 char *temp;51Testing软件测试网|L7|2ZL
 for(i=0;i<5;i++)51Testing软件测试网O t(g szf[
 {
;wYY!z R}]B0  for(j=i+1;j<6;j++)
` HL [V'H0   if(strcmp(str[i],str[j])>0)
sE*b3ixt0f0   {51Testing软件测试网o-k |c!lXLg
    temp=str[j];51Testing软件测试网S J&~8qGF1A
    str[j]=str[i];51Testing软件测试网)IE3jhP,zt
    str[i]=temp;51Testing软件测试网0s1R RT6pvs)R
   }
Tp:mN6Uu @ zjq0~ q/s0 }51Testing软件测试网:fIb!qI/Is
}51Testing软件测试网%h\e$ZD"YW@

51Testing软件测试网s#QOuyv

void main()
0V:Ly_ [H$b0{51Testing软件测试网I*{"t3K f3ZgRX
 int i;51Testing软件测试网 F^ |7Q5y B`-o,C
 char s[6][1024];
%Jxx2WY%dYBm+u0 char *str[6];51Testing软件测试网zWV/c r%~t Q.?XJ
 printf("请输入6个字符串\n");51Testing软件测试网ozgb}
 for(i=0;i<6;i++)
7~M xy"T6t0 {51Testing软件测试网!A(UiNB4o
  gets(s[i]);51Testing软件测试网\$AJw%x`#Y
  str[i]=s[i];51Testing软件测试网7`_ p$ME:a}&Z
 }51Testing软件测试网(Gabpj o
 func(str);51Testing软件测试网i|y9y6wS.]
 printf("排序后\n");
#{&w,W#ds&B"[*G*f0 for(i=0;i<6;i++)51Testing软件测试网-YW g^ Y ~1d5fn r
  puts(str[i]);
s^grOJ%C0}

v;b7fL5iW0

k0tCRVY(xf#B {027、编写一个函数实现对两个字符串的比较。不用使用C语言提供的标准函数strcmp。要求在主函数中输入两个字符串,并输出比较的结果(相等的结果为0,不等时结果为第一个不相等字符的ASCII差值)。
j0d|9f o2u;aE0
#include <stdio.h>
w|7`.Pb6H5k%B0#include <stdlib.h>51Testing软件测试网3xv(DpzO+j%B*B9Z
#include <time.h>
|$no-o9Bd1d2G0#include <math.h>51Testing软件测试网Tc.ET-^A!h
#include <string.h>

_cJ+u0{.|T051Testing软件测试网&J%Sb[T

int func(char *str1,char *str2)51Testing软件测试网 l{S c(vK]0h
{51Testing软件测试网U:Mw%y"v _ ~F'oUU\
 char *p,*q;51Testing软件测试网b&Q[2_[:F5@9o
 p=str1;
NWJy nqQ;T%f \.K0 q=str2;51Testing软件测试网4e!f-yj%\LQa6Ml2@
 while(*p!='\0'&&*q!='\0')
,hOr k6A8k0 {
%l By'N1r{DY7B1D0  if(*p==*q)51Testing软件测试网-I5t`J5g
  {
,L@)SE(OaGB v0   p++;51Testing软件测试网ce,i$])o1V-w1`5g
   q++;
z N2Tf/u/E,G0  }else
~$N ]2DQ.Y/q@0   return *p-*q;
O"]|of0 }51Testing软件测试网5W@NbN9Z3]/E
 if(*p=='\0'&&*q=='\0')
,? ]D7e)QqUN0  return 0;
O5q4A~.{(X;`4H2V0 else if(*p=='\0'&&*q!='\0')51Testing软件测试网!Az*[ w&]
  return -*q;
iS Ns P.^0 else
"YoP].U[:^ M0  return *p;51Testing软件测试网N]}8u:H"MB2n
}

2]Q!}+X/_Nx,n0

)N~\_0N'D0void main()
#@+WrQ;~:g3]s,@0{
{s-N'Yu;g0 char str1[1024];
`C*tL*j6[FdK0 char str2[1024];51Testing软件测试网Y]\/[/s
 printf("输入2个字符串(以回车键分割)\n");51Testing软件测试网phQ1]xq*d'f%g
 gets(str1);
&F2r:w8b4|g-Y0 gets(str2);
)J(T9JA Z4n$C#l3y-B`0 printf("字符串比较的结果为%d\n",func(str1,str2));51Testing软件测试网-Tm3q_%Y0c/vD
}51Testing软件测试网$B7U&iwsL"|

q:|eN6`1N M ?&?028、有一个unsigned long型整数,先要分别将其前2个字节和后2个字节用为两个unsigned int型整数输出(设一个int型数据占2个字节),试编写一函数partition实现上述要求。要求在主函数输入该long型整数,在函数partition中输出结果。51Testing软件测试网 Q!L`_$}
#include <stdio.h>51Testing软件测试网!fC`&w2e0Pk;i\}
#include <stdlib.h>
f"OKZ1f{~,Z`6P0#include <time.h>51Testing软件测试网+].M8F*I1i mK[
#include <math.h>
:w C"ZwJ g0#include <string.h>

K(b}+t$M!s {051Testing软件测试网#wS;w.h8qO1z6?!Z

void partition(unsigned long data)
7Eo~z-B8J0o$u0{
(Jd)Zsn1\!v-DnN0 unsigned int d1,d2;
i QK?D0 d1=data/(long)pow(2,16);51Testing软件测试网GG9L t S(A
 d2=data%(long)pow(2,16);51Testing软件测试网-X3fo;A#W'f9Ud$g
 printf("拆开后为%d,%d\n",d1,d2);51Testing软件测试网;h3\!S1l^u bHM
}

.hO%B4?E\5k0

dcQ2o{T%p7?z0void main()
UD&V,B Q`7v0{51Testing软件测试网2F1px1k#DrI5a)Dv
 unsigned long data;
J(e)o(y%g/| ri)?0 printf("请输入一个长整数:");51Testing软件测试网kls0hf?
 scanf("%ld",&data);51Testing软件测试网7f"bf8D6^m W
 partition(data);
_L'^Un `C0}51Testing软件测试网!w#t y:m\sFV:mx

51Testing软件测试网|eV%`1ISD2}

29、编一程序,能把从终端读入的一串字符中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存(用字符!表示输入字符串的结束)。51Testing软件测试网7{9}7Wn;E2Z
#include <stdio.h>
L&g"LY-I5H7lG0#include <stdlib.h>51Testing软件测试网X*Mew-v G6D M
#include <time.h>
(OxND;Xb4@?q0#include <math.h>
R:c?h7IiN0#include <string.h>

-L%D d2nZYU7r9r.y7k0

y+y'U)f@!s"G0void main()51Testing软件测试网%P"FIc\J
{
;N J0h)Z-D!{(U`0 char s[1024];51Testing软件测试网+]{:`{u.DT]
 int i=0;
i4~ k_u)oU L0 FILE *p;51Testing软件测试网cL@c8kD
 printf("输入一串字符:");
*B!z @%L'`8zY0 gets(s);51Testing软件测试网8P*akX(htcG%F}
 if((p=fopen("test","w+"))==NULL)
Q1F4K)q!B*j-k;W0 {
.V#{9i.mA0  printf("打开文件失败\n");
sX s&s"h:YB4{0  exit(0);51Testing软件测试网4FP0L:Go8|3f J
 }51Testing软件测试网ME fl6oh6z'~5}
    while(s[i]!='\0')
k Gw!b5r#S:|Zc lH6^0 {51Testing软件测试网s2r"L?0gsh+O\
  if(s[i]>='a'&&s[i]<='z')
N*Q$}] m'~0   s[i]=s[i]+('A'-'a');
z(I9XbsOb0  fputc(s[i],p);51Testing软件测试网TPwMQw@G
  i++;
H*y P Qfk0 }51Testing软件测试网 M\E5D&b"at
 fputc('!',p);51Testing软件测试网 e&A&oJW2_W
 fclose(p);
(z~8SNr$UQ^0}51Testing软件测试网b+uZKgvw&i5n

Er-_O'K7h L030、有五个人坐在一起,问第5个人多少岁?他说比第4个人大2岁。问第4个人多少岁?他说比第3个人大2岁。问第3个人多少岁?他说比第2个人大2岁。问第2个人多少岁?他说比第1个人大2岁。最后问第1个人多少岁?他说是10岁。请问第5个人多大?(这是一个递归问题)
7B:zv"Nj}&V+j4E A;K+v0#include <stdio.h>
+b_+@elNrpm0#include <stdlib.h>
!nq.k,oOK1t(e i0#include <time.h>51Testing软件测试网BQQH]-F
#include <math.h>
&py6Y||!ws0#include <string.h>

6RO Q K/v#Mvb051Testing软件测试网 sL p@`

int func(int i)51Testing软件测试网y4q[i)a@ @&N
{51Testing软件测试网bo|R o
 if(i==1)51Testing软件测试网b N~i0d3K5`@4M
  return 10;51Testing软件测试网"\zj d v SL?s
 else51Testing软件测试网*gB`H2@3~%c
  return 2+func(--i);
f|,F:WFb x*LdWv0}51Testing软件测试网-?-KH`U(sqO5FZY

"h kf+D z0void main()
Q/T Nx v3A4D0{51Testing软件测试网,q,tya tg}w U`
 int i=5;
b&I/l3a x [,Dd0 printf("第5个的岁数是%d",func(5));
7n'XGgl}4F~0}51Testing软件测试网\,KCT0f

v8Me2Eb1A6JR(K0完。。。51Testing软件测试网8Vsyk*f_5JM#G pPq


TAG:

 

评分:0

我来说两句

Open Toolbar