中易网

c语言作业. 旅游景点管理程序(大一的作业) 主要用结构体数组实现,用结构体表示每项记录,包含数据

答案:2  悬赏:70  
解决时间 2021-02-23 11:01
c语言作业. 旅游景点管理程序(大一的作业)
主要用结构体数组实现,用结构体表示每项记录,包含数据为:编号、景点名称、旅游地名、所需费用等。
设计各个函数,分别实现以下功能:
(1)录入:完成景点信息的输入;
(2)删除:完成景点信息的删除;
(3)修改:允许对已经录入的数据重新进行编辑、修改;
(4)显示:显示所有景点的信息;
(5)查询:输入旅游地名,显示符合该条件的景点记录,并求所需总费用,并排序输出;
(6)统计:输出各旅游地的景点数和总费用。
(7)退出程序。
设计菜单,通过选择菜单调用以上各函数。
最佳答案
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int SIZE=2;
struct Spot{
char id[10]; //景点编号
char name[20]; //景点名称
char address[50]; //景点地址
float cost; //费用
};
struct Spot scenery[20];
char sub_id[10]; //用于储存要查询、修改、删除景点的编号


void Input(Spot scenery[],int SIZE); //录入函数
void Del(Spot scenery[],char sub_id[],int &SIZE); //删除函数
void Alter(Spot scenery[],char sub_id[],int SIZE); //修改函数
void Output(Spot scenery[],int SIZE); //显示函数
void Select(Spot scenery[],char sub_id[],int SIZE); //查询函数
void Count(Spot scenery[],int SIZE); //统计函数
void Exit(); //退出函数

void Input(Spot scenery[10],int SIZE)
{
int i;
printf("please input\n");
printf("------------------------------------------\n");
for(i=0;i<SIZE;i++)
{
printf("please input id:");
gets(scenery[i].id);
printf("please input name:");
gets(scenery[i].name);
printf("please input address:");
gets(scenery[i].address);
printf("please input cost:");
scanf("%f",&scenery[i].cost);
fflush(stdin); //清空键盘缓冲区
printf("\n");
}
printf("------------------输入完毕----------------\n");
}

void Del(Spot scenery[],char sub_id[],int &SIZE)
{
int i,j;
printf("please input delete spot's id:");
gets(sub_id);
for(i=0;i<SIZE;i++)
{
if(strcmp(scenery[i].id,sub_id)==0)
{
for(j=i;j<SIZE;j++)
scenery[j]=scenery[j+1];
SIZE--;
}
}
printf("-------------删除成功-------------\n");
}

void Alter(Spot scenery[],char sub_id[],int SIZE)
{
int i;
printf("please input alter spot's id:");
gets(sub_id);
for(i=0;i<SIZE;i++)
{
if(strcmp(scenery[i].id,sub_id)==0)
{
printf("alter spot's name is:");
gets(scenery[i].name);
printf("alter spot's address is:");
gets(scenery[i].address);
printf("alter spot's cost is:");
scanf("%f",&scenery[i].cost);
fflush(stdin);
break;
}
}
printf("-------------修改成功------------\n");
}

void Output(Spot scenery[],int SIZE)
{
int i;
printf("output scenery:\n");
printf("----------------------------------------\n");
for(i=0;i<SIZE;i++)
{
printf("id:%s name:%s address:%s cost:%.2f\n",scenery[i].id,scenery[i].name,scenery[i].address,scenery[i].cost);
}
printf("------------------输出完毕---------------\n");
}

void Select(Spot scenery[],char sub_id[],int SIZE)
{
int i;
printf("please input select spot's id:");
gets(sub_id);
printf("-----------------查询结果--------------\n");
for(i=0;i<SIZE;i++)
{
if(strcmp(scenery[i].id,sub_id)==0)
printf("id:%s name:%s address:%s cost:%.2f\n",scenery[i].id,scenery[i].name,scenery[i].address,scenery[i].cost);
}
}

void Count(Spot scenery[],int SIZE)
{
int i;
float sum; //sum用于储存总费用
printf("-------------------统计结果------------\n");
for(i=0,sum=0;i<SIZE;i++)
sum+=scenery[i].cost;
printf("scenery number is:%d cost is:%.2f\n",SIZE,sum);
}

void Exit()
{
printf("----------------已退出--------------\n");
exit(1);
}

void main()
{
int s;
while(1)
{
printf("please input:\n");
printf("1、录入。\n2、删除。\n3、修改。\n4、显示。\n5、查询。\n6、统计。\n7、退出。\n");
scanf("%d",&s);
fflush(stdin);
switch(s)
{
case 1:Input(scenery,SIZE);break;
case 2:Del(scenery,sub_id,SIZE);break;
case 3:Alter(scenery,sub_id,SIZE);break;
case 4:Output(scenery,SIZE);break;
case 5:Select(scenery,sub_id,SIZE);break;
case 6:Count(scenery,SIZE);break;
case 7:Exit();break;
default:printf("please input again:");break;
}
}

}
全部回答
//你想输入几种水果的名字的话,就得创建一个结构体数组变量呀 #include #include struct friut { char name[20]; float price; //价格的话用一个变量来表示就行了,不需要用到数组 }; int main() { struct friut a[5];//比如创建一个结构体数组变量,存放5种水果的名字和价格 int i,n; scanf ("%d",&n);//这里的n就要取小于 5 以下的数了,当然你也可以取更大的数组 for (i=0;i
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯