输入一个合法不含括号的常整数表达式字符串:
2+3*4+5%2
计算这个结果
例
输入:
2+3*4+5%2
输出:
15
注:所有输入均为半角字符,字符串中无空格。仅仅包含+ - * / % 五个运算符号,且都是整数。
http://blog.csdn.net/zwt0112/article/details/54562469
参考这个例子,稍微修改,加上%就可以了。
// expcalc.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/*****************************************************
File name:calculator
Author:??? Version:1.0 Date: 2016-6-12
Description:??????????,???,?,?,?,????????
Calls : 1.insert_operand () ????
2.insert_oper() ?????
3.compare() ????????
4.deal_date() ??????
*****************************************************/
#include <stdio.h> /*?????*/
#include <stdlib.h>
#define MAX_SIZE 1024 /*????*/
int insert_operand(int *operand , int * top_num ,int num) /*???????*/
{
(*top_num) ++;
operand[*top_num] = num; /*????*/
return 0; /*????*/
}
int insert_oper (char * oper , int *top_oper , char ch) /*????????*/
{
(*top_oper)++;
oper[*top_oper] = ch; /*?????*/
return 0; /*????*/
}
int compare(char *oper , int *top_oper , char ch) /*????????*/
{
if((oper[*top_oper] == '-' || oper[*top_oper] == '+') /*???????????????????*/
&& (ch == '*' || ch == '/' || ch == '%'))
{
return 0; /*??????*/
}
else if(*top_oper == -1 || ch == '('
|| (oper[*top_oper] == '(' && ch != ')')) /*??????????;???? ????'('*/
{
return 0; /*??????*/
}
else if (oper[*top_oper] =='(' && ch == ')' ) /*???????????????*/
{
(*top_oper)--;
return 1; /*?()????*/
}
else
{
return -1; /*????????*/
}
}
int deal_date(int *operand ,char *oper ,int *top_num, int *top_oper) /*??????*/
{
int num_1 = operand[*top_num]; /*??????????*/
int num_2 = operand[*top_num - 1];
int value = 0;
if(oper[*top_oper] == '+') /*????*/
{
value = num_1 + num_2;
}
else if(oper[*top_oper] == '-') /*????*/
{
value = num_2 - num_1;
}
else if(oper[*top_oper] == '*') /*????*/
{
value = num_2 * num_1;
}
else if(oper[*top_oper] == '/') /*????*/
{
value = num_2 / num_1;
}
else if(oper[*top_oper] == '%') /*????*/
{
value = num_2 % num_1;
}
(*top_num) --; /*?????????*/
operand[*top_num] = value; /*??????????*/
(*top_oper) --; /*??????????*/
return value;
}
int main()
{
int operand[MAX_SIZE] = {0}; /*???,???*/
int top_num = -1;
char oper[MAX_SIZE] = {0}; /*????,???*/
int top_oper = -1;
char *str = (char *) malloc (sizeof(char) * 100); /*?????(??=)*/
scanf("%s",str);
char* temp;
char dest[MAX_SIZE];
int num = 0;
int i = 0;
while(*str != '\0')
{
temp = dest;
while(*str >= '0' && *str <= '9') /*???????*/
{
*temp = *str;
str ++;
temp ++;
} /*??????*/
if(*str != '(' && *(temp - 1) != '\0') /*???????'('*/
{
*temp = '\0';
num = atoi(dest); /*????????*/
insert_operand(operand, &top_num,num); /*????????*/
}
while(1)
{
i = compare(oper,&top_oper,*str); /*????????*/
if(i == 0)
{
insert_oper(oper,&top_oper,*str); /*?????*/
break;
}
else if(i == 1) /*?????????????*/
{
str++;
}
else if(i == -1) /*??????*/
{
deal_date(operand,oper,&top_num,&top_oper);
}
}
str ++; /*??????????*/
}
printf("num = %d\n",operand[0]); /*????*/
return 0; /*????*/
}
var a = new System.Data.DataTable().Compute(表达式, "");试试看看这个可不可以啊