Руководство пользователя
Интерфейс программного продукта оформлен на английском языке.
После запуска программы пользователю нужно ввести математическое ожидание M1, задающее интервал между поступлением отливок, и длину интервала времени, для которого будет происходить моделирование системы (количество моделируемых моментов времени).
После запуска программы на экран будет выведено среднее время нагрева отливок, средняя конечная температура отливок, суммарное и среднее время ожидания отливок в очереди перед печью, среднее количество заполненных камер печи в единицу времени. Эта же статистика будет записана в файл «Average statistic.txt», находящийся в одной папке с программой. Для завершения программы нужно нажать любую клавишу.
Кроме того, в файл «Interim statistic.txt» будут записаны промежуточные данные для каждого моделируемого момента времени. Промежуточные данные представляют собой данные о десяти камерах. Камеры располагаются в текстовом файле одной строкой через пробел.
Описание одной камеры имеет вид:
|-| пустая камера
|n| камера с отливкой (где n – целое число – температура отливки)
Для каждого момента времени будет выведено также количество отливок, находящихся в очереди.
Приложение в экранные формы
Рисунок B.1 – пример работы программы
Рисунок B.2 – файл с промежуточными данными
Приложение Г
ЛИСТИНГ ПРОГРАММЫ
Г1. Файл Prototype.h
#define COUNT_CHAMBER 10
#define MAX_FURNACE_TEMP 2600
#define FURNACE_COEF_HEAT 0.2
class System;
class Furnace;
class Chamber;
class Statistic;
class QueueCast;
class QueueCastElement;
Г2. Файл Statistic.h
class Average
{
public:
Average();
void AddValue(double);
double GetAverage();
public:
double sum;
int N;
};
Average::Average()
{
sum=N=0;
}
void Average::AddValue(double val)
{
sum=(sum*N+val)/(N+1);
N++;
}
double Average::GetAverage()
{
return sum;
}
class Statistic
{
public:
Statistic();
void WriteToConsole();
void WriteToFile(const char*);
Average avTimeWarm;
Average avEndTemp;
long sumTimeWait;
Average avTimeWait;
Average avNumberChamb;
};
Statistic::Statistic()
{
sumTimeWait=0;
}
void Statistic::WriteToConsole()
{
std::cout << "Average time warm: " << avTimeWarm.GetAverage() << std::endl;
std::cout << "Average end temperature: " << avEndTemp.GetAverage() << std::endl;
std::cout << "Summary time wait in queue: " << sumTimeWait << std::endl;
std::cout << "Average time wait in queue: " << avTimeWait.GetAverage() << std::endl;
std::cout << "Average number of not fill chamb: " << avNumberChamb.GetAverage() << std::endl;
}
void Statistic::WriteToFile(const char *name)
{
std::ofstream F;
F.open(name);
F << "Average time warm: " << avTimeWarm.GetAverage() << std::endl;
F << "Average end temperature: " << avEndTemp.GetAverage() << std::endl;
F << "Summary time wait in queue: " << sumTimeWait << std::endl;
F << "Average time wait in queue: " << avTimeWait.GetAverage() << std::endl;
F << "Average number of not fill chamb: " << avNumberChamb.GetAverage() << std::endl;
F.close();
}
Г3. Файл QueueCastElement.h
class QueueCastElement
{
public:
QueueCastElement(long);
long GetTime();
QueueCastElement *next;
private:
long time;
};
QueueCastElement::QueueCastElement(long _time)
{
time=_time;
next=NULL;
}
long QueueCastElement::GetTime()
{
return time;
}
Г4. Файл QueueCast.h
class QueueCast
{
public:
QueueCast();
void Push(double);
bool NotEmpty();
long GetLast();
void Pop();
int GetLength();
private:
QueueCastElement *head;
QueueCastElement *last;
};
QueueCast::QueueCast()
{
head=last=NULL;
}
void QueueCast::Push(double _time)
{
if (head)
last=last->next=new QueueCastElement(_time);
else
last=head=new QueueCastElement(_time);
}
bool QueueCast::NotEmpty()
{
return head;
}
long QueueCast::GetLast()
{
if (head)
return head->GetTime();
else
return 0;
}
void QueueCast::Pop()
{
if (head)
{
QueueCastElement *E;
E=head;
head=head->next;
delete E;
}
}
int QueueCast::GetLength()
{
if (head)
{
QueueCastElement *E=head;
int k=1;
while (E=E->next)
k++;
return k;
}
else
{
return 0;
}
}
Г5. Файл Chamber.h
class Chamber
{
public:
Chamber();
double GetTemp();
void AddTemp(double);
void PushCast(long,double);
void PopCast(Statistic&,long);
bool GetFill();
long GetTimeBegin();
private:
double temp;
long timeBegin;
bool fill;
};
Chamber::Chamber()
{
fill=false;
}
double Chamber::GetTemp()
{
return temp;
}
void Chamber::AddTemp(double t)
{
temp+=t;
}
void Chamber::PushCast(long _time,double _temp)
{
if (!fill)
{
fill=true;
temp=_temp;
timeBegin=_time;
}
}
void Chamber::PopCast(Statistic &S,long NowTime)
{
S.avTimeWarm.AddValue(NowTime-timeBegin);
S.avEndTemp.AddValue(temp);
fill=false;
}
bool Chamber::GetFill()
{
return fill;
}
long Chamber::GetTimeBegin()
{
return timeBegin;
}
Г6. Файл Furnace.h
class Furnace
{
public:
Furnace();
int FindNotFillChamb();
double GetTemp();
void AddTemp(double);
int GetNumberChamb();
void PushCast(int,double,long);
void DeleteCast(long,QueueCast&,Statistic&);
Chamber& operator[](int);
private:
Chamber chamb[COUNT_CHAMBER];
double temp;
};
Furnace::Furnace()
{
temp=1650;
}
int Furnace::FindNotFillChamb()
{
int i=COUNT_CHAMBER;
while (i--)
if (!chamb[i].GetFill())
return i;
return COUNT_CHAMBER;
}
double Furnace::GetTemp()
{
return temp;
}
void Furnace::AddTemp(double _temp)
{
temp+=_temp;
}
int Furnace::GetNumberChamb()
{
int i=COUNT_CHAMBER;
int k=0;
while (i--)
if (chamb[i].GetFill())
k++;
return k;
}
void Furnace::PushCast(int n,double _temp,long NowTime)
{
if (n>=0 && n<10)
{
if (!chamb[n].GetFill())
{
chamb[n].PushCast(NowTime,_temp);
temp-=(temp-_temp)/GetNumberChamb();
}
}
}
void Furnace::DeleteCast(long NowTime,QueueCast &Q,Statistic &S)
{
int i=COUNT_CHAMBER;
int n;
while (i--)
{
if (chamb[i].GetFill() && chamb[i].GetTemp()>=2200)
{
i=COUNT_CHAMBER;
while (i--)
{
if (chamb[i].GetFill() && chamb[i].GetTemp()>=2000)
{
S.avTimeWarm.AddValue(NowTime-chamb[i].GetTimeBegin());
chamb[i].PopCast(S,NowTime);
}
}
while (Q.NotEmpty() && (n=FindNotFillChamb())!=10)
{
chamb[i].PushCast(NowTime,400);
S.sumTimeWait+=NowTime-Q.GetLast();
S.avTimeWait.AddValue(NowTime-Q.GetLast());
Q.Pop();
}
return;
}
}
}
Chamber& Furnace::operator[](int n)
{
if (n<0 || n>=10)
return chamb[0];
else
return chamb[n];
}
Г7. Файл System.h
class System
{
public:
void Imitate(long,long,Statistic&,const char*,const char*);
double GetCoefOfHeat();
long GetNewInterval(long);
void ShowInterimData(std::ofstream&,int,Furnace&,QueueCast&);
};
void System::Imitate(long TimeWork,long M,Statistic &S,const char *NameInterimFile,const char *NameEndFile)
{
Furnace furn;
QueueCast queue;
long NowTime;
long nextCast=GetNewInterval(M);
int n;
std::ofstream f(NameInterimFile);
for (n=0;n<6;n++)
{
furn.PushCast(n,550+n*50,0);
}
for (NowTime=0;NowTime<TimeWork;NowTime++)
{
if (NowTime==nextCast)
{
if ((n=furn.FindNotFillChamb())!=COUNT_CHAMBER)
furn.PushCast(n,rand()%201+400,NowTime);
else
queue.Push(NowTime);
nextCast+=GetNewInterval(M);
}
n=COUNT_CHAMBER;
while (n--)
{
if (furn[n].GetFill())
{
furn[n].AddTemp((furn.GetTemp()-furn[n].GetTemp())*GetCoefOfHeat());
}
}
furn.AddTemp((MAX_FURNACE_TEMP-furn.GetTemp())*FURNACE_COEF_HEAT);
furn.DeleteCast(NowTime,queue,S);
S.avNumberChamb.AddValue(furn.GetNumberChamb());
ShowInterimData(f,NowTime+1,furn,queue);
}
f.close();
S.WriteToFile(NameEndFile);
}
double System::GetCoefOfHeat()
{
return 0.15+(double)(rand()%201-100)/10000;
}
long System::GetNewInterval(long M)
{
return rand()%(2*M)+1;
}
void System::ShowInterimData(std::ofstream &f,int num,Furnace &furn,QueueCast &queue)
{
int i;
f << "#" << num << std::endl;
f << "Furnace chambers state:" << std::endl;
for (i=0;i<COUNT_CHAMBER;i++)
{
if (furn[i].GetFill())
f << "|" << furn[i].GetTemp() << "|" << " ";
else
f << "|-| ";
}
f << std::endl;
f << "Number of casts in queue: " << queue.GetLength() << std::endl;
}
Г8. main.cpp
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include "Prototype.h"
#include "Statistic.h"
#include "QueueCastElement.h"
#include "QueueCast.h"
#include "Chamber.h"
#include "Furnace.h"
#include "System.h"
using namespace std;
int main()
{
srand(time(NULL));
Statistic S;
System Sys;
char name[256];
long time;
int M;
cout << "Input time of work: ";
do
{
cin >> time;
if (time<=0)
cout << "Incorrect data! Please, reinput time of work: ";
}while (time<=0);
cout << "Input expected value of interval: ";
do
{
cin >> M;
if (M<=0)
cout << "Incorrect data! Please, reinput expected value of interval: ";
}while (M<=0);
Sys.Imitate(time,M,S,"Interim statistic.txt","Average statistic.txt");
S.WriteToConsole();
getch();
}