Lab8 王晨旭Word格式文档下载.docx

上传人:b****6 文档编号:20639285 上传时间:2023-01-24 格式:DOCX 页数:11 大小:189.92KB
下载 相关 举报
Lab8 王晨旭Word格式文档下载.docx_第1页
第1页 / 共11页
Lab8 王晨旭Word格式文档下载.docx_第2页
第2页 / 共11页
Lab8 王晨旭Word格式文档下载.docx_第3页
第3页 / 共11页
Lab8 王晨旭Word格式文档下载.docx_第4页
第4页 / 共11页
Lab8 王晨旭Word格式文档下载.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

Lab8 王晨旭Word格式文档下载.docx

《Lab8 王晨旭Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Lab8 王晨旭Word格式文档下载.docx(11页珍藏版)》请在冰豆网上搜索。

Lab8 王晨旭Word格式文档下载.docx

(2)Definestructurevariable,andinitializethevariable.

(3)sumallthenumberofdaysintheyearstoredinthevariablesum,ifmeetingaleapyear,sum=sum+1;

#include〈stdio.h〉

structdate

{

intyear;

intmonth;

intday;

};

voidmain()

structdated;

intsum=0;

printf("

\ninputthedate:

\n"

);

scanf("

%d%d%d"

&

d.year,&

d.month,&

d.day);

switch(d.month)

{

case1:

sum=0;

break;

case2:

sum=31;

case3:

sum=31+28;

case4:

sum=31+28+31;

case5:

sum=31+28+31+30;

case6:

sum=31+28+31+30+31;

case7:

sum=31+28+31+30+31+30;

case8:

sum=31+28+31+30+31+30+31;

case9:

sum=31+28+31+30+31+30+31+31;

case10:

sum=31+28+31+30+31+30+31+31+30;

case11:

sum=31+28+31+30+31+30+31+31+30+31;

case12:

sum=31+28+31+30+31+30+31+31+30+31+30;

}

sum+=d.day;

/*天数累加*/

if(d.month>

2)/*闰年处理*/

if(d.year%400==0||d.year%100!

=0&

&

d.year%4==0)

sum+=1;

Total:

%d\n"

sum);

}

Runtheprogram:

(1)Inputalegal(合理的)date,PrintScreenofResult:

(2)Inputadatewithanillegal(不合理的)month,PrintScreenofResult:

(3)Inputadatewithanillegalday,PrintScreenofResult:

(4)Rewritetheprogram,soitcangivetherightresultunderanycondition.重编,满足所有情况

PrintScreenofcode:

if(d.month<

=12){

else

printf(“error”);

 

PART3Inputandoutputofthestructure.

Fillintheblank.Writeaprogramtoinitialize初始化anarrayofstructuresthrough来自keyboard键盘

andthenoutputthevalueofthearray.输出值

#include<

stdio.h>

string.h>

main()

{structdate

{intyear;

};

structstu

{charnum[12];

charname[20];

charsex;

intage;

structdatebirthday;

};

typedefstructstuSTU;

STUstudent[3];

inti;

/**********program*******************************/

/***usingforlooptoinputthethreeelements**/

/**********end*********************************/

for(i=0;

i<

3;

i++)

{printf("

\nstudent[%d]num:

%s;

name:

sex:

%c;

age:

%d;

birthday:

%d.%d.%d"

i,student[i].num,student[i].name,student[i].sex,student[i].age,student[i].birthday.year,student[i].birthday.month,student[i].birthday.day);

getch();

PART4PassingArraysofstructures.

Whatdoesthefollowingprogramprint?

structinfo

charitem[20];

doublecost;

};

voidsomeFunction(structinfo[]);

intmain(void)

structinfodata[4]={{"

nut"

0.50},{"

bolt"

0.75}};

someFunction(data);

for(i=0;

i<

4;

i++)

printf("

%s,%.2f\n"

data[i].item,data[i].cost);

voidsomeFunction(structinfovalues[])

2;

if(values[i].cost>

0.60)

strcpy(values[i+2].item,"

washer"

);

values[i+2].cost=0.40;

else

hammer"

values[i+2].cost=12.00;

PART5TypeConversions.

(1)Automaticconversions

main()

{

inta,b=−550;

charc='

7'

floatf1=3.142,f2;

a=b/c;

/*integerdividedbyacharacter*/

printf(“a=%d\n”,a);

a=f1;

/*floattointegerconversion*/

f1=%f……a=%d\n"

f1,a);

f1=b;

/*integertofloatconversion*/

b=%d……f1=%f\n"

b,f1);

f2=b/50.0;

/*integerdividedbyafloat*/

b=%d,f2=%f\n"

b,f2);

(2)Typecasting

{floatx;

inti;

x=3.6;

i=(int)x;

printf(“x=%f,i=%d”,x,i);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 演讲主持

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1