关于结构体数组的问题

一个人吃火锅 2016-01-08 10:16:55
#include<stdio.h>
#include<string.h>

//结构体声明
struct customer
{
int ID;
int enterHour;
int enterMinute;
int enterSecond;
char noon[2];
int num;
int age;
};

struct customer1
{
int ID;
int enterTime;
int enterWaitTime;
int enterCountTime;
int leaveTime;
int num;
int age;
int condition;//1代表在等待区;2代表第一次选择;3代表最终选择;4代表结账完成,已离开
bool youxianji;//优先结账
};

//变量声明
customer enterCst[10000],leaveCst[10000];

customer1 Cst[10000],Cst1[10000],leaveCst1[10000];
customer1 SpecialCountCst[15];
customer1 SampleCountCst[15];
customer1 SureCountCst[15];

int SUM;//总人数
int countAreaClearTime;//结账区清空的时间
int leaveCstSum=0;//已离开的人数
int Special;
int Sample;//记录两种人群的人数

void enterSupermarket()//读入文件内容,并进行预处理,调试已完成
{
SUM=0;
customer1 exchange;
FILE* fp;
fp=fopen("G:\\数据结构与算法分析\\supermarket.txt","r");
if(fp == NULL)
return;
for(int i=0;i<10000;i++)
{
fscanf(fp,"%d %d:%d:%d%s %d %d\n",&enterCst[i].ID,&enterCst[i].enterHour,&enterCst[i].enterMinute,&enterCst[i].enterSecond,&enterCst[i].noon,&enterCst[i].num,&enterCst[i].age);
if(enterCst[i].ID != 0)
SUM++;
}
fclose(fp);

for(int i=0;i<SUM;i++)
{

Cst[i].ID = enterCst[i].ID;
Cst[i].age = enterCst[i].age;
if(strcmp("AM",enterCst[i].noon) == 0)
Cst[i].enterTime = enterCst[i].enterHour*3600 + enterCst[i].enterMinute*60 + enterCst[i].enterSecond;
else if(enterCst[i].enterHour >= 1)
Cst[i].enterTime = enterCst[i].enterHour*3600 + enterCst[i].enterMinute*60 + enterCst[i].enterSecond + 12*3600;
else
Cst[i].enterTime = enterCst[i].enterHour*3600 + enterCst[i].enterMinute*60 + enterCst[i].enterSecond;
Cst[i].num = enterCst[i].num;
Cst[i].enterWaitTime = Cst[i].enterTime + Cst[i].num*10;
if(Cst[i].age <= 10 || Cst[i].age >= 60)
Cst[i].youxianji = true;
else
Cst[i].youxianji = false;
Cst[i].condition = 1;
}

for(int i=0;i<SUM;i++)//直接按进入等待区的时间进行排序
{
for(int j=i+1;j<SUM;j++)
{
if(Cst[i].enterWaitTime > Cst[j].enterWaitTime && Cst[j].enterWaitTime != 0)
{
exchange = Cst[i];
Cst[i] = Cst[j];[code=c]

Cst[j] = exchange;
}
}
}
for(int i=0;i<SUM;i++)
Cst1[i] = Cst[i];
}

void divideCustomer()//将人群分为两类
{
int j1=0,j2=0;
for(int i=0;i<10000;i++)
{
if(Cst[i].age != 0)
{
if(Cst1[i].youxianji == true)
{
SpecialCountCst[j1] = Cst1[i];
j1++;
}
else
{
SampleCountCst[j2] = Cst1[i];
j2++;
}
}
}
Special = j1;
Sample = j2;
}

void main()
{
countAreaClearTime = 0;
enterSupermarket();
divideCustomer();
for(int i=0;i<4;i++)
printf("%d %d\n",SpecialCountCst[i].ID,SpecialCountCst[i].enterWaitTime);
for(int i=0;i<20;i++)
printf("%d %d\n",SampleCountCst[i].ID,SampleCountCst[i].enterWaitTime);
}
//SampleCountCst[15]打印出现错误,其他的都正常。在divideCustomer函数中加入断点调试的时候,显示的数据是没有问题 //的,哪位高手来指教下小弟[/code]
...全文
124 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-01-11
  • 打赏
  • 举报
回复
“多一少一”问题占程序员常犯错误的10%以上! 避免“多一少一”问题的方法之一是将比如<10甚至<5的数代入程序片断,搬手指头心算验证一下程序到底应该写为 x、x-1、x+1中的哪个? <、<=、==、>、>=中的哪个?
一个人吃火锅 2016-01-10
  • 打赏
  • 举报
回复
其实我晚上出去转了一圈回来就想出来了,那天真是脑子太乱了。
lilianjie001 2016-01-09
  • 打赏
  • 举报
回复
customer1 SampleCountCst[15]; 下标是0~14的,for(int i=0;i<20;i++)中是i<20,所以到SampleCountCst[15]已经越界了。 customer1 SampleCountCst[15];定义大于20即可

69,378

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧