Showing posts with label Cpp. Show all posts
Showing posts with label Cpp. Show all posts

Wednesday, 18 April 2012

Object Oriented Programming in Cpp

Main Program main.cpp

#include<iostream>
#include<string>
#include<time.h>

#include"Land.h"
#include"Cost.h"
#include"Creature.h"
#include"Enchantment.h"

using namespace std;

int main() { 
 srand((unsigned)time(0)); 
 Land l1("black", "snow covered swamp"); 
 Land l2("blue", "island"); 
 l1.print(); 
 // for the following array, default constructor is called 5 times 
 // let’s randomly initialize land cards in the default constructor srand called at the beginning of the code 
 Land l3[5];
 // cost object: we need total 3 manas, one of them must be 
 // red mana (the last one) color of other two manas are unimportant 
 // order of the parameters: total, white, blue, black, green, red
 Cost *cost = new Cost(3,0,0,0,0,1); 
 // parameters : name, color, ap, hp, cost, description-flavor text 
 Creature c1("anaba shaman", "red", 2, 2, cost, "just try taking this bull by the horns.");
 delete cost; 
 cost = new Cost(5,1,0,0,0,0); 
 Creature c2("angel of mercy","white",3,3, cost,"when angel of mercy enters the battlefield, you gain 3 life."); 
 delete cost; 
 cost = new Cost(1,0,0,0,0,1); 
 Enchantment e("firebreathing", "red", 1,0, cost,"enchanted creature gets +1/+0 until end of turn.");
 // when a creature is enchanted, update its attack power and hit point 
 if (c2.canEnchantable(e)){
  cout << e.getName() << " is enchantable for " << c2.getName() << endl; 
  c2 + e; 
 } else if (c1.canEnchantable(e)){ 
  cout << e.getName() << " is not enchantable for " << c2.getName() << endl;
  cout << e.getName() << " is enchantable for " << c1.getName() << endl;
  c1 + e; 
 } else 
  cout << e.getName() << " can’t be enchanted to any creature!" << endl;
 if (c1.isAffordable(l3,5)) 
  // if you look closely, you can see that 5 is size :) 
  cout << c1.getName() << " is affordable according to land deck l3 " << endl;
 if (e.isAffordable(l3,5)) 
  cout << e.getName() << " is affordable according to land deck l3" << endl; 
 // show time
 Land *l4[8]; 
 for (int i=0; i<5; i++)
  l4[i]=&l3[i]; 
 l4[5]=&c1;
 l4[6]=&c2; 
 l4[7]=&e; 
 for (int i=0; i<8; i++) 
  l4[i]->print(); 
 getchar();
 return 0; 
}

Land Base class cpp file Land.cpp

#include<iostream>
#include<string>

#include "Land.h"

using namespace std;

Land::Land()//default constructor
{
 int random = rand()%5+1;
 switch(random){
  case 1:
   name = "Plains";
   color = "white";
   break;
  case 2:
   name = "Island";
   color = "blue";
   break;
  case 3:
   name = "Swamp";
   color = "black";
   break;
  case 4 :
   name = "Forest";
   color = "green";
   break;
  case 5: 
   name = "Mountain";
   color = "red";
   break;
 }
}

Land::Land(const string &s1,const string &s2)
{
 name = s1;
 color = s2;
}

void Land::print()const
{
 cout << "Name : "<< name << "  Color : "<< color << endl;
}

Land Base class header file Land.h

#ifndef LAND_H
#define LAND_H
#include<string>
#include<time.h>
#include<cstdlib>
using namespace std;

class Land{//Base Class
private :
 string name;
 string color;
public :
 Land();//default constructor
 Land(const string &,const string &);//copy constructor
 string getname()const {return name;}//return name variable
 string getcolor()const {return color;}//return color variable
 virtual void print()const;//virtual function from polymorphism
};

#endif

Creature Derived Class header file Creature.h

#ifndef CREATURE_H
#define CREATURE_H
#include<string>
#include "Enchantment.h"
#include "Land.h"
#include "Cost.h"

using namespace std;

class Creature: public Land  //public inhertance base class is Land
{
private :
 int ap;//attack point
 int hp;//hit point
 Cost *c1; //Cost pointer 
 string description;//description of creature
public :
 Creature(const string &,const string &,int AP,int,Cost *,const string &);//Constructor
 bool canEnchantable(const Enchantment &);//to control is Creature's color is useful for Land
 string getName()const{return Land::getname();}//return name variable
 bool isAffordable(const Land *,int);//to control is mana enough for Creature
 void print()const; //virtual function from polymorphism
 void operator+(const Enchantment &); //operator overloading
 ~Creature(){delete c1;}//Destructor
};
#endif

Creature Derived Class cpp file Creature.cpp

#include<iostream>
#include<string>

#include "Creature.h"

using namespace std;

Creature::Creature(const string &s1,const string &s2,int AP,int HP,Cost *cost,const string &s3):Land(s1,s2)
{
  ap = AP;
  hp = HP;
  c1 = new Cost(cost->getTotal(),cost->getWhite(),cost->getBlue(),cost->getBlack(),cost->getGreen(),cost->getRed());
  description = s3;
}

bool Creature::canEnchantable(const Enchantment &e)
{
  if(Land::getcolor().compare(e.getColor())==0)//compare function in string library return integer value
   return true;
  else
   return false;
}

void Creature::print()const //virtual function from polymorphism
{
  Land::print(); // virtual  base print function
  cout << "Attack Point : " << ap << "\n"
         << "Hit Point : " << hp << "\n"
  << "Total Mana : " << c1->getTotal() << "\n"
  << Land::getcolor() <<" Mana : " << c1->getmana() << "\n"
  << description << endl;
}

void Creature::operator+(const Enchantment &e) //operator overloading
{
  ap = ap + e.getAp();
  hp = hp + e.getHp();
}

bool Creature::isAffordable(const Land *l,int size)
{ //Mana Array's size = 5 and it includes number of mana colors
 int manas[5] = {0,0,0,0,0};//White,Blue,Black,Green,Red
 if(c1->getTotal()>size)
   return false;
 else{
  for(int i = 0;i<size;i++){
   if(l[i].getcolor().compare("white")==0)
    //increment number of White Mana
    manas[0]++;
   if(l[i].getcolor().compare("blue")==0)
    //increment number of Blue Mana
    manas[1]++;     
   if(l[i].getcolor().compare("black")==0)
    //increment number of Black Mana
    manas[2]++; 
   if(l[i].getcolor().compare("green")==0)
    //increment number of Green Mana
    manas[3]++;
   if(l[i].getcolor().compare("red")==0)
    //increment number of Red Mana
    manas[4]++;
  }
  if(Land::getcolor().compare("white")==0){
   if(c1->getWhite() <= manas[0])
    return true;
  }else if(Land::getcolor().compare("blue")==0){
   if(c1->getBlue() <= manas[1])
    return true;
  }else if(Land::getcolor().compare("black")==0){
   if(c1->getBlack() <= manas[2])
    return true;
  }else if(Land::getcolor().compare("green")==0){
   if(c1->getGreen() <= manas[3])
    return true;
  }else if(Land::getcolor().compare("red")==0){
   if(c1->getRed() <= manas[4])
    return true;
  }
  return false;
 }
}

Enchantment Derived Class header file Enchantment.h

#ifndef ENCHANTMENT_H
#define ENCHANTMENT_H
#include<string>
#include "Cost.h"
#include "Land.h"

using namespace std;

class Enchantment : public Land //public inhertance base class is Land
{
private :
 int inc_ap;//increment atack point
 int inc_hp;//increment hit point;
 string description;//description of enchantment
 Cost *c2;//Cost pointer
public :
 Enchantment(const string &,const string &,int AP,int HP,Cost *,const string &);//Constructor
 string getColor()const{return Land::getcolor();}//get method to access private member called color in base class Land
 string getName()const{return Land::getname();}//get method to access private member called name in base class Land

 bool isAffordable(const Land *,int);//To control is mana enough for Enchantment 
 
 void print()const; //virtual function from polymorphism
 
 int getAp()const {return inc_ap;}//get method to access private inc_ap
 int getHp()const {return inc_hp;}//get method to access private inc_hp
 ~Enchantment(){delete c2;}//destructor
};

#endif

Enchantment Derived Class cpp file Enchantment.cpp

#include<iostream>
#include<string>

#include "Enchantment.h"

using namespace std;

bool Enchantment::isAffordable(const Land *l,int size)
{
 int manas[5] = {0,0,0,0,0}; //White,Blue,Black,Green,Red
 if(c2->getTotal()>size)
   return false;
 else{
  for(int i = 0;i<size;i++){
   if(l[i].getcolor().compare("white")==0)
    //increment number of White Mana
    manas[0]++;
   if(l[i].getcolor().compare("blue")==0)
    //increment number of White Mana
    manas[1]++;     
   if(l[i].getcolor().compare("black")==0)
    //increment number of White Mana
    manas[2]++; 
   if(l[i].getcolor().compare("green")==0)
    //increment number of White Mana
    manas[3]++;
   if(l[i].getcolor().compare("red")==0)
    //increment number of White Mana
    manas[4]++;
  }
  if(Land::getcolor().compare("white")==0){
   if(c2->getWhite() <= manas[0])
    return true;
  }else if(Land::getcolor().compare("blue")==0){
   if(c2->getBlue() <= manas[1])
    return true;
  }else if(Land::getcolor().compare("black")==0){
   if(c2->getBlack() <= manas[2])
    return true;
  }else if(Land::getcolor().compare("green")==0){
   if(c2->getGreen() <= manas[3])
    return true;
  }else if(Land::getcolor().compare("red")==0){
   if(c2->getRed() <= manas[4])
    return true;
  } 
  return false; 
 }
} 

void Enchantment::print()const //virtual function from polymorphism
{
  Land::print();//virtual base print function
  cout << "Increment Attack Point by : " << inc_ap << "\n"
   <<  "Increment Hit Point by : " << inc_hp << "\n"
   << "Total Mana : " << c2->getTotal() << "\n"
   << Land::getcolor() << " Mana : " << c2->getmana() << "\n"
   << description << endl;
  
}

Enchantment::Enchantment(const string &s,const string &s2,int AP,int HP,Cost *cost,const string &s3):Land(s,s2)
{ 
  // Constructor
  inc_ap = AP;
  inc_hp = HP;
  c2 = new Cost(cost->getTotal(),cost->getWhite(),cost->getBlue(),cost->getBlack(),cost->getGreen(),cost->getRed());
  description = s3;
}

Cost Class header file Cost.h

#ifndef COST_H
#define COST_H

using namespace std;

class Cost{
private :
 int Total;
 int White;
 int Blue;
 int Black;
 int Green;
 int Red;
public :
 Cost(int,int,int,int,int,int);
 int getmana()const;
 int getTotal()const {return Total;}
 int getWhite()const {return White;}
 int getBlue()const {return Blue;}
 int getBlack()const {return Black;}
 int getGreen()const {return Green;}
 int getRed()const {return Red;}
};

#endif

Cost Class cpp file Cost.cpp

#include<iostream>
#include<string>

#include "Cost.h"

using namespace std;

Cost::Cost(int total,int white,int blue,int black,int green,int red)
{
 Total = total;
 White = white;
 Blue = blue;
 Black = black;
 Green = green;
 Red = red;
}

int Cost::getmana()const
{
 if(White!=0)
  return White;
 else if(Blue!=0)
  return Blue;
 else if(Black!=0)
  return Black;
 else if(Green!=0)
  return Green;
 else if(Red!=0)
  return Red;
 else
  return 0;
}

OUTPUT

Wednesday, 11 April 2012

Linked List/Bağlantılı liste

List Example with cpp / Bağlantılı liste örneği

#include<iostream>
#include<string>
#include<stdlib.h>
#include<stdio.h>
#include<iomanip>
#include<ctype.h>

using namespace std;
#include "Kayit.h"
typedef struct listeDugumu listeDugumu;
typedef listeDugumu *listeDugumuPtr;


void ekle(listeDugumuPtr *,listeDugumu *,int);
void listeyazdir(listeDugumuPtr);
int bosMu(listeDugumuPtr);
void sil(listeDugumuPtr *,int,int);
void menu(void);
void listebosalt(listeDugumuPtr *);
//void listeyibol(listeDugumuPtr *,int);
int main(){

 listeDugumuPtr baslangicPtr=NULL;
 listeDugumu *kayitPtr;
 kayitPtr =new listeDugumu;
 int dugumsayisi=0;
 int sirano;
 menu();
 int secim;
 cout << "? : ";
 cin >> secim;
 
 while(secim!=8){
   
  switch(secim){
  case 1 :
   cout << "Lutfen eklemek istediginiz kisinin bilgilerini giriniz" << endl;
   cout << "Eklenecek ismi giriniz : ";
   cin.ignore(1000, '\n');
   cin.getline(kayitPtr->isim,AD_UZUNLUK);
      cout <<"Eklenecek Telefon numarasi : ";
   cin >> setw(TEL_UZUNLUK) >> kayitPtr->tel_numarasi;
   cout << endl;
   ekle(&baslangicPtr,kayitPtr,dugumsayisi);
      listeyazdir(baslangicPtr);
   break;
  case 2 :
    if(!bosMu(baslangicPtr)){
    cout << "Silinecek siranoyu giriniz :";
    cin >> sirano;
    sil(&baslangicPtr,sirano,dugumsayisi);
    cout << "Kayit silindi" << endl;
    }else
     cout << "liste bos" << endl;
         break;
  case 3 : 
   listebosalt(&baslangicPtr);
     cout << "liste bosaltildi..." << endl;
   break;
  /*case 4 :
   cout << "listeyi kaca bolmek istiyorsunuz ?" << endl;
   cin >> kac;
   listeyibol(&baslangicPtr,dugumsayisi);
   break;
   */
  default:
   cout << "Yanlis bir giris yaptiniz tekrar giriniz" << endl;
   menu();
        break;
       }
  cout << "? : "; 
  cin >> secim;
 }
 cout << "program kapatılıyor";
 delete kayitPtr;
 
 return EXIT_SUCCESS;
}
void menu(void){
cout << "1-Kayit ekle" << endl;
cout << "2-Kayit sil" << endl;
cout << "3-Listeyi bosalt" << endl;
cout << "4-Listeyi ikiye bol" << endl;
cout << "8-Cikis" << endl;
  
}
void ekle(listeDugumuPtr *sPtr,listeDugumu *savePtr,int dugum_sayisi){
 listeDugumuPtr tara,arka,yeni;
  tara=*sPtr;
  yeni = new listeDugumu;
        *yeni=*savePtr;
  yeni->sonrakiPtr=NULL;
  if(*sPtr==NULL){
  *sPtr=yeni;//ilk düğüm ekleniyor
  dugum_sayisi++;
  return;
     }
  if(strcmp(yeni->isim,(*sPtr)->isim)<0)
  {
   yeni->sonrakiPtr=*sPtr;//Basa ekleniyor
   *sPtr=yeni;
   dugum_sayisi++;
   return;
  }
  while(tara && strcmp(yeni->isim,tara->isim)>0)
  {
  arka=tara;
  tara=tara->sonrakiPtr;
  
  }
  if(tara){
   yeni->sonrakiPtr=tara;//araya ekleniyor
   arka->sonrakiPtr=yeni;
   
  }
  else
   arka->sonrakiPtr=yeni;//sona ekleniyor
      dugum_sayisi++;
}
void listeyazdir(listeDugumuPtr suandakiPtr){
 int i=1;
 if(suandakiPtr==NULL)
  cout << "Rehber bos" << endl;
 else{
  cout << "Liste : " << endl;
    while(suandakiPtr!=NULL){
     cout << i << suandakiPtr->isim << "\t" << suandakiPtr->tel_numarasi << endl;
     suandakiPtr=suandakiPtr->sonrakiPtr;
     i++;
    }
 }
}
int bosMu(listeDugumuPtr basPtr){
   return basPtr==NULL;
}
void sil(listeDugumuPtr *sPtr,int sira,int dugum_sayisi){
 listeDugumuPtr tara,arka;
 int sayac=1;
 tara=*sPtr;
 if(sira<=0){
  cout << "yanlis sira no girdiniz" << endl;
  return;
 }
 if(sira==1){
 *sPtr=(*sPtr)->sonrakiPtr;
 delete tara;
 dugum_sayisi--;
 return;
 }
 while(tara && (sayac<sira)){
 arka=tara;
 tara=tara->sonrakiPtr;
 sayac++;
 }
 if(sayac<sira)
  cout << "silinecek kayit bulunamadi" << endl;
 else{
  arka->sonrakiPtr=tara->sonrakiPtr;
     delete tara;
     dugum_sayisi--;

 }
    
}
void listebosalt(listeDugumuPtr *sPtr){
   listeDugumuPtr tara;
   
    if(*sPtr==NULL)
  cout << "liste bos" << endl;
 else 
  while(*sPtr){
     tara=*sPtr;
  *sPtr=(*sPtr)->sonrakiPtr;
  delete tara;
  }

}

kayit.h is a header file for liste_example.cpp


#define AD_UZUNLUK 30
#define TEL_UZUNLUK 15
struct listeDugumu{
char isim[AD_UZUNLUK];
char tel_numarasi[TEL_UZUNLUK];
struct listeDugumu *sonrakiPtr;
};

Thursday, 15 March 2012

string library

#include<iostream>
#include<conio.h>
#include<string>
typedef string stringer
void main(){
   stringer mailto = "yilmazburk@gmail.com";
   cout << mailto << endl;
}

Tuesday, 13 March 2012

Problem Getline is solved

Getline function sometimes runs badly i mean it's work useless for us so i can help you about getline's useless

/***************************************************************************** 

   using getline function/getline fonksiyonun istenildiği gibi kullanılması

******************************************************************************/
#include<string>
#include<iostream>

using namespace std;

void main(){
string film_name,country;
bool oscar;
int duration,number_of_actors;
double imdb_rating;
string getliner;

cout << "Please enter film's name : " << "\n";
cin.ignore(0,'\n'); //not read space and nextline character
getline(cin,film_name);

cout  << "Please enter film's year : "<< endl;
cin >> year;

cout  << "Please enter film's duration : "<< endl;
cin >> duration;

cout  << "Please enter number of actors : "<< endl;
cin >> number_of_actors;
getline(cin,getliner);

cout  << "Please enter film's country : "<< "\n";
cin.ignore(0,'\n');  //not read space and nextline character
getline(cin,country);

cout  << "Does the film have Oscar 0 or 1 : "<< endl;
cin >> oscar;

cout  << "Please enter imdb rating of the film : "<< endl;
cin >> imdb_rating;
getline(cin,getliner);

cout << "Film added" << endl;

cin.clear();
}

Monday, 16 January 2012

How to create binary tree

#include<iostream>
#include<stdio.h>
#include<time.h>
#include<iomanip>
#include<conio.h>
#define dimension 11
using namespace std;

struct tree{
 int value;
 tree *leftptr;
 tree *rightptr;
};
typedef tree Tree;
typedef Tree *treePtr;
void create(treePtr *,int);
void silme(treePtr *);
void remove(treePtr);
void inorder(treePtr);
void postorder(treePtr);
void preorder(treePtr);
void go_array(treePtr,int[],int *);
void main(){
 srand(time(NULL));
 int val,i,key;  //val means value, i means counter , key is keyboard input
 int counter=0;
 int arrayx[dimension]={0};
 treePtr root = NULL;
 for(i=0;i<11;i++){

  val = rand()%999+1;
  create(&root,val);
 }

 go_array(root,arrayx,&counter);
 cout << "inorder scanning..." << endl;
 inorder(root);
 cout <<endl<<"Preorder scanning..."<<endl;
 preorder(root);
 cout <<endl<<"Postorder scanning..."<<endl;
 postorder(root);
 cout<<endl;
 cout << "Array : "<<endl;
 for(i=0;i<11;i++)
  cout <<arrayx[i] <<"  ";
 cout<<endl;
 cout <<"Sayac = "<<counter<<endl;
 cout <<endl<< "Press 1 to delete a node from to tree or 0 to pass and exit : ";
 cin.ignore(0,'\n');
 cin>>key;
 cout << key<<endl;
 if(key==1)
 remove(root);
 inorder(root);
 getch();
}
void create(treePtr *proot,int qvalue){
 treePtr tara,yeni;
 yeni = new Tree;
 tara = *proot;
 yeni->value=qvalue;
 yeni->leftptr = NULL;
 yeni->rightptr = NULL;
 bool bulundu = false;
 if((*proot)==NULL){//ilk düğüm ekleniyor
  *proot = yeni;
 }
 while(tara!=NULL && bulundu!=true){
  if(qvalue<tara->value){
   if(tara->leftptr!=NULL)
    tara=tara->leftptr;
   else{
    tara->leftptr = yeni;
    bulundu = true;
   }     
  }
  else if(qvalue>tara->value){
    if(tara->rightptr!=NULL)
     tara=tara->rightptr;
    else{
     tara->rightptr = yeni;
     bulundu = true;
    }
  }
  else 
   break;
 }
}
void inorder(treePtr p){
 if(p){
  inorder(p->leftptr);
  cout << p->value <<"  ";
  inorder(p->rightptr);
 }
}
void postorder(treePtr p){
 if(p){
  inorder(p->leftptr);
  inorder(p->rightptr);
  cout << p->value <<"  ";
  
 }
}
void preorder(treePtr p){
 if(p){
  cout << p->value <<"  ";
  inorder(p->leftptr);
  inorder(p->rightptr);
 }
}
void go_array(treePtr p,int q[dimension],int *s){

 if(p){
  
  go_array(p->leftptr,q,s);
  q[*s]=p->value;
  (*s)++;
  go_array(p->rightptr,q,s);
  
  
 }
}
void remove(treePtr p){
 
 int sil;
 cout << "NE SILMEK ISTIYORSUN : ";
 cin.ignore(2,'\n');
 cin >> sil;
 char yon;
 treePtr tara,ust;
 tara = p;
 bool var = false;
 while(tara && var!=true){
  if(sil<tara->value){
    ust=tara;
    yon = 'l';
    tara=tara->leftptr;
  }else if(sil>tara->value){
    ust=tara;
    yon = 'r';
    tara=tara->rightptr;
  }else
   var = true;
 }
 if(var==true){
  if(yon=='l')
   silme(&(ust->leftptr));
  else if(yon=='r')
   silme(&(ust->rightptr));
  else 
   silme(&p);
 }
 else
  cout << "HATA" <<endl;

}
void silme(treePtr *s){
 treePtr r,q;
 r=*s;
 if(r==NULL)
  return;
 else if(r->rightptr==NULL){
  *s=r->leftptr;
  delete r;
 }
 else if(r->leftptr==NULL){
  *s=r->rightptr;
  delete r;
 }
 else {
  for(q=r->rightptr;q->leftptr;q=q->leftptr);
  q=r->leftptr;
  *s=r->rightptr;
  delete r;
 }

}

Tuesday, 10 January 2012

Basic File Reading in C

#include< stdio.h>
#include< stdlib.h>
int main()
{
int hesap;
char ad[30];
double bakiye;
FILE*fPtr;
fPtr=fopen("musteri.txt","r");
if(fPtr==NULL)
printf("Dosya Bozuk!!!");
else{
printf("%-10s%-13s%s\n","hesap","isim","bakiye");
fscanf(fPtr,"%d%s%lf",&hesap,ad,&bakiye);

while(!feof(fPtr)){
printf("%-10d%-13s%7.2f\n",hesap,ad,bakiye);
fscanf(fPtr,"%d%s%lf",&hesap,ad,&bakiye);
}
fclose(fPtr);
}
system("pause");
return 0;
}