代码之家  ›  专栏  ›  技术社区  ›  sruly

将两个程序与管道相结合将不起作用

  •  0
  • sruly  · 技术社区  · 7 年前

    我应该制作两个程序:

    • 首先,程序接受一个命令行参数,该参数是一个数字,然后在执行后接受更多输入,如果其中任何输入等于您输入的命令行编号,则返回true,否则返回false。

    这两个程序都能独立正确地工作,当我尝试流水线传输它们时,它会停止工作( ./generate 1000 50 | ./find 817 ).

    用法:

    ./find #
    

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

    ./generate [seed]
    

    种子生成。

    ./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;
        }
    }
    

    更多 查找.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;
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   chqrlie    7 年前

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

    • 排序是浪费时间,在读取时比较每个输入更简单、更快。
    • 一旦找到针,你就可以跳出这个循环。

    以下是修改后的版本:

    #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;
        }
    }
    
        2
  •  1
  •   sruly    7 年前

    排序正在覆盖来自/生成的前一半左右的条目。