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


2012年11月13日 星期二

利用Makefile技巧,製作release version 或 debug version bin檔 (Using Makefile to compile release or debug bin file)

We can add some LABEL to mark some debug message that need to be show up in "debug version".
Oppositely , we can disable these debug message for "release version"with the
same source.


========= Makefile===============================

ifeq ($(DEBUG),y)
CFLAGS :=$(CFLAGS) -DDEBUG
endif

=========compile command =========================
 release mode:
 make clean; make

debug mode:
make clean; make DEBUG:=y

出貨給客戶,就編release mode給他,如此一來他們不會看到任何message從terminal
跑出來

若是自己要debug 相同的code就編debug mode,方便自己看message
就不需要進去code在修修改改了

=======Source code example==========================

      #ifdef DEBUG
       printf ( "debug message" );
      #endif

  if debug mode , "debug message" will show up , and release mode doesn't.


    為了整齊美觀,可以特地寫一個小function
    把debug message 寫在裡面
    就不會整篇code看到一大堆 #ifdef DEBUG



 
   for example:

   void debug_print_info_variable ( char *info , char *var)
{
    #ifdef DEBUG
    printf ( "%s , %s\n" , info , var );
    #endif
}









.....
....
.......
source code:
src_child_status =  g_match_info_fetch ( child_match_info , 6 ) debug_print_info_variable ("\nSTATUS= %s\n", src_child_status);
src_real_child_status = update_treeview_TransferDeviceStatus( src_child_status );
debug_print_info_variable ("\nREAL STATUS= %s\n", src_real_child_status);
       

二維陣列 行與列 Two-dimensional array row and column

國字的行跟列,其實我自己從國小放學要排隊伍回家,

就常常搞不清楚。

直的是行  還是橫的是行???

轉個方向,好像這樣又是行,那樣才是列。

古語不是有句話 橫看成嶺側成峰,遠近高低皆不同。

長大之後,學程式語言,又遇到 行  與  列

考試常常忘記

How to declare 2-dimensional array:

int array_name [row][column];

for example:

int M[2][3] = { {1,2,3},
                         {4,5,6} };


array layout:

M
(0,0)    1     |  (0,1)    2   |  (0,2)    3    |         ---->row 0

(1,0)   4      |  (1,1)    5   |   (1,2)   6    |         ---->row 1

row-major: (c/c++ default)
1,2,3,4,5,6

column-major:
1,4,2,5,3,6