Treceți la conținutul principal

tcontbancar cpp

#include <iostream.h>
#include <fstream.h>
#include <strstrea.h>
#include <iomanip.h>
#include <string.h>
#include <stdlib.h>


// Coduri returnate de program:
// -0  - iesire normala
// -1 - banca.dat nu poate fi gasit
// -2 - inregistrarea nu poate fi gasita in fisier.
// -3 - fatal error for file operations


#define NMAX_CONT             100
// Numarul maxim admix de cifre pentru un cont
#define NMAX_NUME_TITULAR     100
// Numarul maxim de caractere ce poate fi stocat in campul corespunzator
// numelui titularului. Numarul maxim admis de caractere pentru numele 
// titularului contului respectiv.
#define NMAX_VALUTE           2
// Numarul maxim admis de valute
#define NMAX_PAROLA           100
// Numarul maxim admis de caractere pentru parola
#define DOBANDA               (14/100)


typedef struct 
{
  char titular[NMAX_NUME_TITULAR], numar[NMAX_CONT];
  unsigned valuta;
} Inregistrare;



template<class T> class ContBancar
{

private:
  Inregistrare Continut;
  void InitializareMemorieObiect();
  void StergereMemorieObiect();
public:   
  ContBancar();
  T sold;
  ContBancar(const char *, const char *, const T, 
const unsigned);
  ContBancar(const int);
  ContBancar(const char *);
  ContBancar operator =(ContBancar);
  ContBancar operator +=(ContBancar);
  ContBancar operator +=(T);
  ContBancar operator -=(T);
  friend ostream &operator << (ostream &, ContBancar &);
  friend istream &operator >> (istream &stream, ContBancar &);
  void depun(T);
  void retrag(T);
  T spune_total();
  ~ContBancar();
};

template<class T> void ContBancar<T>::depun(T x)
{
  sold+=x;
}

template<class T> void ContBancar<T>::retrag(T x)
{
  sold-=x;
  if (sold<0)
    sold=0;
}

template<class T> ContBancar<T>::spune_total()
{
  T total=0;
  ifstream banca("banca.dat", ios::binary | ios::in);
  while (!banca.eof())
    {
      ContBancar tmp;
      banca.read((unsigned char *)&tmp, sizeof(Inregistrare));
      total+=tmp.sold;
    }
  banca.close();
  return total;
}

template<class T> ContBancar<T>::ContBancar()
{
  this->InitializareMemorieObiect();
}


template<class T> ContBancar<T>::ContBancar(const char *titular,const char *numar, 
          T sold, unsigned valuta)

{
  this->InitializareMemorieObiect();
  ostrstream iesire_titular(Continut.titular, NMAX_NUME_TITULAR);
  ostrstream iesire_numar(Continut.numar, NMAX_CONT);
  sold=sold;
  Continut.valuta=valuta;
  iesire_titular<<titular<<ends;
  iesire_numar<<numar<<ends; 
}

template<class T> ContBancar<T>::ContBancar(const int i)
{
  this->InitializareMemorieObiect();
  cout<<"Numele este: ";
  cin.get(Continut.titular, NMAX_NUME_TITULAR+1);
  cout<<"Numarul contului este: ";
  cin>>Continut.numar;
  cout<<"Soldul este: ";
  cin>>sold;
  cout<<"Valuta folosita (0-"<<NMAX_VALUTE<<"): ";
  cin>>Continut.valuta;
}

template<class T> ContBancar<T>::ContBancar(const char *numar)
 
{
  register int gasit=0;
  Inregistrare tmp;
  ifstream banca("banca.dat", ios::in | ios::binary);
  if (!banca)
    {
      cerr<<"Fisierul \"banca.dat\" nu exista... Voi parasi aplicatia acum..."
<<endl;
      cerr<<"(Mesaj din ContBancar::ContBancar(char *) ---> constructor"<<endl;
      exit(-1);
    }
  while (!banca.eof())
    {
      banca.read((unsigned char *)&tmp, sizeof(Inregistrare));
      if (!strcmp(numar, tmp.numar))
 {
   gasit=1;
   break;
 }
    }
  banca.close();
  if (!gasit)
    {
      cerr<<"Nu pot gasi respectivul cont in fisierul banca.dat"<<endl;
      cerr<<"Prin urmare obiectul nu poate fi initializat cu acest constructor"<<endl;
      cerr<<"Parasesc aplicatia..."<<endl;
      exit(-2);
    }
  else
    {
      banca.seekg((-1)*sizeof(Inregistrare), ios::cur);
      this->InitializareMemorieObiect();
      Continut.valuta=tmp.valuta;
      sold=tmp.sold;
      ostrstream iesire_titular(Continut.titular, NMAX_NUME_TITULAR);
      ostrstream iesire_numar(Continut.numar, NMAX_CONT);
      iesire_titular<<tmp.titular<<ends;
      iesire_numar<<tmp.numar<<ends; 
    }
}

template<class T> void ContBancar<T>::InitializareMemorieObiect()
{
  ostrstream iesire_titular(Continut.titular, NMAX_NUME_TITULAR);
  ostrstream iesire_numar(Continut.numar, NMAX_CONT);
  iesire_titular<<ends;  // Sirurile sunt pentru inceput vide.
  iesire_numar<<ends;


template<class T> void ContBancar<T>::StergereMemorieObiect()  
{
  
  register int i;
  for (i=0; i<NMAX_CONT; i++)
    Continut.numar[i]='\0';
  for (i=0; i<NMAX_NUME_TITULAR; i++)
    Continut.titular[i]='\0';
}

ContBancar ContBancar::operator =(ContBancar operand2)
{
  sold+=operand2.sold;
  operand2.sold=0;
  return *this;
}

ContBancar ContBancar::operator +=(ContBancar operand2)
{
  sold+=operand2.sold;
  return *this;
}

ContBancar ContBancar::operator +=(T operand2)
{
  sold+=operand2;
  return *this;
}

ContBancar ContBancar::operator -=(T operand2)
{
  if (operand2>sold)
    {
      cerr<<"Eroare in operatorul \"-=\""<<endl;
      exit(-5);
    }
  sold-=operand2;
  return *this;
}

ostream &operator << (ostream &stream, ContBancar &cont)
{
  stream<<"Numele titularului: "<<cont.Continut.titular<<endl;
  stream<<"Numarul contului: "<<cont.Continut.numar<<endl;
  stream<<"Soldul: "<<cont.sold<<endl;
  stream<<"Valuta folosita: "<<cont.Continut.valuta<<endl;
  return stream;
}

istream &operator >> (istream &stream, ContBancar &cont)
{
  cout<<"Numele este: ";
  stream.get(cont.Continut.titular, NMAX_NUME_TITULAR+1);
  cout<<"Numarul contului este: ";
  stream>>cont.Continut.numar;
  cout<<"Soldul este: ";
  stream>>cont.sold;
  cout<<"Valuta folosita (0-"<<NMAX_VALUTE<<"): ";
  stream>>cont.Continut.valuta;
  return stream;
}

ContBancar::~ContBancar()
{
  register int gasit=0, count=0, stare_fisier=0;
  Inregistrare tmp;
  fstream banca("banca.dat", ios::binary | ios::in | ios::out);
  if (!banca)
    {
      cerr<<"Fisierul \"banca.dat\" nu exista si nu il pot genera... \
Voi parasi aplicatia acum..."<<endl;
      cerr<<"(Mesaj din ContBancar::~ContBancar() ---> destructor"<<endl;
      exit(-1);
    }
  while (!banca.eof() && !gasit)
    {
      banca.read((unsigned char *)&tmp, sizeof(Inregistrare));
      if (!strcmp(Continut.numar, tmp.numar))
 gasit=1;
      else
 if (!banca.eof())
   count++;
    }
  stare_fisier=banca.rdstate();
  if ((stare_fisier & ios::eofbit) || (stare_fisier & ios::failbit))
    banca.clear();
  if (stare_fisier & ios::badbit)
    {
      cerr<<"Eroare fatala legata de operatiile cu fisierul \"banca.dat\"...\
Programul se va inchide..."<<endl;
      exit(-3);
    }
  banca.seekp(count*sizeof(Inregistrare), ios::beg);
  banca.write((unsigned char *)&Continut, sizeof(Inregistrare));
  banca.flush();
  banca.close();
  this->StergereMemorieObiect();
  cout<<"Destructorul a fost apelat."<<endl;
}


int main()
{
  // Liniile comentate contin un exemplu aidtional
//   cout<<"-------------------------------------------------------------------"<<
//     endl;
//   ContBancar<int> *oInstanta1=new ContBancar<int>();
//   // Se foloseste un constructor simplu cu care vom produce o instanta cu
//   // ajutorul careia vom exemplifica utilizarea operatorilor "<<" si ">>".
//   cout<<"Obiectul *oInstanta1 -----> Introduceti datele: "<<endl;
//   cin>>*oInstanta1; 
//   // Atentie: nu se foloseste pointer-ul, ci instanta spre care indica acesta
//   cout<<"Continutul obiectului *oInstanta1 este: "<<endl;
//   cout<<*oInstanta1;
//   delete oInstanta1;
//   // Aceeasi observatie ca si la (*).
  cout<<"-------------------------------------------------------------------"<<
    endl;
  ContBancar<int> *oInstanta=new ContBancar<int>("Mircea Stancu", "4",
           10, 0);
  cout<<*oInstanta<<endl<<"Se scrie in fisier..."<<endl;
  delete oInstanta;
  cout<<"-------------------------------------------------------------------"<<
    endl;
  ContBancar<int> *oInstanta2=new ContBancar<int>("4");
  // S-a folosit constructorul care citeste din fisier.
  cout<<"La contul 4 avem: "<<*oInstanta2;
  cout<<"Acum veti modifica acest cont: "<<endl;
  // Pot fi adaugate si liniile de mai jos
//   cout<<"Va rugam nu modificati numarul contului, altfel se va genera un\
//  nou cont."<<endl;
//   cin>>*oInstanta2;
//   cout<<"Contul modificat este: "<<*oInstanta2<<endl;
  cout<<"Acest cont va fi scris acum in \"banca.dat\""<<endl;
  delete oInstanta2;
  // A fost apelat destructorul care a scris continutul obiectului inapoi in
  // fisier si apoi a distrus datele din memorie. (*)
  cout<<"-------------------------------------------------------------------"<<
    endl;
  cout<<"Introduceti datele pentru un nou cont:"<<endl;
  ContBancar<int> *oInstanta3=new ContBancar<int>(1);
  // Constructorul care citeste de la consola datele pentru obiect
  cout<<"Datele introduse sunt: "<<endl<<*oInstanta3<<endl;
  cout<<"Acest cont va fi scris acum in \"banca.dat\""<<endl;
  delete oInstanta3;
  // Aceeasi observatie ca si la (*).
  cout<<"-------------------------------------------------------------------"<<
    endl;
  ContBancar<int> *oInstantaX=new ContBancar<int>("4");
  cout<<"Totalul din \"banca.dat\" in lei: "<<oInstantaX->spune_total()<<endl;
  delete oInstantaX;
  return 0;
}


Comentarii

Postări populare de pe acest blog

WINDOWS 10 COMPUTER FREEZING PROBLEM SOLVED

good news : a BIOS UPDATE can resolve the problem but just for a Windows 7 on 64 bits o.s. and the system is not stable all the time. even after  bios update the system can freeze.
new info : u can try to low the screen brightness and see if this error appear so often after 
news: last info !!! maybe a virus. scann our system now with an antivirus i generate this error using other device ( a tablet pc) connected in the same network and the laptop i have this problem just freeze  http://thehackernews.com/2013/10/backdoor-found-in-chinese-tenda.html

news : if u use a tenda router this make couse all this problems



what i discover so far :
1.the electric company have many failure and affect the main ISP router/switch for building  also the router/switch installed by the ISP may be affected by overheating and will crash after a long utilisation on heat conditions 2.the router/switch of ISP affect any router of the user between this router and pc/laptop of client 3.the router and any other device of t…

program principal cpp

#include "clasa.h" #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define DELAY 9000000 void delay() { for(long i=0;i<DELAY;i++); } //constructor cu initializare de la tastatura BigInt::BigInt() {char x; signed char t[400]; int i; printf("\nNumarul cu semn "); do s=getche(); while((s!='+')&&(s!='-')); n=0; do {x=getche(); t[n]=x-'0'; n++; } while((x>='0')&&(x<='9')); n--; for(i=0;i<n;i++) nr[i]=t[n-i-1]; } //constructor cu initializare prin parametri BigInt::BigInt(char semn,signed char numar[],int dim) {int i; s=semn; n=dim; for(i=0;i<n;i++) nr[i]=numar[n-i-1]; } //transform un int negativ in pozitiv int BigInt::Pozitiv(int x) {int a,vb; a=0; vb=0; while(vb==0) if((x+a)==0) vb=1; else a=a+1; x=a; return x; } //constructor dintr-un nr int obisnuit BigInt::BigInt(int x) {int i; if(x>=0) s='+'…

o aplicatie php localitati romania

//APLICATIA SE REFERA LA BAZA DE DATE SIRUTA

//dragtable.js


/* dragtable v1.0 June 26, 2008 Dan Vanderkam, http://danvk.org/dragtable/ http://code.google.com/p/dragtable/ \Bsortabledraggable\B Instructions: - Download this file - Add <script src="dragtable.js"></script> to your HTML. - Add class="draggable" to any table you might like to reorder. - Drag the headers around to reorder them. This is code was based on: - Stuart Langridge's SortTable (kryogenix.org/code/browser/sorttable) - Mike Hall's draggable class (http://www.brainjar.com/dhtml/drag/) - A discussion of permuting table columns on comp.lang.javascript Licensed under the MIT license. */ // Here's the notice from Mike Hall's draggable script: //***************************************************************************** // Do not remove this notice. // // Copyright 2001 by Mike Hall. // See http…