#include <stdio.h>
//大风的丑陋代码
int numberStack[400]={0};//初始化stack,第零位代表长度
//使用的数据结构:栈
//使用的算法:将第一个数字扔进栈,然后接着读取后面的数字。
//如果是加号相连,则把数字扔进栈,如果是乘号相连 ,则pop出栈中的一个数字,并相乘,再扔进栈。
//最后把栈中的数字全部相加得结果
void push (int newElement,int stack[400])
{
//不告诉你
//Deadline 2017.1.1 ,慢慢想
}
int pop(int stack[400])//若栈为空则返回0
{
//不告诉你
//Deadline 2017.1.1 ,慢慢想
}
int main()
{
int number;
char opeChar;
scanf("%d",&number);
push(number % 10000,numberStack);
while ( (opeChar = getchar() )!=EOF)
{
scanf("%d",&number);
if (opeChar == '+')
{
push(number% 10000,numberStack);
}
if (opeChar =='*')//本次运算符是*
{
push( (pop(numberStack)*(number%10000)) % 10000,numberStack);
}
}
while (numberStack[0]>1)
{
push( ( pop(numberStack)+pop(numberStack) ) %10000,numberStack);
}
printf("%d\n",pop(numberStack));
}
comments:当然,本题不一定要用栈,只是那时,我做这道题的时候刚好学了下栈的写法,于是就拿来用了。
推荐大家看看伟元的代码。
关于栈(stack)
In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out). Additionally, a peek operation may give access to the top without modifying the stack.
来源:wiki_stack