编程练习——c语言编程题精选(21-30题)
上一篇 / 下一篇 2009-03-11 23:11:47 / 个人分类:编程基础
#j)L L6J-RpX021、定义一个结构体变量(包括年、月、日),计算该日在本年中为第几天?(注意考虑闰年问题),要求写一个函数days,实现上面的计算。由主函数将年月日传递给days函数,计算后将日子传递回主函数输出
kHJpk0#include <stdio.h>51Testing软件测试网B1e$I
Z4J'w%c(Op
#include <stdlib.h>51Testing软件测试网 WOp;b3[0Vzj
#include <time.h>
?
M?*u%f,Rn0#include <math.h>
g zH:G
g0struct TIME
T)`9Jr+w
c6l{5M M0{
t
L#|rHg)y0 int year;
6B7f+qL]
|Rps#W0 int month;
1e)X[8S5|n0 int day;51Testing软件测试网&WvZ6xz@;T1~
};51Testing软件测试网6j+Pf:Uo
int is_runyear(int year)
/]7SYm~
D0{
4P Cz
\v0 if((year%4==0&&year%100!=0)||(year%400==0))
J[n1W p5M/a)wmH"J0 return 1;51Testing软件测试网.m
Z[E"J0L |
else
4@Ly8iv h1wK0 return 0;51Testing软件测试网
Q1Gg?N
}
int check_data(struct TIME time)
0`#wb"~/?z0{
w-W7j+N;L%n
g#}0 if(time.year<1)51Testing软件测试网&B1j*w7KX6M
{51Testing软件测试网1mvqtvE
printf("输入的年份错误\n");
8pfE;A5N!]d1{0 return -1;
,h2T n ^
Pm8C
y;R-i0 }
:JEY&@N.V e0 if(time.month<1||time.month>12)51Testing软件测试网mm V@N.B
{
y;VyTYK0 printf("输入的月份错误\n");
(G.?;FI0]G/P0 return -2;51Testing软件测试网%l5tgT+Y9k"n
}
+?1Sb TH7h0 switch(time.month)51Testing软件测试网 @Il1l}&bR)a
{
j)l$Yp$QX*j:YG0 case 1:
)gZ~`2~0 case 3:
7L
P$b-r.?:ZF!s+Ym0Y{0 case 5:51Testing软件测试网m0O0P,f dQ0S'@
case 7:51Testing软件测试网yod/}W5n3{
case 8:
%|d*ry*{| w ZT3H V0 case 10:51Testing软件测试网$c5eEb/aK^@
case 12:if(time.day<1||time.day>31)51Testing软件测试网w'd/Oo~\
{
;ozGp0D/u0 printf("输入的日期错误\n");51Testing软件测试网:yl3t;D[3`'o
return -3;51Testing软件测试网,z}Tp9M@
r"K
}
!x#_#UK-u"d.dH0 break;51Testing软件测试网e{N ip8fq%S(A
case 4:51Testing软件测试网9j*aQ0XEvI2^
case 6:51Testing软件测试网y-P,jS0f4X2SMq
case 9:51Testing软件测试网/r8I2jC&q/E
case 11:if(time.day<1||time.day>30)51Testing软件测试网1rvV S(Kd#i,J1\"|
{
/vnB p!P&mb0 printf("输入的日期错误\n");51Testing软件测试网
Z3U`{^
return -3;51Testing软件测试网tYe-\
^#R;cd2N
}
h2o [4N+?6Yi1}0 break;51Testing软件测试网&E tE~7?.EGRR&\
case 2:if(is_runyear(time.year)==1&&(time.day<1||time.day>29))51Testing软件测试网8v3\m/d4_"l3k8|
{
0oF&}.wF8T C0 printf("输入的日期错误\n");51Testing软件测试网Iv:?B'u
return -3;
,m@)^/Y7W @L,c:|0 }
m.^]e
U1Vhcc0 else if(is_runyear(time.year)!=1&&(time.day<1||time.day>28))51Testing软件测试网^,O9T!km
{
"I
k%l%h!H:J0 printf("输入的日期错误\n");51Testing软件测试网-y+y6a.P#cRI
\
return -3;51Testing软件测试网R*A c$W/]1y
}51Testing软件测试网`;PB6X#n|3u S
q
f8u
}
U0RcR
XgB+l0Bh`0 return 0;
?Iv#{.mqK-I0}51Testing软件测试网2R}&cBP@d
/S^"?)C*wcL7z4HHKQ0int days(struct TIME time)51Testing软件测试网{(a!p:\6~4o6RP7G
{51Testing软件测试网Kh/W{5q(\*@"J:F
int flag=0;
6Cw ePV+^._J0 int result=0;
6XVX.o5lI/n,SXsc-d7L&d0 flag=is_runyear(time.year);
wIL+`e0 switch(time.month)51Testing软件测试网{@Zc3d
{
J]H$`"[ ox0 case 12:result+=30;51Testing软件测试网 ]B8X
Y._;^
case 11:result+=31;
jjA'q!Cl Q&e0 case 10:result+=30;51Testing软件测试网"~:J"Z3h%_3\jxV
case 9:result+=31;51Testing软件测试网~hTjIY
case 8:result+=31;51Testing软件测试网ji1iR/^Q;yj+L
case 7:result+=30;51Testing软件测试网7jl(ThI
case 6:result+=31;51Testing软件测试网%Z2r
M$e
p2fp1z
case 5:result+=30;51Testing软件测试网V!^L%?,Ph6y_
case 4:result+=31;
w3\[f9Ln,S0 case 3:if(flag==1)
bej8t-s3T.h0 result+=29;51Testing软件测试网 N cdJl d
else51Testing软件测试网EZ.UvY
x
result+=28;
%?u&`X(AI,zM|0 case 2:result+=31;
"GpqA+Cq@'p7fu0 }51Testing软件测试网"fu0\F.~
return result+time.day;51Testing软件测试网t:aD*Y-U
}
void main()51Testing软件测试网'`]^[0W7M
{51Testing软件测试网(fld(^-J7n3g8L
struct TIME time;
.L+o:v!|%NJ+u0 int recode;51Testing软件测试网K-s*s'Z&Y*D
printf("请输入年月日,以空格分开\n");
~:P6J1JV(a1n0 scanf("%d%d%d",&time.year,&time.month,&time.day);51Testing软件测试网 d|'M q7pAg~7J
if((recode=check_data(time))!=0)51Testing软件测试网^W-e~Mc/{
{51Testing软件测试网-Vw*J LmO
printf("输入数据错误,错误码为%d\n",recode);
c3jOP](_
x0 exit(0);
(XLn6^5li0 }
u(qk*E0m8v0 printf("是该年的第%d天",days(time));51Testing软件测试网+d
}{BHD1r
}
22、建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中的结点包括该学号,则输出该结点内容后,并将其结点删去。51Testing软件测试网-i3j^'i-l
51Testing软件测试网Wt+s]6AhH_!B#include <stdio.h>51Testing软件测试网.G(K2kZ2Q
#include <stdlib.h>51Testing软件测试网:t0F9r9Xdd#N,R
#include <time.h>
D0c\i%l+S6z0#include <math.h>51Testing软件测试网(a"[6i9]k
`
#include <string.h>
7tF(k/DF#x@Q-@S0struct STU
|UQU8A
x!g*h d0{51Testing软件测试网"[m A)V'B~Yh7M
char number[20];
-Z2|Gh;Y'q"Y9M0 char name[20];51Testing软件测试网6xGh&^'[|,M
int sex; //0:女 1:男51Testing软件测试网{F+ds@QV6V~
int age;51Testing软件测试网GFbp(U Yb;u
struct STU *next_stu;51Testing软件测试网-n:DQt4X&~^5a.g6?)?]Q5o
};
int n;51Testing软件测试网8^t,ht#\2Q8j&m1M
51Testing软件测试网zP_+BCstruct STU *creat_stu()
~l|dWEz6loX0{
4xib[DR9h0xay0 struct STU *p,*before,*head;51Testing软件测试网K6xDS9Q"Ci3i0m
int n=0;51Testing软件测试网7M]}.}H
int temp;51Testing软件测试网iR-nPsJ
p=before=head=NULL;
;x6w1@^ ^-O&V