您的位置:首页 > 其它

文章标题

2016-10-16 09:37 162 查看
1.将一个要统计的数据信息存放在一个文件里面,如a.txt

void Readfile(POAct * mypoact)

2.将文本文件里的字符串解析出来,转换成数字存储在自定义类型的变量里,并存放在自定义数组里面

void ToStoreData(POAct * ppact, char * parr)

3.根据题目给出的条件进行运算

void CalclutData(POAct * pact)

4.自定义的数据类型

typedef struct Data
{
int year[1];
int month[1];
int day[1];
}PData;

typedef struct Times       // 时间类型
{
int starthours[1];
int startmint[1];
int endhours[1];
int endmint[1];
}PTmies;

typedef struct OneActivty    //一次活动需要的类型
{
struct Data mdata;
struct Times mtime;
int persons;
int Income[1];
int Payment[1];
int rentgyms[1];
int retainedprof[1];
}POAct, MPData[MAX];


编译环境 windows 下的visual Studio 2013

#include<stdio.h>
#include<iostream>
using namespace std;
#define MAX 10
typedef struct Data { int year[1]; int month[1]; int day[1]; }PData; typedef struct Times // 时间类型 { int starthours[1]; int startmint[1]; int endhours[1]; int endmint[1]; }PTmies; typedef struct OneActivty //一次活动需要的类型 { struct Data mdata; struct Times mtime; int persons; int Income[1]; int Payment[1]; int rentgyms[1]; int retainedprof[1]; }POAct, MPData[MAX];

// 将字符串类型的数据 转化成结构体类型中
void ToStoreData(POAct * ppact, char * parr)
{
int len = strlen(parr);

if (ppact == NULL) exit(1);
int i = 0;
int value = 0;
int digits = 0;
while (*parr != '\0')
{
for (i; i < 4; ++i)
{
digits = *parr - '0';
value *= 10;
value += digits;
parr++;
}
ppact->mdata.year[0] = value; //获得了年份

parr++;
value = 0;
for (i = 5; i < 7; ++i)
{
digits = *parr - '0';
value *= 10;
value += digits;
parr++;
}
ppact->mdata.month[0] = value;

parr++;
value = 0;
for (i = 8; i < 10; ++i)
{
digits = *parr - '0';
value *= 10;
value += digits;
parr++;
}
ppact->mdata.day[0] = value;

parr++; //获得时间段
value = 0;
for (i = 11; i < 13; ++i)
{
digits = *parr - '0';
value *= 10;
value += digits;
parr++;
}
ppact->mtime.starthours[0] = value;
parr++;

value = 0; // 获得时间段
for (i = 14; i < 16; ++i)
{
digits = *parr - '0';
value *= 10;
value += digits;
parr++;
}
ppact->mtime.startmint[0] = value;
parr++;

value = 0;
for (i = 17; i < 19; ++i)
{
digits = *parr - '0';
value *= 10;
value += digits;
parr++;
}
ppact->mtime.endhours[0] = value;
parr++;

value = 0;
for (i = 20; i < 22; ++i)
{
digits = *parr - '0';
value *= 10;
value += digits;
parr++;
}
ppact->mtime.endmint[0] = value;
parr++;

value = 0; // 获得人数
for (i = 23; i < len; ++i)
{
digits = *parr - '0';
value *= 10;
value += digits;
parr++;
}
ppact->persons = value;
}
//return ppact;
}
void CalclutData(POAct * pact)
{
//计算要租用几个体育馆
int np = 0;
int t = 0;
if (pact->persons < 4)
{
pact->rentgyms[0] = 0;
pact->Income[0] = 0;
}
else
{
pact->Income[0] = pact->persons * 30;
np = pact->persons / 6;
t = pact->persons % 6;
if (np == 0)
{
pact->rentgyms[0] = 1;
}
else if (np == 1)
{
pact->rentgyms[0] = 2;
}
else if (np == 2 || np == 3)
{
if (t >= 4)
{
pact->rentgyms[0] = np + 1;
}
else
{
pact->rentgyms[0] = np;
}
}
else if (np > 3)
{
pact->rentgyms[0] = np;
}
}

//计算一个日期是星期几
int y, m, d, c, w;
y = pact->mdata.year[0];
m = pact->mdata.month[0];
d = pact->mdata.day[0];
if (m == 1 || m == 2) //判断月份是否为1或2
{
y--;
m += 12;
}
c = y / 100;
y = y - c * 100;
w = (c / 4) - 2 * c + (y + y / 4) + (13 * (m + 1) / 5) + d - 1;
while (w < 0) w += 7;
w %= 7;
int h; // 租用的时间
int tprice; //临时存储租用体育馆的价格
int tag = 0; //作为下面计算的标记
if (w == 0 || w == 6) // 判断是星期几 星期天和星期六
{
if (tag == 0 && pact->mtime.starthours[0] >= 18 && pact->mtime.endhours[0] <= 22) //18~22 60
{
tag = 1;
h = pact->mtime.endhours[0] - pact->mtime.starthours[0];
tprice = h * 60;
pact->Payment[0] = tprice * pact->rentgyms[0];

}
else if (tag == 0 && pact->mtime.starthours[0] >= 12 && pact->mtime.endhours[0] > 18)//结束时间超过18点
{
tag = 1;
h = pact->mtime.endhours[0] - 18;
tprice = h * 60 + (pact->mtime.starthours[0] - 12) * 50;
pact->Payment[0] = tprice * pact->rentgyms[0];
}

else if (tag == 0 && pact->mtime.starthours[0] >= 12 && pact->mtime.endhours[0] <= 18) //12~18 50
{
tag = 1;
h = pact->mtime.endhours[0] - pact->mtime.starthours[0];
tprice = h * 50;
pact->Payment[0] = tprice * pact->rentgyms[0];
}
else if (tag == 0 && pact->mtime.starthours[0] >= 9 && pact->mtime.endhours[0] > 12) // 结束时间超过12点
{
tag = 1;
h = pact->mtime.endhours[0] - 12;
tprice = h * 50 + (12 - pact->mtime.starthours[0]) * 40;
pact->Payment[0] = tprice * pact->rentgyms[0];
}

else if (tag == 0 && pact->mtime.starthours[0] >= 9 && pact->mtime.endhours[0] <= 12) //9~12 *40
{
tag = 1;
h = pact->mtime.endhours[0] - pact->mtime.starthours[0];
tprice= h * 40;
pact->Payment[0] = tprice * pact->rentgyms[0];
}
}
else //周内星期一到星期五 有各自的判断区间
{
if (tag == 0 && pact->mtime.starthours[0] >= 20 && pact->mtime.endhours[0] <= 22) //20~22 60
{
tag = 1;
h = pact->mtime.endhours[0] - pact->mtime.starthours[0];
tprice = h * 60;
pact->Payment[0] = tprice * pact->rentgyms[0];
}

else if (tag == 0 && pact->mtime.starthours[0] >= 18 && pact->mtime.endhours[0] <= 20) //18~20 80
{
tag = 1;
h = pact->mtime.endhours[0] - pact->mtime.starthours[0];
tprice = h * 80;
pact->Payment[0] = tprice * pact->rentgyms[0];
}

else if (tag == 0 && pact->mtime.starthours[0] >= 18 && pact->mtime.endhours[0] <= 22) // 结束时间超过20点 *60
{
tag = 1;
h = pact->mtime.endhours[0] - 20;
tprice = h * 60 + (20 - pact->mtime.starthours[0]) * 50;
pact->Payment[0] = tprice * pact->rentgyms[0];
}

else if (tag == 0 && pact->mtime.starthours[0] >= 12 && pact->mtime.endhours[0] <= 18) //12~18 50
{
tag = 1;
h = pact->mtime.endhours[0] - pact->mtime.starthours[0];
tprice = h * 50;
pact->Payment[0] = tprice * pact->rentgyms[0];
}

else if (tag == 0 && pact->mtime.starthours[0] >= 12 && pact->mtime.endhours[0] <= 20) // 结束时间超过18点 *80
{
tag = 1;
h = pact->mtime.endhours[0] - 18;
tprice = h * 80 + (12 - pact->mtime.starthours[0]) * 50;
pact->Payment[0] = tprice * pact->rentgyms[0];
}

if (tag == 0 && pact->mtime.starthours[0] >= 9 && pact->mtime.endhours[0] <= 12) //9~12 *30
{
tag = 1;
h = pact->mtime.endhours[0] - pact->mtime.starthours[0];
tprice = h * 30;
pact->Payment[0] = tprice * pact->rentgyms[0];
}

else if (tag == 0 && pact->mtime.starthours[0] >= 9 && pact->mtime.endhours[0] <= 18) // 结束时间超过12点 *50
{
tag = 1;
h = pact->mtime.endhours[0] - 12;
tprice = h * 50 + (12 - pact->mtime.starthours[0]) * 30;
pact->Payment[0] = tprice * pact->rentgyms[0];
}
}
pact->retainedprof[0] = pact->Income[0] - pact->Payment[0];
}
void Readfile(POAct * mypoact) // 读文件操作,数据文件存储在当前目录的a.txt下
{
FILE * fp = fopen("a.txt", "r+");
if (NULL == fp)
{
exit(1);
}
char strmp[27];
int i = 0;

char *tmp = strmp;
int ret;
while (1)
{
ret = fread(tmp++, 1, 1, fp);

if (*(tmp - 1) == '\n' || ret == 0)
{
*--tmp = 0;
printf("%s\n", strmp);
ToStoreData(mypoact++,strmp); //调用将字符串转换成成PActOne类型,并存储在结构体数组里面
tmp = strmp;
if (ret == 0) break;
continue;
}
}
}
void CountCalOne(POAct * pact)
{
for (int i = 0; i < MAX; ++i)
{
CalclutData(pact++);
}
printf("\n");
}
void PrintData(POAct * phead) //打印数据
{
if (phead == NULL)
return;
int i = 0;
int totalinc = 0;
int totalpaym = 0;
int profit = 0;
POAct * p = phead;
for(i; i < MAX; ++i)
{
totalinc += (p++)->Income[0];
p--;
totalpaym += (p++)->Payment[0];
}
profit = totalinc - totalpaym;
for (i = 0; i < MAX; ++i)
{
printf("%d-%d-%d %d:%d0~%d:%d0 %d -%d %d\n",phead->mdata.year[0],
phead->mdata.month[0],phead->mdata.day[0],
phead->mtime.starthours[0],phead->mtime.startmint[0],
phead->mtime.endhours[0],phead->mtime.endmint[0],
phead->Income[0],phead->Payment[0],
phead->persons);
phead++;
}
printf("Total Income : %d\n",totalinc);
printf("Total Payment: %d\n",totalpaym);
printf("Profit %d:\n",profit);
}
void main()
{

POAct *mact = NULL;
POAct MData[MAX];
Readfile(MData);
CountCalOne(MData);
PrintData(MData);
}



运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  思沃作业