用栈实现迭代求值

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
# define maxSize 100 
int caF(int m)
{	int cum=1;
	int s[maxSize];int top=-1;
	while(m!=0)
	{
		s[++top]=m;
		m/=3;
	}
	while(top!=-1)
		cum*=s[top--];
	return cum;
}
int factorial(int m)
{
	if(m==0)
		return 1;
	else
		return m*factorial(m/3);
}
int main()
{
	int m=100;//
	int x=factorial(m);
	printf("%d",x);
}

/**求解迭代函数  F(m)=m*F(m/3)     当m>0时 
				  F(m)=1		   当m=9时**/ 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言和实现表达式求值的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_STACK_SIZE 100 typedef struct { int top; int data[MAX_STACK_SIZE]; } Stack; void init_stack(Stack *s) { s->top = -1; } int is_empty(Stack *s) { return s->top == -1; } int is_full(Stack *s) { return s->top == MAX_STACK_SIZE - 1; } void push(Stack *s, int value) { if (is_full(s)) { printf("Stack overflow\n"); exit(EXIT_FAILURE); } s->data[++s->top] = value; } int pop(Stack *s) { if (is_empty(s)) { printf("Stack underflow\n"); exit(EXIT_FAILURE); } return s->data[s->top--]; } int peek(Stack *s) { if (is_empty(s)) { printf("Stack is empty\n"); exit(EXIT_FAILURE); } return s->data[s->top]; } int evaluate(char *expression) { Stack operand_stack; Stack operator_stack; init_stack(&operand_stack); init_stack(&operator_stack); char *token = strtok(expression, " "); while (token) { if (isdigit(token[0])) { int operand = atoi(token); push(&operand_stack, operand); } else if (token[0] == '+' || token[0] == '-') { while (!is_empty(&operator_stack) && peek(&operator_stack) != '(') { int op = pop(&operator_stack); int op2 = pop(&operand_stack); int op1 = pop(&operand_stack); if (op == '+') { push(&operand_stack, op1 + op2); } else { push(&operand_stack, op1 - op2); } } push(&operator_stack, token[0]); } else if (token[0] == '*' || token[0] == '/') { while (!is_empty(&operator_stack) && (peek(&operator_stack) == '*' || peek(&operator_stack) == '/')) { int op = pop(&operator_stack); int op2 = pop(&operand_stack); int op1 = pop(&operand_stack); if (op == '*') { push(&operand_stack, op1 * op2); } else { push(&operand_stack, op1 / op2); } } push(&operator_stack, token[0]); } else if (token[0] == '(') { push(&operator_stack, token[0]); } else if (token[0] == ')') { while (!is_empty(&operator_stack) && peek(&operator_stack) != '(') { int op = pop(&operator_stack); int op2 = pop(&operand_stack); int op1 = pop(&operand_stack); if (op == '+') { push(&operand_stack, op1 + op2); } else { push(&operand_stack, op1 - op2); } } pop(&operator_stack); } token = strtok(NULL, " "); } while (!is_empty(&operator_stack)) { int op = pop(&operator_stack); int op2 = pop(&operand_stack); int op1 = pop(&operand_stack); if (op == '+') { push(&operand_stack, op1 + op2); } else if (op == '-') { push(&operand_stack, op1 - op2); } else if (op == '*') { push(&operand_stack, op1 * op2); } else { push(&operand_stack, op1 / op2); } } return pop(&operand_stack); } int main() { char expression[100]; printf("Enter an expression: "); fgets(expression, sizeof(expression), stdin); expression[strlen(expression) - 1] = '\0'; // remove newline character int result = evaluate(expression); printf("Result: %d\n", result); return 0; } ``` 该代码使用两个:一个用于操作数,一个用于操作符。它通过迭代表达式中的每个标记来处理表达式。如果标记是一个数字,则将其作为操作数推送到操作数中。如果标记是+或-,则将其推送到操作符中,并尝试将操作符中的操作符弹出并应用于操作数中的操作数,直到操作符为空或操作符的顶部是左括号。如果标记是*或/,则将其推送到操作符中,并尝试将操作符中的*或/操作符弹出并应用于操作数中的操作数,直到操作符为空或操作符的顶部是+或-操作符或左括号。如果标记是左括号,则将其推送到操作符中。如果标记是右括号,则将操作符中的操作符弹出并应用于操作数中的操作数,直到遇到左括号。最后,将剩余的操作符从操作符中弹出并应用于操作数中的操作数,直到操作符为空。最终,操作数中应该只有一个元素,该元素是表达式的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值