C Programming Language 4-6 lesson question reference answer incorrect

Source: Internet
Author: User
Tags vars

4-6 if the use of official "matching" (in fact, other people wrote) reference books, the answer code is not correct, can not successfully achieve the goal. However, slightly modified to:

First, the global definition of the Var parameter (the name itself), the main code is consistent with the recommendation:

int var;//defines VAR here in main () {int type,i;
    Double op1,op2,v;
    Char S[MAXOP];
    void clean (void);

    Double vars[26];
    for (i=0;i<26;i++) vars[i]=0.0;
            while ((Type=getop (s))!=eof) {switch (type) {case Number:push (atof (s));
        Break
            Case ' + ': Push (Pop () +pop ());
        Break
            Case Func:mathfunc (s);
        Break
            Case ' * ': Push (Pop () *pop ());
        Break
            Case '-': Op2=pop ();
            Push (pop ()-op2);
        Break
            Case '/': Op2=pop ();
            if (op2!=0.0) push (pop ()/op2);
            else printf ("Error:zero divisor\n");
        Break
            Case '% '://Modulo addition operation Op2=pop ();
            if (op2!=0.0) push (Fmod (pop (), OP2));
            else printf ("Divisor cannot be 0\n");
        Break Case ' \ n ': printf ("\t%.8g\n",Pop ());
        Break
            Case '? '://print stack top element op2=pop ();
            printf ("\t%.8g\n", OP2);
            Push (OP2);
        Break
            Case ' C '://Empty stack clean ();
        Break
            Case ' s ': Op1=pop ();
            Op2=pop ();
            Push (OP1);
            Push (OP2);
        Break
            Case ' = ': Pop ();
                if (var>= ' a ' &&var<= ' Z ') {vars[var-' a ']=pop ();
            else printf ("Record variable Failed");
        Break
            Default:if (type>= ' a ' &&type<= ' Z ') push (vars[type-' a '));
            else if (type== ' V ') push (v);
            else printf ("Error:unknown command%s\n", s);
        Break
} return 0; }

Next, modify the getop to increase the capital check:

int Getop (char s[]) {int i,c; while ((S[0]=c=getch ()) = = ' | |
    c== ' t ');
    S[1]= ' ";
    i=0;
        if (Isupper (c)) {//increase the capital letter detection Var=c;
    return C;
        } if (Islower (c)) {int p=c;
        Here c is changed, so it needs to be staged with a new variable, otherwise the expected result is not achieved while (Islower (S[++i]=c=getch ()));
        S[i]= ' ";
            if (c!=eof) {ungetch (c);
        } if (strlen (s) >1) {return FUNC;
        } else{return p; } if (!isdigit (c) && c!= '. '

    && c!= '-') return C;
                Check here for negative if (c = = '-') {if (IsDigit (S[++i]=c=getch ())) {While isdigit (S[++i]=c=getch ()))
        ; else if ((S[++i]=c=getch ()) = = '. ')
        {while (IsDigit (S[++i]=c=getch ()));
            else{if (c!=eof) Ungetch (c);
            Return '-'; } if (IsDigit (c)) while IsDigit (s[++i]=c=getch ()));
        if (c = = '. ')
    while (IsDigit (S[++i]=c=getch ()));
    S[i]= ' ";
    if (c!=eof) Ungetch (c);
return number; }

OK, but there's more than one way to do it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.