doitfgts
1
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.
2
Name:
Anonymous
2011-03-19 4:48
Write your own eval, Jock-chan .
3
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);
4
Name:
Anonymous
2011-03-19 5:22
-sprintf(buf, "echo %s | bc >arse");
+sprintf(buf, "echo %s | bc >arse", expression);
5
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];
}
6
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.
7
Name:
Anonymous
2011-03-19 7:45
>>1
Read K&R. It has this exact exercise.
8
Name:
Anonymous
2011-03-19 7:52
9
Name:
Anonymous
2011-03-19 11:04
10
Name:
Anonymous
2011-03-19 11:10
>>8
polish notation infix notation >
11
Name:
Anonymous
2011-03-19 11:46
>>10
polish notation < infix notation
12
Name:
Anonymous
2011-03-19 11:52
>>11
Enjoy you're inferior.
13
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)
14
Name:
Anonymous
2011-03-19 12:23
yo'ure = autism; (Yo'ure a faggot)
15
Name:
Anonymous
2011-03-19 12:31
>>13
Y = λf.(λx.f (x x)) (λx.f (x x));
(YHBT -> YHBT Have Been Trolled)
16
Name:
Anonymous
2011-03-19 15:47
░░░░░░░░░░░░░░░▄░░░░░░░░░░░░░░░
░░░░░░░░░░░░░▄▀█░░░░░░░░░░░░░░░
░░░░░░░░░░░▄▀░░█░░░░░░░░░░░░░░░
░░░░░░░░░▄▀░░▄▀░░░░░░░░░░░░░░░░
░░░░░░░░█▄░▄▀░░░░░░░░▄█▄░░░░░░░
░░░░░░░░█░▀▄░░░░░░░▄▀░█░▀▄░░░░░
░░░░░░░░▀▄░░▀▄░░░▄▀░░▄▀▄░░▀▄░░░
░▄░░░░░░░░▀▄░░▀▄▀░░▄▀░░░▀▄░░▀▄░
░█▀▄░░░░░░░░▀▄▀█▀▄▀░░░░░░░▀▄░█░
░█░░▀▄░░░░░▄▀░░█░░▀▄░░░░░░░░▀█░
░░▀▄░░▀▄░▄▀░░▄▀░▀▄░░▀▄░░░░░░░░░
░░░░▀▄░░█░░▄▀░░░░░▀▄░▄█░░░░░░░░
░░░░░░▀▄█▄▀░░░░░░░░▄▀░█░░░░░░░░
░░░░░░░░▀░░░░░░░░▄▀░░▄▀░░░░░░░░
░░░░░░░░░░░░░░░▄▀░░▄▀░░░░░░░░░░
░░░░░░░░░░░░░░░█░▄▀░░░░░░░░░░░░
░░░░░░░░░░░░░░░█▀░░░░░░░░░░░░░░