Fille - date.h
#include <stdio.h>
#include <iostream.h>
using namespace std;
struct cities
{
char *name;
int residents;
};
template <class Queue_cities> class CT
{
private:
Queue_cities *temporary;
int begin,end,value;
public:
CT(int many)
{
value=many;
temporary=new Queue_cities[value+1];
end=0;
begin=0;
}
void Insert(Queue_cities temp)
{
temporary[end++]=temp;
if(end>value)
end=0;
}
int Work(Queue_cities &temp)
{
if(begin==end)
return -1;
temp=temporary[begin++];
if(begin>value)
begin=0;
return 1;
}
bool is_Empty()
{
if(begin!=end)
return false;
else
return true;
}
};
C++ Builder:
#include <vcl.h>
#pragma hdrstop
#include "m_next02.h"
#include "date.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
private: // User declarations
struct cities *usa_cities;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
usa_cities=new struct cities[10];
usa_cities[0].name="New York";
usa_cities[0].residents=8300000;
usa_cities[1].name="Houston";
usa_cities[1].residents=2200000;
usa_cities[2].name="Phoenix";
usa_cities[2].residents=1500000;
usa_cities[3].name="Dallas";
usa_cities[3].residents=1200000;
usa_cities[4].name="Detroit";
usa_cities[4].residents=700000;
usa_cities[5].name="Chicago";
usa_cities[5].residents=2700000;
usa_cities[6].name="San Jose";
usa_cities[6].residents=900000;
usa_cities[7].name="San Francisco";
usa_cities[7].residents=800000;
usa_cities[8].name="Philadelphia";
usa_cities[8].residents=1500000;
usa_cities[9].name="Charlotte";
usa_cities[9].residents=700000;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int i;
CT<char *> name_cities(10);
CT<int> resident_cities(10);
for(i=0;i<10;i++)
{
name_cities.Insert(usa_cities[i].name);
resident_cities.Insert(usa_cities[i].residents);
}
for(i=0;i<11;i++)
{
char *temporary_cities;
int temporary_residents;
int temp1,temp2;
temp1=name_cities.Work(temporary_cities);
temp2=resident_cities.Work(temporary_residents);
if(temp1==1 && temp2==1)
{
ListBox1->Items->Add("City - "+AnsiString(temporary_cities)+" has "+IntToStr(temporary_residents)+" residents");
}
}
if(name_cities.is_Empty() && resident_cities.is_Empty());
ListBox1->Items->Add("Queue is empty");
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(SaveDialog1->Execute())
{
ListBox1->Items->SaveToFile(SaveDialog1->FileName);
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
delete usa_cities;
Close();
}