赞
踩
第一步,我打算将excel表格的文件转化成.txt的文本文件,方便我们对文件数据的读取。
第二步,既然获得了数据的txt文件,那在编程中读取文件信息即可。
//打开文件,只读
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("打开文件失败\n");
}
用fopen函数打开文件,该函数在库中的声明为FILE * fopen ( const char * filename, const char * mode );
函数作用是打开参数filename中指定其名称的文件,并将其与可在将来的操作中由返回的FILE 指针标识的流相关联。流上允许的操作及其执行方式由 mode 参数定义。
通俗的话说就是,打开第一个参数名的文件,你能对这个文件进行的操作由第二个参数决定,最后函数会返回该文件的FILE*文件指针,其中第二个参数选择如下:
“r” 读:打开文件进行输入操作。该文件必须存在。
“w” 写:为输出操作创建一个空文件。如果已存在同名文件,则会丢弃其内容,并将该文件视为新的空文件。
“一” 附加:在文件末尾打开文件进行输出。输出操作始终将数据写入文件末尾,并对其进行扩展。重新定位操作(fseek、fsetpos、rewind)将被忽略。如果文件不存在,则创建该文件。
“r+” 读取/更新:打开文件进行更新(输入和输出)。该文件必须存在。
“w+” 写入/更新:创建一个空文件并打开它进行更新(用于输入和输出)。如果已存在同名文件,则会丢弃其内容,并将该文件视为新的空文件。
“A+” 追加/更新:打开一个文件进行更新(输入和输出),所有输出操作都在文件末尾写入数据。重新定位操作(fseek、fsetpos、rewind)会影响下一个输入操作,但输出操作会将位置移回文件末尾。如果文件不存在,则创建该文件。
第三步自然就是文件数据的处理。
typedef struct Data
{
char Date[20]; //日期
char Time[20]; //时间
char Encoding[20]; //编码
int SalesVolume; //销量
char UnitPrice[20]; //单价
char SalesType[20]; //销售类型
char DiscountSale[20]; //打折销售
char SingleCategory[20]; //单品类
char BroadCategory[20]; //大类
char WholesalePrice[20]; //批发价格
char ItemLossRate[20]; //单品损耗率
char CostPlusPricing[20]; //成本加成定价
}Data;
for (Data* New = Initdata(); 12 == fscanf(pf, "%s%s%s%lf%s%s%s%s%s%s%s%s", &New->Date, &New->Time, &New->Encoding, &n, &New->UnitPrice, &New->SalesType, &New->DiscountSale, &New->SingleCategory, &New->BroadCategory, &New->WholesalePrice, &New->ItemLossRate, &New->CostPlusPricing);)
创建一个存储数据用的结构体用于储存从文件中读取的数据,使用fscanf函数将读取到的数据存到结构体中。
fscanf函数在库中的声明为int fscanf ( FILE * stream, const char * format, … );
函数作用是从流中读取数据,并根据参数格式将其存储到附加参数指向的位置。其他参数应指向格式字符串中相应格式说明符指定的类型的已分配对象。成功后,该函数返回成功填充的参数列表的项数。
通俗的话说就是,读取第一个参数对应的文件数据,读取数据的类型和个数由第二个参数决定,之后写上对应读取数据数量的存放地址。该函数会返回你读取数据的个数。所以在该代码的循环中,一次循环读取12个数据,当读取完所有数据后,fscanf返回值为0,循环结束。
//判断时间段(共10个判断)
if (New->Date[3] == '0' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 4 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 10)
{
//判断大类(共6个判断)
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS1->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS1->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS1->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS1->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS1->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS1->solanacea += New->SalesVolume;
}
最后就是处理读取到的数据了,在for循环中用判断语句筛选即可。
该代码则是对一段时间中各大类的销量进行分类,第一层if判断时间段,第二层if判断大类。
在调试过程中发现,小数会在小数点后17位有2的误差,在87万分数据中可能会放大误差,所以我将读取到的浮点数*100+0.1后在转化为整形,这样可以消除因此产生误差的可能性。
#pragma warning(disable:4996)
#include<iostream>
#include <iomanip>
#include"definition.h"
using namespace std;
int main()
{
double n; //记销量
//int i = 50; //测试查看前i个销量数据
BroadCategory* BC = InitBroadCategory();//大类字符串
//各时间段各大类销量
BroadCategorySalesVolume* BCS1 = InitBroadCategorySalesVolume();//20年7到10
BroadCategorySalesVolume* BCS2 = InitBroadCategorySalesVolume();// 11到12
BroadCategorySalesVolume* BCS3 = InitBroadCategorySalesVolume();// 21年1到3
BroadCategorySalesVolume* BCS4 = InitBroadCategorySalesVolume();// 4到10
BroadCategorySalesVolume* BCS5 = InitBroadCategorySalesVolume();// 11到12
BroadCategorySalesVolume* BCS6 = InitBroadCategorySalesVolume();// 22年1到3
BroadCategorySalesVolume* BCS7 = InitBroadCategorySalesVolume();// 4到10
BroadCategorySalesVolume* BCS8 = InitBroadCategorySalesVolume();// 11到12
BroadCategorySalesVolume* BCS9 = InitBroadCategorySalesVolume();// 23年1到3
BroadCategorySalesVolume* BCS10 = InitBroadCategorySalesVolume();// 4到6
//录入
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("打开文件失败\n");
}
//读取大类名
fscanf(pf, "%s%s%s%s%s%s", BC->capsicum, BC->anthophyllum, BC->AquaticRhizomes, BC->EdibleMushroom, BC->cauliflower, BC->solanacea);
//读取数据
for (Data* New = Initdata(); 12 == fscanf(pf, "%s%s%s%lf%s%s%s%s%s%s%s%s", &New->Date, &New->Time, &New->Encoding, &n, &New->UnitPrice, &New->SalesType, &New->DiscountSale, &New->SingleCategory, &New->BroadCategory, &New->WholesalePrice, &New->ItemLossRate, &New->CostPlusPricing);)
{
New->SalesVolume = n * 1000 + 0.1;
//测试查看前i个销量数据
/*for (i--; i >= 0; )
{
cout << New->SalesVolume << endl;
break;
}*/
//判断时间段(共10个判断)
if (New->Date[3] == '0' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 4 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 10)
{
//判断大类(共6个判断)
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS1->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS1->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS1->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS1->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS1->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS1->solanacea += New->SalesVolume;
}
}
else if (New->Date[3] == '0' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 11 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 12)
{
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS2->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS2->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS2->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS2->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS2->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS2->solanacea += New->SalesVolume;
}
}
else if (New->Date[3] == '1' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 1 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 3)
{
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS3->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS3->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS3->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS3->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS3->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS3->solanacea += New->SalesVolume;
}
}
else if (New->Date[3] == '1' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 4 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 10)
{
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS4->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS4->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS4->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS4->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS4->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS4->solanacea += New->SalesVolume;
}
}
else if (New->Date[3] == '1' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 11 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 12)
{
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS5->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS5->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS5->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS5->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS5->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS5->solanacea += New->SalesVolume;
}
}
else if (New->Date[3] == '2' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 1 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 3)
{
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS6->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS6->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS6->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS6->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS6->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS6->solanacea += New->SalesVolume;
}
}
else if (New->Date[3] == '2' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 4 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 10)
{
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS7->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS7->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS7->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS7->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS7->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS7->solanacea += New->SalesVolume;
}
}
else if (New->Date[3] == '2' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 11 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 12)
{
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS8->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS8->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS8->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS8->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS8->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS8->solanacea += New->SalesVolume;
}
}
else if (New->Date[3] == '3' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 1 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 3)
{
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS9->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS9->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS9->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS9->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS9->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS9->solanacea += New->SalesVolume;
}
}
else if (New->Date[3] == '3' && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) >= 4 && ((New->Date[5] - 48) * 10 + New->Date[6] - 48) <= 10)
{
if (strcmp(New->BroadCategory, BC->capsicum) == 0)
{
BCS10->capsicum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->anthophyllum) == 0)
{
BCS10->anthophyllum += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->AquaticRhizomes) == 0)
{
BCS10->AquaticRhizomes += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->EdibleMushroom) == 0)
{
BCS10->EdibleMushroom += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->cauliflower) == 0)
{
BCS10->cauliflower += New->SalesVolume;
}
else if (strcmp(New->BroadCategory, BC->solanacea) == 0)
{
BCS10->solanacea += New->SalesVolume;
}
}
New = Initdata();
}
//输出
cout << "20年7到10月" << endl << "辣椒类:" /*<< fixed << setprecision(3)*/ << BCS1->capsicum << endl << "花叶类:" << BCS1->anthophyllum << endl << "水生根茎类:" << BCS1->AquaticRhizomes << endl;
cout << "食用菌:" << BCS1->EdibleMushroom << endl << "花菜类:" << BCS1->cauliflower << endl << "茄类:" << BCS1->solanacea << endl << endl;
cout << "20年11到21年3月" << endl << "辣椒类:" << BCS2->capsicum + BCS3->capsicum << endl << "花叶类:" << BCS2->anthophyllum + BCS3->anthophyllum << endl << "水生根茎类:" << BCS2->AquaticRhizomes + BCS3->AquaticRhizomes << endl;
cout << "食用菌:" << BCS2->EdibleMushroom + BCS3->EdibleMushroom << endl << "花菜类:" << BCS2->cauliflower + BCS3->cauliflower << endl << "茄类:" << BCS2->solanacea + BCS3->solanacea << endl << endl;
/*cout << "21年1到3月" << endl << "辣椒类:" << BCS3->capsicum << endl << "花叶类:" << BCS3->anthophyllum << endl << "水生根茎类:" << BCS3->AquaticRhizomes << endl;
cout << "食用菌:" << BCS3->EdibleMushroom << endl << "花菜类:" << BCS3->cauliflower << endl << "茄类:" << BCS3->solanacea << endl << endl;*/
cout << "21年4到10月" << endl << "辣椒类:" << BCS4->capsicum << endl << "花叶类:" << BCS4->anthophyllum << endl << "水生根茎类:" << BCS4->AquaticRhizomes << endl;
cout << "食用菌:" << BCS4->EdibleMushroom << endl << "花菜类:" << BCS4->cauliflower << endl << "茄类:" << BCS4->solanacea << endl << endl;
cout << "21年11到22年3月" << endl << "辣椒类:" << BCS5->capsicum + BCS6->capsicum << endl << "花叶类:" << BCS5->anthophyllum + BCS6->anthophyllum << endl << "水生根茎类:" << BCS5->AquaticRhizomes + BCS6->AquaticRhizomes << endl;
cout << "食用菌:" << BCS5->EdibleMushroom + BCS6->EdibleMushroom << endl << "花菜类:" << BCS5->cauliflower + BCS6->cauliflower << endl << "茄类:" << BCS5->solanacea + BCS6->solanacea << endl << endl;
/*cout << "22年1到3月" << endl << "辣椒类:" << BCS6->capsicum << endl << "花叶类:" << BCS6->anthophyllum << endl << "水生根茎类:" << BCS6->AquaticRhizomes << endl;
cout << "食用菌:" << BCS6->EdibleMushroom << endl << "花菜类:" << BCS6->cauliflower << endl << "茄类:" << BCS6->solanacea << endl << endl;*/
cout << "22年4到10月" << endl << "辣椒类:" << BCS7->capsicum << endl << "花叶类:" << BCS7->anthophyllum << endl << "水生根茎类:" << BCS7->AquaticRhizomes << endl;
cout << "食用菌:" << BCS7->EdibleMushroom << endl << "花菜类:" << BCS7->cauliflower << endl << "茄类:" << BCS7->solanacea << endl << endl;
cout << "22年11到23年3月" << endl << "辣椒类:" << BCS8->capsicum + BCS9->capsicum << endl << "花叶类:" << BCS8->anthophyllum + BCS9->anthophyllum << endl << "水生根茎类:" << BCS8->AquaticRhizomes + BCS9->AquaticRhizomes << endl;
cout << "食用菌:" << BCS8->EdibleMushroom + BCS9->EdibleMushroom << endl << "花菜类:" << BCS8->cauliflower + BCS9->cauliflower << endl << "茄类:" << BCS8->solanacea + BCS9->solanacea << endl << endl;
/*cout << "23年1到3月" << endl << "辣椒类:" << BCS9->capsicum << endl << "花叶类:" << BCS9->anthophyllum << endl << "水生根茎类:" << BCS9->AquaticRhizomes << endl;
cout << "食用菌:" << BCS9->EdibleMushroom << endl << "花菜类:" << BCS9->cauliflower << endl << "茄类:" << BCS9->solanacea << endl << endl;*/
cout << "23年4到6月" << endl << "辣椒类:" << BCS10->capsicum << endl << "花叶类:" << BCS10->anthophyllum << endl << "水生根茎类:" << BCS10->AquaticRhizomes << endl;
cout << "食用菌:" << BCS10->EdibleMushroom << endl << "花菜类:" << BCS10->cauliflower << endl << "茄类:" << BCS10->solanacea << endl << endl;
fclose(pf);
return 0;
}
#pragma once
#include<iostream>
#include<string>
using namespace std;
typedef struct Data
{
char Date[20]; //日期
char Time[20]; //时间
char Encoding[20]; //编码
int SalesVolume; //销量
char UnitPrice[20]; //单价
char SalesType[20]; //销售类型
char DiscountSale[20]; //打折销售
char SingleCategory[20]; //单品类
char BroadCategory[20]; //大类
char WholesalePrice[20]; //批发价格
char ItemLossRate[20]; //单品损耗率
char CostPlusPricing[20]; //成本加成定价
}Data;
typedef struct BroadCategory
{
char capsicum[20];//辣椒类
char anthophyllum[20];//花叶类
char AquaticRhizomes[20];//水生根茎类
char EdibleMushroom[20];//食用菌
char cauliflower[20];//花菜类
char solanacea[20];//茄类
}BroadCategory;
typedef struct BroadCategorySalesVolume
{
int capsicum;//辣椒类
int anthophyllum;//花叶类
int AquaticRhizomes;//水生根茎类
int EdibleMushroom;//食用菌
int cauliflower;//花菜类
int solanacea;//茄类
}BroadCategorySalesVolume;
//初始化数据
Data* Initdata()
{
Data* New = (Data*)malloc(sizeof(Data));
if (New == NULL)
{
printf("数据创建失败");
return NULL;
}
New->SalesVolume = 0;
return New;
}
//初始化大类字符串
BroadCategory* InitBroadCategory()
{
BroadCategory* New = (BroadCategory*)malloc(sizeof(BroadCategory));
if (New == NULL)
{
printf("数据创建失败");
return NULL;
}
return New;
}
//初始化大类销量
BroadCategorySalesVolume* InitBroadCategorySalesVolume()
{
BroadCategorySalesVolume* New = (BroadCategorySalesVolume*)malloc(sizeof(BroadCategorySalesVolume));
if (New == NULL)
{
printf("数据创建失败");
return NULL;
}
New->capsicum = 0;//辣椒类
New->anthophyllum = 0;//花叶类
New->AquaticRhizomes = 0;//水生根茎类
New->EdibleMushroom = 0;//食用菌
New->cauliflower = 0;//花菜类
New->solanacea = 0;//茄类
return New;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。