您的位置:首页 > 编程语言 > C语言/C++

对于C语言面向对象封装性的改进尝试——基于一个简易计算器的代码

2014-11-27 01:51 726 查看
    C语言本身是为底层硬件服务的语言,操作系统才是它的天下——唯C独霸!威武我大C大法

很多人黑C是面向过程的,没有java C艹 里面的class关键字,于是C就不能OOP?

OOP是一种思想,为了更建议的提供用户接口,并保护数据的私有性。对象= 属性+行为.

@凯旋冲锋 这里有一篇专门探讨C语言以及面向对象的问题的博文。推荐。

 http://blog.csdn.net/kxcfzyk/article/details/21331945

------------------------------------------------

下面,我们开始对之前一段很单纯的C代码进行改进,在改进的过程中,探究C语言OOP的可能性。

这里是我们等待改进的代码:http://blog.csdn.net/cinmyheart/article/details/41518775

int computer(void)  

{  

    char string[ARRAYSIZE] = {0};

我们会发现,这里我们仅仅是把标准输入流输入的字符串储存在一个定长的数组里面,数组长度由宏定义ARRAYSIZE决定。这是常用的技巧。但是如果我们把输入的字符串看做一个对象呢?

对象 = 属性+行为。

我们可以定义一个结构体,然后把string封装进去,接着把对于string操作的函数统统封装进去,把string的长度大小各种信息封装进去,这样是不是struct string结构体很“丰富”,包含了,你可能对string的所有操作。

这个时候还有人说string不是个对象?

下面是我的一个简单实例:

struct string
{
/*
** The size of this object
*/
int size;

/*
** The length of string which is in this object
*/
int length;

/*
**	Method for this object to read meassage from
** Input--stream.
*/
struct string* (*read)(struct string*p_string);

/*
**	Method for release thid object
*/
void (*release)(struct string* p_string);

char   str[0];
};


对象确定了,其他的就都好说,顶多是实现时候会遇到一些小问题。

我没怎么写过java 但是知道有个“神奇魔法”般的东西 new,“上帝说要有光” new light.

C 语言怎么办?

#include "stack.h"

struct string* new_string(void);


细节呢?自己实现

#include "stack.h"

struct string* new_string(void)
{
struct string* p_string = NULL;

p_string = (struct string*)malloc(BUFSIZ);

if(!p_string)
{
printf("malloc failed in "
"function %s()\n",__FUNCTION__);
return NULL;
}

p_string->read    = str_read;
p_string->release = del_string;
p_string->size    = BUFSIZ;
p_string->length  = BUFSIZ - sizeof(struct string);

return p_string;
}


我们在使用的时候可以很轻松的调用

struct string* p_string = new_string();

来创建一个对于字符串操作的“类”,其实这里就是结构体。

我们同样可以很轻松的释放掉我们申请的内存

p_string->release(p_string);


-----------------------

于此,给出改进的版本,希望路过高手交流指教。

/*********************************************************
Code writer : EOF
Code file   : stack.h
Code date   : 2014.11.27
Email	    : jasonleaster@gmail.com

Code description:

Here is a implementation of a naive computer
It based on two stack scheme which was found by Dijkstra.

*********************************************************/
#ifndef _STACK_H
#define _STACK_H 1

#define EMPTY     0
#define NON_EMPTY 1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

struct node
{
int data;
struct node* next;
};

struct string
{
/*
** The size of this object
*/
int size;

/*
** The length of string which is in this object
*/
int length;

/*
**	Method for this object to read meassage from
** Input--stream.
*/
struct string* (*read)(struct string*p_string);

/*
**	Method for release thid object
*/
void (*release)(struct string* p_string);

char   str[0];
};

int computer(void);
struct string* new_string(void);
struct string* str_read(struct string* p_string);
void           del_string(struct string* p_string);

struct node* creat_stack(void);
int push_stack(struct node** pp_top,int number);
int is_empty(struct node* p_node);
void release_stack(struct node* p_top);

#endif


/*************************************************************
code writer : EOF
Code file   : computer_test.c
code date   : 2014.03.03
e-mail      : jasonleaster@gmail.com

code purpose :
This is just a test code for "Lisa" that
I created. If there is something wrong with my code, please
touche me by e-mail.

#ATTENTION#
You must input with '(' and ')' for each operator,
otherwise you will be puzzle about the result of output.

Usage:
(1+2) would output 3
(1+((2*3)+1))  == 8

If there is something wrong with my code, please touch me by e-mail.

******************************************************************/
#include "stack.h"

int main()
{
int number = 0;
printf("Hello ! This is a naive computer."
"Her name is 'Lisa' :)\n");

number = computer();

printf("The result of your inputed :%d\n",number);

return 0;
}


关于stack的实现部分不在给出,和之前的实现版本一样。

如果您又想法改进的更好,希望不吝文字交流讨论!

最后,很感谢@凯旋冲锋,我们一起讨论,希望能探究C从淳朴的面向过程,到如果使之具有OOP的思想。

                                   2014.11.27

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐