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

📄 main.c

📁 一个算计器的实现源码., 可以进行一般的运算
💻 C
字号:

/*
 * calculator.c   
 * Original Author: zhough@ruijie.com.cn, 2007-8-15 
 *   文件功能的简要说明
 * 该文件的功能是:编写一个具有加'+',减'-',乘'*',除'/',求模'%'等功能的计算器程序。
 *在计算器的设计中使用了逆波兰表示发替代普通的中缀表示法。
*/



#include <stdio.h>
#include <stdlib.h>       /* for atof() */
#include"function.h"
#include <math.h>

#define MAXOP 100      /* max size of operand or operator */
#define NUMBER '0'      /* signal that a number was found */

main()
{
int type;
double op2;
char s[MAXOP];
char reverse[MAXOP]; 
int j=0 ;
float s1;

double temp1,temp2;


while ((type = getop(s)) != EOF) 
	{
		
     switch (type) 
		 	{
         case NUMBER:
         	/*如果字符串中含有‘-’ , 则将它转化为对应的负数值*/
						if (s[0]=='-')
						{
								j=0;
								while( s[++j]!='\0')
									reverse[j-1]=s[j];
								reverse[j-1]='\0' ;						
								s1=0-atof(reverse);
								push(s1);
								printf("s=%f\n", s1);
								printf("this is a <0 number\n"); 
							
						}
						else	
						    {
						    	printf("this is a >0 number\n"); 
								  push(atof(s));
							}
						break;     
         case '+':
              push(pop() + pop());
              break;
         case '*':
              push(pop() * pop());
              break;
         case '-':
              op2 = pop();                                                     
              push(pop() - op2);
               
              break;
         case '/':
	            op2 = pop();
	            if (op2 != 0.0)
	              push(pop() / op2);
	            else
	              printf("error: zero divisor\n");
	              break;
         case '%':  /*求模运算*/
              op2=pop();
              if(op2!=0.0)
              /*如果操作数中含有负数,则将它们都转换为正数,然后在求它们的模*/
                push(fmod( abs( pop()), abs(op2)));  
              else
                printf("error: zero divisor\n");
             break;
        case '\n':
             printf("\t%.8g\n", pop());
             break;
        default:
	           printf("error: unknown command %s\n", s);
	           break;
               }
    }
         return 0;
}


















⌨️ 快捷键说明

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