| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 324 人关注过本帖
标题:关于stack postfix calculator 请大师指导啊
只看楼主 加入收藏
工程小生
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-12-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
关于stack postfix calculator 请大师指导啊
首先是先要读取文件, 文件里面是些数字与预算符号。然后要把读取的文件让它自动运算!然后我可以单独读取文件,但是不清楚怎么把他加入进去。这个我写的程序是可以自己输入数字自己运算的, 但是还有个就是 我输入4,8,/,它得的是2,按道理是0.5! 它是用8/4做的 !求指导呀   
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#define  STACKSIZE 6

int  stack_pointer = 0;
int  STACKDISPLAY = 0;
long double stack[STACKSIZE] = {0.0};

int string_equal(const char* first, const char *second)
{
    return (strcmp(first, second) == 0);
}
void show_stack(char message[])
{
   
   
    int  i;
    if (!STACKDISPLAY)
    {
        return;                 
    }
   
    printf("Displaying stack at end of function %s\nStack contents:\n\r", message);
    for (i = stack_pointer; i >= 0; i--)
        printf("stack[%d] = %f\n\r", i, stack[i]);
    printf("\n\n");
}

double pop_off_stack(void)
{
    double return_value;
    return_value = stack[stack_pointer];
   
    if (stack_pointer > 0)
        
    {
        stack_pointer--;
    }
   
    show_stack("pop_off_stack");
   
    return return_value;
}

double top_of_stack_value(void)
{
    return stack[stack_pointer];
}

double push_onto_stack(double value)
{
    int  i;
    stack[++stack_pointer] = value;
    assert(stack_pointer < (STACKSIZE + 1));
    if (stack_pointer == STACKSIZE)
    {
        for (i = 0; i < STACKSIZE; i++)
            stack[i] = stack[i + 1];
        stack_pointer = STACKSIZE - 2;
    }
   
    show_stack("push_onto_stack");
    return value;
}


int main(int argc, char **argv)
{
    char c, instr[80];
    char input[80]; memset(input, '\0', 80);
    double aDouble;
    FILE*pRead;
   
    while (1)
    {
        
        scanf("%s", &input);
        
        long double numericalInput = atof(input);
        
        if(numericalInput == 0.0)
        {
            // then try parsing the commands, like "chs", "abs", "sin", etc.
            // use the stringEquals() method.
            if(string_equal(input, "+"))
            {
                // addition
                printf(" The sum is %f\n", push_onto_stack(pop_off_stack() + pop_off_stack()));
            } else if (string_equal(input, "-")) {
                // subtraction goes here
                printf(" The sum is %f\n", push_onto_stack(pop_off_stack() - pop_off_stack()));
            } else if (string_equal(input, "*")) {
                //multiplcation here
                printf(" The sum is %f\n", push_onto_stack(pop_off_stack() * pop_off_stack()));
            } else if (string_equal(input, "/")) {
                //division here
                printf( "The sum is %f\n", push_onto_stack(pop_off_stack() / pop_off_stack()));
            } else if (string_equal(input, "chs")) {
                //change signs
                printf( "The number is %f\n", push_onto_stack(pop_off_stack()*(-1)));
            } else if (string_equal(input, "abs")) {
                //absolute value
                printf("the result is: %f\n", push_onto_stack(abs((double)pop_off_stack())));
            } else if (string_equal(input, "sqrt")) {
                //square root here
                printf("the result is: %f\n", push_onto_stack(sqrt((double)pop_off_stack())));
            } else if (string_equal(input, "sin")) {
                //sin function here
                printf("the result is: %f\n", push_onto_stack(sin((double)pop_off_stack())));
            } else if (string_equal(input, "cos")) {
                //cos function here
                printf("the result is: %f\n", push_onto_stack(cos((double)pop_off_stack())));
            } else if (string_equal(input, "tan")) {
                //tan function here
                printf("the result is: %f\n", push_onto_stack(tan((double)pop_off_stack())));
            } else if (string_equal(input, "q") || string_equal(input, "Q")) {
                //quitting program
                printf("Quitting Good Bye \n");
                exit(0);
            }
               
        } else {
           
            push_onto_stack (numericalInput);
        }
    }
   

  return 0;               
}

搜索更多相关主题的帖子: calculator include 
2012-12-11 02:35
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:20 
楼主的代码,额。。

4,8,/,先让4入栈,然后8入栈,检测到 / 出栈,但是 8先出栈,然后才是 4,所以结果是 2


[fly]存在即是合理[/fly]
2012-12-11 11:08
快速回复:关于stack postfix calculator 请大师指导啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014257 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved