C++ 用于跟踪“的数据结构”;“消费”;
大家好,C++ 用于跟踪“的数据结构”;“消费”;,c++,C++,大家好, 我正在为我的C++类小项目写一个小应用程序。 它是关于管理电信公司的客户、线路和服务。 我应该使用消费量类,它应该表示一项服务/线路的每月消费量 class Consumption { private: Ligne* m_line;//the line related to this consumption Service* m_service;//service used by this line int m_month; int
我正在为我的C++类小项目写一个小应用程序。 它是关于管理电信公司的客户、线路和服务。
我应该使用
消费量
类,它应该表示一项服务/线路的每月消费量
class Consumption
{
private:
Ligne* m_line;//the line related to this consumption
Service* m_service;//service used by this line
int m_month;
int m_year;
int m_units;//units of m_service used in the m_month of m_year
public:
//some getters and setters
double price();//returns m_units * the service's price/unit
};
一条线路每月最多可使用2次服务。消费将用于为某一行开具账单。
我的问题是,跟踪创造的消费的最佳方式是什么?我的意思是什么样的数据结构可能是最好的使用?我应该对班级做些改变吗?
请注意,我没有使用文件存储任何内容。
非常感谢您的帮助……矢量是以类似数组的方式存储内容并保持对任何元素的恒定访问时间的最佳结构。信息技术这就像你有一个动态大小的数组 第二个选择是deque。如果您计划拥有大量数据,请使用此工具,因为它具有更好的存储管理,并且您可以从前面和后面编辑它,而不仅仅是像矢量那样从后面编辑 最后一个可能是列表。如果你计划对数据进行大的编辑,比如删除新元素(不仅仅是访问它们),你应该考虑这一点,因为它在插入/删除元素方面具有线性复杂度,而前2个则有线性加上线性的时间,直到位置和结束之间的元素数量。 因此,结论:
- 向量-最简单
- deque-适用于大数据存储
- 列表-适用于存储的大编辑
#include <vector>
#包括
编辑:
我知道你的意思是别的,但我把它放在这里补充一下
你真的需要上消费课吗?我的意思是,若你们把所有关于行的数据都放在line类中,那个么它会更符合逻辑。这意味着,如果需要的话,我要么将每月使用的服务存储在vector中,要么立即计算价格并记住上个月的服务。我还将创建一个名为program的类,该类将存储所使用的服务,并让它处理所使用的服务的数量。如果已经有2个服务,它可以放弃新服务,也可以重写旧服务。你们也可以把程序的返回价格乘以使用的时间。大概是这样的:
#include <vector>
using namespace std;
class Service{
public:
int getPrice(){return monthPrice;}
void setPrice(const int &p) { monthPrice = p; }
private:
int monthPrice;
};
class Program{
public:
Program(){servicesUsed = 0; monthTime = 0;}
Program(const int &t) : monthTime(t) {servicesUsed = 0;}
void setTime(int t){monthTime = t;}
void addService(Service &s);
int price(); //calculate price of program
private:
int monthTime; //time that program was used
Service services[2]; //services used in program
int servicesUsed;
};
void Program::addService(Service &s){
if(servicesUsed < 2){ // Discarding solution
services[servicesUsed] = s;
servicesUsed++;
}
}
int Program::price(){
int pP = 0;
for(int i = 0; i < servicesUsed; i++){
pP += services[i].getPrice();
}
pP *= monthTime;
return pP;
}
class Line{
public:
Program *addMonth(const int &t); //will return handle for month
int consuption(); //calculate full line consuption
private:
vector<Program> monthList; //store data about services per month
};
Program *Line::addMonth(const int &t){
monthList.push_back(Program(t));
return &monthList.back();
}
int Line::consuption(){
int p = 0;
for(unsigned int i = 0; i < monthList.size(); i++){
p += monthList[i].price();
}
return p;
}
int main(){
//showcase
Program *handle;
Service s1,s2,s3;
s1.setPrice(50);
s2.setPrice(75);
s3.setPrice(100); //probably read from file
Line line;
handle = line.addMonth(30); // monthTime probably also from file
handle->addService(s1);
handle->addService(s2);
handle->addService(s3);
handle = line.addMonth(60);
handle->addService(s3);
handle->addService(s2);
int p = line.consuption();
return 0;
}
#包括
使用名称空间std;
班级服务{
公众:
int getPrice(){return monthPrice;}
void setPrice(const int&p){monthPrice=p;}
私人:
国际月价格;
};
班级计划{
公众:
Program(){servicesUsed=0;monthTime=0;}
程序(const int&t):monthTime(t){servicesUsed=0;}
void setTime(int t){monthTime=t;}
无效附加服务(服务与服务);
int price();//计算程序的价格
私人:
int monhtime;//使用该程序的时间
服务服务[2];//程序中使用的服务
使用的int服务;
};
无效程序::添加服务(服务与服务){
if(servicesUsed<2){//丢弃解决方案
服务[servicesUsed]=s;
servicesUsed++;
}
}
int程序::price(){
int-pP=0;
对于(int i=0;iaddService(s1);
handle->addService(s2);
handle->addService(s3);
handle=line.addMonth(60);
handle->addService(s3);
handle->addService(s2);
int p=line.consuption();
返回0;
}
应该可以正常工作,但可能需要进一步修改;) 你在用一种糟糕的方法来解决你的问题 你说:什么结构最适合解决(你的问题) 首先,必须对要保存的数据进行建模,并对数据的组织方式进行建模,我建议使用数据库设计中常用的
和
然后,您就可以选择最适合您的数据结构。我建议研究一下STL,它们虽然不漂亮,但效率很高,而且你不需要重新发明轮子:)
消费
对象集合执行什么操作。你想要随机访问吗?你要插入吗?托收的典型操作是什么?基本上,当为某一行开具账单时,需要进行托收,以确定每次消费的行和服务,然后添加与行号匹配的行和服务……并且在添加时确保一切都得到遵守(不超过2个服务/行/月,等等)…因此,我正在寻找组织这一切的最佳方法只是一个旁注:您的消费
类可能缺少析构函数、复制构造函数和赋值运算符。原因是您有指针成员,如果它们指向动态分配的内存,则您将希望释放析构函数中的内存,并定义深度复制操作。实际上,“消耗”包含在项目论文中,但无论如何,您的答案非常有用。.程序类有点取代消耗,而且你的想法更有效。非常感谢!!!