test;
#include<iostream>
#include<stdlib.h>
#include<cctype>
#include<vector>
#include<string>
using namespace std;
void main()
{
string inputString;
cin>>inputString;
unsigned int inputLen = inputString.size();
vector<int> vecNum;
vector<char> vecPunct;
//存储输入的算术表达式(操作数和操作符分开存储)
string::iterator iterInput = inputString.begin();
string::const_iterator first = inputString.begin();
string::const_iterator last = first;
while(iterInput != inputString.end())
{
if(ispunct(*iterInput))
{
string temp ;
last = iterInput-1;
temp.assign(first,last);
int num = atoi(temp.c_str());
vecNum.push_back(num);
temp ="";
while(ispunct(*iterInput))
{
if(*iterInput == '=')
{
vecPunct.push_back('=');
}
iterInput++;
}
char punctTemp = *(iterInput-1);
vecPunct.push_back(punctTemp);
first = iterInput+1;
}
else
{
iterInput++;
}
}
//开始执行计算过程
vector<char>::iterator iterPunct = vecPunct.begin();
vector<int>::iterator iterNum = vecNum.begin();
int result = *iterNum;
iterNum++;
while(iterPunct != vecPunct.end())
{
switch(*iterPunct)
{
case '+': result += (*iterNum); break;
case '-': result -= (*iterNum); break;
case '*': result *= (*iterNum); break;
case '/': result /= (*iterNum); break;
case '=': cout<<result<<endl; break;;
default: cout<<"算术表达式输入有误!"<<endl; break;;
}
}
}
HWY:
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<string.h>
#include "stdafx.h"
using namespace std;
int main()
{
int i=0;
float result=0;
int m;
char Char;
vector<char>charVector1;//存放 数字
vector<char>charVector2;//存放字符
vector<char>charVector;
while(cin>>Char)
charVector.push_back(Char);
vector<char>::iterator iter;
iter=charVector.begin();
if(*iter!='#')
{
while(*iter!=charVector.end())
{
while(isdigit(*iter))
{
charVector1.push_back(*iter);
charVector.pop_back(*iter);
}
//charVector2.push_back(charVector.begin());
m=atoi(charVector1);
while(!isdigit(*iter))
{
char b;
b=*iter;
charVector2.push_back(b);
charVector.pop_back(b);
}
vector<char>::iterator iter1;
iter1=charVector.end();
switch(*(--iter1))
{
case'+':
result=result+m;break;
case'-':
if(!i)
result=result-m;// 注意a【0】
else
result=a[i];break
case'*':
if(!i)
result=result*m;// 注意result初始值为0
else
result=a[i];break;
case'/':
if(!i)
result=result/m;// 注意a【0】
else
result=m;break;
case'=':
break;
}
}
}
else
cin>>"null";
char *str;
*str=itoa(result,char*String,10);
cout<<str;
return 0;
}