C primier plus 结构和其他数据形式 14.18.7

借鉴原版书的例子代码,很有参考意义。

//14.18编程练习.7(参考原著)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int count=0;//作为全局变量。
            //全局变量是指在程序开头的说明部分定义和说明的量。它的作用域分为两种情况:
            //(1)在全局变量和局部变量不同名时,其作用域是整个程序(即单独函数可以改变其值)。
            //(2)在全局变量和局部变量同名时,全局变量的作用域不包含同名局部变量的作用域。

void read_file(struct book *,char *);//从指定的文件地址中读取结构类型的函数。
void display(struct book *);
void change(struct book *);
void reduce(struct book *);
void add(struct book *);
void write_file(struct book *,char *);//写入指定文件指针的结构类型。

#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 10

struct book
{
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};
int size = sizeof(struct book);//声明的结构的大小。

int main(void)
{
	struct book library[MAXBKS];//结构数组!
	char command[10];

	read_file(library,"book.dat");//开始就显示文件已有的内容。
	printf("select the functions:\n");//菜单展示。
	printf("d:dispaly all book                c:change a book\n");
	printf("r:reduce a book                   a:add books\n");
	printf("other:save and quit:");

	while(1)
	{
		gets(command);
		switch(command[0])//这个表达式可以自动屏蔽掉多余的输入字符。
		{
		case 'd': display(library);break;
		case 'c': change(library);break;
		case 'r': reduce(library);break;
		case 'a': add(library);break;
		default: write_file(library,"book.dat");
		}
		printf("select d/c/r/a/other:");
	}
		return 0;
}


void read_file(struct book *p,char *filename)
{
	FILE *pbooks;
	if((pbooks=fopen(filename,"a+b"))==NULL)//a+打开一个文本文件,可以进行更新(读取和写入);
	{                                       //向已有的文件的尾部追加内容,如果该文件不存在则先创建之;
		fputs("Can't open book.dat file\n",stderr);//可以读取整个文件,但写入时只能追加内容;b表示二进制。
		exit(EXIT_FAILURE);
	}
	rewind(pbooks);//指定到文件初始位置。
	while(count<MAXBKS && fread(&p,size,1,pbooks)==1)
		count++;
	fclose(pbooks);
	printf("Read %s successfully! There are %d books.\n",filename,count);//此时count是文件中已存在的library结构个数。
}

void display(struct book *p)
{
	if(count==0)
		printf("There is no book.\n");
	printf("Book list for %d books:\n",count);
	for(int i=0;i<count;i++)
		printf("%s by %s : $%.2f\n",p[i].title,p[i].author,p[i].value);
}

void change(struct book *p)//“修改该书目记录的内容,即替换”。
{
	char title[MAXTITL];
	printf("Input the name of the book which you want change: ");
	gets(title);
	for(int i=0;i<count;i++)//遍历已有的书目。
		if(strcmp(p[i].title,title)==0)
		{
			puts("Please add new a book title.");
			gets(p[i].title);
			puts("Now enter the author.");
			gets(p[i].author);
			puts("Now enter the value.");
			scanf("%f",&p[i].value);
			while(getchar()!='\n')
				continue;
			printf("The book has been changed!\n");
			return;
		}
			printf("Error! The book is not in the list!\n");
			return;//因为函数类型是void,return;只是用来防止两个提示性输出同时输出。
}

//这个函数模块是核心部分,仔细思考这么进行删除并替换的!!!!
void reduce(struct book *p)//“如果删除记录,把空出来的数组空间留给下一个要记录的书目。”
{
	char title[MAXTITL];
	if(count==0)
		printf("Error! Book list is empty!\n");
	printf("Input the name of the book which you want reduce: ");
	gets(title);
	for(int i=0;i<count;i++)
		if(strcmp(p[i].title,title)==0)
		{
			p[i]=p[count-1];//“最简单的方法就是对存储在该数组空间内的所有的数据进行改变。”——结构是可以直接赋值的!!!!!(但是数组之间是不可以的)
			strcpy(p[count-1].title," ");
			strcpy(p[count-1].author," ");
			p[count-1].value=0;
			count--;//这个count标明删除后的数组结构,为后续添加数据起重要重用。
			printf("%s has been delete from list\n",title);
			return;
		}
			printf("Error! %s is not in book list.\n",title);
			return;
}

void add(struct book *p)
{
	if(count==MAXBKS)
	{
		fputs("Error! The book.dat file is full.\n",stderr);
		exit(EXIT_FAILURE);
	}
	puts("Please add new book title.");
	puts("Press [enter] at the start of a line to stop.");
	while(count<MAXBKS && gets(p[count].title)!=NULL && p[count].title[0]!='\0')//结构数组内部是可以乱排?只要数组下标符合规定就可以了。
	{//有个隐秘的点,count标明已经表示到达某个数组了?!
		puts("Now enter the author.");
		gets(p[count].author);
		puts("Now enter the value.");
		scanf("%f",&p[count].value);
		count++;
		while(getchar()!='\n')
			continue;
		if(count<MAXBKS)
			puts("Enter the next title.");
		else
			puts("The book list file is full.");
	}
}

void write_file(struct book *p,char *filename)
{
	FILE *pbooks;
	pbooks=fopen(filename,"w+b");//wb打开文件,但会自动删除截断已有的内容,目的是清空所有文件!!!!——“最后再把信息集写入文件中。”
	pbooks=fopen(filename,"r+b");//与w+b对应,先清空,再整体r+b写入!!!
	fwrite(p,size,count,pbooks);
	fclose(pbooks);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值