I have the file: capitals
London 8800000
Berlin 3400000
Moscow 13200000
Warsaw 1700000
Roma 2800000
Amsterdam 742000
Oslo 660000
Copenhagen 520000
Bratislava 450000
Madrid 3200000
And next I make FIFO-Queues and uses data.
#include <iostream>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
using namespace std;
#define max 10
template <class Queue> class QQ
{
private:
int start,end,size;
Queue *temp_Queue;
public:
QQ(int x)
{
size=x;
temp_Queue=new Queue[size+1];
start=0;
end=0;
}
bool is_Empty()
{
if(start==end)
return true;
else
return false;
}
int Run(Queue &temp_Q)
{
if(start==end)
return -1;
temp_Q=temp_Queue[start++];
if(start>size)
start=0;
return 1;
}
void Insert(Queue temp_Q)
{
temp_Queue[end++]=temp_Q;
if(end>size)
end=0;
}
~QQ()
{
delete temp_Queue;
}
};
struct Capital
{
char name[20];
int population;
};
int main(int argc, char **argv)
{
struct Capital *Date=new struct Capital[max];
FILE *file;
int i;
file=fopen("capitals","r");
for(i=0;i<max;i++)
{
char temp[20];
fscanf(file,"%s",temp);
strcpy(Date[i].name,temp);
fscanf(file,"%d",&Date[i].population);
}
fclose(file);
QQ<int> population_capitals(max);
QQ<char*> names_capitals(max);
for(i=0;i<max-1;i++)
{
population_capitals.Insert(Date[i].population);
names_capitals.Insert(Date[i].name);
}
for(i=0;i<max;i++)
{
int cap;
char *temp_capitals;
int temp_Q1,temp_Q2;
temp_Q1=names_capitals.Run(temp_capitals);
temp_Q2=population_capitals.Run(cap);
if(temp_Q1==1 && temp_Q2==1)
printf("Capital - %s ,population - %d",temp_capitals,cap);
printf("\n");
}
delete Date;
return 0;
}
Brak komentarzy:
Prześlij komentarz