采购清单统计C++/C语言

先输入正整数n,(n代表采购商品的种类),再输入n种商品的商品名、生产商、采购日期、采购数量、单价,然后根据输入的日期,统计此时间段内各个生产商的采购总价及采购清单(采购清单按生产商和时间顺序由小到大排列)。

【输入形式】7

PC DELL 20180112 1 10000.00

PC HP 20180918 3 9000.00

DISPLAYER DELL 20180505 2 6000.00

ROUTER HUAWEI 20181130 5 8000.00

PC HUAWEI 20180315 2 8400.00

SERVER DELL 20180228 3 9200.00

DISPLAYER HP 20181011 1 3200.00

20180101 20181130


【输出形式】

PC                  DELL    20180112  1  10000.00   10000.00

SERVER          DELL    20180228  3  9200.00     27600.00

DISPLAYER     DELL    20180505  2  6000.00     12000.00

                       DELL                                           49600.00

 

PC                    HP      20180918  3  9000.00    27000.00

DISPLAYER      HP      20181011  1  3200.00    3200.00

                        HP                                             30200.00

 

PC                   HUAWEI  20180315  2  8400.00    16800.00

ROUTER          HUAWEI  20181130  5  8000.00    40000.00

                       HUAWEI                                         56800.00

 

                                                                              136600.00

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

struct Product {
    std::string name;
    std::string manufacturer;
    std::string date;
    int quantity;
    double price;
};

int main() {
    int n;
    std::cout << "请输入商品种类数n: ";
    std::cin >> n;

    std::vector<Product> products;
    for (int i = 0; i < n; i++) {
        Product product;
        std::cout << "请输入第" << i + 1 << "个商品的信息:" << std::endl;

        std::cout << "商品名:";
        std::cin >> product.name;

        std::cout << "生产商:";
        std::cin >> product.manufacturer;

        std::cout << "采购日期:";
        std::cin >> product.date;

        std::cout << "采购数量:";
        std::cin >> product.quantity;

        std::cout << "单价:";
        std::cin >> product.price;

        products.push_back(product);
    }

    std::string start_date, end_date;
    std::cout << "请输入需要统计的时间段(起始日期 结束日期):";
    std::cin >> start_date >> end_date;

    // 统计采购总价
    std::vector<std::pair<std::string, double>> total_price_by_manufacturer;
    for (const auto& product : products) {
        if (product.date >= start_date && product.date <= end_date) {
            bool found = false;
            for (auto& p : total_price_by_manufacturer) {
                if (p.first == product.manufacturer) {
                    p.second += product.price * product.quantity;
                    found = true;
                    break;
                }
            }
            if (!found) {
                total_price_by_manufacturer.push_back(std::make_pair(product.manufacturer, product.price * product.quantity));
            }
        }
    }

    // 按照生产商和时间顺序排序
    std::sort(products.begin(), products.end(), [](const Product& p1, const Product& p2) {
        if (p1.manufacturer != p2.manufacturer) {
            return p1.manufacturer < p2.manufacturer;
        } else {
            return p1.date < p2.date;
        }
    });

    // 输出采购清单
    std::cout << "采购清单:" << std::endl;
    for (const auto& product : products) {
        if (product.date >= start_date && product.date <= end_date) {
            std::cout << product.manufacturer << "\t" << product.date << "\t" << product.name 
                      << "\t" << product.quantity << "\t" << product.price << std::endl;
        }
    }

    // 输出各个生产商的采购总价
    std::cout << "各个生产商的采购总价:" << std::endl;
    for (const auto& p : total_price_by_manufacturer) {
        std::cout << p.first << "\t" << p.second << std::endl;
    }

    return 0;
}