Treceți la conținutul principal

clasa string cpp

// proiect1.cpp : Defines the entry point for the console application.
//


#include<stdio.h>
#include<string.h>
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<iostream>



class String
{
 char *psir;
 int lg;
 void setString(char *s)
 { psir=new char[strlen(s)+1];
  strcpy(psir,s);
  lg=strlen(s);
 }
 
  public:
   
   //constructor de clasa
 String(char *s1="")
 {
  setString(s1);
 }
   //copy constructor
 String(String &s2)
 { 
  setString(s2.psir);
 }
   //destructor
 ~String()
 { delete [] psir;}
 static char* abrevieri[];
 static char* neabrev[];
 int operator!();
 friend String operator+(String &, String &);
 friend String operator+(String &, char);
 friend String operator+(String &, char *);
 friend String operator+(char *, String &);
 String &operator+=(char);
 String &operator+=(char *);
 char &operator[](int);
 int operator==(String &);
 int operator!=(String &);
 int operator>(String &);
 int operator<(String &);
 int operator>=(String &);
 int operator<=(String &);
 char *abrev();
   //iesire de pe fisier standard pe disc
 friend ostream &operator<<(ostream &out, String &s)
 {
  out<<s.psir; return out; 
 }
   //intrare pe fisier standard pe disc
 friend istream &operator>>(istream &in, String &s)
 {
  cout<<"\n Sirul:";
  in>>s.psir;
  return in;
 }
   //iesire de pe disc in fisier
 friend ofstream &operator<<(ofstream &fout, String &s)
 {
  fout<<s.psir<<"\tlungime\t"<<s.lg<<"caractere\t"<<endl;
  return fout;
 }
   //intrare e disc in fisier
 friend ifstream &operator>>(ifstream &fin, String &s)
 {
  char aux[50];
  fin>>aux;
  s.lg=strlen(aux);
  delete s.psir;
  s.psir=new char [s.lg+1];
  strcpy(s.psir,aux);
  return fin;
 }
   //operatorul cast
 operator char()
 { return *psir;}
  
 String &operator=(String&);
 
};

   //test sir vid
 int String::operator!()
 { 
  if(lg==0) return 0;
  else return 1;
 }
   //concatenarea a doua siruri
 String operator+(String &s1, String &s2)
 {
  String aux;
  aux.lg=s1.lg+s2.lg;
  aux.psir=new char[aux.lg+1];
  strcpy(aux.psir,s1.psir);
  strcat(aux.psir,s2.psir);
  return aux;
 }
   //concatenarea unui sir cu un caracter
 String operator+(String &s1, char c)
 {
  String aux;
  aux.lg=s1.lg+1;
  aux.psir=new char[aux.lg+1];
  strcpy(aux.psir,s1.psir);
  aux.psir[aux.lg-1]=c;
  aux.psir[aux.lg]='\0';
  return aux;
 }
   //concatenarea unui sir cu char[]
 String operator+(String &s1, char *sirc)
 {
  String aux;
  aux.lg=s1.lg+strlen(sirc);
  aux.psir=new char[aux.lg+1];
  strcpy(aux.psir,s1.psir);
  strcat(aux.psir,sirc);
  return aux;

 }
   //concatenarea unui char[] cu un sir
 String operator+(char *sirc, String &s1)
 {
  String aux;
  aux.lg=strlen(sirc)+s1.lg;
  aux.psir=new char[aux.lg+1];
  strcpy(aux.psir,sirc);
  strcat(aux.psir,s1.psir);
  return aux;
 }
   //concatenarea unui sir existent cu un char 
 String &String::operator+=(char c)
 {
  char *aux;
  aux=new char[lg+1];
  strcpy(aux,psir);
  lg++;
  delete psir;
  psir=new char[lg+1];
  psir=strcpy(psir,aux);
  psir[lg-1]=c;
  psir[lg]='\0';
  delete aux;
  return (*this);
 }
   //concatenarea unui sir existent cu char[]
 String &String::operator+=(char *sirc)
 {
  char *aux;
  lg=lg+strlen(sirc);
  aux=new char[lg+1];
  strcpy(aux,psir);
  strcat(aux,sirc);
  delete psir;
  psir=new char[lg+1];
  psir=strcpy(psir,aux);
  delete aux;
  return (*this);
 }
   //extragere caracter
 char &String::operator[](int poz)
 { poz--;
  if(poz<lg) return psir[poz];
  else { cout<<"\nSirul nu are atatea caractere!";
    static char err='\0';
    return err;}
 }
   //comparatii intre siruri
 int String::operator==(String &s1)
 {
  return (strcmp(psir,s1.psir)==0); 
 }
 int String::operator!=(String &s1)
 {
  return (strcmp(psir,s1.psir)!=0); 
 }
 int String::operator>(String &s1)
 {
  return (strcmp(psir,s1.psir)>0); 
 }
 int String::operator<(String &s1)
 {
  return (strcmp(psir,s1.psir)<0); 
 }
 int String::operator>=(String &s1)
 {
  return (strcmp(psir,s1.psir)>=0); 
 }
 int String::operator<=(String &s1)
 {
  return (strcmp(psir,s1.psir)<=0); 
 }
   //atribuire
 String &String::operator=(String &sir)
 {
  if(&sir!=this) { delete [] psir;
       lg=sir.lg;
       setString(sir.psir);
      }
  return *this;
  

 }
 char *String::abrevieri[16]={"abs","etc","pt","urm","info","adv","adj","reg","rom","fr","lat","bl","tel","str","jud","cca"};
 char *String::neabrev[16]={"modul","etcetera","pentru","urmator","informatie","adverb","adjectiv","regiune","roman","francez","latin","bloc","telefon","strada","judet","circa"};
 char *String::abrev()
 {
  int i=0,gasit=0;
  while(i<16)
  {
   if(strcmp(psir,neabrev[i])==0) return abrevieri[i];
   else i++;
  }
  return psir;
 }
void main()
{ 
 int raspuns;
 do
 {
 system("cls");
 cout<<"\nS-a folosit 1 pentru fals si 0 pentru adevarat!";
 String sir1("alfabet"), sir2("etcetera"),  sir4, sir5, sir; 
 char sir3='a';
 
 cout<<"\n sir1:"<<sir1;
 cout<<"\t sir2:"<<sir2; 
 cout<<"\t sir3:"<<sir3;
 cout<<"\t sir4:"<<sir4;
 cout<<"\t sir5:"<<sir5;
 cout<<"\t sir:"<<sir;
 char c[20];
 cout<<"\n\nSirul introdus de la tastatura:"; cin>>c;
 char d;
 cout<<"\nCaracterul introdus de la tastatura:"; cin>>d;
 int poz;
 //test sir vid
 cout<<"\n Sirul 1 este vid?"<<"\t"<<!sir1;
 cout<<"\nSirul 3 este vid?"<<"\t"<<!sir3;
 //comparatii intre siruri
 cout<<"\nComparatii intre siruri:";
 cout<<"\n sir1==sir3 ?"<<"\t"<<(sir1==sir3);
 cout<<"\n sir4==sir5?"<<"\t"<<(sir4==sir5);
 cout<<"\n sir1!=sir5 ?"<<"\t"<<(sir1!=sir5);
 cout<<"\n sir2<sir3 ?"<<"\t"<<(sir2<sir3);
 //concatenari
 cout<<"\nConcatenari:";
 sir4=sir1;
 sir4=sir4+sir3;
 cout<<"\nConcatenare sir existent cu char sir1+sir3 : "<<sir4;
 cout<<"\nConcatenare a doua siruri existente sir1 + sir2 :"<<sir1 + sir2;
 cout<<"\nConcatenare sir existent cu sir nou sir2 + c :"<<sir2 + c;
 cout<<"\nConcatenare  sir existent cu caracter nou:"<<sir2+d;
 cout<<"\nSirul sir1 este:"<<sir1;
 sir1+=c;
 cout<<"\nConcatenarea a doua siruri folosind operatorul+=:"<<sir1;
 //cast catre caracter prin furnizarea primului element din sir
 cout<<"\nPrimul caracter din sirul sir1:"<<(char)sir1;
 //test pentru extragere caracter
 cout<<"\nPozitia:";cin>>poz;
 cout<<"\nCaracterul de pe pozitia data din sir1 este:"<<sir1[poz];
 cout<<"\nPozitia:";cin>>poz;
 cout<<"\nCaracterul de pe pozitia data din sir1 este:"<<sir1[poz];
 //prescurtarea sirurilor folosind functia abrev();
 cout<<"\nPrescurtarea sirului sir2 este:"<<sir2.abrev();
 cout<<"\nPrescurtare sir1:"<<sir1.abrev();
 //intrare la nivel de fisier
 ofstream fout("fis.txt");
 if(!fout) cout<<"Eroare deschidere fisier!!!";
 fout<<sir1<<c;
 cout<<"\nScriere in fisier:"<<sir1<<"\t"<<c;
 fout.close();
 //iesire la nivel de fisier
 ifstream fin("fis.txt");
 if(!fin) cout<<"Fisier negasit!";
 fin>>sir2;
 cout<<"\nCitire din fisier:"<<c<<"\n";
 fin.close();
 cout<<"\nDoriti sa continuati? (0 pentru nu si 1 pentru da)";
 cin>>raspuns;
 }
 while(raspuns==1);

}

Comentarii

Postări populare de pe acest blog

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='+'…

NUMERE PRIME ALGORITM C++

// NUMERE PRIME ALGORITM C++//  reediting from scratch //on this page is just the study for a next algoritm for generating the parime nr series like Fibonnaci or ....if possibile

74111121313417374414124343447 if u know the red part you can generate the orange part
1 0 1 111112222 1 1 23

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…