debugging 使用like ./double square 8时给出错误结果

j1dl9f46  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(96)

我正在编写程序来执行一些链操作,但当我运行时,它给出了错误的结果,就像程序被命名为double.c一样

./double square 2
output : 4

但当我像这样

./double square 8
output:0
expected output:256

我已经使用了分叉和一些其他的是我的程序必须使用它们的要求,请你能发现任何逻辑错误吗
我正在尝试执行像./double square square .... 8这样的链式操作,但它没有按我想要的方式工作。我尝试的是

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <math.h>

#include <sys/wait.h>
#include <stdlib.h>
#define EPSILON 0.001

double simple_abs(double x)
{
    return x > 0 ? x : -x;
}

double simple_sqrt(double x)
{
    double previous = 0;
    double guess = x;

    while (simple_abs(guess - previous) > EPSILON)
    {
        previous = guess;
        guess = previous - (previous * previous - x) / (2 * previous);
    }

    return guess;
}
int lop(int status) {
    return (status >> 8) & 0xff;
}

int square(int n) {
printf("number = %d  and square = %d", n , n*n);
    return n * n;
}

int root(int n) {
    return (int) simple_sqrt((double) n);
}

int doubleVal(int n) {
    return n * 2;
}

int main(int argc, char *argv[]) {
    int value = atoi(argv[argc - 1]);
    int result = value;
    if (argc > 2){
    result = doubleVal(result);
    for (int i = 1; i < argc - 1; i++) {
int pid = fork();
        if (pid == 0) {
            if (*argv[i] == 's' && *(argv[i]+1) == 'q' && *(argv[i]+2) == 'u' && *(argv[i]+3) == 'a' && *(argv[i]+4) == 'r' && *(argv[i]+5) == 'e' && *(argv[i]+6) == '\0'){
                result = square(result);
            } else if (argv[i][0] == 'r' && argv[i][1] == 'o' && argv[i][2] == 'o' && argv[i][3] == 't' && argv[i][4] == '\0')  {
                result = root(result);
            } else if (argv[i][0] == 'd' && argv[i][1] == 'o' && argv[i][2] == 'u' && argv[i][3] == 'b' && argv[i][4] == 'l' && argv[i][5] == 'e' && argv[i][6] == '\0') {
                result = doubleVal(result);
            } else {
                printf("Unknown operation: %s\n", argv[i]);
                exit(1);
            }
            exit(result);
        } else {
            int status;
            wait(&status);
            result = (status >> 8) & 0xff;
        }
    }
    }
    else {
    result = doubleVal(result);
    }

    printf("Result: %d\n", result);
    return result;
}
olhwl3o2

olhwl3o21#

你在循环之前把结果加倍了。这是不对的。在square()输出中添加了一个新行,并且使用strcmp()作为原始代码,这让我的眼睛很痛。

#include <string.h>

int square(int n) {
    printf("number = %d  and square = %d\n", n , n*n);
    return n * n;
}

int main(int argc, char *argv[]) {
    int value = atoi(argv[argc - 1]);
    int result = value;
    if (argc > 2){
        for (int i = 1; i < argc - 1; i++) {
            if(fork()) {
                int status;
                wait(&status);
                result = (status >> 8) & 0xff;
            } else {
                if(!strcmp(argv[i], "square"))
                    result = square(result);
                else if(!strcmp(argv[i], "root"))
                    result = root(result);
                else if(!strcmp(argv[i], "double"))
                    result = doubleVal(result);
                else {
                    printf("Unknown operation: %s\n", argv[i]);
                    exit(1);
                }
                exit(result);
            }
        }
    } else
        result = doubleVal(result);

    printf("Result: %d\n", result);
    return result; // 0 for success but I digress
}

考虑使用atol(),因为你不能检测atoi()输入中的错误。目前,如果没有命令行参数,你最终会执行atoi(*argv),它可能会计算为0,除非你的程序以数字开头。也许显式检查argv > 1
示例会话:

$ ./a.out double square root 2
number = 4  and square = 16
Result: 4

相关问题