Chinaunix首页 | 论坛 | 博客
  • 博客访问: 688928
  • 博文数量: 156
  • 博客积分: 3402
  • 博客等级: 中校
  • 技术积分: 1639
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-13 14:06
个人简介

业余编程爱好者

文章分类

全部博文(156)

文章存档

2014年(1)

2013年(13)

2012年(46)

2011年(38)

2010年(58)

分类: LINUX

2012-03-26 12:22:09

cforth.c代码更新,解决了多余的每一个空格键会在DS栈留下一个0的问题。代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. int ds[256],rs[256];
  4. int *dsp=ds,*rsp=rs;
  5. int flag;
  6. #include "code_words.h"
  7. #include "colon_words.h"

  8. char inputBuff[1024];
  9. char strBuff[30];

  10. int main()
  11. {
  12.     printf("Cforth 0.1.0, Copyright (C) 2008-2012 Free Software Foundation, Inc.\n");
  13.     printf("Cforth comes with ABSOLUTELY NO WARRANTY; for details type `license'\n");
  14.     printf("Type `bye' to exit\n");
  15.     while (1){
  16.         int i=0;
  17.         int j=0;
  18.         gets(inputBuff);
  19.         while(inputBuff[j] != '\0'){
  20.             if(inputBuff[j]!= ' '){
  21.                 strBuff[i] = inputBuff[j];
  22.                 i++;
  23.                 j++;
  24.             }
  25.             else {
  26.                 strBuff[i]='\0';
  27.                 if ( !strcmp(".s",strBuff) )        showDS();
  28.                 else if( !strcmp("dup",strBuff) )    dup();
  29.                 else if( !strcmp(".",strBuff) )     showtopDS();
  30.                 else if( !strcmp(">r",strBuff) )    tor();
  31.                 else if( !strcmp("r>",strBuff) )     rto();
  32.                 else if( !strcmp("drop",strBuff) )     drop();
  33.                 else if( !strcmp("+",strBuff) )     add();
  34.                 else if( !strcmp("-",strBuff) )     sub();
  35.                 else if( !strcmp("*",strBuff) )     mul();
  36.                 else if( !strcmp("/d",strBuff) )     ddiv();
  37.                 else if( !strcmp("/",strBuff) )     div();
  38.                 else if( !strcmp("%",strBuff) )     mod();
  39.                 else if( isNum() )                    chgNum();
  40.                 else if( !strcmp("say",strBuff) )     say();
  41.                 else if( !strcmp("bye",strBuff) )     return 0;
  42.                 else printf("\n[%s]?\n",strBuff);
  43.          
  44.                 for(i=0;i<30;i++) strBuff[i]='\0';
  45.                 i=0;
  46.                 j++;
  47.                }
  48.           }
  49.           showOK();
  50.       }
  51.     return 0;
  52. }

  53. int isNum()
  54. {
  55.     char *str=strBuff;
  56.     while(*str) {
  57.         if(*str<'0' || *str>'9') return 0;
  58.         str++;
  59.     }
  60.     return 1;
  61. }

  62. int chgNum()
  63. {
  64.     char *str=strBuff;
  65.     int sum=0;
  66.     while(*str) {
  67.         sum=10*sum+(int)(*str-'0');
  68.         str++;
  69.     }
  70.     //如果这个数字字符串只有一个'\0',则直接返回0。
  71.     //用来解决多余的空格键误操作DS的BUG
  72.     if(str==&strBuff[0]) return 0;
  73.     push(sum);
  74.     return 0;
  75. }

  76. int showOK()
  77. {
  78.     printf("OK\n");
  79.     return 0;
  80. }

  81. int say()
  82. {
  83.     gets(strBuff);
  84.     printf("cforth: [%s]\n",strBuff);
  85.     return 0;
  86. }


阅读(954) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~