使用execlp系统调用制作计算器

时间:2015-03-09 14:18:28

标签: unix exec

我在exec()的帮助下在UNIX上制作计算器。 以下是我的代码:

#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<cstdlib>
using namespace std;

int main(int argc,char *argv[])
{
    int j=0,i=0;
    int y;
    pid_t pid;
    if(argv[j][i]=='+')
    { //checks if add/difference etc

        pid_t p1= fork(); // creates a process

        if(p1==0)
        {
            cout<<"child"<<endl;
            char arg1=argv[j][i];
            char arg2=argv[j+1][i];

            int sum=execlp("sum","arg1","arg2",NULL);
            cout<<sum;
            exit(y);
        }
        else
        {
            cout<<"parent";
            pid= wait(&y);
            cout<<pid<<endl;
        }
        j++;
        i++;
    }
    else if(argv[j][i]=='*')
    {
        pid_t p1= fork();

        if(p1==0)
        {
            cout<<"child"<<endl;
            char arg1=argv[j][i];
            char arg2=argv[j+1][i];

            int sum=execlp("division","arg1","arg2",NULL);
            cout<<sum;
            exit(y);
        }
        else
        {
            cout<<"parent";
            pid= wait(&y);
            cout<<pid<<endl;
        }

        j++;
        i++;
    }
    else if(argv[j][i]=='-')
    {
        pid_t p1= fork();

        if(p1==0)
        {
            cout<<"child"<<endl;
            char arg1=argv[j][i];
            char arg2=argv[j+1][i];

            int sum=execlp("difference","arg1","arg2",NULL);
            cout<<sum;
            exit(y);
        }
        else
        {
            cout<<"parent";
            pid= wait(&y);
            cout<<pid<<endl;
        }

        j++;
        i++;
    }
    else if(argv[j][i]=='/')
    {
        pid_t p1= fork();

        if(p1==0)
        {
            cout<<"child"<<endl;
            char arg1=argv[j][i];
            char arg2=argv[j+1][i];

            int sum=execlp("multiply","arg1","arg2",NULL);
            cout<<sum;
            exit(y);
        }
        else
        {
            cout<<"parent";
            pid= wait(&y);
            cout<<pid<<endl;
        }    

        j++;
        i++;
    }

    return 0;
}

但它没有显示答案! 我的问题是如何解析参数,如果我输入9 + 8然后如何解析?并获得所需的输出?我也使用正确的execlp命令并向其传递正确的参数吗?

p.sum,difference,division和multiply是我的.cpp文件。

1 个答案:

答案 0 :(得分:1)

这是一个沿着相同路线的C解决方案,看看你是否可以猜到它是如何工作的!

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    for (--argc; argc > 0; --argc)
    {
        FILE *file = fopen("_.c", "w");
        fprintf(file,
            "#include <stdio.h>\n"
            "#include <math.h>\n"
            "int main()\n"
            "{\n"
                "\tdouble a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;\n"
                "\tprintf(\"%%f\\n\", (double)(%s));\n"
                "\treturn 0;\n"
            "}\n",
                argv[argc]);
        fclose(file);

        system("gcc _.c -lm -o _ ; ./_");
    }

    return 0;
}

如何编译:

gcc -o calc calc.c

样品运行:

$ ./calc '(4.0 + 3.0 - 5.0)/9.0'
0.222222

$ ./calc 'p = sin(1.5), q = cos(1.5), p * p + q * q'
1.000000

从右到左输出多个命令:

$ ./calc '5 - 6' '8 * 3' '2 + 5'
7.000000
24.000000
-1.000000

一个问题,你必须明确指出常量是浮点数,特别是如果涉及除法:

$ ./calc '1.0 / 3.0'
0.333333

$ ./calc '1 / 3'
0.000000