鱼C论坛

 找回密码
 立即注册
查看: 2093|回复: 3

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

[复制链接]
发表于 2020-6-10 18:26:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

typedef struct OPTR{
        char stack[STACKSIZE];
        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[c.top];
            return 1;
        }
}
char popchar(OPTR *c,char *op){
        if(c->top<0)
        return 0;
        *op=c->stack[c->top--];
        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[][7] =     
        {
                { '>','>','<','<','<','>','>' },
                { '>','>','<','<','<','>','>' },
                { '>','>','>','>','<','>','>' },
                { '>','>','>','>','<','>','>' },
                { '<','<','<','<','<','=','0' },
                { '>','>','>','>','0','>','>' },
                { '<','<','<','<','<','0','=' },
        };

         index1 = Index(theta1);
         index2 = Index(theta2);
        return priority[index1][index2];
}
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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-10 18:27:36 | 显示全部楼层
用户从键盘输入一行含有+、-、*、/、(、)和数字的数学表达式,程序计算出表达式的结果,显示在屏幕上。例如:用户输入3*(2+5)- 1.5,屏幕显示最终结果:19.50
1.        能接收从键盘输入的一行数学表达式,能判定其是否是合法的数学表达式,即:是否只包含+、-、*、/、(、)和数字。
2.        数学表达式中可以含有空格
3.        数字不仅仅限于整数,也可以是小数;
4.        能识别错误的表达式,例如:3(2)+;
5.        使用顺序栈存储运算符,使用链栈存储运算数
这是要求
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-10 22:10:16 | 显示全部楼层
请问楼主可不可以
  1. 这样发代码用方括号括住的code和/code把内容包起来
复制代码


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

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

  3. int main(){
  4. //代码
  5. }

  6. type function(parameter a, parameter b){
  7. //代码
  8. }
复制代码

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

遇到这种直接贴一大坨代码的我看都懒得看。干脆直接按你要求写一个好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-10 22:13:54 | 显示全部楼层
Darth_EF 发表于 2020-6-10 22:10
请问楼主可不可以

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

麻烦你帮我写一个呗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 17:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表