C++ 显示在 txt 文件中找到的唯一日期及其相关价格
C++ Display unique date and its related price found in txt file
目前我有一个将用户输入存储在文本文件中的系统,以及一个计算当月每日销售额并显示当天销售额和该月总计的函数。
例如,下面是 4 月份的一些文本文件数据:
1|Bag|2|3|6|20Apr2015
2|File|2.3|5|11.5|20Apr2015
3|File|2.3|5|15.5|20Apr2015
4|Book|0.9|5|4.5|22Apr2015
5|Pencil|0.9|5|4.5|25Apr2015
6|Ruler|0.9|5|4.5|20Jul2015
7|Eraser|0.9|5|4.5|20Jul2015
8|Bag|0.9|5|4.5|28Apr2015
9|File|0.9|5|4.5|20Apr2015
这是我当前的输出:
20Apr2015 $11.5
20Apr2015 $15.5
22Apr2015 $4.5
25Apr2015 $4.5
28Apr2015 $4.5
20Apr2015 $4.5
Grand Total $45
但我想要的结果应该是这样的:
20Apr2015 $31.5
22Apr2015 $4.5
25Apr2015 $4.5
28Apr2015 $4.5
Grand Total $45
它应该合并每天的所有销售。
以下是我计算每日销售额的代码。谁能指导我如何获得我想要的输出?
struct TransactionPile
{
// to record item information.
string itemid;
string itemdesc;
float unitprice;
int quantity;
float totalprice;
string date;
time_t now = time(0);
};
//Function to calculate daily sales
void computeDailySales()
{
// current date/time based on current system
time_t now = time(0);
// convert now to string form
char* ct = ctime(&now);
// convert now to tm struct for UTC
tm *ltm = localtime(&now);
char* dt = asctime(ltm);
string itemid;
float totalprice;
float totalsales = 0;
string date;
int year;
int month;
int day;
int found;
string syear;
string smonth;
string sday;
string sdate;
//Get the value of year
year = (1900 + ltm->tm_year);
//Convert the year to string and store into syear
syear = static_cast<ostringstream*>( &(ostringstream() << year) )->str();
//Get the value of month
month = (1 + ltm->tm_mon);
//Convert the month to string and store into smonth
smonth =static_cast<ostringstream*>( &(ostringstream() << month) )->str();
//Get the value of day
day = ltm->tm_mday;
//Convert the day to string and store into sday
sday = static_cast<ostringstream*>( &(ostringstream() << day) )->str();
//Store the different month name in smonth if condition met
if (smonth =="1")
{
smonth ="Jan";
}
if (smonth =="2")
{
smonth ="Feb";
}
if (smonth =="3")
{
smonth ="Mar";
}
if (smonth =="4")
{
smonth ="Apr";
}
if (smonth =="5")
{
smonth ="May";
}
if (smonth =="6")
{
smonth ="Jun";
}
if (smonth =="7")
{
smonth ="Jul";
}
if (smonth =="8")
{
smonth ="Aug";
}
if (smonth =="9")
{
smonth ="Sep";
}
if (smonth =="10")
{
smonth ="Oct";
}
if (smonth =="11")
{
smonth ="Nov";
}
if (smonth =="12")
{
smonth ="Dec";
}
//Concatenate the sday, smonth, syear and store inside sdate
sdate = sday + smonth + syear;
cout<<endl;
for(int i = 0; i < MAX- 1; i++)
{
if(transactionpile[i].itemid !="")
{
//Find all related record according to the value in smonth
found = transactionpile[i].date.find(smonth);
if(found<transactionpile[i].date.length())
{
std::sort(transactionpile.begin(),transactionpile.end(),
[](TransactionPile a, TransactionPile b)
{return a.rawdate<b.rawdate;});
if(i<MAX-2&&transactionpile[i].date==transactionpile[i+1].date)
{
transactionpile[i+1].totalprice+=transactionpile[i].totalprice;
continue;
}
//Add the totalprice of each found records to totalsalesmonth[k]
totalsales += transactionpile[i].totalprice;
}
}
}
//Print out the total sales made on the day itself by system date
cout << " " << "Total Sale" << endl;
cout << " " << totalsales <<endl;
}
在输出transactionpile
之前,请使用日期对列表进行排序。
如果你想得到一个很好的排序(以便输出列表将按顺序排列),请将time_t
值保留在你的结构中。现在假设这个新值将存储在rawdate
字段中。对于排序,您可以使用以下内容:
#include <algorithm>
...
std::sort(transactionpile.begin(),transactionpile.end(),
[](TransactionPile a, TransactionPile b){return a.rawdate<b.rawdate;}
/*lambda function: required because std::sort doesn't know how to sort a TransactionPile
);
之后,您只需要通过添加以下内容来更改输出代码的代码:
if(i<MAX-2&&transactionpile[i].date==transactionpile[i+1].date){
transactionpile[i+1].totalprice+=transactionpile[i].totalprice;
continue; //goes to the next iteration (merge with the current one before printing)
}
这样,由于您的列表已排序,您将消除重复项并最终获得良好的价值。这可能不是最干净的解决方案,但它可以完成工作并且不会在代码中添加太多行。
相关文章:
- 查询SQLite数据库中的日期
- 何时在引用或唯一指针上使用移动语义
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 计算排序向量的向量中唯一值的计数
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- Log4cpp:以UTC/GMT时区打印日期
- 通过组合不同的类型来创建唯一的id
- 使用Unique_ptr确保工厂中的对象唯一
- c++多进程编写一个唯一的文件
- 使用C++的日期库读取时间
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 日期格式为C++
- 如何更改唯一指针向量的可见性
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 如何在 C++20 计时中为日期添加天数?
- C++尝试深度复制唯一指针时出现内存访问冲突
- 具有引用成员的结构是否具有唯一的对象表示形式
- C++ 显示在 txt 文件中找到的唯一日期及其相关价格