⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.c

📁 一个算计器的实现源码., 可以进行一般的运算
💻 C
字号:
/*
 * function.c       
 * Original Author: zhough@ruijie.com.cn, 2007-8-15    
 *   文件功能的简要说明
 * 该文件的功能是:该文件包含了四个函数,分别是:
 *int getch(void); 取一个字符(可能是压回的字符)
 *void ungetch(int); 把字符压回到输入中。
 *int getop(char []); 获取下一个运算符或数值操作数
 *void push(double f);把f压入到值栈中
 *double pop(void);弹出并返回栈顶值
*/


#include <stdio.h>
#include <stdlib.h>  
#include <ctype.h>
#include "function.h"


#define BUFSIZE 100
#define NUMBER '0'      /* signal that a number was found */
#define MAXVAL 100 /* maximum depth of val stack */

 
int flag=0; 
int bufp = 0;
int sp = 0; /* next free stack position */

char buf[BUFSIZE]; /* buffer for ungetch */
double val[MAXVAL]; /* value stack */
   
   
/* push: push f onto value stack */
void push(double f)
{
   if (sp < MAXVAL)
     val[sp++] = f;
   else
    printf("error: stack full, can't push %g\n", f);
}


/* pop: pop and return top value from stack */
double pop(void)
{
		  if (sp > 0)
		    return val[--sp];
		  else
		 {
		   printf("error: stack empty\n");
		  return 0.0;
		 }
}


void ungetch(int c)    /* push character back on input */
{
		if (bufp >= BUFSIZE)
		  printf("ungetch: too many characters\n");
		else
		  buf[bufp++] = c;
}


int getch(void) /* get a (possibly pushed-back) character */
{
  return (bufp > 0) ? buf[--bufp] : getchar();
}


/* getop: get next character or numeric operand */
int getop(char s[])
{
		  int i, c;
		  /*删除表达式前面的‘ ’ 和’/t‘字符*/
		  while ((s[0] = c = getch()) == ' ' || c == '\t')
		              ;
		 s[1] = '\0';
		 	 
		  /*考虑有负数存在的情况*/
		   if(c=='-')
		    {
				    	  	/*判断它的下一个是否是数字*/
				    	  	  c=getch();
				    	    /* isdigit()功能:判断字符c是否为数字 说明:当c为数字0-9时,返回非零值,否则返回零*/    
		    	  	 if (isdigit(c) )
					    	  	  {
				    	  	    	  i = 0;
				    	  	    	  s[++i]=c;
					               while (isdigit(s[++i] = c = getch()))
					                    ;
					                  
					                  /*is a dot */
					              if (c == '.') /* collect fraction part */    
					                while (isdigit(s[++i] = c = getch()))
					                      ;
					               s[i] = '\0';
					               if (c != EOF)
					               ungetch( c);
					              return NUMBER;
					    	  	  }
				    	 	else
				    	 	{
				    	 	
				    	 		 printf("this is no a number\n");
				    	 		 
				    	 	  	return '-'  ; /* not a number */
				    	 	}
		   }
		   
			 if (!isdigit(c) && c != '.')
			     return c; /* not a number */	     
			  i = 0;
			  if (isdigit(c)) /* collect integer part */
			/* isdigit()功能:判断字符c是否为数字 说明:当c为数字0-9时,返回非零值,否则返回零*/
			  while (isdigit(s[++i] = c = getch()))
			         ;
			  if (c == '.') /* collect fraction part */
			     while (isdigit(s[++i] = c = getch()))
			             ;
						 s[i] = '\0';
						 if (c != EOF)
						ungetch( c);
			
			   return NUMBER;
}








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -