C语言程序设计结构体实验报告Word格式.docx
《C语言程序设计结构体实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《C语言程序设计结构体实验报告Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
\n第%d位学生的基本信息如下:
\n\n"
i);
学号:
(%d)\t姓名:
(%s)\t性别:
(%c)\t年龄:
(%d)\n\n"
stu->
num,stu->
name,stu->
sex,stu->
age);
三科成绩:
(%.2f,%.2f,%.2f)\n\n"
score[0],stu->
score[1],stu->
score[2]);
总成绩:
(%.2f)平均成绩:
(%.2f)\n"
sum,aeg);
\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
);
}
intmain()
inti,n;
Thenumberofstudentsis:
"
scanf("
%d"
&
n);
structStu*stu=(structStu*)calloc(n,sizeof(structStu));
for(i=0;
i<
n;
i++)
{
printf("
请输入第%d位学生的基本信息:
\n"
i+1);
scanf("
(stu+i)->
num);
姓名:
%s"
(stu+i)->
name);
性别:
%c"
sex);
年龄:
%f%f%f"
score[0],&
score[1],&
putchar('
\n'
}
===================================================================\n"
fun(stu+i,i+1);
return0;
题目二:
计算每一个结构体的两个变量所表示的长度之和(单位:
厘米)
要求:
(1)分别定义公制METRIC(成员包括:
米、厘米)和英制BRITISH(成员包括:
英尺、英寸)两个结构体;
(2)为每一个结构定义两个变量。
structMETRIC{
floatm;
floatcm;
}m1,m2;
structBRITISH{
floatfoot;
floatinches;
}b1,b2;
Entertheinfoofm1(米,厘米):
%f%f"
m1.m,&
m1.cm);
Entertheinfoofm2(米,厘米):
m2.m,&
m2.cm);
\nEntertheinfoofm2(英尺,英寸):
b1.foot,&
b1.inches);
Entertheinfoofm2(英尺,英寸):
b2.foot,&
b2.inches);
\nSumofm1andm2is:
%.2f(厘米)\n"
(m1.m+m2.m)*100+m1.cm+m2.cm);
Sumofb1andb2is:
%.2f(厘米)\n\n"
(b1.inches+b2.inches)*30.48+(b1.foot+b2.foot)*2.54);
题目三:
编写一个函数用于计算某个日期是相应年份的第几天
声明一个描述日期(年、月、日)的结构体类型。
structtime{
intyear;
intmonth;
intday;
};
voidfun(structtimetime)
inta,b,sum=0;
for(a=1;
a<
time.month;
a++)
if(a==4||a==6||a==9||a==11)
sum+=30;
elseif(a==2)
{
if((time.year%4==0)&
&
(time.year%100!
=0||time.year%400==0))
sum+=29;
else
sum+=28;
}else
sum+=31;
sum+=time.day;
%d-%d-%d是该年的第%d天!
time.year,time.month,time.day,sum);
structtimetime;
Enterthedate(Example:
xxxxxxxx)(Endwith00000000):
while(scanf("
%d%d%d"
&
time.year,&
time.month,&
time.day)!
=EOF)
if(time.year==0&
time.month==0&
time.day==0)
break;
fun(time);
题目四:
定义两个函数分别用于输入和输出N个STUDENT结构体变量的信息
定义两个结构体STUDENT和BIRTHDAY。
其中结构体STUDENT包含成员有:
姓名(charname[20]),学号(intnumber),出生日期(BIRTHDAYbirth);
结构体BIRTHDAY包含的成员有:
年份(intyear),月份(intmonth)和日期(intday)。
structBIRTHDAY{
intyear;
intmonth;
intday;
structSTUDENT{
intnumber;
structBIRTHDAYbirth;
voidinput(structSTUDENT*stu,intn)
inti;
putchar('
i<
n;
第%d位学生的信息:
\n姓名:
stu[i].name);
stu[i].number);
生日:
%d-%d-%d"
stu[i].birth.year,&
stu[i].birth.month,&
stu[i].birth.day);
if(i!
=n-1)
putchar('
else
printf("
输入完毕!
voidoutput(structSTUDENT*stu,intn)
\n第%d位学生的信息如下:
(%s)\t学号:
(%d)\t生日:
(%d-%d-%d)\n"
stu[i].name,stu[i].number,stu[i].birth.year,stu[i].birth.month,stu[i].birth.day);
输出完毕!
intn;
请输入待输入资料的学生的数目N:
structSTUDENT*stu=(structSTUDENT*)calloc(n,sizeof(structSTUDENT));
input(stu,n);
====================================================================\n"
output(stu,n);
题目五:
函数sortInfo(……)是按照总成绩从高到低的顺序进行排序的。
如果两位学生的总成绩相等,则按照学号从小到大排序。
1 声明一个STUDENT结构体,成员有:
姓名(charname[20]),学号(intnumber),总成绩(floattotalScore)
2 在main函数中定义一个长度为N的结构体数组arr[],调用函数sortInfo(structSTUDENT*arr,intn)对N位学生的信息进行排序
floattotalScore;
第%d位学生的基本信息:
%f"
stu[i].totalScore);
voidsortInfo(structSTUDENT*stu,intn)
inti,j;
structSTUDENTtemp;
for(j=1;
j<
n-i;
j++)
if((stu[i].totalScore<
stu[i+j].totalScore)||(stu[i].totalScore==stu[i+j].totalScore&
(stu[i].number)>
(stu[i+j].number)))
{
temp=stu[i];
stu[i]=stu[i+j];
stu[i+j]=temp;
}
}
(%d)\t总成绩:
stu[i].name,stu[i].number,stu[i].totalScore);
排序之前的初始状态:
sortInfo(stu,n);
排序之后的最终状态:
题目六:
编程模拟一个时钟(时间实时更新)
1 声明一个时间结构体TIME,包含成员:
时(inthour),分(intminute),秒(intsecond)。
定义函数updata(……)用于更新时间。
2 假设当前时刻为23:
59:
59,则调用函数updata将得到的下一刻时间为00:
00:
00
假设当前时刻为23:
45:
56,则调用函数updata将得到的下一刻时间为23:
57
Windows.h>
structTIME{
inthour;
intminute;
intsecond;
updata(structTIME*t)
if(t->
second==59)
t->
second=0;
if(t->
minute==59)
t->
minute=0;
if(t->
hour==23)
t->
hour=0;
}else
(t->
hour)++;
}else
(t->
minute)++;
}else
(t->
second)++;
structTIMEt;
请设定此刻时钟的时间(XX:
XX:
XX):
%d:
t.hour,&
t.minute,&
t.second);
while
(1)
%02d:
%02d\r"
t.hour,t.minute,t.second);
updata(&
t);
Sleep(1000);
四、讨论