对于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不是个对象?
下面是我的一个简单实例:
对象确定了,其他的就都好说,顶多是实现时候会遇到一些小问题。
我没怎么写过java 但是知道有个“神奇魔法”般的东西 new,“上帝说要有光” new light.
C 语言怎么办?
细节呢?自己实现
我们在使用的时候可以很轻松的调用
struct string* p_string = new_string();
来创建一个对于字符串操作的“类”,其实这里就是结构体。
我们同样可以很轻松的释放掉我们申请的内存
-----------------------
于此,给出改进的版本,希望路过高手交流指教。
关于stack的实现部分不在给出,和之前的实现版本一样。
如果您又想法改进的更好,希望不吝文字交流讨论!
最后,很感谢@凯旋冲锋,我们一起讨论,希望能探究C从淳朴的面向过程,到如果使之具有OOP的思想。
2014.11.27
很多人黑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
相关文章推荐
- C语言简单计算器(基于栈和逆波兰表达式)这几天忙着开学的事宜,一直没时间刷提,就把今天刚做好的课程设计——简易计算器贴在这给大家参考!(计算小数时要用0减正数并用括号括起来,这一点不足以后再改进)
- 一个基于 osip 库的 UAC 和 UAS 的代码整理
- 我的一个 C语言代码
- 以下是在网上看到的一个用C语言编写的木马源程序代码大家
- 趣味C—改进C语言编写简易计算器的思路和过程
- 基于自组网技术的智能无线抄表方案 现代生活中,水表、电表和煤气表的抄录和收费,是城市生活的一个大问题。人工入室抄表,扰民不说,还可能给居民带来不安全因素;对于各职能公司来说,派人上门抄表也无形中耗费了大量人力资源和时间。近年来,信息化社会在逐 步改变人们的
- 一个基于设计模式的面向对象的框架
- 一个正确的c语言链表代码(中间也有些bug)
- 请大家看一下一个c语言中的链表问题,下面的代码是有错误的!!请大家说出错误的原因,以及修改的方法!!!
- 一个用C语言连接PostgreSQL的测试代码
- 一个简单的AJAX实现,基于C#的ASP.Net,包括服务器端的程序代码
- 对于一个糟糕的设计来说,通过阅读C++代码你可以很容易地识别出它。
- 一个C语言的原代码,有8处错误,望大家帮我修改一下。
- 突发奇想,对于a+b问题的一个另类代码。。。
- 不到140行的C#代码编写一个简单的面向对象的数据库(OODB)
- 要复制就能用的 谢谢有急用 要求200行左右 有代码 在c语言的环境下编一个运动的图形程序
- 以下这段代码是一个内存缓存,对于数据较少时很有用
- 一个基于 osip 库的 UAC 和 UAS 的代码整理
- 一个基于jQuery ajax和.net httphandler 的超轻异步框架,千行代码完成。
- (C语言) 怎么用代码实现一个学生成绩管理系统