bool Expression::opcal(int currentop)
 {
    int op=sc.top();
    if(op>7)
    {
        if(sn.empty()) return false;
        double m=mypop(sn);
        m=FunCal(op,m);
        if(m!=m||m+1==m)
           return false;
        sn.push(m);
        sc.pop();
    }
     for(op=sc.top();PRIORITY[currentop][op]<=0;op=sc.top())
     {
        if(sn.empty()) return false;
        double m=mypop(sn);
        if(sn.empty()) return false;
        double n=mypop(sn);
        sn.push(myBasicCal(n,m,op));
        sc.pop();
     }
     sc.push(currentop);
     return true;
 }
Example #2
0
int main() {
    /*
	printf("Hello!\n");

	const char* fileName = "out.txt";
    FILE* fp = fopen(fileName, "wb");

    int i = 0x4949;
    char* str = "test";
    size_t len = strlen(str);

    fwrite(&i, sizeof(int), 1, fp);
    fwrite(str, sizeof(char), len, fp);
    fclose(fp);

    fp = fopen(fileName, "rb");
    int k = 0;
    char buf[1024];
    bzero(buf, 1024);

    fread(&k, sizeof(int), 1, fp);
    printf("%d\n", k);
    fread(buf, sizeof(char), 1024, fp);
    printf("%s\n", buf);
    fclose(fp);
    */


    int stack[MAXSIZE] = {0};
    int top = 0;

    int i = 0;
    for (i = 0; i < MAXSIZE+1; i++) {
        mypush(i, stack, &top);
        mysize(stack, &top);
    }

    for (i = 0; i < MAXSIZE+1; i++) {
        mypop(stack, &top);
        mysize(stack, &top);
    }



	return 0;
}
 bool Expression::LegalAndCal()
 {
     //double t=0;
     int op=0;
     for(int i=0;expstring[i]!='\0';++i)
     {
         if(expstring[i].isLetter())
         {
             int begin=i;
             while(expstring[i]!='(')
             {
                 if(expstring[i]=='\0')
                     return false;
                ++i;
             }
             int fnum=FunNum(begin-1,i);
             if(fnum==-1) return false;
             //if(fcresult!=fcresult||fcresult+1==fcresult) return false;
             //sn.push(fcresult);
             sc.push(fnum);
             sc.push(4);
         }
         else
         {
         switch(expstring[i].unicode())
         {
         case '.':
         case '0':
         case '1':
         case '2':
         case '3':
         case '4':
         case '5':
         case '6':
         case '7':
         case '8':
         case '9':
         {
             int b=i;
             while((expstring[i]<='9'&&expstring[i]>='0')||expstring[i]=='.')
             {
                ++i;
             }
             QString numstring=expstring.mid(b,i-b);
             --i;
             if(!MyToDouble(numstring)) return false;
             break;
         }
         case '+':
             if(!opcal(0)) return false;
             break;
         case '-':
             /*if(sn.empty())
             {
                 ++i;
                 int b=i;
                 if(expstring[i]=='(')
                 {
                     while(expstring[i]!=')'&&expstring[i]!='\0')
                     {
                         ++i;
                     }
                     if(expstring[i]=='\0') return false;
                     QString temp=expstring.mid(b+1,i-b-1);
                     Expression et(temp);
                     if(!et.LegalAndCal())
                     {
                         return false;
                     }
                     sn.push(0-et.GetResult());
                 }
                 else
                 {
                    while((expstring[i]<='9'&&expstring[i]>='0')||expstring[i]=='.')
                    {
                        ++i;
                    }
                    QString numstring=expstring.mid(b-1,i-b+1);
                    --i;
                    if(!MyToDouble(numstring)) return false;
                 }
             }
             else
             {*/
             if(i-1<0||expstring[i-1]=='(')
             {
                 sn.push(0);
             }
             if(!opcal(1)) return false;
             break;
         case '*':
             if(!opcal(2)) return false;
             break;
         case '/':
             if(!opcal(3)) return false;
             break;
         case '%':
             if(!opcal(6)) return false;
             break;
         case '^':
             if(!opcal(5)) return false;
             break;
         /*case ' ':
         {
             int begin=i;
             while(expstring[i]!='(')
             {
                 if(expstring[i]=='\0')
                     return false;
                ++i;
             }
             int fnum=FunNum(begin,i);
             if(fnum==-1) return false;
             //if(fcresult!=fcresult||fcresult+1==fcresult) return false;
             //sn.push(fcresult);
             sc.push(fnum);
             sc.push(4);
             break;
         }*/
         case '(':
         {
             /*int b=i;
             while(expstring[i]!=')'&&expstring[i]!='\0')
             {
                 ++i;
             }
             if(expstring[i]=='\0') return false;
             QString temp=expstring.mid(b+1,i-b-1);
             Expression et(temp);
             if(!et.LegalAndCal())
             {
                 return false;
             }
             sn.push(et.GetResult());

             break;*/
             sc.push(4);
             break;
         }
         case ')':
         {
             while(sc.size()>1&&(op=sc.top())!=4)
             {
                 /*if(op>7)
                 {
                     if(sn.empty()) return false;
                     double m=mypop(sn);
                     m=FunCal(op,m);
                     sn.push(m);
                     sc.pop();
                     if(m!=m||m+1==m)
                        return false;
                     continue;
                 }*/
                 if(sn.empty()) return false;
                 double m=mypop(sn);
                 if(sn.empty()) return false;
                 double n=mypop(sn);
                 sn.push(myBasicCal(n,m,op));
                 sc.pop();
             }
             sc.pop();
             if(sc.empty()) return false;
             if((op=sc.top())>7)
             {
                 if(sn.empty()) return false;
                 double m=mypop(sn);
                 m=FunCal(op,m);
                 sn.push(m);
                 sc.pop();
                 if(m!=m||m+1==m)
                    return false;
             }
             break;
         }
         default:
             return false;
         }
         }
     }
     while(sc.size()>1)
     {
         op=sc.top();
         if(op==4)
         {
             sc.pop();
             continue;
         }
         if(op>7)
         {
             if(sn.empty()) return false;
             double m=mypop(sn);
             m=FunCal(op,m);
             sn.push(m);
             sc.pop();
             if(m!=m||m+1==m)
                 return false;
             continue;
         }
         if(sn.empty()) return false;
         double m=mypop(sn);
         if(sn.empty()) return false;
         double n=mypop(sn);
         sn.push(myBasicCal(n,m,op));
         sc.pop();
     }
     //sc.pop();
     if(sn.size()!=1)return false;
     result=mypop(sn);
     return true;
 }