Skip to content

Instantly share code, notes, and snippets.

@nikoncode
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save nikoncode/a7e2d97b27301f79f52f to your computer and use it in GitHub Desktop.

Select an option

Save nikoncode/a7e2d97b27301f79f52f to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <string>
#include <vector>
#include <windows.h>
#pragma GCC diagnostic ignored "-Wwrite-strings"
using namespace std;
class Purchase {
private:
unsigned int cost;
unsigned int amount;
unsigned short day;
unsigned short dtype;
unsigned int dvalue;
constexpr static char dictionary[][30] = {
"Полная стоимость",
"Скидка %u%%",
"Скидка на доставку %u руб.",
"Другой товар на %u руб."
};
public:
Purchase(unsigned int c, unsigned int a, unsigned short d, unsigned short dt, unsigned int dv) {
cost = c;
amount = a;
day = d;
dtype = dt;
dvalue = dv;
}
unsigned int full_price() {
unsigned int price = cost * amount;
switch (dtype) {
case 1: return price * (1 - dvalue / 100.0);
case 2:
case 3: return price - dvalue;
default: return price;
}
}
static void write_title() {
write_line();
printf("|%-14s|%-30s|%-8s|%-4s|\n",
"Цена x кол-во",
"Скидка",
"Всего",
"День"
);
write_line();
}
static void write_line() {
for (int i = 0; i < 61; i++) printf("-");
printf("\n");
}
static void write_text(char* text) {
printf("|%59s|\n", text);
}
void write() {
char temp[30];
sprintf(temp, dictionary[dtype], dvalue);
printf("|%8u x %3u|%-30s|%8u|%4u|\n", cost, amount, temp, full_price(), day);
}
unsigned short get_day() {
return day;
}
};
constexpr char Purchase::dictionary[][30];
class Container {
private:
vector<Purchase> c;
public:
void add(Purchase& element) {
c.push_back(element);
}
void sort_by_day() {
sort(c.begin(), c.end(), [] (Purchase a, Purchase b) -> bool {
return a.get_day() < b.get_day();
});
}
void write_all() {
Purchase::write_title();
for (Purchase elem : c){
elem.write();
Purchase::write_line();
}
if (c.size() == 0) {
Purchase::write_text("Таблица пуста.");
}
}
void write_on_10() {
Purchase::write_title();
int count = 0;
for (Purchase elem : c) {
if (elem.get_day() == 10) {
count++;
elem.write();
Purchase::write_line();
}
}
if (count == 0) {
Purchase::write_text("В таблице нет товаров проданных 10го числа.");
Purchase::write_line();
}
}
};
int main() {
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
Purchase p[] = {
Purchase(10000, 6, 10, 0, 0),
Purchase(17770, 1, 11, 1, 17),
Purchase(66550, 2, 22, 2, 25000),
Purchase(100000, 7, 10, 3, 500000),
Purchase(12250, 25, 1, 1, 56),
Purchase(1000000, 1, 2, 1, 17),
Purchase(2000000, 6, 17, 2, 85000),
Purchase(15900, 12, 3, 1, 99),
Purchase(700000, 2, 31, 2, 22)
};
Container c;
for (int i = 0; i < sizeof(p) / sizeof(p[0]); i++) {
c.add(p[i]);
}
printf("Все покупки:\n");
c.write_all();
printf("Сортировка:\n");
c.sort_by_day();
c.write_all();
printf("Покупки за 10тое:\n");
c.write_on_10();
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment