我应该制作两个节目:
这两个程序都可以正常工作,当我尝试管道它们时它会停止工作(./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;
}
答案 0 :(得分:1)
Sort会覆盖./generate的前半部分条目。
答案 1 :(得分:1)
您的搜索程序太复杂了:对输入进行排序是没用的,尝试读取所有输入实际上是不正确的:
以下是修改后的版本:
#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;
}
}