【大风】matrix:expression题目代码分享

#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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值