怎么使用结构体数组(C语言)
4个回答
展开全部
代码仅供参考:
#include "stdio.h"
#include "conio.h"
struct books
{
char *name;
char *press;
char *author;
float price;
};
struct books lib[]={{"C language","UESTC","Zhuang San",12.50},{"PASCAL","UESTC","Li Si",10.00},{"English Reading","UESTC","Wang wu",8.00},{"########","#####","######",0}};//由于篇幅有限,这里只给出三本书的信息
main()
{
find();
printf("Press any key continue...\n");
getch();
}
//图书查询子函数
find()
{
struct books *p;
char sm[100];
int i=0;
p=&lib[i];
printf("Please input the name of books:\n");
gets(sm);
printf("The information of the books:\n");
while(p->price!=0)//用p->price成员的值控制循环
{
if(strcmp(p->name,sm)==0)
{
printf("%18s%8s%8s%7.2f\n",p->name,p->author,p->press,p->price);
break;//当字符比较函数值为0,表明该书已找到,打印并跳出循环
}
i++;//否则将下标加1移向下一元素
p=&lib[i];//让指针再指向这一元素后继续查找
}
if(p->price==0)//p->price=0,表明整个书库都已查完并没有找到所要的书
printf("Sorry ! Your inquires the book was not found.\n");
}
#include "stdio.h"
#include "conio.h"
struct books
{
char *name;
char *press;
char *author;
float price;
};
struct books lib[]={{"C language","UESTC","Zhuang San",12.50},{"PASCAL","UESTC","Li Si",10.00},{"English Reading","UESTC","Wang wu",8.00},{"########","#####","######",0}};//由于篇幅有限,这里只给出三本书的信息
main()
{
find();
printf("Press any key continue...\n");
getch();
}
//图书查询子函数
find()
{
struct books *p;
char sm[100];
int i=0;
p=&lib[i];
printf("Please input the name of books:\n");
gets(sm);
printf("The information of the books:\n");
while(p->price!=0)//用p->price成员的值控制循环
{
if(strcmp(p->name,sm)==0)
{
printf("%18s%8s%8s%7.2f\n",p->name,p->author,p->press,p->price);
break;//当字符比较函数值为0,表明该书已找到,打印并跳出循环
}
i++;//否则将下标加1移向下一元素
p=&lib[i];//让指针再指向这一元素后继续查找
}
if(p->price==0)//p->price=0,表明整个书库都已查完并没有找到所要的书
printf("Sorry ! Your inquires the book was not found.\n");
}
展开全部
先定义一个结构体 假设说是
struct a
{
……;
};
然后就
struct a[NUM];
就可以和平常一样用了:
比如说a[1].***之类的。
struct a
{
……;
};
然后就
struct a[NUM];
就可以和平常一样用了:
比如说a[1].***之类的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先定义结构体
struct book
{
char bookname[10];
int useyn;/*可描述两种状态“借出”或“未借出”*/
char neirong[100];
};
再用book定义结构体类型的数据,如:struct book book;
struct book
{
char bookname[10];
int useyn;/*可描述两种状态“借出”或“未借出”*/
char neirong[100];
};
再用book定义结构体类型的数据,如:struct book book;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询