一道面试题目//C++



//struct Material{  Quota{ struct Bill{是题目给出的结构体。
/*材料*/
struct Material{
    string code;
    string name;
    string spec;
    string vendor;
    string unit;
    double quantity;
    double cost; //这个是材料单价。。。取名这样。。。坑
    double cost_sum;
};

/*定额*/
struct Quota{
    string code;
    string name;
    string content;
    string unit;
    double quantity;
    double cost;
    double cost_sum;
    std::list<Material* > materials;
};


/*清单*/
struct Bill{
    string code;
    string name;
    string spec;
    string content;
    string unit;
    double quantity;
    double cost;
    double cost_sum;
    std::list<Quota* > quotas;
};




实现一种统计方法,汇总制定清单项目,std::list<Bill*>中各种材料的使用量,以及该材料被哪些额定所使用,并记录使用量

注:a.材料汇总时,编码.名称,规格型号,厂商,几辆单位,单价均相同时才被视为同一材料。

b.代码中加入有效注释


我只找了一个Bill,只是一个测试,没优化过,速度慢。

// 计价软价.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <time.h>
using namespace std;

int serial = 0;
//struct Material{  Quota{ struct Bill{是题目给出的结构体。
/*材料*/
struct Material{
    string code;
    string name;
    string spec;
    string vendor;
    string unit;
    double quantity;
    double cost;
    double cost_sum;
};

/*定额*/
struct Quota{
    string code;
    string name;
    string content;
    string unit;
    double quantity;
    double cost;
    double cost_sum;
    std::list<Material* > materials;
};


/*清单*/
struct Bill{
    string code;
    string name;
    string spec;
    string content;
    string unit;
    double quantity;
    double cost;
    double cost_sum;
    std::list<Quota* > quotas;
};



struct Record
{
    Quota* qutoa;
    double cost;//题目要求的是统计用量,所以没统计cost——sum了
};

struct Summary{
    Material material;     //这个是记录是什么材料的

    std::vector<Record> record;     //这个是记录该材料对应的定额包含了该材料的使用量
};

void help();
void visit(int c);
void show(void(*vi)(int));
void porduceQuotaData(vector<Quota> &qt);
void produceMaterialData(vector<Material> &m);
void AddQuotaToBill(Bill &bl, vector<Quota>& qt);
void AddMaterialToQuota(vector<Quota>& qt, vector<Material>& mt);
void Summarise();
void Summarise(Bill &bl, std::vector<Summary> &Qt);
bool MatreialEqual(Material &a, Material &b);
//返回统计中含有的materia 序号
int AlreadHaveMaterial(std::vector<Summary> &Qt, Summary &q);
int AlreadHaveMaterial(std::vector<Summary> &Qt, Material &q);
bool QutaEqual(Quota *a, Quota &b);
int findQuto(std::vector<Summary> &summary, int ID, Quota &qt);
//返回材料是否相同0
bool MatreialEqual(Material &a, Material &b)
{
    if (
        a.code == b.code &&
        a.name == b.name &&
        a.spec == b.spec &&
        a.vendor == b.vendor&&
        a.unit == b.unit &&
        a.cost == b.cost)
        return true;
    return false;
}








void help()
{
    return;
}



void visit(int c)
{
    printf("%d\n", c);
}

void show(void(*vi)(int))
{
    vi(5);
}

void porduceQuotaData(vector<Quota> &qt)
{
    char s[64] = { 0 };
    _itoa_s(serial, s, 10);
    Quota q;
    q.code = (string)s;//为了测试方便,我只用了数字代替code
    q.name = "name";
    q.content = "else";
    q.unit = "true";
    q.quantity = 0;
    q.cost = 0;
    q.cost_sum = 0;
    qt.push_back(q);
    serial++;
}

void produceMaterialData(vector<Material> &m)
{
    Material mt;
    char code = rand() % 10; //ascii测试
    char str[2] = { '0' + code, 0 };
    /***********************该部分都相同材料才被视为同一材料************************************/
    mt.code = (string)str;    //为了测试方便,我只用了数字代替code
    mt.name = "name";
    mt.spec = "huge";
    mt.vendor = "manufactoryID";
    mt.unit = "Ton";
    mt.cost = 1 + rand() % 2;
    /***********************************************************/
    mt.quantity = rand() % 10;
    mt.cost_sum = mt.quantity*mt.cost;
    m.push_back(mt);
}

void AddQuotaToBill(Bill &bl, vector<Quota>& qt)
{
    for (int i = 0; i < qt.size(); i++)
    {
        bl.quotas.push_back(&qt[i]);
    }
};

void AddMaterialToQuota(vector<Quota>& qt, vector<Material>& mt)
{
    int id = qt.size();
    if (0 == id) return;
    for (int i = 0; i < mt.size(); i++)
    {
        qt[rand() % id].materials.push_back(&mt[i]);
    }
};

void Summarise(Bill &bl, std::vector<Summary> &Qt)
{

    //int Id = 0;
    for (std::list<Quota* >::iterator itc = bl.quotas.begin(); itc != bl.quotas.end(); itc++)
    {
        for (std::list<Material* >::iterator it = (*itc)->materials.begin(); it != (*itc)->materials.end(); it++)
        {
            bool flag = true;
            Summary q;
            q.material.code = (*it)->code;
            q.material.cost = (*it)->cost;
            q.material.cost_sum = (*it)->cost_sum;
            q.material.name = (*it)->name;
            q.material.quantity = (*it)->quantity;
            q.material.spec = (*it)->spec;
            q.material.unit = (*it)->unit;
            q.material.vendor = (*it)->vendor;
            Record rc;
            rc.cost = q.material.cost;
            rc.qutoa = *itc;
            int ret = AlreadHaveMaterial(Qt, q);
            if (-1 == ret)                                      //在汇总的表里没有查到材料的话,就新增加一个material
            {
                Qt.push_back(q);
                Qt[Qt.size() - 1].record.push_back(rc);
                //    Id = Qt.size() - 1; //
            }
            else
            {
                Qt[ret].material.quantity += q.material.quantity;  //汇总表里有这个的话,就累计
                Qt[ret].material.cost_sum += q.material.cost_sum;
                //Qt[ret].material.cost += q.material.cost;      //单价无法累加
                int rett = findQuto(Qt, ret, *rc.qutoa); //如果在该material里的记录查到已经包含了额定的话就想该额定追加数值,返回值是该序列号,否则就添加一个额定
                if (-1 == rett)
                {
                    Qt[ret].record.push_back(rc);
                }
                else
                {
                    Qt[ret].record[rett].cost += rc.cost;
                }
            }

        }


    }

    return;
}


bool QutaEqual(Quota *a, Quota &b)
{
    if (
        a->code == b.code &&
        a->content == b.content &&
        a->name == b.name &&
        a->unit == b.unit)
        return true;
    return false;

}

int findQuto(std::vector<Summary> &summary, int ID, Quota &qt)
{
    for (int i = 0; i < summary[ID].record.size(); i++)
    {
        if (QutaEqual(summary[ID].record[i].qutoa, qt))
            return i;
    }
    return -1;
}



int AlreadHaveMaterial(std::vector<Summary> &Qt, Summary &q)
{
    for (int i = 0; i < Qt.size(); i++)
    {
        if (MatreialEqual(Qt[i].material, q.material))
            return i;
    }
    return -1;

}

int AlreadHaveMaterial(std::vector<Summary> &Qt, Material &q)
{
    for (int i = 0; i < Qt.size(); i++)
    {
        if (MatreialEqual(Qt[i].material, q))
            return i;
    }
    return -1;

}

int main(int argc, char* argv[])
{
    srand((unsigned int)time(NULL));    //生成随机种子,目的是获取随机的列表信息,保证测试正确性
    Bill bill;                             //生成一份清单                
    vector<Quota> qt;                    //生成一份向量额定表    
    vector<Material> mt;                //生成一份向量物料表
    std::vector<Summary> QuantityUsed; //各种材料的使用量,编码,名称,规格型号,厂商,计量单位,单价相同的时候才会统计一起

    for (int i = 0; i < 50; i++)produceMaterialData(mt);    //向向量表里追加生成的表
    for (int i = 0; i < 5; i++)porduceQuotaData(qt);    //向向量表里追加生成的表

    //把quota列表加入到bill
    AddQuotaToBill(bill, qt);
    //把material列表加入到quota里,因为qutoa不是一个,所以模拟的时候要随机加入
    AddMaterialToQuota(qt, mt);

    Summarise(bill, QuantityUsed);

    for (std::list<Quota* >::iterator itc = bill.quotas.begin(); itc != bill.quotas.end(); itc++)
    {
        cout << "额定表:" << (*itc)->code << endl;
        for (std::list<Material* >::iterator it = (*itc)->materials.begin(); it != (*itc)->materials.end(); it++)
        {
            std::cout << "材料编号:" << (*it)->code << "单价" << (*it)->cost << " ";
        }
        cout << endl;
    }

    cout << "统计结果" << endl;

    for (int i = 0; i < QuantityUsed.size(); i++)
    {
        cout << "材料 code:" << QuantityUsed[i].material.code << " ";
        cout << "name:" << QuantityUsed[i].material.name << " ";
        cout << "spec:" << QuantityUsed[i].material.spec << " ";
        cout << "vendor:" << QuantityUsed[i].material.vendor << " ";
        cout << "单价:" << QuantityUsed[i].material.cost << " " << endl;

        for (std::vector<Record>::iterator itc = QuantityUsed[i].record.begin(); itc != QuantityUsed[i].record.end(); itc++)
        {
            cout << "额定表:" << (itc->qutoa)->code << " 用量为:" << (itc->cost) << endl;
        }
    }

    system("pause");

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值