user-0 提问: 高分   求计算机源代码 

回复:
user: user-1
content: 我也来一个，通过链表方式处理 typedef struct _Operation{ 	char	opt;					//操作符 	int		a;						//操作数1 	int		b;						//操作数2 	struct _Operation* previous;	//前一个操作 	struct _Operation* next;		//后一个操作 } Operation;  const char* expression = "1*180+2*3-40/2*5-63/3*4";  void main(){ 	Operation* current = NULL; 	Operation* next = NULL; 	Operation* first = NULL;  	char opt; 	const char* priority = "*/+-"; 	int i,pidx,result,finish=0; 	char* str = (char*)malloc(strlen(expression)+1); 	strcpy(str,expression);  	for(i=strlen(str);i>0;){ 		i--;  		opt = str[i]; 		if(opt=='+' || opt=='-' || opt=='*' || opt=='/'){ 			current = (Operation*)malloc(sizeof(Operation)); 			current->b = atoi(str+i+1);		//操作符 			current->opt = opt; 			current->previous = NULL; 			current->next = next; 			if(next){ 				next->a = current->b; 				next->previous = current; 			}  			str[i] = 0; 			next = current; 		} 	} 	if(current){ 		current->a = atoi(str); 		first = current;			//至此，所有操作均保存在链表中  		for(pidx=0;pidx<=2;pidx+=2){ 			current = first; 			while(current){ 				next=current->next; 				//优先处理*/再处理+- 				if(current->opt == priority[pidx] || current->opt == priority[pidx+1]){ 					result = current->opt == '*' ? current->a * current->b : (current->opt=='/' ? current->a / current->b : (current->opt=='+' ? current->a + current->b : current->a - current->b)); 					if(current->previous){ 						current->previous->b = result; 						current->previous->next = current->next; 					} 					if(current->next){ 						current->next->a = result; 						current->next->previous = current->previous; 					} 					if(first == current) 						first = next; 					free(current);		//处理一个释放一个 				}  				current = next;			//处理下一操作 			} 		} 	}else{ 		//无运算符，直接返回结果 		result = atoi(str); 	}  	printf("%s=%d",expression,result); 	free(str); }

user: user-2
content: 看http://jingyan.baidu.com/article/22fe7ced67cd443002617f98.html  每步有详细讲解，代码均有详细注释

user: user-3
content: 给个简单, 无错误处理 int mixop(int left) {     int right;     char op;     if(scanf("%c",&op)!=1||(op=='\n')) return left;     scanf("%d",&right);     switch(op){         case '+':return left+mixop(right);         case '-':return left-mixop(right);         case '*':return mixop(left*right);         case '/':return mixop(left/right);         default: return left;     } }  int main() {     int left;     while(~scanf("%d",&left)){         printf("%d\n",mixop(left));     } }

user: user-4
content: 需要三个步骤： 1. 解析字符串表达式为中缀表达式； 2. 将中缀表达式转换为后缀表达式：参考【调度场算法】（https://zh.wikipedia.org/wiki/%E8%B0%83%E5%BA%A6%E5%9C%BA%E7%AE%97%E6%B3%95）； 3. 计算后缀表达式。  试着写了个代码，没仔细检查，也没做优化，可以参考一下：  /*  * calc.c  *   * Copyright 2015 Che Hongwei <htc.chehw@gmail.com>  *   * This program is free software; you can redistribute it and/or modify  * it under the terms of the GNU General Public License as published by  * the Free Software Foundation; either version 2 of the License, or  * (at your option) any later version.  *   * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *   * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,  * MA 02110-1301, USA.  *   *   */   #include <stdio.h> #include <stdlib.h> #include <string.h>  #define BOOL int #define FALSE (0) #define TRUE (!FALSE)  enum ERRCODE { 	ERRCODE_INVALID = -1, 	ERRCODE_SUCCESS = 0, 	ERRCODE_FAILED = 1 };  enum NODE_TYPE { 	NODE_TYPE_invalid = -1, 	NODE_TYPE_val = 0, 	NODE_TYPE_op = 1 };   struct Node { 	int nodeType; 	union 	{ 		int value; 		char op; 	}; //	Node * next; };   #define MAX_NODES (100)    int shunting_yard(const char * in_str, struct Node out_queue[MAX_NODES]); double compute(struct Node queue[MAX_NODES], int count);   int main(int argc, char **argv) { 	const char * expr = "3+4*2/2-1*8"; 	struct Node queue[MAX_NODES]; 	int count; 	double result; 	 	memset(queue, 0, sizeof(queue)); 	 	printf("expr: %s\n", expr); 	 	count = shunting_yard(expr, queue); 	 	result = compute(queue, count); 	printf("result = %f\n", result); 	 	return 0; }  static BOOL is_op(char c) { 	switch(c) 	{ 		case '+': case '-': case '*': case '/': return TRUE; 		default: break; 	} 	return FALSE; }  static int op_prior(char op1, char op2) { 	if(op1 == '*' || op1 == '/') 	{ 		if(op2 == '+' || op2 == '-') return 1; 		else return 0; 	}else 	{ 		if(op2 == '+' || op2 == '-') return 0; 		else return -1; 	} }  /*************************  * int shunting_yard(const char * in_str, Node * out_queue)  * return value: 0 == error; >0 == element count; -1 = invalid parameters  **************************/ int shunting_yard(const char * in_str, struct Node out_queue[MAX_NODES]) { 	if(NULL == in_str || NULL == out_queue) return -1; 	 	const char * p, * begin, * p_end; 	char stack[MAX_NODES], op; 	int queue_index = 0; 	int stack_index = 0; 	 	memset(stack, 0, sizeof(stack)); 	 	begin = p = in_str; 	p_end = begin + strlen(in_str); 	 	// 为了简化代码，不考虑中间出现空格的情况。 	while(p < p_end) 	{ 		if(*p >= '0' && *p <= '9')  		{ 			p++; 			if(p == p_end || *p == '=') 			{ 				// push value to node queue 				out_queue[queue_index].nodeType = NODE_TYPE_val; 				out_queue[queue_index++].value = atoi(begin); 				break; 			} 		} 		else 		{ 			if(!is_op(*p)) return -1; // must be num or op 			 			// push value to node queue 			out_queue[queue_index].nodeType = NODE_TYPE_val; 			out_queue[queue_index++].value = atoi(begin); 			 			// 检查stack中运算符的优先级 			if(stack_index > 1) 			{ 				if(op_prior(stack[stack_index - 1], stack[stack_index - 2]) > 0) 				{ 					out_queue[queue_index].nodeType = NODE_TYPE_op; 					out_queue[queue_index++].op = stack[stack_index - 1]; 					stack_index--; 				} 				 			} 			 			op = *p++;			 			stack[stack_index++] = op;		 			begin = p; 			 		} 	}  	 	while(stack_index > 0) 	{ 		out_queue[queue_index].nodeType = NODE_TYPE_op; 		out_queue[queue_index++].op = stack[--stack_index];		 	} 	 	return queue_index; }   double compute(struct Node queue[MAX_NODES], int count) { 	double result; 	double stack[MAX_NODES]; 	int stack_index = 0; 	double val1, val2; 	int i; 	 	for(i = 0; i < count; i++) 	{ 		if(queue[i].nodeType == NODE_TYPE_val) 		{ 			stack[stack_index++] = (double)queue[i].value; 		}else 		{ 			if(stack_index < 2)  			{ 				fprintf(stderr, "invalid expression format!\n"); 				return 0.0; 			} 			val1 = stack[stack_index - 2]; val2 = stack[stack_index - 1]; 			stack_index -= 2; 			switch(queue[i].op) 			{ 				case '+':  					result = val1 + val2;					 					stack[stack_index++] = result; 					break; 				case '-': 					result = val1 - val2;					 					stack[stack_index++] = result; 					break; 				case '*':  					result = val1 * val2;					 					stack[stack_index++] = result; 					break; 				case '/': 					result = val1 / val2;					 					stack[stack_index++] = result; 					break; 				default: 					fprintf(stderr, "invalid expression format!\n"); 					return 0.0; 			} 		} 	} 	return result; 	 }