C语言程序设计项目教程胡顺兴习题答案_第1页
C语言程序设计项目教程胡顺兴习题答案_第2页
C语言程序设计项目教程胡顺兴习题答案_第3页
C语言程序设计项目教程胡顺兴习题答案_第4页
C语言程序设计项目教程胡顺兴习题答案_第5页
已阅读5页,还剩49页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

项目一选择题1-5ABBAD二、填空题1.函数2.英文半角3.变量、常量、表达式三、综合题1.编写程序,在终端输出如下语句。学以明理,行以致远,好好学习,做新时代有为青年!#include<stdio.h>intmain(){printf("学以明理,\n");printf("行以致远,\n");printf("好好学习,\n");printf("做新时代有为青年!\n");return0;}编写程序,输出某个智能家居系统卧室区域的基本信息。智能床已根据屋主睡眠习惯调整至最舒适的角度,床垫温度维持在26℃,室内湿度50%,智能床头灯2个,智能床头灯亮度为10%;智能闹钟设定为早上6:30唤醒服务。#include<stdio.h>intmain(){printf("智能床已根据屋主睡眠习惯调整至最舒适的角度\n");printf("床垫温度维持在26℃\n");printf("室内湿度50%\n");printf("智能床头灯2个\n");printf("智能床头灯亮度为10%\n");printf("智能闹钟设定为早上6:30唤醒服务\n");return0;}项目二选择题1-5CBABB6-10BAADB11-12AC二、填空题1.下划线2.#define3.%.2f4.35.自动类型转换6.A7.确定8.确定三、综合题1.编写一个程序,接收用户输入的两个整数,计算并显示它们的和、差、积和商(考虑除零情况)。#include<stdio.h>intmain(){intnum1,num2;//获取用户输入printf("请输入第一个整数:");scanf("%d",&num1);printf("请输入第二个整数:");scanf("%d",&num2);//计算和、差、积printf("\n计算结果:\n");printf("和:%d+%d=%d\n",num1,num2,num1+num2);printf("差:%d-%d=%d\n",num1,num2,num1-num2);printf("积:%d*%d=%d\n",num1,num2,num1*num2);//计算商(检查除数是否为0)if(num2!=0){printf("商:%d/%d=%.2f\n",num1,num2,(float)num1/num2);}Else{printf("商:除数不能为0\n");}return0;}编写一个程序,让用户可以输入一个摄氏温度,并转换为华氏温度输出。(摄氏转华氏:F=C*9/5+32)#include<stdio.h>intmain(){floatcelsius,fahrenheit;//获取用户输入的摄氏温度printf("请输入摄氏温度:");scanf("%f",&celsius);//计算华氏温度fahrenheit=celsius*9.0/5.0+32;//输出结果printf("%.2f°C=%.2f°F\n",celsius,fahrenheit);return0;}编写一个简单的程序,使用getchar()和putchar()来实现回显功能,将用户输入的小写字母自动转为大写输出,其他字符(数字、符号等)原样输出,输入以回车键结束。#include<stdio.h>intmain(){intc;//使用int存储字符,以处理EOFprintf("请输入内容(按回车结束):");//循环读取字符直到遇到换行符或文件结束符while((c=getchar())!='\n'&&c!=EOF){//判断是否为小写字母(ASCII97-122)if(c>='a'&&c<='z'){putchar(c-32);//转换为大写(ASCII值减32)}else{putchar(c);//其他字符原样输出}}printf("\n输入结束\n");return0;//返回0表示程序正常结束}从键盘上输入三个大写字母,将其转换成小写字母并输出。#include<stdio.h>intmain(){//定义变量:ch1、ch2、ch3charch1,ch2,ch3;//从键盘上输入三个大写字母printf("请输入三个大写字母(用空格分隔):\n");scanf("%c%c%c",&ch1,&ch2,&ch3);//将大写字母转换为小写字母(ASCII码值加上32)ch1=ch1+32;ch2=ch2+32;ch3=ch3+32;//输出转换好的三个小写字母printf("转换后的小写字母为:%c%c%c\n",ch1,ch2,ch3);return0;}设计一个算法求a,b,c这三个不同的数中最小的数,绘制出流程图。

项目三选择题1-5DACAB6-7DB二、填空题1.(a>=b)&&(a>=c)2.m%n==03.(x>=y)&&(y>=z)4.(x>=1&&x<=10)||(x>=20&&x<=30)5.if(x<1)y=x;elseif(x<10)y=2*x-1;elsey=3*x-11;printf("y=%d",y);6.switch(c){case'Y':case'y':printf("赞成");break;case'N':case'n':printf("反对");break;default:printf("弃权");}7.x=(x<0)?-x:x;printf("%d",x);8.printf("%d是偶数",x);printf("%d是奇数",x);三、综合题1.输入一个整数,若此整数既是6的倍数又是4的倍数,则输出“%d是6的倍数也是4的倍数”,否则输出“No”。#include<stdio.h>intmain(){intnum;//输入整数printf("请输入一个整数:");scanf("%d",&num);//判断是否是6和4的倍数(即是否能被12整除)if(num%12==0){printf("%d是6的倍数也是4的倍数\n",num);}else{printf("No\n");}return0;}输入3个整数x,y,z,将这3个数由小到大输出。#include<stdio.h>intmain(){intx,y,z;inttemp;//输入三个整数printf("请输入三个整数(用空格分隔):");scanf("%d%d%d",&x,&y,&z);//排序:使得x<=y<=zif(x>y){temp=x;x=y;y=temp;}if(x>z){temp=x;x=z;z=temp;}if(y>z){temp=y;y=z;z=temp;}//输出排序后的结果printf("从小到大排序的结果:%d%d%d\n",x,y,z);return0;}3.编写程序计算分段函数的值。输入实数x,计算并输出下列分段函数f(x)的值,输出时保留1位小数。#include<stdio.h>intmain(){floatx,result;//输入实数xprintf("请输入实数x:");scanf("%f",&x);//计算分段函数if(x==10){result=1/x;}else{result=x;}//输出结果,保留1位小数printf("f(%.1f)=%.1f\n",x,result);return0;}输入学生成绩,若成绩为95分以上,输出“A”;85~94分输出“B”;75~84分输出“C”;65~74分输出“D”;64分以下输出“E”。#include<stdio.h>intmain(){intscore;//输入学生成绩printf("请输入学生成绩(0-100):");scanf("%d",&score);//判断成绩等级if(score>=95&&score<=100){printf("A\n");}elseif(score>=85){printf("B\n");}elseif(score>=75){printf("C\n");}elseif(score>=65){printf("D\n");}elseif(score>=0){printf("E\n");}else{printf("输入的成绩无效!\n");}return0;}5.企业发放的奖金根据利润提成。利润(i)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润i,求应发放奖金总数?#include<stdio.h>intmain(){doubleprofit,bonus=0.0;//输入当月利润(单位:万元)printf("请输入当月利润(万元):");scanf("%lf",&profit);//计算奖金if(profit>100){bonus+=(profit-100)*0.01;profit=100;}if(profit>60){bonus+=(profit-60)*0.015;profit=60;}if(profit>40){bonus+=(profit-40)*0.03;profit=40;}if(profit>20){bonus+=(profit-20)*0.05;profit=20;}if(profit>10){bonus+=(profit-10)*0.075;profit=10;}bonus+=profit*0.1;//输出奖金总额printf("应发放奖金总数:%.2f万元\n",bonus);return0;}

项目四选择题1-5ACBCA6-9BDDD二、填空题1.i<=992.t=a和t%a==0&&t%b==0s+=t*k;和t=-t;i=0;i<9;i++和j=1;j<=9-i;j++(c=getchar())和m:n6.MNOJKLMNOGHIJKLMNO三、综合题1.编写程序对x=1,2,3,…,10,求f(x)=x*x-5*x+sin(x)的最小值。intmain(){doublemin_value;intmin_x;intx;doublecurrent_value;x=1;min_value=x*x-5*x+sin(x);min_x=x;for(x=2;x<=10;x++){current_value=x*x-5*x+sin(x);if(current_value<min_value){min_value=current_value;min_x=x;}}printf("当x=%d时,f(x)取得最小值:%lf\n",min_x,min_value);return0;}编写程序,假设有数字1、2、3、4,则能组成多少个互不相同且无重复数字的3位数,并输出这些数字。#include<stdio.h>intmain(){intcount=0;inti,j,k;for(i=1;i<=4;i++){for(j=1;j<=4;j++){for(k=1;k<=4;k++){if(i!=j&&i!=k&&j!=k){printf("%d%d%d\n",i,j,k);count++;}}}}printf("总共可以组成%d个互不重复的三位数。\n",count);return0;}一个皮球从100m高度自由下落,每次落地后反弹回原高度的一半,再落下,再反弹。编写程序,计算当皮球第10次落地时,皮球运动的路程为多少?第10次反弹的高度为多少?#include<stdio.h>intmain(){doubleheight=100;doubletotal_distance=height;inti;for(i=2;i<=10;i++){height/=2;total_distance+=2*height;}printf("当皮球第10次落地时,总共运动的路程为:%lf米\n",total_distance);printf("第10次反弹的高度为:%lf米\n",height/2);return0;}编写程序,输出100以内的所有素数。#include<stdio.h>#include<math.h>intmain(){inti,j;for(i=2;i<=100;i++){intis_prime=1;intsqrt_i=sqrt(i);for(j=2;j<=sqrt_i;j++){if(i%j==0){is_prime=0;break;}}if(is_prime){printf("%d",i);}}printf("\n");return0;}5.编写程序,计算鸡兔同笼问题,已知鸡和兔的总数量为30,总脚数为90,问鸡和兔分别有多少只?#include<stdio.h>intmain(){inttotal_animals=30;inttotal_feet=90;intx;inty;for(x=0;x<=total_animals;x++){y=total_animals-x;if(2*x+4*y==total_feet){printf("鸡有:%d只,兔有:%d只\n",x,y);break;}}return0;}

项目五选择题1-6BCBABB二、判断题1-6√×√√√√三、填空题1.5四、综合题编写一个程序,计算一个整型数组的平均值:#include<stdio.h>intmain(){intarr[]={10,20,30,40,50};intsum=0;intlength=sizeof(arr)/sizeof(arr[0]);inti;for(i=0;i<length;i++){sum+=arr[i];}floataverage=(float)sum/length;printf("Average:%.2f\n",average);return0;}编写一个程序,将一个二维数组的行和列互换(转置)。#include<stdio.h>intmain(){intarr[2][3]={{1,2,3},{4,5,6}};inttransposed[3][2];inti,j;for(i=0;i<2;i++){for(j=0;j<3;j++){transposed[j][i]=arr[i][j];}}printf("Transposedarray:\n");for(i=0;i<3;i++){for(j=0;j<2;j++){printf("%d",transposed[i][j]);}printf("\n");}return0;}

项目六选择题1-5CABBA6-10ADCCC11-12CB二、判断题1-5×√√√√

项目七选择题1-5BDABD6-8CDC二、填空题1.int*ptr;2.a或&a[0]3.*ptr4.允许函数修改调用者提供的变量的值5.任何类型6.&三、综合题1.编写一个C语言程序,统计某智能家居品牌5名员工的销绩效奖励总和,并通过指针输出结果。(已知:5名员工的销售绩效奖励分别为5500元、6000元、3000元、7000元。)#include<stdio.h>intmain(){intsales[5]={5500,6000,3000,7000,4500};int*ptr;intsum=0;inti;//将循环变量i移到循环外部声明ptr=sales;for(i=0;i<5;i++){sum+=*(ptr+i);}printf("5名员工的销售绩效奖励总和为:%d元\n",sum);return0;}2.编写一个C语言程序,使用指针遍历并输出1~12月的智能家居生产数量。(已知:生产数量数组为{10000,15200,9800,15000,18000,10000,13500,14000,19000,16000,12000,12000})#include<stdio.h>intmain(){intproduction[12]={10000,15200,9800,15000,18000,10000,13500,14000,19000,16000,12000,12000};int*ptr;inti;ptr=production;for(i=0;i<12;i++){printf("第%2d月生产数量:%d\n",i+1,*(ptr+i));}return0;}

项目八选择题1-2BA二、填空题1.N2.->三、综合题1.定义一个结构体

Employee,包含员工的姓名、工号和工资。声明并初始化一个包含5个员工的结构体数组,使用结构体指针遍历数组并输出每个员工的信息。#include<stdio.h>#include<string.h>structEmployee{charname[50];intid;floatsalary;};intmain(){structEmployeeemployees[5]={{"张三",1001,5000.0},{"李四",1002,6000.5},{"王五",1003,7000.0},{"赵六",1004,8000.5},{"孙七",1005,9000.0}};structEmployee*ptr;inti;ptr=employees;for(i=0;i<5;i++){printf("姓名:%s,工号:%d,工资:%.2f\n",ptr->name,ptr->id,ptr->salary);ptr++;}return0;}2.编写一个程序,使用结构体指针来修改结构体数组中的某个元素的值,并输出修改后的结果。#include<stdio.h>#include<string.h>structEmployee{charname[50];intid;floatsalary;};intmain(){structEmployeeemployees[3]={{"张三",1001,5000.0},{"李四",1002,6000.0},{"王五",1003,7000.0}};structEmployee*ptr;inti;ptr=&employees[1];//指向第二个员工strcpy(ptr->name,"新名字");//修改姓名ptr->id=1004;//修改工号ptr->salary=8500.0;//修改工资for(i=0;i<3;i++){printf("姓名:%s,工号:%d,工资:%.2f\n",employees[i].name,employees[i].id,employees[i].salary);}return0;}

项目九选择题1-4ABCD二、填空题1.写模式(创建新文件或覆盖已有文件)2.追加模式3.读写模式(文件必须已存在)4.二进制写模式(创建新文件或覆盖已有文件)三、综合题1.编写一个程序,要求用户输入多个智能家居设备的状态数据,并将这些数据写入文本文件。然后从文件中读取数据并显示。#include<stdio.h>#include<string.h>structDevice{charname[50];intstatus;//0:off,1:onfloattemperature;};intmain(){FILE*fp;structDevicedev;inti,n;charfilename[]="devices.txt";fp=fopen(filename,"w");if(fp==NULL){printf("无法打开文件进行写入。\n");return1;}printf("请输入要录入的设备数量:");scanf("%d",&n);for(i=0;i<n;i++){printf("请输入第%d个设备的名称:",i+1);scanf("%s",);printf("请输入设备状态(0=关闭,1=开启):");scanf("%d",&dev.status);printf("请输入当前温度:");scanf("%f",&dev.temperature);fprintf(fp,"%s%d%.2f\n",,dev.status,dev.temperature);}fclose(fp);fp=fopen(filename,"r");if(fp==NULL){printf("无法打开文件进行读取。\n");return1;}printf("\n读取设备信息:\n");while(fscanf(fp,"%s%d%f",,&dev.status,&dev.temperature)!=EOF){printf("设备名称:%s,状态:%s,温度:%.2f°C\n",,dev.status?"开启":"关闭",dev.temperature);}fclose(fp);return0;}编写一个程序,要求用户输入多个智能家居设备的状态数据,并将这些数据写入二进制文件。然后从文件中读取数据并显示。#include<stdio.h>#include<string.h>structDevice{charname[50];intstatus;//0:off,1:onfloattemperature;};intmain(){FILE*fp;structDevicedev;inti,n;charfilename[]="devices.dat";fp=fopen(filename,"wb");if(fp==NULL){printf("无法打开文件进行写入。\n");return1;}printf("请输入要录入的设备数量:");scanf("%d",&n);for(i=0;i<n;i++){printf("请输入第%d个设备的名称:",i+1);scanf("%s",);printf("请输入设备状态(0=关闭,1=开启):");scanf("%d",&dev.status);printf("请输入当前温度:");scanf("%f",&dev.temperature);fwrite(&dev,sizeof(structDevice),1,fp);}fclose(fp);fp=fopen(filename,"rb");if(fp==NULL){printf("无法打开文件进行读取。\n");return1;}printf("\n读取设备信息:\n");while(fread(&dev,sizeof(structDevice),1,fp)==1){printf("设备名称:%s,状态:%s,温度:%.2f°C\n",,dev.status?"开启":"关闭",dev.temperature);}fclose(fp);return0;}编写一个程序,要求用户可以选择追加数据到文本文件中,而不是覆盖原有数据。#include<stdio.h>#include<string.h>intmain(){FILE*fp;charfilename[]="data.txt";charchoice;charline[100];printf("是否要追加数据到文件?(Y/N):");scanf("%c",&choice);if(choice=='Y'||choice=='y'){fp=fopen(filename,"a");if(fp==NULL){printf("无法打开文件。\n");return1;}printf("请输入要追加的内容(输入END结束):\n");while(1){getchar();//清除换行符或残留字符fgets(line,sizeof(line),stdin);if(strncmp(line,"END",3)==0){break;}fputs(line,fp);}fclose(fp);}else{printf("未追加任何数据。\n");}fp=fopen(filename,"r");if(fp==NULL){printf("无法打开文件进行读取。\n");return1;}printf("\n当前文件内容:\n");while(fgets(line,sizeof(line),fp)!=NULL){printf("%s",line);}fclose(fp);return0;}编写一个程序,要求用户可以选择追加数据到二进制文件中,而不是覆盖原有数据。#include<stdio.h>#include<string.h>structDevice{charname[50];intstatus;//0:off,1:onfloattemperature;};intmain(){FILE*fp;structDevicedev;charfilename[]="devices.dat";charchoice;intn,i;printf("是否要追加数据到二进制文件?(Y/N):");scanf("%c",&choice);if(choice=='Y'||choice=='y'){fp=fopen(filename,"ab");if(fp==NULL){printf("无法打开文件。\n");return1;}printf("请输入要添加的设备数量:");scanf("%d",&n);for(i=0;i<n;i++){printf("请输入第%d个设备的名称:",i+1);scanf("%s",);printf("请输入设备状态(0=关闭,1=开启):");scanf("%d",&dev.status);printf("请输入当前温度:");scanf("%f",&dev.temperature);fwrite(&dev,sizeof(structDevice),1,fp);}fclose(fp);}else{printf("未追加任何数据。\n");}fp=fopen(filename,"rb");if(fp==NULL){printf("无法打开文件进行读取。\n");return1;}printf("\n当前二进制文件中的设备信息:\n");while(fread(&dev,sizeof(structDevice),1,fp)==1){printf("设备名称:%s,状态:%s,温度:%.2f°C\n",,dev.status?"开启":"关闭",dev.temperature);}fclose(fp);return0;}

项目十一、综合题1、在现有系统中添加一个新类型的智能设备——智能门锁。智能门锁有以下属性:设备ID、状态(开/关)、安全等级(1-5)。请完成以下任务:(1)修改现有的结构体定义,使其能够支持智能门锁。(2)实现add_device()函数对智能门锁的支持,包括设置安全等级。(3)扩展control_device()函数以适应智能门锁的状态控制,并确保安全等级在控制过程中不被修改。#include<stdio.h>#include<string.h>#include<time.h>#defineMAX_DEVICES10#defineMAX_TASKS10typedefenum{LIGHT,CURTAIN,AIR_CONDITIONER,SMART_LOCK//新增设备类型:智能门锁}DeviceType;typedefenum{HOME_MODE,AWAY_MODE,SLEEP_MODE}SceneMode;typedefstruct{chardevice_id[16];DeviceTypetype;intstatus;union{//使用union区分不同设备的参数intpower;//灯光、窗帘用功率百分比,空调用温度intsecurity_level;//智能门锁使用安全等级}param;charlast_update[20];}SmartDevice;typedefstruct{chartask_id[16];chardevice_id[16];intaction;intpower;charexec_time[6];}ScheduledTask;SmartDevicedevices[MAX_DEVICES];intdevice_count=0;ScheduledTasktasks[MAX_TASKS];inttask_count=0;voidadd_device(constchar*id,DeviceTypetype,intstatus,intvalue);voidcontrol_device(constchar*id,intnew_status,intnew_value);voidset_scene_mode(SceneModemode);voidadd_scheduled_task(constchar*dev_id,intaction,intpower,constchar*time);voidcheck_scheduled_tasks();voiddisplay_devices();voiddisplay_tasks();voidshow_main_menu();voidinit_default_devices();intmain(){intchoice;charinput[20];chardev_id[16];intaction;intvalue;intmode;chartime[6];init_default_devices();do{check_scheduled_tasks();show_main_menu();scanf("%d",&choice);switch(choice){case1:display_devices();break;case2:printf("输入设备ID:");scanf("%s",dev_id);printf("操作(0=关,1=开):");scanf("%d",&action);printf("参数值:");scanf("%d",&value);control_device(dev_id,action,value);break;case3:printf("选择场景模式:\n");printf("1.回家模式\n2.离家模式\n3.睡眠模式\n");scanf("%d",&mode);if(mode>=1&&mode<=3){set_scene_mode(mode-1);}else{printf("无效选择\n");}break;case4:printf("输入设备ID:");scanf("%s",dev_id);printf("操作(0=关,1=开):");scanf("%d",&action);printf("参数值:");scanf("%d",&value);printf("执行时间(HH:MM):");scanf("%s",time);add_scheduled_task(dev_id,action,value,time);break;case5:display_tasks();break;case6:device_count=0;init_default_devices();break;case0:printf("系统正在退出...\n");break;default:printf("无效选择,请重新输入\n");}while(getchar()!='\n');}while(choice!=0);return0;}voidadd_device(constchar*id,DeviceTypetype,intstatus,intvalue){time_tnow;structtm*tm_now;if(device_count>=MAX_DEVICES){printf("错误:设备数量已达上限\n");return;}strcpy(devices[device_count].device_id,id);devices[device_count].type=type;devices[device_count].status=status;switch(type){caseLIGHT:caseCURTAIN:caseAIR_CONDITIONER:devices[device_count].param.power=value;break;caseSMART_LOCK:if(value<1||value>5){printf("警告:安全等级超出范围,默认设为3\n");value=3;}devices[device_count].param.security_level=value;break;}now=time(NULL);tm_now=localtime(&now);strftime(devices[device_count].last_update,20,"%Y-%m-%d%H:%M:%S",tm_now);device_count++;printf("设备%s添加成功\n",id);}voidcontrol_device(constchar*id,intnew_status,intnew_value){inti;time_tnow;structtm*tm_now;for(i=0;i<device_count;i++){if(strcmp(devices[i].device_id,id)==0){devices[i].status=new_status;//只有非SMART_LOCK设备才设置power参数if(devices[i].type!=SMART_LOCK){devices[i].param.power=new_value;}now=time(NULL);tm_now=localtime(&now);strftime(devices[i].last_update,20,"%Y-%m-%d%H:%M:%S",tm_now);printf("设备%s状态已更新\n",id);return;}}printf("错误:未找到设备%s\n",id);}voidset_scene_mode(SceneModemode){time_tnow;structtm*tm_now;chartime_str[20];inti;now=time(NULL);tm_now=localtime(&now);strftime(time_str,20,"%Y-%m-%d%H:%M:%S",tm_now);switch(mode){caseHOME_MODE:control_device("LIGHT_001",1,70);control_device("CURTAIN_01",1,100);control_device("AC_001",1,24);printf("[%s]已设置为回家模式\n",time_str);break;caseAWAY_MODE:for(i=0;i<device_count;i++){control_device(devices[i].device_id,0,0);}printf("[%s]已设置为离家模式\n",time_str);break;caseSLEEP_MODE:control_device("LIGHT_001",1,30);control_device("CURTAIN_01",0,0);control_device("AC_001",1,26);printf("[%s]已设置为睡眠模式\n",time_str);break;}}voidadd_scheduled_task(constchar*dev_id,intaction,intpower,constchar*time){if(task_count>=MAX_TASKS){printf("错误:定时任务数量已达上限\n");return;}sprintf(tasks[task_count].task_id,"TASK_%02d",task_count+1);strcpy(tasks[task_count].device_id,dev_id);tasks[task_count].action=action;tasks[task_count].power=power;strcpy(tasks[task_count].exec_time,time);task_count++;printf("定时任务添加成功:%s将在%s执行\n",dev_id,time);}voidcheck_scheduled_tasks(){time_tnow;structtm*tm_now;charcurrent_time[6];inti;now=time(NULL);tm_now=localtime(&now);sprintf(current_time,"%02d:%02d",tm_now->tm_hour,tm_now->tm_min);for(i=0;i<task_count;i++){if(strcmp(tasks[i].exec_time,current_time)==0){control_device(tasks[i].device_id,tasks[i].action,tasks[i].power);printf("定时任务%s已执行\n",tasks[i].task_id);}}}voiddisplay_devices(){inti;constchar*type_str;constchar*status_str;charparam_str[20];//替换原来的power_strprintf("\n%-12s%-15s%-8s%-12s%s\n","设备ID","类型","状态","参数/等级","更新时间");for(i=0;i<device_count;i++){switch(devices[i].type){caseLIGHT:type_str="灯光";sprintf(param_str,"%d%%",devices[i].param.power);break;caseCURTAIN:type_str="窗帘";sprintf(param_str,"%d%%",devices[i].param.power);break;caseAIR_CONDITIONER:type_str="空调";sprintf(param_str,"%d℃",devices[i].param.power);break;caseSMART_LOCK:type_str="智能门锁";sprintf(param_str,"L%d",devices[i].param.security_level);break;}status_str=devices[i].status?"开":"关";printf("%-12s%-15s%-8s%-12s%s\n",devices[i].device_id,type_str,status_str,param_str,devices[i].last_update);}}voiddisplay_tasks(){inti;printf("\n%-10s%-12s%-8s%-8s%s\n","任务ID","设备ID","动作","参数","执行时间");for(i=0;i<task_count;i++){printf("%-10s%-12s%-8s%-8d%s\n",tasks[i].task_id,tasks[i].device_id,tasks[i].action?"开":"关",tasks[i].power,tasks[i].exec_time);}}voidshow_main_menu(){printf("\n===智能家居控制系统===\n");printf("1.显示设备状态\n");printf("2.控制设备\n");printf("3.设置场景模式\n");printf("4.添加定时任务\n");printf("5.显示定时任务\n");printf("6.初始化默认设备\n");printf("0.退出系统\n");printf("请选择操作:");}voidinit_default_devices(){add_device("LIGHT_001",LIGHT,0,50);add_device("CURTAIN_01",CURTAIN,0,0);add_device("AC_001",AIR_CONDITIONER,0,26);add_device("LOCK_001",SMART_LOCK,0,4);//示例智能门锁}2、当前系统在重启后会丢失所有设备状态信息。要求实现数据的持久化存储功能,使得设备状态可以在程序重启后恢复。具体要求如下:(1)设计一种文件格式用于存储设备信息(可以是文本或二进制格式)。(2)在每次设备状态更新时,自动将更改保存到文件中。(3)在程序启动时加载之前保存的设备状态。#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>#defineMAX_DEVICES10#defineMAX_TASKS10#defineFILE_NAME"devices.dat"typedefenum{LIGHT,CURTAIN,AIR_CONDITIONER,SMART_LOCK}DeviceType;typedefenum{HOME_MODE,AWAY_MODE,SLEEP_MODE}SceneMode;typedefstruct{chardevice_id[16];DeviceTypetype;intstatus;union{intpower;intsecurity_level;}param;charlast_update[20];}SmartDevice;typedefstruct{chartask_id[16];chardevice_id[16];intaction;intpower;charexec_time[6];}ScheduledTask;SmartDevicedevices[MAX_DEVICES];intdevice_count=0;ScheduledTasktasks[MAX_TASKS];inttask_count=0;//函数声明voidadd_device(constchar*id,DeviceTypetype,intstatus,intvalue);voidcontrol_device(constchar*id,intnew_status,intnew_value);voidset_scene_mode(SceneModemode);voidadd_scheduled_task(constchar*dev_id,intaction,intpower,constchar*time);voidcheck_scheduled_tasks();voiddisplay_devices();voiddisplay_tasks();voidshow_main_menu();voidinit_default_devices();voidsave_devices_to_file(constchar*filename);voidload_devices_from_file(constchar*filename);intmain(){intchoice;charinput[20];chardev_id[16];intaction;intvalue;intmode;chartime[6];//启动时尝试加载设备状态load_devices_from_file(FILE_NAME);if(device_count==0){printf("未找到历史设备,正在初始化默认设备...\n");init_default_devices();//如果没有设备才初始化默认设备}do{check_scheduled_tasks();show_main_menu();scanf("%d",&choice);switch(choice){case1:display_devices();break;case2:printf("输入设备ID:");scanf("%s",dev_id);printf("操作(0=关,1=开):");scanf("%d",&action);printf("参数值:");scanf("%d",&value);control_device(dev_id,action,value);save_devices_to_file(FILE_NAME);//更新后自动保存break;case3:printf("选择场景模式:\n");printf("1.回家模式\n2.离家模式\n3.睡眠模式\n");scanf("%d",&mode);if(mode>=1&&mode<=3){set_scene_mode(mode-1);save_devices_to_file(FILE_NAME);//场景模式改变也保存}else{printf("无效选择\n");}break;case4:printf("输入设备ID:");scanf("%s",dev_id);printf("操作(0=关,1=开):");scanf("%d",&action);printf("参数值:");scanf("%d",&value);printf("执行时间(HH:MM):");scanf("%s",time);add_scheduled_task(dev_id,action,value,time);break;case5:display_tasks();break;case6:device_count=0;init_d

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论