【发布时间】:2014-03-05 20:05:30
【问题描述】:
我需要在 execl 中传递两个整数值,我将它们转换为 (const char*),然后传递它们。在 execl 中,我想将参数传递给其他 cpp 文件,然后这些程序将计算,然后它们将返回结果。
我认为 (add/min/mul/div) 程序在 execl 中调用不正确。
static int sum_calculation(tree *&root , tree *&root2, int value1 , int value2 , int result_tree)
{
if(root->l_child == NULL && root->r_child == NULL)
return root->data;
else
{
value1 = sum_calculation(root->l_child , root2 , value1 , value2 , result_tree);
value2 = sum_calculation(root->r_child , root2 , value1 , value2 , result_tree);
stringstream str1 , str2;
str1 << value1;
str2 << value2;
string temp_str1 = str1.str();
string temp_str2 = str2.str();
const char* ch1 = (char*) temp_str1.c_str();
const char* ch2 = (char*) temp_str2.c_str();
int fd2[2];
pipe(fd2);
if(fork() == 0)
{
const char *adder = "add";
const char *multiplier = "mul";
const char *subtractor = "min";
const char *divider = "div";
if(root->data == 43)
execl(adder , ch1 , ch2);
else if(root->data == 45)
execl(subtractor , ch1 , ch2);
else if(root->data == 42)
execl(multiplier , ch1 , ch2);
else if(root->data == 47)
execl(divider , ch1 , ch2);
close(fd2[0]);
write(fd2[1] , &result_tree , sizeof(result_tree));
exit(0);
}
else
{
close(fd2[1]);
read(fd2[0] , &result_tree , sizeof(result_tree));
//wait();
}
root->data = result_tree;
delete_node(root2 , root);
return result_tree;
}
}
添加函数为:
#include <sstream>
#include <string.h>
#include<iostream>
using namespace std;
int main(int argc , char *argv[])
{
int Result , value1 , value2;
stringstream convert1(argv[1]);
stringstream convert2(argv[2]);
if ( !(convert1 >> value1) )
Result = 0;
if ( !(convert2 >> value2) )
Result = 0;
Result = value1 + value2;
return Result;
}
min(subtratction) / mul / div cpp's 类似于 add 谢谢你的帮助。
【问题讨论】: