ACCBD 发表于 2020-6-10 18:26:13

简易计算器代码有问题麻烦大佬看一下指点一下

#include <windows.h>
#include <stdio.h>
#include <string.h>
#define STACKSIZE 1000

typedef struct OPTR{
        char stack;
        int top;
        int stacksize;
}OPTR;
int initstack(OPTR *c){
        c->top=-1;
        c->stacksize=STACKSIZE;
        return 1;
}
typedef struct OPND{
        double data;
        struct OPND *next;
}OPND;
typedef struct linkstack{
        OPND *top;
       
}linkstack;
void Initlinkstack(linkstack *S)
{
       S=(linkstack*)malloc(sizeof(linkstack));
       S->top=NULL;
       
}
char pushchar(OPTR *c,char op);
char Gettopcahr(OPTR c,char *op);
char popchar(OPTR *c,char *op);
char pushchar(OPTR *c,char op){
        if(c->top=c->stacksize-1)
        return 0;
   c->stack[++c->top]=op;
   return 1;
}
char Gettopchar(OPTR c,char *op){
    if(c.top<0)
    return 0;
    else{
            *op=c.stack;
            return 1;
        }
}
char popchar(OPTR *c,char *op){
        if(c->top<0)
        return 0;
        *op=c->stack;
        return 1;
}
double pushnum(linkstack *S,double e);
double Gettopnum(linkstack S,double *e);
double popnum(linkstack *S,double *e);
double pushnum(linkstack *S,double e){
        OPND *p;
        p=(OPND*)malloc(sizeof(OPND));
        if(p==NULL)
        return 0;
        p->data=e;
        p->next=S->top;
        S->top=p;
        return 1;
}
double Gettopnum(linkstack S,double *e){
        if(S.top==NULL)
        return 0;
        else{
                *e=S.top->data;
                return 1;
        }
}
double popnum(linkstack *S,double *e){
        OPND *p;
        if(S->top==NULL)
        return 0;
        *e=S->top->data;
        p=S->top;
        S->top=S->top->next;
        free(p);
        return 1;
}
char judgment(char ch);
char judgment(char ch){
        if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='\n')
        return 1;
        else
        return 0;
}
int Index(char theta);
int Index(char theta)   
{
        int index = 0;
        switch (theta)
        {
        case '+':
                index = 0;
                break;
        case '-':
                index = 1;
                break;
        case '*':
                index = 2;
                break;
        case '/':
                index = 3;
                break;
        case '(':
                index = 4;
                break;
        case ')':
                index = 5;
                break;
        case '#':
                index = 6;
        default:break;
        }
        return index;
}
int getIndex(theta1) {
        return 0;
}
char Priority(char theta1, char theta2);
char Priority(char theta1, char theta2)
{
        int index1, index2;
       char priority[] =   
        {
                { '>','>','<','<','<','>','>' },
                { '>','>','<','<','<','>','>' },
                { '>','>','>','>','<','>','>' },
                { '>','>','>','>','<','>','>' },
                { '<','<','<','<','<','=','0' },
                { '>','>','>','>','0','>','>' },
                { '<','<','<','<','<','0','=' },
        };

       index1 = Index(theta1);
       index2 = Index(theta2);
        return priority;
}
char precede(char a,char b);
char precede(char a,char b){
        Priority(Index(a),Index(b));
        return 0;
}
double calculate(double a,char ch,double b);
double calculate(double a,char ch,double b){
        switch(ch){
        case'+': return a + b; break;
        case'-': return a - b; break;
        case'/': return a / b; break;
        case'*': return a * b; break;
        default: return 0;
}
}
double answer();
double answer()
{
        char ch;
        char theta;double a,b;
        int v=0;
        OPTR OPTR;
        linkstack OPND;
        Initlinkstack(&OPND);
        pushchar(&OPTR,'#');
        printf("请输入表达式并以#结尾:");
        scanf_s("%c",&ch);
        Gettopchar(OPTR,&theta);
        while(ch!='#'&&theta!='#'){
                if(judgment(ch)!=1)
                {
                        int temp;
                        temp=ch-'0';
                        ch=getchar();
                        while(judgment(ch)!=1)
                        {
                                temp=temp*10+ch-'0';
                                ch=getchar();
                       }
                pushnum(&OPND,temp);
               }
               else{
                       Gettopchar(OPTR,&theta);
                       switch(precede(theta,ch))
                       {
                               case'<':
                                       pushchar(&OPTR,ch);ch=getchar();break;
                                       case'=':
                                               popchar(&OPTR,&ch);ch=getchar();break;
                                               case'>':
                                                       popchar(&OPTR,&ch);popnum(&OPND,&a);popnum(&OPND,&b);
                                                       pushnum(&OPND,calculate(a,theta,b));break;
                                                      
                       }
               }
       }
       while(popchar(&OPTR,&ch)!=0)
       {
               v=v*10+ch-'0';
       }
       return v;
}
int main()
{
        double i;
        i=answer();
        printf("%f",i);
        return 0;
}

ACCBD 发表于 2020-6-10 18:27:36

用户从键盘输入一行含有+、-、*、/、(、)和数字的数学表达式,程序计算出表达式的结果,显示在屏幕上。例如:用户输入3*(2+5)- 1.5,屏幕显示最终结果:19.50
1.        能接收从键盘输入的一行数学表达式,能判定其是否是合法的数学表达式,即:是否只包含+、-、*、/、(、)和数字。
2.        数学表达式中可以含有空格
3.        数字不仅仅限于整数,也可以是小数;
4.        能识别错误的表达式,例如:3(2)+;
5.        使用顺序栈存储运算符,使用链栈存储运算数
这是要求

Darth_EF 发表于 2020-6-10 22:10:16

请问楼主可不可以这样发代码用方括号括住的code和/code把内容包起来

另外能不能不要写成这么难看,起码加些注释好吗?

//注释这个函数用来干什么,参数的含义,返回值的含义
type function(parameter a, parameter b);

int main(){
//代码
}

type function(parameter a, parameter b){
//代码
}

最好还要分文件,一个头文件一个实现头文件的函数的源文件和一个main源文件。

遇到这种直接贴一大坨代码的我看都懒得看。干脆直接按你要求写一个好了

ACCBD 发表于 2020-6-10 22:13:54

Darth_EF 发表于 2020-6-10 22:10
请问楼主可不可以

另外能不能不要写成这么难看,起码加些注释好吗?


麻烦你帮我写一个呗
页: [1]
查看完整版本: 简易计算器代码有问题麻烦大佬看一下指点一下