C++ 显示在 txt 文件中找到的唯一日期及其相关价格

C++ Display unique date and its related price found in txt file

本文关键字:日期 唯一 txt 显示 文件 C++      更新时间:2023-10-16

目前我有一个将用户输入存储在文本文件中的系统,以及一个计算当月每日销售额并显示当天销售额和该月总计的函数。

例如,下面是 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)
}

这样,由于您的列表已排序,您将消除重复项并最终获得良好的价值。这可能不是最干净的解决方案,但它可以完成工作并且不会在代码中添加太多行。