Код программы.
Consol.h
#pragma once
#include <string>
#include <conio.h>
#include <iostream>
#include "TVMarket.h"
class Consol
{
TVMarket NewTVMarket;
int Str2Int(string str);
void ONOFFTV();
void clrscr();
void AddTV ();
void ShouList();
void DelTV();
void SetChannel();
public:
Consol();
~Consol();
};
Consol.cpp
#include "stdafx.h"
#include "Consol.h"
using namespace std;
int Consol::Str2Int(string str)
{
char* end_ptr;
long val = strtol(str.c_str(), &end_ptr, 10);
if (*end_ptr)
{
cout<< "Error";
return 0;
}
if ((val == LONG_MAX || val == LONG_MIN) && errno == ERANGE)
{
cout<<"Out size";
return 0;
}
return val;
}
void Consol::clrscr()
{
system ("cls");
}
void Consol::AddTV ()
{
Consol::clrscr();
printf ("Enter name:");
string name;
getline (cin,name);
NewTVMarket.AddTV(1,name);
}
void Consol::ShouList()
{
clrscr();
NewTVMarket.ShowTVSet();
getch();
}
void Consol::ONOFFTV()
{
Consol::clrscr();
printf ("Enter nomer TV:");
string NomerTV;
getline (cin,NomerTV);
printf ("Enter 1 esli VKL 0 esli VIKL");
string Power;
getline (cin,Power);
int err = NewTVMarket.SetPower(Str2Int(NomerTV),Str2Int(Power));
if (err != 0) printf ("Error");
getch();
}
void Consol::SetChannel()
{
Consol::clrscr();
printf ("Enter nomer TV:");
string NomerTV;
getline (cin,NomerTV);
printf ("Enter nomer Kanala:");
string NomerKanala;
getline (cin,NomerKanala);
int err = NewTVMarket.SetChannelInN(Str2Int(NomerTV),Str2Int(NomerKanala));
if (err == 3) printf ("TV is OFF");
getch();
}
void Consol::DelTV()
{
clrscr();
int err = NewTVMarket.DelTV();
if (err ==0) cout << "TV Udalen";
else cout << "Error Delet";
getch();
}
Consol::Consol ()
{
char InChar = 0;
while (InChar != '6')
{
clrscr();
printf(" Menu \n Nazmite: \n 1. Dobavit TV \n 2. Prosmotret spisok \n 3. Udalit TV \n 4. Set Channel \n 5. Set Power \n 6. Vixod \n");
InChar = getch();
switch (InChar)
{
case '1': AddTV();
break;
case '2': ShouList();
break;
case '3':DelTV();
break;
case '4':SetChannel();
break;
case '5':ONOFFTV();
break;
}
}
}
Consol::~Consol ()
{
}
TVMarket.h
#pragma once
#include <list>
#include "TVSet.h"
using namespace std;
class TVMarket
{
list<TVSet> lst;
public:
TVMarket(void);
int SetChannelInN(int NT, int NK);
int SetPower(int NT, bool Power);
~TVMarket(void);
void AddTV(int channel,std::string Name);
int DelTV();
void ShowTVSet();
};
TVMarket.cpp
#include "StdAfx.h"
#include "TVMarket.h"
using namespace std;
TVMarket::TVMarket(void)
{
}
TVMarket::~TVMarket(void)
{
lst.clear();
}
void TVMarket::AddTV(int channel,std::string Name)
{
TVSet NewTVSet(channel,Name);
lst.push_front(NewTVSet);
std::list<TVSet>::iterator it;
}
int TVMarket::SetPower(int NT, bool Power)
{
bool flag = false;
if (lst.empty()) return 1;
int schet=1;
std::list<TVSet>::iterator ite;
for ( ite = lst.begin(); ite != lst.end();++ite)
{
if (schet == NT)
{
if (Power == true) ite->PowerON();
else ite->PowerOFF();
flag = true;
}
schet++;
}
if (!flag) return 2;
else return 0;
}
int TVMarket::SetChannelInN(int NT, int NK)
{
bool flag = false;
if (lst.empty()) return 1;
int schet=1;
std::list<TVSet>::iterator ite;
for ( ite = lst.begin(); ite != lst.end();++ite)
{
if (schet == NT)
{
int err = ite->SetChannelinN(NK);
if (err != 0) return 3;
flag = true;
}
schet++;
}
if (!flag) return 2;
else return 0;
}
int TVMarket::DelTV()
{
std::list<TVSet>::iterator it;
if (!lst.empty())
{
it = lst.begin();
lst.erase(it);
return 0;
}
else
return 1;
}
void TVMarket::ShowTVSet()
{
int schet=1;
std::list<TVSet>::iterator ite;
for ( ite = lst.begin(); ite != lst.end();++ite)
{
cout << schet<<" " <<ite->GetName()<< " nomer kanala "<<" ";
if(ite->GetPower())
{
cout<< ite->GetChannel()<< " ON ";
}
else
{
cout<< "OFF";
}
cout << "\n";
schet++;
}
}
TVSet.h
#pragma once
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class TVSet
{
int Channel;
bool Power;
string Name;
public:
TVSet(int channel,string name);
bool GetPower();
~TVSet(void);
int GetChannel(void);
int SetChannelinN(int N);
void PowerON();
void PowerOFF();
string GetName();
};
TVSet.cpp
#include "stdafx.h"
#include "TVSet.h"
#include <string>
using namespace std;
TVSet::TVSet(int channel, string name)
{
Power = false;
Channel = channel;
Name = name;
}
TVSet::~TVSet(void)
{
}
int TVSet:: GetChannel(void)
{
if (Power == true)
{
return Channel;
}
else
{
return 0;
}
}
string TVSet::GetName()
{
return Name;
}
int TVSet::SetChannelinN(int N)
{
if (Power == true)
{
Channel = N;
return 0;
}
else
{
return 1;
}
}
void TVSet::PowerON()
{
Power = true;
}
void TVSet::PowerOFF()
{
Power = false;
}
bool TVSet::GetPower()
{
return Power;
}