您有两个问题。一:
我怎么能读罪,从文件
日志等其他:
我怎么能......把它们放到数组
第一个问题:从文件中读取。使用strtok
将一行文本分隔为“单词”。使用strcmp
来检查函数名称。
int main(){
char str[] = "2 3 +";
char *p;
for (p = strtok(str, " "); p != NULL; p = strtok(NULL, " ")) {
if (isdigit(*p)) { // it's a number
long val = strtol(p, &p, 10);
printf("%ld\n", val);
} else { // it's a name of a function
if (strcmp(p, "+") == 0)
puts("Add");
else if (strcmp(p, "-") == 0)
puts("Subtract");
else if (strcmp(p, "sin") == 0)
puts("Sine");
else if (strcmp(p, "log") == 0)
puts("Logarithm");
else if ...
...
else
fputs("Error!", stderr);
}
}
}
第二个问题:在添加到阵列。我建议使用tagged union:
enum type {CONSTANT, UNARY_FUNCTION, BINARY_FUNCTION, X};
struct operation
{
enum type type;
union {
double val;
double (*func1)(double); // a pointer to a function with 1 argument
double (*func2)(double, double); // a pointer to a function with 2 arguments
};
};
在这里,有4种可能的类型的“操作” - 它可以是一个数,一个一元函数(像sin
),一个二进制函数(像+
)或独立的变量x
。
要令牌p
转换为operation
:
char *p;
struct operation o;
...
if (isdigit(*p)) { // it's a number
o.type = CONSTANT;
o.val = strtol(p, &p, 10);
} else { // it's a name of a function or "x"
if (strcmp(p, "x") == 0)
{
o.type = X;
}
else if (strcmp(p, "+") == 0)
{
o.type = BINARY_FUNCTION;
o.func2 = plus;
}
else if (strcmp(p, "sin") == 0)
{
o.type = UNARY_FUNCTION;
o.func1 = sin;
}
else if (strcmp(p, "log") == 0)
{
o.type = UNARY_FUNCTION;
o.func1 = log;
}
...
}
这里的功能plus
增加了两个数字。标准库没有它(不像sin
,它有),所以我必须自己定义它:最后
double plus(double x, double y) {return x + y;}
,如果你有operation
对象的数组:
struct operation my_array[1000];
您可以添加对象数组:
struct operation my_array[1000];
size_t my_array_size = 0;
for (...)
{
... // parse input file
struct operation o;
... // parse individual token and convert it to an "operation"
my_array[my_array_size++] = o; // add to the array
}
该计划的主要部分是USI ng数组计算任何值为x
的编码函数的值。现在你已经将你的函数编码为一个操作数组,计算本身将很容易。只需创建一个临时堆栈值并应用每个操作。
我无法解析您的示例字符串。函数应该以中缀格式('2 * sin(x * pi)')还是后缀('x pi sin 2 mult')给出? –
示例字符串仅用于读取数字。该函数应该以后缀格式给出。 – femix
你在记事本中写你的代码吗?使用一些IDE! –