将两个程序与管道结合起来是行不通的

Combining two programs with pipe won't work

我应该制作两个程序:

这两个程序都可以正确地独立工作,当我尝试对它们进行流水线处理时它停止工作 (./generate 1000 50 | ./find 817)。

用法:

./find #

如果找到数字:输出 true,否则输出 false

./generate [seed]

生成种子。

当我尝试组合这两个命令时它不起作用,我会看到 generate 命令生成了一个数字,并为该数字写了 ./find,但结果不是真的。

find.c

的源代码
/**
 * Prompts user for as many as MAX values until EOF is reached, 
 * then proceeds to search that "haystack" of values for given needle.
 *
 * Usage: ./find needle
 *
 * where needle is the value to find in a haystack of values
 */

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

#include "helpers.h"

// maximum amount of hay
const int MAX = 65536;

int main(int argc, string argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: ./find needle\n");
        return -1;
    }

    // remember needle
    int needle = atoi(argv[1]);

    // fill haystack
    int size;
    int haystack[MAX];
    for (size = 0; size < MAX; size++)
    {
        // wait for hay until EOF
        printf("\nhaystack[%i] = ", size);
        int straw = get_int();
        if (straw == INT_MAX)
        {
            break;
        }

        // add hay to stack
        haystack[size] = straw;
    }
    printf("\n");

    // sort the haystack
    sort(haystack, size);

    // try to find needle in haystack
    if (search(needle, haystack, size))
    {
        printf("\nFound needle in haystack!\n\n");
        return 0;
    }
    else
    {
        printf("\nDidn't find needle in haystack.\n\n");
        return 1;
    }
}

更多find.c源代码

 /**
 * helpers.c
 *
 * Helper functions for Problem Set 3.
 */

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)
{
    // TODO: implement a searching algorithm
    if(values[4] < 0) {
        return false; 
    }
    if(value < 4) {
        printf("Valid usage: ./search array value\n"); 
        return 52;
    }
    //

    for( long long i = 0 ; i < n ; i++ )
    {
        if (value == values[i])
        {
            return true;
        }

        printf("%i", values[i]);    
    }

    return false;
}

/**
 * Sorts array of n values.
 */


void sort(int values[], int n)
{

    int smallest = values[0];
    int smallestSpot = 0;
    for (long long i = 0; i < n ; i++)
    {

        for(long long j = i; j < n - i ; j++) //find the smallest int in array
        {

            if(values[j] < smallest)
            {
                smallestSpot = j;
                smallest = values[j];
            }

            values[smallestSpot] = values[i];
            values[i] = smallest;

        }

    }
    return;
}

./生成源代码

/**
 * generate.c
 *
 * Generates pseudo random numbers in [0,MAX), one per line.
 *
 * Usage: generate n [s]
 *
 * where n is number of pseudo random numbers to print
 * and s is an optional seed
 */

#define _XOPEN_SOURCE

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

// upper limit on range of integers that can be generated
#define LIMIT 65536

int main(int argc, string argv[])
{
    // Make sure user gave enough inputs
    if (argc != 2 && argc != 3)
    {
        printf("Usage: ./generate n [s]\n");
        return 1;
    }

    // convert the string that is inputted to an integer
    int n = atoi(argv[1]);

    // if user gives a seed, use that seed
    if (argc == 3)
    {
        srand48((long) atoi(argv[2]));
    }
    else 
    {
        srand48((long) time(NULL));
    }

    // create this amount of random inputs
    for (int i = 0; i < n; i++)
    {
        printf("%i\n", (int) (drand48() * LIMIT));
    }

    // success
    return 0;
}

排序正在覆盖 ./generate 的前半部分条目。

您的搜索程序太复杂:对输入进行排序是没有用的,为此目的尝试读取所有输入实际上是不正确的:

  • 将所有行读入数组是一种浪费space。
  • 排序是浪费时间,在读取时比较每个输入更简单、更快。
  • 你的排序算法是假的:你没有将最小值交换到下一个索引中,你只是覆盖那里的值,错位的值将丢失。
  • 一找到针就可以跳出循环。

这是修改后的版本:

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

#include "helpers.h"

// maximum amount of hay
const int MAX = 65536;

int main(int argc, char *argv[]) {

    // ensure proper usage
    if (argc != 2) {
        printf("Usage: ./find needle\n");
        return 2;
    }

    // convert needle
    int needle = atoi(argv[1]);
    int found = 0;

    // parse input
    for (int count = 0; count < MAX; count++) {
        // wait for hay until EOF
        int straw = get_int();
        if (straw == INT_MAX) {
            break;
        }
        if (straw == needle) {
            found = 1;
            break;
        }
    }

    // report status
    if (found) {
        printf("\nFound needle in haystack!\n\n");
        return 0;
    } else {
        printf("\nDidn't find needle in haystack.\n\n");
        return 1;
    }
}