/* 979 名前: デフォルトの名無しさん Mail: sage 投稿日: 2011/11/29(火) 21:17:42.06 */
/* [1] 授業単元:コマンドライン引数 */
/* [2] 問題文(含コード&リンク): */
/* コマンドライン引数を用いて,以下の計算ができる簡易電卓プログラムを作成せよ. */
/* 1. 2 ~5項式までの四則演算(計算順もちゃんと考慮すること) */
/* (出力例)C :¥Programming>dentaku 2 + 5 / 5 + 4 * 2 + 1 */
/* 答えは 12 */
/* C :¥Programming>dentaku 2 * 3 + 1 */
/* 答えは 7 */
/* 2.()を用いた演算(かっこの中の計算を最優先する) */
/* (出力例)C :¥Programming>dentaku ( 2 + 3 ) * 7 * ( 3 - 1 ) */
/* 答えは 70 */
/* 3. (sin, cos, tan)の演算 */
/* (出力例)C :¥Programming>dentaku sin(90) * 2 + 1 - 2 */
/* 答えは 1 */
/* 4.logの演算 */
/* (出力例)C :¥Programming>dentaku log(3) * 2 + 1 */
/* 答えは 1.9542425094 */
/* 5. 半角スペースを入れ忘れても演算可能 */
/* (出力例)C :¥Programming>dentaku 1-2*(3+1)-2 */
/* 答えは 9 */
/* [3] 環境 */
/* [3.1] OS:Windows7 */
/* [3.2] コンパイラ名とバージョン:Borland C++ Compiler 5.5 */
/* [3.3] 言語:C言語 */
/* [4] 期限:12/3まで */
/* [5] その他の制限: */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define N 100
void
pp(char v[][20])
{
int i;
for (i = 0; *v[i]; i++)
puts("\n---------------"); }
double
calc(char *s)
{
char *w
= malloc(N
), *p
, *p2
, v
[N
][20]; int i, diff;
double acc;
/* ()calc */
while ((p
= strchr(s
, ')'))) { p2 = p + 1;
*p = '\0';
while (*p != '(')
p--;
*p = '\0';
sprintf(w
, "%s %g %s", s
, calc
(p
+ 1), p2
); }
/* tokenize */
i = 0;
while (p) {
i++;
}
/* log calc */
for (i = 0; *v[i]; i++) {
if (strcmp("log", v
[i
]) == 0) { }
}
/* sin calc */
for (i = 0; *v[i]; i++) {
if (strcmp("sin", v
[i
]) == 0) { }
}
/* cos calc */
for (i = 0; *v[i]; i++) {
if (strcmp("cos", v
[i
]) == 0) { }
}
/* tan calc */
for (i = 0; *v[i]; i++) {
if (strcmp("tan", v
[i
]) == 0) { }
}
/* compact */
diff = 0;
for (i = 0; *v[i]; i++) {
if (*v[i] == ' ')
diff++;
if (diff && *v[i] != ' ')
}
/* - calc */
for (i = 0; *v[i]; i++) {
}
}
/* / calc */
for (i = 0; *v[i]; i++) {
}
}
/* * calc */
for (i = 0; *v[i]; i++) {
}
}
/* pp(v); */
/* + calc */
acc = 0;
for (i = 0; *v[i]; i++)
return acc;
}
char *
insertBlank(char *s)
{
*w = '\0';
while (*s) {
} else {
}
s++;
}
return w;
}
double
dentaku(char *s)
{
return calc(insertBlank(s));
}
int
main(int argc,char**argv)
{
char *s[] = {
"2 + 5 / 5 + 4 * 2 + 1",
"2 * 3 + 1",
"( 2 + 3 ) * 7 * ( 3 - 1 )",
"sin(90) * 2 + 1 - 2",
"log(3) * 2 + 1",
"1-2*(3+1)-2",
"log(log(123))",
"log log 123",
"1+( log(2 *sin (5*log (2)+1)/2)+3*4)"
};
int i;
for (i = 0; i<9; i++)
printf("input = %s\nresult = %g\n\n", s
[i
], dentaku
(s
[i
]));
return 0;
}
LyogOTc5IOWQjeWJjTog44OH44OV44Kp44Or44OI44Gu5ZCN54Sh44GX44GV44KTICBNYWlsOiBzYWdlIOaKleeov+aXpTogMjAxMS8xMS8yOSjngaspIDIxOjE3OjQyLjA2ICAgKi8KLyogWzFdIOaOiOalreWNmOWFg++8muOCs+ODnuODs+ODieODqeOCpOODs+W8leaVsCAqLwovKiBbMl0g5ZWP6aGM5paHKOWQq+OCs+ODvOODiSbjg6rjg7Pjgq8p77yaICovCi8qIOOCs+ODnuODs+ODieODqeOCpOODs+W8leaVsOOCkueUqOOBhOOBpu+8jOS7peS4i+OBruioiOeul+OBjOOBp+OBjeOCi+ewoeaYk+mbu+WNk+ODl+ODreOCsOODqeODoOOCkuS9nOaI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uaXplICovCiAgaSA9IDA7CiAgc3NjYW5mKHMsICIlcyIsIHZbaV0pOwogIHAgPSBzdHJ0b2socywgIiAiKTsKICB3aGlsZSAocCkgewogICAgc3ByaW50Zih2W2ldLCAiJXMiLCBwKTsKICAgIHAgPSBzdHJ0b2soTlVMTCwgIiAiKTsKICAgIGkrKzsKICB9CiAgc3ByaW50Zih2W2ldLCAiJXMiLCAiIik7CgogIC8qIGxvZyBjYWxjICovCiAgZm9yIChpID0gMDsgKnZbaV07IGkrKykgewogICAgaWYgKHN0cmNtcCgibG9nIiwgdltpXSkgPT0gMCkgewogICAgICBzcHJpbnRmKHZbaV0sICIlZyIsIGxvZzEwKGF0b2YodltpICsgMV0pKSk7CiAgICAgIHNwcmludGYodltpICsgMV0sICIgIik7CiAgICB9CiAgfQoKICAvKiBzaW4gY2FsYyAqLwogIGZvciAoaSA9IDA7ICp2W2ldOyBpKyspIHsKICAgIGlmIChzdHJjbXAoInNpbiIsIHZbaV0pID09IDApIHsKICAgICAgc3ByaW50Zih2W2ldLCAiJWciLCBzaW4oYXRvZih2W2kgKyAxXSkgLyAxODAgKiBNX1BJKSk7CiAgICAgIHNwcmludGYodltpICsgMV0sICIgIik7CiAgICB9CiAgfQoKICAvKiBjb3MgY2FsYyAqLwogIGZvciAoaSA9IDA7ICp2W2ldOyBpKyspIHsKICAgIGlmIChzdHJjbXAoImNvcyIsIHZbaV0pID09IDApIHsKICAgICAgc3ByaW50Zih2W2ldLCAiJWciLCBjb3MoYXRvZih2W2kgKyAxXSkgLyAxODAgKiBNX1BJKSk7CiAgICAgIHNwcmludGYodltpICsgMV0sICIgIik7CiAgICB9CiAgfQoKICAvKiB0YW4gY2FsYyAqLwogIGZvciAoaSA9IDA7ICp2W2ldOyBpKyspIHsKICAgIGlmIChzdHJjbXAoInRhbiIsIHZbaV0pID09IDApIHsKICAgICAgc3ByaW50Zih2W2ldLCAiJWciLCB0YW4oYXRvZih2W2kgKyAxXSkgLyAxODAgKiBNX1BJKSk7CiAgICAgIHNwcmludGYodltpICsgMV0sICIgIik7CiAgICB9CiAgfQoKICAvKiBjb21wYWN0ICovCiAgZGlmZiA9IDA7CiAgZm9yIChpID0gMDsgKnZbaV07IGkrKykgewogICAgaWYgKCp2W2ldID09ICcgJykKICAgICAgZGlmZisrOwogICAgaWYgKGRpZmYgJiYgKnZbaV0gIT0gJyAnKQogICAgICBzcHJpbnRmKHZbaSAtIGRpZmZdLCAiJXMiLCB2W2ldKTsKICB9CiAgc3ByaW50Zih2W2kgLSBkaWZmXSwgIiVzIiwgIiIpOwoKICAvKiAtIGNhbGMgKi8KICBmb3IgKGkgPSAwOyAqdltpXTsgaSsrKSB7CiAgICBpZiAoc3RyY21wKCItIiwgdltpXSkgPT0gMCkgewogICAgICBzcHJpbnRmKHZbaV0sICIrIik7CiAgICAgIHNwcmludGYodltpICsgMV0sICIlZyIsIC1hdG9mKHZbaSArIDFdKSk7CiAgICB9CiAgfQoKICAvKiAvIGNhbGMgKi8KICBmb3IgKGkgPSAwOyAqdltpXTsgaSsrKSB7CiAgICBpZiAoc3RyY21wKCIvIiwgdltpXSkgPT0gMCkgewogICAgICBzcHJpbnRmKHZbaV0sICIqIik7CiAgICAgIHNwcmludGYodltpICsgMV0sICIlZyIsIDEgLyBhdG9mKHZbaSArIDFdKSk7CiAgICB9CiAgfQoKICAvKiAqIGNhbGMgKi8KICBmb3IgKGkgPSAwOyAqdltpXTsgaSsrKSB7CiAgICBpZiAoc3RyY21wKCIqIiwgdltpXSkgPT0gMCkgewogICAgICBzcHJpbnRmKHZbaV0sICIgIik7CiAgICAgIHNwcmludGYodltpICsgMV0sICIlZyIsIGF0b2YodltpIC0gMV0pICogYXRvZih2W2kgKyAxXSkpOwogICAgICBzcHJpbnRmKHZbaSAtIDFdLCAiMCIpOwogICAgfQogIH0KCiAgLyogcHAodik7ICovCgogIC8qICsgY2FsYyAqLwogIGFjYyA9IDA7CiAgZm9yIChpID0gMDsgKnZbaV07IGkrKykKICAgIGFjYyArPSBhdG9mKHZbaV0pOwogIHJldHVybiBhY2M7Cn0KCmNoYXIgICAgICAgICAgICoKaW5zZXJ0QmxhbmsoY2hhciAqcykKewogIGNoYXIgICAgICAgICAgICp3ID0gbWFsbG9jKE4pOwoKICAqdyA9ICdcMCc7CiAgd2hpbGUgKCpzKSB7CiAgICBpZiAoc3RyY2hyKCIrLSovKCkiLCAqcykpIHsKICAgICAgc3RyY2F0KHcsICIgIik7CiAgICAgIHN0cm5jYXQodywgcywgMSk7CiAgICAgIHN0cmNhdCh3LCAiICIpOwogICAgfSBlbHNlIHsKICAgICAgc3RybmNhdCh3LCBzLCAxKTsKICAgIH0KICAgIHMrKzsKICB9CgogIHJldHVybiB3Owp9CgoKZG91YmxlCmRlbnRha3UoY2hhciAqcykKewogIHJldHVybiBjYWxjKGluc2VydEJsYW5rKHMpKTsKfQoKaW50Cm1haW4oaW50IGFyZ2MsY2hhcioqYXJndikKewoKICBjaGFyICAgICAgICAgICAqc1tdID0gewogICAgIjIgICsgIDUgIC8gIDUgICsgIDQgICAqICAgMiAgICsgICAxIiwKICAgICIyICogMyArIDEiLAogICAgIiggMiArIDMgKSAqIDcgKiAoIDMgLSAxICkiLAogICAgInNpbig5MCkgKiAyICsgMSAtIDIiLAogICAgImxvZygzKSAqIDIgKyAxIiwKICAgICIxLTIqKDMrMSktMiIsCiAgICAibG9nKGxvZygxMjMpKSIsCiAgICAibG9nIGxvZyAxMjMiLAogICAgIjErKCAgbG9nKDIgKnNpbiAoNSpsb2cgKDIpKzEpLzIpKzMqNCkiCiAgfTsKICBpbnQgICAgICAgICAgICAgaTsKICBmb3IgKGkgPSAwOyBpPDk7IGkrKykKICAgIHByaW50ZigiaW5wdXQgPSAlc1xucmVzdWx0ID0gJWdcblxuIiwgc1tpXSwgZGVudGFrdShzW2ldKSk7CgogIHJldHVybiAwOwp9