計算四則運算表達式

題目描述
對於一個不存在括號的表達式進行計算
輸入描述:
存在多種數據,每組數據一行,表達式不存在空格
輸出描述:
輸出結果
示例1
輸入
6/2+3+3*4
輸出
18

#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<vector>
#include<list>
#include<iomanip>
#include<sstream>
using namespace std;

#define N 1000
#define inf -99999

int main(){
double a[N];

double t;
while(cin>>t){
	double fin = 0;
	memset(a,0,sizeof(a));
	char ch;
	a[1] = t;
	int i = 1;
	while(cin>>ch){
		cin>>t;
		if(ch=='+')
		  a[++i] = t;
		  else if(ch=='-')
		   a[++i] = -t;
		  else if(ch=='*')
		  a[i] = a[i]*t;
		  else{
		  	a[i] = a[i]/t;	
		  }
	}
	for(int j =1;j<=i;j++){
	
		 fin = fin +a[j];
	}
	 cout<<fin<<endl;
}
 return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章