做网站需要学什么软件刷粉网站推广便宜
题目一
第一步:创建一个 struct Student 类型的数组 arr[3],初始化该数组中3个学生的属性
第二步:编写一个叫做save的函数,功能为 将数组arr中的3个学生的所有信息,保存到文件中去,使用fread实现fwrite
第三步:编写一个叫做load的函数,功能为 将文件中保存的3个学生信息,读取后,写入到另一个数组 brr 中去
第四步:编写一个叫做 show的函数,功能为 遍历输出 arr 或者 brr 数组中的所有学生的信息
第五步:编写一个叫做 setMath 的函数,功能为 修改 文件中 所有学生的数学成绩
代码
#include "student.h"NodePtr createHead()
{NodePtr H=(NodePtr)malloc(sizeof(Node));if(NULL==H){printf("申请空间失败\n");return NULL;}H->tail=H;H->next=NULL;printf("头结点初始化完毕\n\n");return H;
}
NodePtr addData(NodePtr H)
{NodePtr newnode=(NodePtr)malloc(sizeof(Node));if(NULL==newnode){printf("新节点申请失败\n");return NULL;}//原最后一个数据 指向处理H->tail->next=newnode;//头结点指向处理if(NULL==H->next){H->next=newnode;//头指针处理}H->tail=newnode;//尾指针处理//本节点指针域处理newnode->next=NULL;//本节点数据域处理printf("姓名:");fscanf(stdin,"%s",newnode->data.name);printf("数学:");fscanf(stdin,"%lf",&newnode->data.math);printf("语文:");fscanf(stdin,"%lf",&newnode->data.chinese);printf("英语:");fscanf(stdin,"%lf",&newnode->data.english);printf("物理:");fscanf(stdin,"%lf",&newnode->data.physical);printf("化学:");fscanf(stdin,"%lf",&newnode->data.chemical);printf("生物:");fscanf(stdin,"%lf",&newnode->data.biological);return newnode;
}void save(NodePtr H,const char* Name_of_File)
{if(NULL==H){printf("保存失败:地址不合法\n");return;}//打开文件FILE* wfp=fopen(Name_of_File,"w");NodePtr temp=H;//用于遍历while(temp->next!=NULL){temp=temp->next;fwrite(&temp->data,sizeof(temp->data),1,wfp);}//关闭文件fclose(wfp);printf("数据已写入:%s\n\n",Name_of_File);
}void show(NodePtr H)
{NodePtr temp=H;if(NULL==H){printf("地址不合法\n");return;}while(temp->next!=NULL){temp=temp->next;printf("%s ",temp->data.name);printf("%lf ",temp->data.math);printf("%lf ",temp->data.chinese);printf("%lf ",temp->data.english);printf("%lf ",temp->data.physical);printf("%lf ",temp->data.chemical);printf("%lf\n",temp->data.biological);}printf("链表显示完毕\n\n");}void load(NodePtr H,const char* Name_of_File)
{//打开文件FILE* rfp=fopen(Name_of_File,"r");while(1){NodePtr newnode=(NodePtr)malloc(sizeof(Node));int res=fread(&newnode->data,sizeof(newnode->data),1,rfp);if(EOF==res|| 0==res){free(newnode);newnode=NULL;break;}//指向处理//原最后一个数据 指向处理H->tail->next=newnode;H->tail=newnode;//尾指针处理//本节点指针域处理newnode->next=NULL;//本节点数据域处理}//关闭文件fclose(rfp);printf("数据已载入链表\n\n");
}
结果
题目
使用 fread 和 fwrite 将一张任意bmp图片改成德旗
代码
int main(int argc, const char *argv[])
{FILE* wfp=fopen("photo.bmp","r+");int width=0,height=0;fseek(wfp,18,SEEK_SET);fread(&width,4,1,wfp);fread(&height,4,1,wfp);fseek(wfp,54,SEEK_SET);unsigned char red[3]={0,0,255};unsigned char yellow[3]={0,255,255};//黄色unsigned char black[3]={0,0,0};//黑色for(int i=0;i<width*height;i++){if(i>width*height*2/3){fwrite(black,3,1,wfp);}else if(i>width*height/3){fwrite(red,3,1,wfp);}else{fwrite(yellow,3,1,wfp);}}fclose(wfp);return 0;
}
结果