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

📄 asscal.h

📁 这个也是一个计算器用VISUAL C++设计
💻 H
字号:
#ifndef AssCal_h_
#define AssCal_h_

#include <iostream>
#include <stack>
#include <string.h>
//#include "stdafx.h"



#include <stdio.h>
#include <stdlib.h>
using namespace std;

int mat[10][10]=
{
	{-1,-1,1,1,1,-1,-1},
	{-1,-1,1,1,1,-1,-1},
	{-1,-1,-1,-1,1,-1,-1},
	{-1,-1,-1,-1,1,-1,-1},
	{1,1,1,1,1,0,2},
	{-1,-1,-1,-1,2,-1,-1},
	{1,1,1,1,1,2,0},
};

char optr[10]="+-*/()#";

int find_pos(char ch)
{
	for(int i=0;i<=6;i++)
	{
		if(optr[i]==ch)return i;
	}
	return -1;
}

double operation(double a,char op,double b)
{
//	printf("%lf %c %lf\n",a,op,b);
	switch(op)
	{
	case '+':
		return a+b;
	case '-':
		return a-b;
	case '*':
		return a*b;
	case '/':
		return a/b;
	}
	return 0;
}

double calculate(CString instr)
{
	char c,ch;
	char str[100];
	int i,p=0,fh=1,g=1;
	double k=0;
	stack<double>fstack;
	stack<char>chstack;
	chstack.push('#');
	instr+='#';
	c=instr[p++];		
	while(c!='#'|| chstack.top()!='#')
	{
		int pos=find_pos(c);
		if(pos==-1)
		{
				for(i=0;pos==-1;i++)
				{    
					 str[i]=c;
					 c=instr[p++];
					 pos=find_pos(c);
				}
				 double temp=atof(str);
 				 for(int i=0;i<100;i++)
				 {
                      str[i]='\0';
				 } 
                 double n=fmod(k,2);
				 if(g==0&&n==1)
				 {
					 temp=-temp;
					 g=1;
				 }
				 fstack.push(temp);
				 fh=0;
		}
		else
		{
			    if(pos==4)
				{
					fh=1;
				}
				if(pos==1&&fh==1)
				{
                    g=0;c=instr[p++];k++;continue;
				}
				ch=chstack.top();
				int p1=find_pos(ch);
				int p2=find_pos(c);
				switch(mat[p1][p2])
				{
				case 1:
					chstack.push(c);c=instr[p++];
					break;
				case 0:
					chstack.pop();c=instr[p++];
					break;
				case -1:
					double a,b;
					b=fstack.top();
					fstack.pop();
					a=fstack.top();
					fstack.pop();
					ch=chstack.top();
					chstack.pop();
					fstack.push(operation(a,ch,b));
					break;
				}
		}
	}	
	return fstack.top();
}

#endif //AssCal_h_

⌨️ 快捷键说明

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