Untitled
程式問題unknown
plain_text
2 years ago
5.5 kB
8
Indexable
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
class Product
{
private:
int price;
int cost;
int revenue;
int unitProfit;
int totalProfit;
public:
char* name;
int salesQty;
Product(const char* n, int p, int c, int sq);
~Product();
bool isInFrontOf(const Product& prod, int criterion) const;
static int totalProfitTopK(Product** products, int productCnt, int k, int criterion);
};
Product::Product(const char* n, int p, int c, int sq)
{
name = new char[strlen(n) + 3];
strcpy(name, n);
price = p;
cost = c;
salesQty = sq;
revenue = price * salesQty;
unitProfit = price - cost;
totalProfit = unitProfit * salesQty;
}
Product::~Product()
{
delete[] name;
name = nullptr;
}
//選定不同規則時,排序商品的狀況
bool Product::isInFrontOf(const Product& prod, int criterion) const
{
switch (criterion)
{
default: return strcmp(name, prod.name) < 0;
case 1:
if(price == prod.price)
{
return strcmp(name, prod.name) < 0;
}
else
{
return price > prod.price;
}
case 2:
if(cost == prod.cost)
{
return strcmp(name, prod.name) < 0;
}
else
{
return cost > prod.cost;
}
case 3:
if(unitProfit == prod.unitProfit)
{
return strcmp(name, prod.name) < 0;
}
else
{
return unitProfit > prod.unitProfit;
}
case 4:
if(salesQty == prod.salesQty)
{
return strcmp(name, prod.name) < 0;
}
else
{
return salesQty > prod.salesQty;
}
case 5:
if(revenue == prod.revenue)
{
return strcmp(name, prod.name) < 0;
}
else
{
return revenue > prod.revenue;
}
case 6:
if(totalProfit == prod.totalProfit)
{
return strcmp(name, prod.name) < 0;
}
else
{
return totalProfit > prod.totalProfit;
}
}
}
//算總收益
int Product::totalProfitTopK(Product** products, int productCnt, int k, int criterion)
{
//按照指定規則排序
sort(products, products + productCnt, [criterion](Product* a, Product* b)
{
return a->isInFrontOf(*b, criterion);
});
int totalProfitTopK = 0;
for (int i = 0; i < k && i < productCnt; i++)
{
//cout << "CHECK: " << products[i]->name << " " << products[i]->price << " " << products[i]->cost << " " << products[i]->salesQty<< endl;
//cout << "totalProfit" << products[i]->totalProfit << endl;
//cout << "R" << products[i]->revenue << endl;
//cout <<"NAME: " << products[i]->name << endl;
//cout << "cost: " << products[i]->cost << endl;
totalProfitTopK += products[i]->totalProfit;
}
return totalProfitTopK;
}
int main()
{
int productNum = 0;
int saleTime = 0;
int searchTime = 0;
int productCnt = 0;
int checkT = 0;
cin >> productNum >> saleTime >> searchTime;
const int nameLen = 100;
const int senLen = 200;
int* totalProfitTopK = new int [searchTime];
char targetChar = 'S';
Product** products = new Product*[productNum];
cin.ignore();
while(true)
{
if(checkT == searchTime)
{
break;
}
char line[senLen];
char delim[] = ",";
char delimB[] = " ";
int price, cost, salesQty;
cin.getline(line, senLen);
//如果是開頭 T 的話不會有逗號,計算totalProfit
if(strchr(line, ',') == nullptr)
{
char* start = strtok(line, delimB);
start = strtok(nullptr, delimB);
int criterion = atoi(start);
start = strtok(nullptr, delimB);
int k = atoi(start);
//cout << criterion <<","<<k<< endl;
totalProfitTopK[checkT] = Product::totalProfitTopK(products, productCnt, k, criterion);
checkT++;
}
char* name = strtok(line, delim);
//開頭為S,增加銷售量
if(strlen(name) == 1 && name[0] == targetChar)
{
char* namePlus = strtok(nullptr, delim);
int salesQtyPlus = atoi(strtok(nullptr, delimB));
for(int i = 0; i < productNum; i++)
{
char* testName = products[i]->name;
bool test = true;
if(strlen(testName) != strlen(namePlus))
{
test = false;
}
for(int j = 0; j < strlen(namePlus); j++)
{
if(testName[j] != namePlus[j])
{
test = false;
}
}
if(test == true)
{
//cout <<"G"<< endl;
products[i]->salesQty += salesQtyPlus;
//cout << products[i]->name<<"," << products[i]->salesQty<< endl;
}
}
}
//開頭為商品資訊時,輸入進 Product
else
{
price = atoi(strtok(nullptr, delimB));
cost = atoi(strtok(nullptr, delimB));
salesQty = atoi(strtok(nullptr, delimB));
products[productCnt] = new Product(name, price, cost, salesQty);
productCnt ++;
}
}
//輸出總收益
for(int i = 0; i < searchTime; i++)
{
if(i == 0)
{
cout << totalProfitTopK[i];
}
else
{
cout << "," << totalProfitTopK[i];
}
}
//delete
for (int i = 0; i < productCnt; i++)
{
delete products[i];
}
delete[] totalProfitTopK;
delete products;
products = nullptr;
totalProfitTopK = nullptr;
return 0;
}Editor is loading...