2013年6月25日 星期二

C 陣列結構 (C language struct array sample)

#include <stdio.h>
#define Data 20
#define BOOKNO 10
typedef struct
{
int No;
int Quantity;    
     
}BookSale;


int Sale_top(int book[] , int count);

int main(void)
{
   BookSale Details[Data] ={0};
   Details[0].No =4;
   Details[0].Quantity =10;
   Details[1].No =1;
   Details[1].Quantity =11;
   Details[2].No =1;
   Details[2].Quantity =12;
   Details[3].No =3;
   Details[3].Quantity =13;
   Details[4].No =1;
   Details[4].Quantity =14;
   Details[5].No =5;
   Details[5].Quantity =15;
   Details[6].No =6;
   Details[6].Quantity =16;
   Details[7].No =2;
   Details[7].Quantity =100;
   Details[8].No =9;
   Details[8].Quantity =22;
 
  // printf("%d %d\n" ,Details[5].No,Details[5].Quantity );
 
   int NoSaleCount[BOOKNO+1]={0};
   int i,j;
   int big;
   for (i = 0 ; i< Data ; i ++)
   {
      int k;
      k = Details[i].No;
      NoSaleCount[k] = NoSaleCount[k] + Details[i].Quantity;
     
   }
 
   for (j = 1 ; j < BOOKNO+1 ; j++)
   {
     printf("No:%d " , j );
     printf("總銷售 :%d \n" ,  NoSaleCount[j] );
     
   }  
 
   big =  Sale_top( NoSaleCount,BOOKNO+1 );
   printf("第一名總銷售 :%d \n" , big );
    system("PAUSE");
    return 0;
 
}

int Sale_top(int book[] , int count)
{
  int max=0;
  int i , top;

  for (i = 1 ; i < BOOKNO+1 ; i++)
   {
     if (book[i]>max)
     {
        max = book[i];          
     }              
 
   }
 
   top = max;
   return top;
}

2013年5月24日 星期五

C++ virtual function sample


#include<iostream>
using namespace std;


class Employee{
    private:
 
    protected:    
      int base_salary;  
    public:
         
           Employee()
           {
              //cout<< "hello!"<< endl;
              base_salary = 24000;    
           }        
           virtual void calculateSalary()=0;  
   
};

class Manager : public  Employee
{
    protected:
       int Manager_salary;    
    public:
         
       void calculateSalary()
       {
          Manager_salary = base_salary + 30000;
          cout << "Manager Salary :" << Manager_salary << endl;
       }        
   
   
   
};

class Staff : public   Employee
{
    protected:
       int Staff_salary;    
    public:
         
       void calculateSalary()
       {
          Staff_salary = base_salary + 10000;
          cout << "Staff Salary :" << Staff_salary << endl;
       }        
   
   
   
};



int main()
{

  Manager obj;
  obj.calculateSalary();
  Staff obj_1;
  obj_1.calculateSalary();


   
 system("PAUSE");
    return 0;  
}


C++ overloading Sample


C++ overloading
Def:  the same class , the same method, but different argument .
        function , friend function , constructor , member function can implement "Overloading".






#include<iostream>
using namespace std;



class Base{
      friend void Base_friend( Base &T ,int a);
      friend void Base_friend( Base &T ,int a , int b);
private:

    int x;      
       
public:
    Base()
    {
         cout << "Base Construster" << endl;
    }  
   
    Base (int _x)   //Base Constructor overloading
    {
         cout << "Base Constructor overloading" << endl;
         x=_x;
         cout << "x=" << x << endl;
       
    }

    void People()
    {
      cout<< "Base People" << endl;  
    }                
   
    void People(int a)   // member function overloading  
    {
       cout << "People" <<" "<< a << endl;
    }
   

     
     
};

//friend function
void Base_friend( Base &friend_obj ,int a)
{
  friend_obj.x = a;
  cout << "Base_friend x="<<friend_obj.x << endl;    
   
}  

//friend function
void Base_friend( Base &friend_obj ,int a , int b)
{
  friend_obj.x = a;
  cout << "Base_friend overloading x="<<friend_obj.x << endl;
  cout << "b =" << b << endl;
   
}
///////////////////////////////////////////////////////////////////
// function overloading
void People()
{
    cout<< "People" << endl;
}

void People (int x)
{
   cout << "People" << " "<< x << endl;
}  


////////////////////////////////////////////////////////////////////
int main()
{
People();
People (30);
Base obj;
obj.People();
obj.People(7);
Base obj1(2);
Base_friend(obj1 ,8);
Base_friend(obj1 ,8 , 10);
system("PAUSE");
    return 0;
}


2013年5月14日 星期二

C++ 使用物件的方法--五種


#include <iostream>
using namespace std;

class Count{

public:
  void setX(int a);    
  void show();    
private:
  int x;                          
};

void Count::setX(int a)      
{
     x =a;    
}

void Count::show()
{
 cout << x <<endl;  
}

  
int main(void)
{
   
   Count a;
   a.setX(2);
   a.show();
//////////////////////////////  
   Count b;
   Count *ptr = &b; //pointer
   b.setX(12);
   b.show();
//////////////////////////////
   Count c;
   Count &ref = c;
   ref.setX(45);
   ref.show();  
////////////////////////////////////////  
   Count *test = new Count;
   test->setX(44);
   test->show();
///////////////////////////////////////
   (*test).setX(443);
   (*test).show();
/////////////////////////////////////////////    
   
   
    system("PAUSE");
    return 0;
 
}

2013年1月12日 星期六

使用C++ vector 實作(堆疊)stack

使用C++ vector 實作(堆疊)stack

自己寫了一個小程式,使用vector的好處是他可以不用預設大小

你要在後面塞幾個數字都可以XD~

有人說vector 類似陣列,其實他只是看起來很像陣列

我倒覺得他比較像 linklist

以下是我的sample code:


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using std::vector;

void pop (vector<int> & );
void push (vector<int> & , int);
void outputVector (vector <int> &);
int main()
{
 vector < int > Stack;
 int input_num ;
 int check;
   
   
    while ( check !=3  )
    {
       cout << endl;
       cout << "PLEASE ENTER A NUMBER TO STACK" << endl;
       cout << "-----------------------------" << endl;
       cout << "(1) PUCH" << endl;
       cout << "(2) POP" << endl;
       cout << "(3) QUIT" << endl;
       cout << "-----------------------------" << endl;
       cout << endl;
       cin >> check;    
         switch (check)
         {
           case 1:    
              cout << "PUSH a integer :";
              cin >> input_num;
              push ( Stack ,  input_num);
              break;  
           case 2:  
              cout << "POP" << endl;
              pop (Stack);
              break;
           case 3:
              outputVector (Stack);
              break;      
          }    
   
    }          
 system("PAUSE");
 return 0;  
}

////////////////////////////////////////////////////////////
void push ( vector<int> &array , int input_num)
{
  cout << "push:" << input_num << endl;
  if ( input_num >= 0)
  {
  array.push_back( input_num );
  outputVector (array);
  }

}


//////////////////////////////////////////////////////////
void pop (vector<int> &array)
{
   cout << "pop"  ;
   array.pop_back();
   outputVector (array);
}


/////////////////////////////////////////////////////////
void outputVector (vector<int> &array)
{
  cout << "Stack is : " ;
  for (int i = 0 ; i < array.size() ;i++ )  
  {
      cout<< array[i] << " ";
  }
  cout << endl;
}

這是執行結果
選1  : push elementary
選2  : pop
選3  : quit