0

**

This is my source code about calculator,I also use online materials, but it has a problem, i just plus the number from 1 to 9, if I use 10 plus another number, the result always is 1, please teach me how to fix it, thanks you

#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PI 3.14
struct STACK
{
    int *array;
    int Max;    
    int Top;
};

bool InitStack(STACK &s,int MaxNumItem)
{
    s.Max=MaxNumItem;
    s.array=new int[s.Max];
        if(s.array==NULL) return false;
            s.Top=-1;
            return true;
}
bool IsStackEmpty(const STACK &s)
{
    if(s.Top<0) return true;
    else return false;
}
bool IsStackFULL(const STACK &s)
{
    if(s.Top==s.Max-1) return true;
    else return false;
}
void printStack(const STACK &s)
{
    if(IsStackEmpty(s)==true)
        {
            printf("Stack is empty! No data to show!\n");
            return;
        }
    for(int i=0;i<=s.Top;i++)
        {
            printf("%5d ",s.array[i]);
        }
    printf("\n");
}
int Pushstack2(STACK &s,int InItem)
{
    if(IsStackFULL(s)==true) return false;
        s.array[++s.Top]=InItem;
        return true;
}
int Popstack2(STACK &s)
{
    int OutItem;
    if(IsStackEmpty(s)==true) return -1;
    else
        {
            OutItem=s.array[s.Top--];
            return OutItem;
        }
}

int IsGreaterOrEqual(char c)
{
    switch(c)
        {
            case 'c': return 3;
            case 's': return 3;
            case '*': return 2;
            case '/': return 2;
            case '+': return 1;
            case '-': return 1;
            case '(': return 0;
        }
}
void infixtopostfix(char* p1,char* p2,int &p)
{
    STACK s;
    InitStack(s,100);
    char temp,c;
    int e=0;
    for(int i=0;i<=strlen(p1);i++)
    {
        c=p1[i];
        switch(c)
            {
                case '(': {Pushstack2(s,c); break;}
                case 'c': goto sosanh;
                case 's': goto sosanh;
                case '+': goto sosanh;
                case '-': goto sosanh;
                case '/': goto sosanh;
                case '*': goto sosanh;
                case ')': 
                {   while(s.array[s.Top]!='('&&IsStackEmpty(s)==false)
                    {
                        temp=char(Popstack2(s));
                        p2[e++]=temp;
                    }
                if(s.array[s.Top]=='(') 
                    {
                        temp=char(Popstack2(s)); 
                        break;
                    }
                }
            default: 
                {
                    if(c>=48&&c<=57&&c!='c'&&c!='s')
                        {
                            p2[e++]=c; break;
                        }
                    break;
                }
            sosanh:
                {
                    if(IsStackEmpty(s)==true) {Pushstack2(s,c); break;}
                    else
                        {
                            temp=char(Popstack2(s));
                            if(IsGreaterOrEqual(temp)>=IsGreaterOrEqual(c)) {p2[e++]=temp; Pushstack2(s,c); break;}
                            else {Pushstack2(s,temp); Pushstack2(s,c);break;}
                        }
                }
            }
}
while(IsStackEmpty(s)==false)
{
    temp=char(Popstack2(s));
    p2[e++]=temp;
}
p=e;
for(int j=0;j<e;j++)
{
    printf("%c",p2[j]);
}

}
void cal(char* p2,int p)
{
    STACK s;
    InitStack(s,100);
    char c;
    int kq,temp1,temp2,flag=1;
    float goc1,goc2;
    for(int i=0;i<p;i++)
        {
            c=p2[i];
            switch (c)
                {
                    case '+':
                    {
                        temp1=Popstack2(s);
                        temp2=Popstack2(s);
                        kq=temp1+temp2;
                        Pushstack2(s,kq);
                        break;
                    }
                    case '-':
                    {
                        temp1=Popstack2(s);
                        temp2=Popstack2(s);
                        kq=temp2-temp1;
                        Pushstack2(s,kq);
                        break;
                    }
                    case '*':
                    {
                        temp1=Popstack2(s);
                        temp2=Popstack2(s);
                        kq=temp1*temp2;
                        Pushstack2(s,kq);
                        break;
                    }   
                    case '/':
                    {
                        temp1=Popstack2(s);
                        temp2=Popstack2(s);
                        if(temp1==0) {flag=0; goto Result;}
                        else
                        {
                            kq=temp2/temp1;
                            Pushstack2(s,kq);
                        }
                        break;
                    }
                    case 's':
                    {
                        temp1=Popstack2(s);
                        temp2=Popstack2(s);
                        goc1=temp2*10+temp1;
                        kq=sin(goc1*PI/180);
                        Pushstack2(s,kq);
                    }
                    /*case 'c':
                    {
                    temp1=Popstack2(s);
                    temp2=Popstack2(s);
                    goc2=temp2*10+temp1;
                    kq=cos(goc2*PI/180);
                    Pushstack2(s,kq);
                    }*/
                    default:
                    {
                        if(c>=48&&c<=57&&c!='c'&&c!='s')
                        {
                            Pushstack2(s,atoi(&c));
                            break;
                        }
                    }
                }
        }
        Result:
        {
            if(flag==0) printf("Math error!\n (Can't devide by zero)\n");
            else
                {
                    kq=Popstack2(s);
                    printf("ket qua la: %d\n",kq);
                }
        }
}
void main()
{
    int loop;
    do
        {
            char p1[100];
            char p2[100];
            int number,p;
            printf("nhap chuoi: ");
            fflush(stdin);
            gets_s(p1,100);
            fflush(stdin);
            puts(p1);
            infixtopostfix(p1,p2,p);
            cal(p2,p);
            printf("ban hay thu lai? \n nhap so: ");
            scanf_s("%d",&loop);
        }
    while(loop==1);
}**
2
Contributors
1
Reply
31
Views
3 Years
Discussion Span
Last Post by vmanes
1

In your cal function, it looks like you only push single digits onto the stack, not the full value of any multidigit input.

c=p2[i];

.....

default:
{
    if(c>=48&&c<=57&&c!='c'&&c!='s')
    {
        Pushstack2(s,atoi(&c));
        break;
    }

Edited by vmanes

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.