免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: captivated
打印 上一主题 下一主题

[其他] 好吧。代码确实是有优雅和不优雅的差别的。 [复制链接]

论坛徽章:
36
子鼠
日期:2013-08-28 22:23:29黄金圣斗士
日期:2015-12-01 11:37:51程序设计版块每日发帖之星
日期:2015-12-14 06:20:00CU十四周年纪念徽章
日期:2015-12-22 16:50:40IT运维版块每日发帖之星
日期:2016-01-25 06:20:0015-16赛季CBA联赛之深圳
日期:2016-01-27 10:31:172016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之福建
日期:2016-04-07 11:25:2215-16赛季CBA联赛之青岛
日期:2016-04-29 18:02:5915-16赛季CBA联赛之北控
日期:2016-06-20 17:38:50技术图书徽章
日期:2016-07-19 13:54:03程序设计版块每日发帖之星
日期:2016-08-21 06:20:00
11 [报告]
发表于 2013-10-29 15:40 |只看该作者
回复 8# captivated


    他叔叔这是忙神马去了啊

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
12 [报告]
发表于 2013-10-29 15:43 |只看该作者
回复 11# cokeboL


        学习怎么全方位地控制 CPU 中... 主要是多核 x86 和 ARM~

论坛徽章:
36
子鼠
日期:2013-08-28 22:23:29黄金圣斗士
日期:2015-12-01 11:37:51程序设计版块每日发帖之星
日期:2015-12-14 06:20:00CU十四周年纪念徽章
日期:2015-12-22 16:50:40IT运维版块每日发帖之星
日期:2016-01-25 06:20:0015-16赛季CBA联赛之深圳
日期:2016-01-27 10:31:172016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之福建
日期:2016-04-07 11:25:2215-16赛季CBA联赛之青岛
日期:2016-04-29 18:02:5915-16赛季CBA联赛之北控
日期:2016-06-20 17:38:50技术图书徽章
日期:2016-07-19 13:54:03程序设计版块每日发帖之星
日期:2016-08-21 06:20:00
13 [报告]
发表于 2013-10-29 17:14 |只看该作者
回复 12# captivated


  流弊                 

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
14 [报告]
发表于 2013-10-30 06:32 |只看该作者
  1. /* EXPRESSION CACULATOR */

  2. #include <ctype.h>
  3. #include <float.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>

  8. int nstr_trim(const char *s, int off, int lmt)
  9. {
  10.         int i;

  11.         for (i = off; i < lmt; ++i) {
  12.                 if (!isspace(s[i])) {
  13.                         break;
  14.                 }
  15.         }
  16.         return i;
  17. }

  18. int nstr_cmp(const char *s, int off, int lmt, const char *tar)
  19. {
  20.         return strncmp(s + off, tar, strlen(tar));
  21. }

  22. int get_oprand(const char *s, int *io_off, int lmt, int *o_offset, int *o_limit, double *o_value)
  23. {
  24.         int off, limit;
  25.         char *endptr;
  26.         double value;

  27.         off = *io_off;
  28.         off = nstr_trim(s, off, lmt);
  29.         value = strtod(s + off, &endptr);
  30.         if (value < -DBL_EPSILON || value > DBL_EPSILON) {
  31.                 if (value > -HUGE_VAL && value < HUGE_VAL) {
  32.                         limit = endptr - s;
  33.                         if (o_offset != NULL) {
  34.                                 *o_offset = off;
  35.                         }
  36.                         if (o_limit != NULL) {
  37.                                 *o_limit = limit;
  38.                         }
  39.                         if (o_value != NULL) {
  40.                                 *o_value = value;
  41.                         }
  42.                         off = limit;
  43.                         *io_off = off;
  44.                         return 0;
  45.                 }
  46.         }
  47.         return -1;
  48. }

  49. int get_operator(const char *s, int *io_off, int lmt, int *o_type)
  50. {
  51.         int off;

  52.         off = *io_off;
  53.         off = nstr_trim(s, off, lmt);
  54.         if (nstr_cmp(s, off, lmt, "+") == 0) {
  55.                 *o_type = 0;
  56.                 ++off;
  57.                 *io_off = off;
  58.                 return 0;
  59.         }else if (nstr_cmp(s, off, lmt, "-") == 0) {
  60.                 *o_type = 1;
  61.                 ++off;
  62.                 *io_off = off;
  63.                 return 0;
  64.         }else if (nstr_cmp(s, off, lmt, "*") == 0) {
  65.                 *o_type = 2;
  66.                 ++off;
  67.                 *io_off = off;
  68.                 return 0;
  69.         }else if (nstr_cmp(s, off, lmt, "/") == 0) {
  70.                 *o_type = 3;
  71.                 ++off;
  72.                 *io_off = off;
  73.                 return 0;
  74.         }
  75.         return -1;
  76. }

  77. int get_operator_prefix(const char *s, int *io_off, int lmt, int *o_type)
  78. {
  79.         int off;

  80.         off = *io_off;
  81.         off = nstr_trim(s, off, lmt);
  82.         if (nstr_cmp(s, off, lmt, "+") == 0) {
  83.                 *o_type = 0;
  84.                 ++off;
  85.                 *io_off = off;
  86.                 return 0;
  87.         }else if (nstr_cmp(s, off, lmt, "-") == 0) {
  88.                 *o_type = 1;
  89.                 ++off;
  90.                 *io_off = off;
  91.                 return 0;
  92.         }else if (nstr_cmp(s, off, lmt, "(") == 0) {
  93.                 *o_type = 4;
  94.                 ++off;
  95.                 *io_off = off;
  96.                 return 0;
  97.         }
  98.         return -1;
  99. }

  100. int get_operator_postfix(const char *s, int *io_off, int lmt, int *o_type)
  101. {
  102.         int off;
  103.        
  104.         off = *io_off;
  105.         off = nstr_trim(s, off, lmt);
  106.         if (nstr_cmp(s, off, lmt, ")") == 0) {
  107.                 *o_type = 5;
  108.                 ++off;
  109.                 *io_off = off;
  110.                 return 0;
  111.         }
  112.         return -1;
  113. }

  114. int cac_exp(int op, double opd1, double opd2, double *o_value)
  115. {
  116.         switch (op) {
  117.         case 0:
  118.                 *o_value = opd1 + opd2;
  119.                 return 0;
  120.         case 1:
  121.                 *o_value = opd1 - opd2;
  122.                 return 0;
  123.         case 2:
  124.                 *o_value = opd1 * opd2;
  125.                 return 0;
  126.         case 3:
  127.                 *o_value = opd1 / opd2;
  128.                 return 0;
  129.         default:
  130.                 return -1;
  131.         }
  132. }

  133. int try_operator(const char *s, int off, int lmt, int *o_type)
  134. {
  135.         return get_operator(s, &off, lmt, o_type);
  136. }

  137. int compare_op(int op1, int op2)
  138. {
  139.         op1 >>= 1;
  140.         op2 >>= 1;
  141.         if (op1 < op2) {
  142.                 return -1;
  143.         }else if (op1 > op2) {
  144.                 return 1;
  145.         }
  146.         return 0;
  147. }

  148. /*
  149. * load_expression returns:
  150. *
  151. * 0  ok, o_value store expression's value
  152. * 1  oh no, lower priviledge
  153. * -1 error, gramer error
  154. */

  155. int load_expression(const char *s, int *io_off, int lmt, int op, double *io_opd)
  156. {
  157.         int off;
  158.         int type, op2;
  159.         double value;
  160.         int load_operand(const char *s, int *io_off, int lmt, double *o_value);
  161.        

  162.         off = *io_off;
  163.         if (get_operator(s, &off, lmt, &type) == 0) {
  164.                 if (load_operand(s, &off, lmt, &value) == 0) {
  165.                         if (try_operator(s, off, lmt, &op2) == 0) {
  166.                                 if (compare_op(op2, type) == 1) {
  167.                                         if (load_expression(s, &off, lmt, type, &value) == 0) {
  168.                                                 cac_exp(type, *io_opd, value, io_opd);
  169.                                                 if (load_expression(s, &off, lmt, op, io_opd) == 0) {
  170.                                                         *io_off = off;
  171.                                                         return 0;
  172.                                                 }
  173.                                         }
  174.                                 }else {
  175.                                         cac_exp(type, *io_opd, value, io_opd);
  176.                                         if (load_expression(s, &off, lmt, op, io_opd) == 0) {
  177.                                                 *io_off = off;
  178.                                                 return 0;
  179.                                         }
  180.                                 }
  181.                         }else {
  182.                                 cac_exp(type, *io_opd, value, io_opd);
  183.                                 *io_off = off;
  184.                                 return 0;
  185.                         }
  186.                 }
  187.         }else {
  188.                 *io_off = off;
  189.                 return 0;
  190.         }
  191.         return -1;
  192. }

  193. int load_operand(const char *s, int *io_off, int lmt, double *o_value)
  194. {
  195.         int off;
  196.         int offset, limit;
  197.         int type;
  198.         double value;

  199.         off = *io_off;
  200.         if (get_operator_prefix(s, &off, lmt, &type) == 0) {
  201.                 switch (type) {
  202.                 case 0:
  203.                         if (load_operand(s, &off, lmt, &value) == 0) {
  204.                                 *o_value = value;
  205.                                 *io_off = off;
  206.                                 return 0;
  207.                         }
  208.                         break;
  209.                 case 1:
  210.                         if (load_operand(s, &off, lmt, &value) == 0) {
  211.                                 *o_value = -value;
  212.                                 *io_off = off;
  213.                                 return 0;
  214.                         }
  215.                         break;
  216.                 case 4:
  217.                         if (load_operand(s, &off, lmt, &value) == 0) {
  218.                                 if (load_expression(s, &off, lmt, -1, &value) == 0) {
  219.                                         if (get_operator_postfix(s, &off, lmt, &type) == 0) {
  220.                                                 if (type == 5) {
  221.                                                         *o_value = value;
  222.                                                         *io_off = off;
  223.                                                         return 0;
  224.                                                 }
  225.                                         }
  226.                                 }
  227.                         }
  228.                         break;
  229.                 default: /* ERROR: invalid operator */
  230.                         break;
  231.                 }
  232.         }else if (get_oprand(s, &off, lmt, &offset, &limit, &value) == 0) {
  233.                 *o_value = value;
  234.                 *io_off = off;
  235.                 return 0;
  236.         }
  237.         return -1;
  238. }

  239. int caculator(const char *s, double *o_value)
  240. {
  241.         int off, lmt;
  242.         int type;
  243.         double value;

  244.         off = 0;
  245.         lmt = strlen(s);
  246.         type = -1;
  247.         if (load_operand(s, &off, lmt, &value) == 0) {
  248.                 if (load_expression(s, &off, lmt, type, &value) == 0) {
  249.                         *o_value = value;
  250.                         return 0;
  251.                 }
  252.         }
  253.         return -1;
  254. }

  255. int main(void)
  256. {
  257.         char exp_buf[256];
  258.         double value;

  259.         puts("I am an expression caculator.");
  260.         while (puts("Please input an expression: Ctrl-Z to exit")
  261.                 , fgets(exp_buf, sizeof(exp_buf), stdin) != NULL) {
  262.                 if (caculator(exp_buf, &value) == 0) {
  263.                         printf("= %g\n", value);
  264.                 }else {
  265.                         puts("Oh, It's not a valid expression, Input again.");
  266.                 }
  267.         }
  268.         return 0;
  269. }
复制代码

论坛徽章:
6
酉鸡
日期:2013-10-18 21:53:40射手座
日期:2013-10-26 19:40:18技术图书徽章
日期:2013-10-26 20:39:15巳蛇
日期:2013-10-26 22:13:11狮子座
日期:2013-11-02 16:45:58水瓶座
日期:2013-11-04 07:42:19
15 [报告]
发表于 2013-10-30 07:53 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
59
2015年亚洲杯之约旦
日期:2015-01-27 21:27:392015年亚洲杯之日本
日期:2015-02-06 22:09:41拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015元宵节徽章
日期:2015-03-06 15:50:392015年亚洲杯之阿联酋
日期:2015-03-19 17:39:302015年亚洲杯之中国
日期:2015-03-23 18:52:23巳蛇
日期:2014-12-14 22:44:03双子座
日期:2014-12-10 21:39:16处女座
日期:2014-12-02 08:03:17天蝎座
日期:2014-07-21 19:08:47
16 [报告]
发表于 2013-10-30 08:19 |只看该作者
回复 12# captivated


    怪熟熟好。。。。


PS: 学这个米用的, 用到时再学不迟, 这世上CPU何其多。。。。
如果以前没学过, 先学一个倒是有必要。

论坛徽章:
1
双子座
日期:2013-11-06 17:18:01
17 [报告]
发表于 2013-10-30 09:22 |只看该作者
captivated,好久不见,你推荐的修养一书,我一直在翻,好几遍了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP