Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

doitfgts

Name: Anonymous 2011-03-19 2:25

Write a function in C that gets passed a pointer to a null-terminated string containing an arithmetic expression such as "3*(4+5)-1" and returns the correct value.  It must be able to handle +,-,*,/, and parenthesis.  No implied multiplication, no negative numbers, usual order of operations, and the passed string is guaranteed to have matched parenthesis and proper syntax.

Name: Anonymous 2011-03-19 4:48

Write your own eval, Jock-chan.

Name: Anonymous 2011-03-19 5:21

ENTERPRSIE QUALITY SOLUTION

char buf[1024];
sprintf(buf, "echo %s | bc >arse");
system(buf);
FILE* f = fopen("arse","r");
fread(buf, 1, 1024);
fclose(f);
printf("%s\n",buf);

Name: Anonymous 2011-03-19 5:22

-sprintf(buf, "echo %s | bc >arse");
+sprintf(buf, "echo %s | bc >arse", expression);

Name: Anonymous 2011-03-19 6:33

because boredom.


float asdf(const char *inp_t) {
    char *inp = strdup(inp_t);
    float *arr = (float *)malloc(strlen(inp_t) * sizeof(float));
    unsigned pos=0, c=0;

    while (inp[pos] != 0) {
        switch (inp[pos]) {
            case '+':case '-':case '*':case '/':
                arr[c++] = inp[pos++];
                break;

            case '(': {
                int lpar = pos++;

                for (int t = 1; t > 0; pos++)
                    t += (inp[pos]=='(')-(inp[pos]==')');

                inp[pos-1] = 0;
                arr[c++] = asdf(&(inp[lpar+1]));
                break;
            }
            default: {
                int len = strcspn(&(inp[pos]), "+-*/()");
                char tmp = inp[pos+len];
                inp[pos+len] = 0;
                arr[c++] = atoi(&(inp[pos]));
                inp[pos=pos+len] = tmp;
            }
        }
    }

    if (--c == 0) return arr[0];

    for (int i=1; i<=c-1; i+=2) {
        if (arr[i] == '-') arr[i+1] = -arr[i+1];

        if (arr[i] == '/' || arr[i] == '*') {
            arr[i+1] = arr[i-1]*((arr[i]=='/')/arr[i+1] + (arr[i]=='*')*arr[i+1]);
            arr[i-1] = 0;
        }
    }

    for (int i=c; i>0; i-=2) arr[i-2] += arr[i];

    return arr[0];
}

Name: Anonymous 2011-03-19 7:02

>>5
>arr[i+1] = arr[i-1]*((arr[i]=='/')/arr[i+1] + (arr[i]=='*')*arr[i+1]);

your code makes my eyes bleed.

Name: Anonymous 2011-03-19 7:45

>>1
Read K&R. It has this exact exercise.

Name: Anonymous 2011-03-19 7:52

>>7
You mean this one?

http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_5:Exercise_10

That's for polish notation, much simpler.

Name: Anonymous 2011-03-19 11:04

>>8
polish my dick

Name: Anonymous 2011-03-19 11:10

>>8
polish notation infix notation >

Name: Anonymous 2011-03-19 11:46

>>10
polish notation < infix notation

Name: Anonymous 2011-03-19 11:52

>>11
Enjoy you're inferior.

Name: Anonymous 2011-03-19 12:08

>>12
you're = you are; (You are a dog -> You're a dog)
your = something that is yours; (Your dog barks)

Name: Anonymous 2011-03-19 12:23

yo'ure = autism; (Yo'ure a faggot)

Name: Anonymous 2011-03-19 12:31

>>13
Y = λf.(λx.f (x x)) (λx.f (x x)); (YHBT -> YHBT Have Been Trolled)

Name: Anonymous 2011-03-19 15:47


    ░░░░░░░░░░░░░░░▄░░░░░░░░░░░░░░░
    ░░░░░░░░░░░░░▄▀█░░░░░░░░░░░░░░░
    ░░░░░░░░░░░▄▀░░█░░░░░░░░░░░░░░░
    ░░░░░░░░░▄▀░░▄▀░░░░░░░░░░░░░░░░
    ░░░░░░░░█▄░▄▀░░░░░░░░▄█▄░░░░░░░
    ░░░░░░░░█░▀▄░░░░░░░▄▀░█░▀▄░░░░░
    ░░░░░░░░▀▄░░▀▄░░░▄▀░░▄▀▄░░▀▄░░░
    ░▄░░░░░░░░▀▄░░▀▄▀░░▄▀░░░▀▄░░▀▄░
    ░█▀▄░░░░░░░░▀▄▀█▀▄▀░░░░░░░▀▄░█░
    ░█░░▀▄░░░░░▄▀░░█░░▀▄░░░░░░░░▀█░
    ░░▀▄░░▀▄░▄▀░░▄▀░▀▄░░▀▄░░░░░░░░░
    ░░░░▀▄░░█░░▄▀░░░░░▀▄░▄█░░░░░░░░
    ░░░░░░▀▄█▄▀░░░░░░░░▄▀░█░░░░░░░░
    ░░░░░░░░▀░░░░░░░░▄▀░░▄▀░░░░░░░░
    ░░░░░░░░░░░░░░░▄▀░░▄▀░░░░░░░░░░
    ░░░░░░░░░░░░░░░█░▄▀░░░░░░░░░░░░
    ░░░░░░░░░░░░░░░█▀░░░░░░░░░░░░░░

Don't change these.
Name: Email:
Entire Thread Thread List