هدر
#ifndef GUARD_Automat_h
#define GUARD_Automat_h
#include <iostream>
#include <string>
#include <conio.h>
#include <map>
enum coinType {TI=10,TYVE=20};
enum seddelType {FEMTI=50,EN_HUND=100,TO_HUND=200,TRE_HUND=300,FEM _HUND=500, INVALID};
using namespace std;
struct indhold {coinType type; int antal;};
struct notEnoughMoney{};
class Automat
{
public:
void addCoins(coinType c,int antal);
void print();
void veksel(seddelType s);
private:
int temp, temp1;
int antalTi, antalTyve;
int ialt;
indhold saldo;
map <coinType, indhold> indholdMap;
void opdaterSaldo();
void udRegn(int amount);
void printVeksel();
void printAddCoins();
void reset();
};
#endif
cpp ها
#include "Automat.h"
int main()
{
system("color ED");
Automat m;
m.print();
m.addCoins( TYVE, 1 );
m.addCoins( TI, 3 );
m.print();
m.veksel(FEMTI);
m.print();
m.addCoins( TYVE, 3 );
m.veksel(INVALID);
m.veksel(FEMTI);
m.print();
m.addCoins( TYVE, 3 );
m.veksel(EN_HUND);
m.print();
_getch();
return 0;
}
و cpp توابع
#include "Automat.h"
void Automat::reset()
{
addCoins(TYVE,0);
addCoins(TI,0);
ialt=0;
}
void Automat::veksel(seddelType s)
{
int i=0;
if (s==INVALID)
{
cout << endl;
cout << "\n\n\n******************************************* ******" << endl;
cout << "*\t\t\tFEJL:\t\t\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t!Seddel ej gyldig!\t\t*" << endl;
cout << "************************************************* " << endl;
cout << "\t\t";
system("DATE/t");
cout << "\t\t";
system("TIME/t");
cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " << endl;
}
else
{
try {
udRegn(s);
}
catch (notEnoughMoney)
{
cout << endl << "\n\n\n******************************************* ******" << endl;
cout << "*\t\t\tFEJL:\t\t\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t!Der er ikke det rette antal mQnter!\t*" << endl;
cout << "************************************************* " << endl;//dvs vekselautomat skal have mindst en tier. cout << "\n";
cout << "\t\t";
system("DATE/t");
cout << "\t\t";
system("TIME/t");
cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " << endl;
}
}
}
void Automat::udRegn(int amount)
{
reset();
opdaterSaldo();
map<coinType, indhold>::iterator searchTyve;
searchTyve=indholdMap.find(TYVE);
antalTyve=searchTyve->second.antal;
map<coinType, indhold>::iterator searchTi;
searchTi=indholdMap.find(TI);
antalTi=searchTi->second.antal;
if (amount>ialt)
{
throw notEnoughMoney();
}
else
{
if (amount != FEMTI) //dvs der ønskes veksles en 100, 200 eller 500 kroneseddel.
{
if ((amount/TYVE)<=antalTyve)
{
temp=amount/TYVE;
cout << endl << "************************************************* " << endl;
cout << "*\t" << amount << "-kroneseddelen er vekslet til:\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "*\t" << temp << "stk.\t\t20-kronemQnter\t\t*" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "************************************************* " << endl;
cout<<"\t\t Vekselautomat Malik'n Farhad A/S" << endl << endl << endl << endl;
searchTyve->second.antal = (antalTyve - temp);
}
else if ((amount/TYVE)-antalTyve<=(2*antalTi) && antalTyve !=0)
{
temp = antalTyve;
temp1 = (amount-(antalTyve*TYVE))/TI;
cout << endl << "************************************************* " << endl;
cout << "*\t" << amount << "-kroneseddelen er vekslet til:\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "*\t" << temp << "stk.\t\t20-kronemQnter\t\t*" << endl;
cout << "*\t" << temp1 << "stk.\t\t10-kronemQnter\t\t*" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "************************************************* " << endl;
cout<<"\t\t Vekselautomat Malik'n Farhad A/S" << endl << endl << endl << endl;
searchTyve->second.antal= 0;
searchTi->second.antal= antalTi-temp1;
}
else //if (antalTyve==0 && (amount/antalTi)<=(amount/TI))
{
temp = amount/TI;
cout << endl << "************************************************* " << endl;
cout << "*\t" << amount << "-kroneseddelen er vekslet til:\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "*\t" << temp << "stk.\t\t10-kronemQnter\t\t*" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "************************************************* " << endl;
cout<<"\t\t Vekselautomat Malik'n Farhad A/S" << endl << endl << endl << endl;
searchTi->second.antal = antalTi-temp;
}
}
else //dvs der ønsked veksles en 50 kroneseddel.
{
if (antalTi==0)
{
throw notEnoughMoney();
}
else if (antalTyve==0)
{
temp1 = 5;
cout << endl << "************************************************* " << endl;
cout << "*\t" << amount << "-kroneseddelen er vekslet til:\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "*\t" << temp1 << "stk.\t\t10-kronemQnter\t\t*" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "************************************************* " << endl;
cout<<"\t\t Vekselautomat Malik'n Farhad A/S" << endl << endl << endl << endl;
searchTi->second.antal=(antalTi-5);
}
else if(antalTyve==1)
{
temp = 1;
temp1 = 3;
cout << endl << "************************************************* " << endl;
cout << "*\t" << amount << "-kroneseddelen er vekslet til:\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "*\t" << temp << "stk.\t\t20-kronemQnter\t\t*" << endl;
cout << "*\t" << temp1 << "stk.\t\t10-kronemQnter\t\t*" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "************************************************* " << endl;
cout<<"\t\t Vekselautomat Malik'n Farhad A/S" << endl << endl << endl << endl;
searchTyve->second.antal=0;
searchTi->second.antal=antalTi-temp1;
}
else
{
temp = 2;
temp1 = 1;
cout << endl << "************************************************* " << endl;
cout << "*\t" << amount << "-kroneseddelen er vekslet til:\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "*\t" << temp << "stk.\t\t20-kronemQnter\t\t*" << endl;
cout << "*\t" << temp1 << "stk.\t\t10-kronemQnter\t\t*" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "************************************************* " << endl;
cout<<"\t\t Vekselautomat Malik'n Farhad A/S" << endl << endl << endl << endl;
searchTyve->second.antal=(antalTyve-2);
searchTi->second.antal=antalTi-temp1;
}
}
}
}
void Automat::opdaterSaldo()
{
map<coinType, indhold>::iterator saldo;
saldo = indholdMap.begin();
while(saldo != indholdMap.end())
{
int antal= saldo-> second.antal;
int type= saldo-> second.type;
ialt=ialt+(antal*type);
saldo++;
}
}
void Automat::addCoins(coinType c,int antal)
{
std::map<coinType, indhold>::iterator search;
search = indholdMap.find(c);
if(search != indholdMap.end() )
{
search->second.type = c;
temp = search->second.antal;
search->second.antal = antal + temp;
indholdMap.insert(pair<coinType, indhold>(c,saldo));
if (antal == 0)
{
}
else
{
cout <<"\n\n******************************************* ******" << endl;
cout << "*\t\tDer er tilføjet:\t\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "*\t\t" << antal << "stk.\t" << c << "-kronemQnter\t\t*" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "************************************************* " << endl;
cout<<"\t\t Vekselautomat Malik'n Farhad A/S" << endl << endl << endl;
}
}
else
{
saldo.type = c;
saldo.antal = antal;
indholdMap.insert(pair<coinType, indhold>(c,saldo));
if (antal == 0)
{
}
else
{
cout <<"\n\n******************************************* ******" << endl;
cout << "*\t\tDer er tilføjet:\t\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "*\t\t" << antal << "stk.\t" << c << "-kronemQnter\t\t*" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "************************************************* " << endl;
cout<<"\t\t Vekselautomat Malik'n Farhad A/S" << endl << endl << endl;
}
}
}
void Automat::print()
{
int i=0;
map<coinType, indhold>::iterator print;
print = indholdMap.begin();
if (print == indholdMap.end())
{
cout << "\n\n********************************************* ****" << endl;
cout << "*\t\tOBSERVATION:\t\t\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\tAutomaten er tom!\t\t*" << endl;
cout << "************************************************* " << endl;// << endl << endl << endl;
}
else
{
ialt=0;
std::cout<<std::endl;
cout<< "\n\n********************************************* ****" << endl;
cout<< "*\t\tSTATUS FOR AUTOMATEN\t\t*" << endl;
cout << "* --------------------------------------------- *" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout<<"*\tAntal:\t\t\tMønttype:\t*"<<endl;
while(print != indholdMap.end())
{
cout << "*\t" << print-> second.antal << " stk.\t\t\t";
int antal= print-> second.antal;
cout << print-> second.type << " kr.\t\t*" <<endl;
int type=print-> second.type;
ialt=ialt+(antal*type);
print++;
}
cout << "* --------------------------------------------- *" << endl;
cout << "*\tTotal saldo:\t\t" << ialt << " kr.\t\t*" << endl;
cout << "*\t\t\t\t\t\t*" << endl;
cout << "************************************************* " << endl;
cout<<"\t\t Vekselautomat Malik'n Farhad A/S" << endl << endl << endl;
}
}
ممنون می شم اگر اشکالی دیدید بهم بگید.... ممنون