上海电力学院C语言程序设计实验十文档格式.docx
- 文档编号:17367559
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:12
- 大小:324.85KB
上海电力学院C语言程序设计实验十文档格式.docx
《上海电力学院C语言程序设计实验十文档格式.docx》由会员分享,可在线阅读,更多相关《上海电力学院C语言程序设计实验十文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
scanf("
%d%d%d"
&
date.year,&
date.month,&
date.day);
\n%d%d%dis:
%ddays\n"
date.year,date.month,date.day,days(date.year,date.month,date.day));
}
intdays(intyear,intmonth,intday)
{inti,day_sum=0;
staticintday_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30};
for(i=0;
i<
month;
i++)
day_sum+=day_tab[i];
day_sum+=day;
if((year%4==0&
&
year%100!
=0||year%400==0)&
month>
=3)
day_sum+=1;
returnday_sum;
}
2.输入和运行以下程序,分析运行结果。
uniondata
{inti[2];
floata;
longb;
charc[4];
}u;
{scanf("
%d,%d"
u.i[0],&
u.i[1]);
i[0]=%d,i[1]=%d,a=%f,b=%1d,c[0]=%c,c[1]=%c,c[2]=%c,c[3]=%c"
u.i[0],u.i[1],u.a,u.b,u.c[0],u.c[1],u.c[2],u.c[3]);
①输入两个整数10000、20000给u.i[0]和u.i[l],分析运行结果。
②然后将scanf语句改为:
scanf("
%ld"
u.b);
输入60000给b,分析运行结果。
%ld"
u.b);
共用体union共有8字节长度
更改前:
10000占用了低4个字节,20000占用了高4个字节,int是整形,i[0]读取低4个字节,即10000,i[1]读取高4个字节,即20000,a是浮点型,读取低4个字节,有效位6位,即0.000000,b是长整型,存储方式与int相同,读取低4个字节,所以b=10000,char是字符型,c[0]读取最低的4位,但输入的不是单个字符,使用%c输出会导致乱码,c[1],c[2],c[3]同理,若输出改为%d,则会分别读取8位输入数字对应的二进制数,并将其转成十进制。
更改后:
60000占用了低4个字节,int是整形,i[0]为60000,float为0.000000,long是长整型,值与int相同,所以b=60000,字符数组仍旧乱码。
3、程序填空。
(1)结构体变量案例。
编程输出两个小孩的记录信息。
1个男孩和1个女孩,其姓名首字母分别为“R”和“H”;
男孩15岁,女孩比男孩小1岁子;
男孩的成绩为75,女孩的成绩为82。
#include<
intmain()
{
structchildren/*定义结构体类型*/
{
charinitial;
/*姓名首字母*/
intage;
/*年龄*/
intgrade;
/*成绩*/
}boy,girl;
boy.initial='
R'
;
girl.initial='
H'
boy.age=15;
girl.age=boy.age-1;
girl.grade=82;
boy.grade=75;
boy:
%cis%dyearsoldandgotagradeof%d\n"
boy.initial,boy.age,boy.grade);
girl:
girl.initial='
girl.age=boy.age-1,girl.grade=82);
return0;
(2)结构体数组案例。
设有三个候选人,Zhang,Wang和Li,设有10个人参加投票,每次输入一个得票的候选人的名字,要求最后输出各候选人的得票结果,请设计程序对候选人的得票数进行统计。
#include<
string.h>
structperson/*候选人信息结构体,这是一个全局的结构体数组*/
{charname[20];
/*姓名*/
intcount;
/*得票数*/
}stu[3]={"
Zhang"
0,"
Wang"
Li"
0};
{inti,j;
charleader_name[20];
for(i=1;
=10;
i++)/*设有10个人参加投票*/
%s"
leader_name);
/*输入得票人姓名*/
for(j=0;
j<
3;
j++)
if(strcmp(leader_name,stu[j].name)==0)
stu[j].count++;
}
\n"
i<
3;
i++)
%5s:
%d\n"
stu[i].name,stu[i].count);
(3)指向结构体的指针案例。
每个学生包括学号、姓名和成绩数据,要求找出成绩最高者的姓名和成绩。
(设有4个学生)。
structstudent
intnum;
charname[20];
floatscore;
}stu[4],*p;
inti,temp=0;
floatmax;
4;
scanf("
%d%s%f"
stu[i].num,&
stu[i].name,&
stu[i].score);
max=stu[0].score;
{if(stu[i].score>
max)
{
max=stu[i].score;
temp=i;
}
p=stu+temp;
\nThemaxscore:
No.:
%d\nname:
%s\nscore:
%4.1f\n"
p->
num,p->
name,p->
score);
(4)结构体数组中存放有3个人的姓名和年龄,以下程序可以输出其中最年长者的姓名和年龄。
#include<
structman
}person[]={{"
Lifeng"
20},{"
Yangfan"
21},{"
Sunping"
18}};
inti,k;
k=0;
if(person[i].age>
person[k].age)k=i;
%s\n%d\n"
person[k].name,person[k].age);
4、编写程序,然后上机调试运行。
(1)学生记录由学号、姓名、成绩组成,使用结构体数组,将下列学生的数据按成绩从高到低排序后显示到屏幕上。
voidmain()
intnum;
charname[20];
floatscore;
}stu[5],temp;
inti,j,N=5;
5;
scanf("
%d%s%f"
N-1;
for(j=0;
N-(i+1);
if(stu[j].score>
stu[j+1].score)
{
temp=stu[j+1];
stu[j+1]=stu[j];
stu[j]=temp;
}
printf("
%d%s%f\n"
stu[i].num,stu[i].name,stu[i].score);
(2)学生记录由学号、姓名、成绩组成,保存在文本文件中,将下列学生中不及格学生的姓名显示出来,并保存到另一个二进制文件中。
stdlib.h>
structstudent
charname[100];
intscore;
}students[5];
inti;
FILE*fo,*fi;
fo=fopen("
disqualified.txt"
"
wb"
fi=fopen("
student.txt"
r"
fscanf(fi,"
%d%s%d"
&
students[i].num,&
students[i].name,&
students[i].score);
if(students[i].score<
60)
printf("
%s,不及格\n"
students[i].name),
fprintf(fo,"
%d%s%d\n"
students[i].num,students[i].name,students[i].score);
三、实验总结
将程序编写和测试过程中出现的各种问题(需要的话可以截图)、解决方法详细总结在这部分。
四、实验报告要求
1、根据实验内容中各个不同实验的要求写出测试数据和调试结果(要求采用截图的方式给出程序运行结果)。
2、编写相应的程序,写出完整的程序代码,包括注释,注意书写的层次结构。
3、整理上机步骤,总结经验和体会。
4、上交的实验报告分两种形式:
打印稿由部分同学上交,电子稿全部提交,电子稿的命名方法统一采用形式:
8位学号_姓名.doc以及所有的源代码文件
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 上海 电力 学院 语言程序设计 实验