主程式
改用全域變數省結構空間。
http://damody.googlecode.com/files/example_C_use_OOP2.7z

#include <stdio.h>
#include <stdlib.h>
#include "calc_cell.h"
#include "calc_cell2.h"

int main()
{
    calc_cell2 myfun;
    calc_cell mycell = {0};
    myfun.m_cell.m_num = 0;
      init_calc_cell_funtion();
    printf("mycell data: %f\n", mycell.m_num);
    printf("mycell data add 10: %f\n", calc_cell_funtion.add(&mycell, 10));
    printf("mycell data mul 10: %f\n", calc_cell_funtion.mul(&mycell, 10));
    printf("mycell data div 5: %f\n", calc_cell_funtion.div(&mycell, 5));
    printf("mycell data sub 100: %f\n", calc_cell_funtion.sub(&mycell, 100));

    init_calc_cell2_function();
    printf("myfun data: %f\n", calc_cell2_function.get(&myfun));
    printf("myfun data add 10: %f\n", calc_cell2_function.add(&myfun, 10));
    printf("myfun data mul 10: %f\n", calc_cell2_function.mul(&myfun, 10));
    printf("myfun data div 5: %f\n", calc_cell2_function.div(&myfun, 5));
    printf("myfun data sub -10: %f\n", calc_cell2_function.sub(&myfun, -10));

    printf("myfun data log10: %f\n", calc_cell2_function.log10(&myfun));
    printf("myfun data log2: %f\n", calc_cell2_function.log2(&myfun));    
    printf("myfun data exp: %f\n", calc_cell2_function.exp(&myfun));
    system("pause");
}

calc_cell.h

#ifndef _CALC_CELL_H_
#define _CALC_CELL_H_
struct calc_cell
{
    double m_num;
};
typedef struct calc_cell calc_cell, *LPcalc_cell;
struct tag_calc_cell_funtion
{
    double (*add)(LPcalc_cell, double);
    double (*sub)(LPcalc_cell, double);
    double (*mul)(LPcalc_cell, double);
    double (*div)(LPcalc_cell, double);
};
extern struct tag_calc_cell_funtion calc_cell_funtion;

void init_calc_cell_funtion();
double calc_cell_add(LPcalc_cell cell, double number);
double calc_cell_sub(LPcalc_cell cell, double number);
double calc_cell_mul(LPcalc_cell cell, double number);
double calc_cell_div(LPcalc_cell cell, double number);
#endif

calc_cell2.h

#ifndef _CALC_CELL2_H_
#define _CALC_CELL2_H_

#include <math.h>
#include "calc_cell.h"

struct calc_cell2
{
    calc_cell m_cell;
};
typedef struct calc_cell2 calc_cell2, *LPcalc_cell2;
struct tag_calc_cell2_function
{
    double (*get)(LPcalc_cell2);
    void (*set)(LPcalc_cell2, double number);
    double (*add)(LPcalc_cell2, double);
    double (*sub)(LPcalc_cell2, double);
    double (*mul)(LPcalc_cell2, double);
    double (*div)(LPcalc_cell2, double);
    double (*log2)(LPcalc_cell2);
    double (*log10)(LPcalc_cell2);
    double (*exp)(LPcalc_cell2);
};
extern struct tag_calc_cell2_function calc_cell2_function;

void init_calc_cell2_function();
double calc_cell2_add(LPcalc_cell2 fun, double number);
double calc_cell2_sub(LPcalc_cell2 fun, double number);
double calc_cell2_mul(LPcalc_cell2 fun, double number);
double calc_cell2_div(LPcalc_cell2 fun, double number);
double calc_cell2_log2(LPcalc_cell2 fun);
double calc_cell2_log10(LPcalc_cell2 fun);
double calc_cell2_exp(LPcalc_cell2 fun);
double calc_cell2_get(LPcalc_cell2 fun);
void calc_cell2_set(LPcalc_cell2 fun, double number );
#endif

讓地獄深紅的天亮 發表在 痞客邦 PIXNET 留言(0) 人氣()