[英]Can't see program's output in the terminal after using fork and pipe in c
[英]I can't see the problem with my program's output even after 6 hours of programming
該程序通過首先獲取數據量及其值,然后按降序對其進行排序,最后求和,從而處理“實驗科學數據”(實際上只是整數)。
問題是由於某種原因,數據在輸出中被歸零(我無法弄清楚)。 我確實認為問題出在sort_data函數中。 但是我可能是錯的。 我絲毫不知道為什么要這么做。
該程序應該這樣輸出
========================================================
Program Number: 3
Programmer:
PCC Course Number: CS227
========================================================
Miscellaneous operations on your two whole numbers
This program processes experimental scientific data.
- - - - - - - - - - - - - - - - - - - - - - - - - -
How many data values are there (2 to 100, 0 = quit): 4
Enter data value 1: 3
Enter data value 2: 5
Enter data value 3: 4
Enter data value 4: 8
The data in descending order (with duplicates noted):
- - - - - - - - - - - - - - - - - - - - - - - - - - -
3.00
4.00 (duplicate)
5.00 (duplicate)
8.00 (duplicate)
---------
20.00 total
但它是這樣輸出的
========================================================
Program Number: 3
Programmer:
PCC Course Number: CS227
========================================================
Miscellaneous operations on your two whole numbers
This program processes experimental scientific data.
- - - - - - - - - - - - - - - - - - - - - - - - - -
How many data values are there (2 to 100, 0 = quit): 4
Enter data value 1: 3
Enter data value 2: 5
Enter data value 3: 4
Enter data value 4: 8
The data in descending order (with duplicates noted):
- - - - - - - - - - - - - - - - - - - - - - - - - - -
0.00
0.00 (duplicate)
0.00 (duplicate)
0.00 (duplicate)
---------
0.00 total
我無法弄清楚這個問題,我已經嘗試了好幾個小時才結束。 我失去理智。 請幫助我快死了。
/**********************************************************************/
/* */
/* This program processes experimental scientific data by first */
/* getting the quantity of data and their values, sort them in */
/* descending order, and finally summing. */
/* */
/**********************************************************************/
#include <stdio.h> /* printf, scanf */
#include <stdlib.h> /* malloc, free, exit(0) */
#include <string.h> /* memcpy */
/**********************************************************************/
/* Symbolic Constants */
/**********************************************************************/
#define COURSE_NUMBER "CS227" /* PCC assigned course number */
#define PROGRAM_NUMBER 3 /* Teacher assigned program number */
#define LAST_NAME "Lokey" /* The Programmer's last name */
#define MAX_CHOICE 100 /* Max choice */
#define MIN_CHOICE 2 /* Minimum choice */
#define DATA_ALLOC_ERR 1 /* Cannot allocate data memory */
#define DATA_SORT_ERR 2 /* Cannot allocate sort memory */
#define QUIT 0 /* Program value to quit */
/**********************************************************************/
/* Function Prototypes */
/**********************************************************************/
void print_heading(); /* Print the program heading */
void print_instructions(); /* Prints program instructions */
int retrive_quantity(); /* Get data quantity */
void get_data(float *p_data_start, int quantity);
/* Get data values */
void sort_data(float *p_data_start, int quantity);
/* Sorts data in order */
void prints_data(float *p_data_start, int quantity);
/* Prints the data */
float sum_data(float *p_data_start, int quantity);
/* Sums the data */
void print_sum(float sum); /* Prints data's sum */
/**********************************************************************/
/* Main Function */
/**********************************************************************/
int main()
{
float *p_data; /* Points to the data */
int quantity; /* Quantity of data values */
/* Prints program heading */
printf("\n\n\n\n\n\n");
print_heading();
/* Loops processing data until user quits */
while(print_instructions(), (quantity = retrive_quantity()) != QUIT)
{
/* Allocate memory for the data and then aborts */
/* program with errors if memory could not be allocated */
if((p_data = (float *)malloc(sizeof(*p_data) * quantity)) == NULL)
{
printf("\nError %d in main.", DATA_ALLOC_ERR);
printf("\nCannot allocate memory for the data.");
printf("\nThe program is aborting.");
exit (DATA_SORT_ERR);
}
/* Retrieves, sorts, and sums the data */
get_data (p_data, quantity);
sort_data (p_data, quantity);
prints_data (p_data, quantity);
print_sum (sum_data(p_data, quantity));
/* Releases the data */
free(p_data);
}
/* Thanks and says goodbye to the user */
printf("\nThanks for your processing data. Have a nice day!");
printf("\n\n\n\n\n");
return 0;
}
/**********************************************************************/
/* Prints the program instructions */
/**********************************************************************/
void print_instructions()
{
printf("\nThis program processes experimental scientific data.");
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - ");
return;
}
/**********************************************************************/
/* Retrieves data quantity */
/**********************************************************************/
int retrive_quantity()
{
int quantity; /* Quantity of data values */
do
{
printf("\nHow many data values are there (%d to %d, %d = quit): ",
MIN_CHOICE, MAX_CHOICE, QUIT);
scanf(" %d", &quantity);
}
while((quantity < MIN_CHOICE || quantity > MAX_CHOICE) && quantity
!= QUIT);
return quantity;
}
/**********************************************************************/
/* Retrieves data values */
/**********************************************************************/
void get_data(float *p_data_start, int quantity)
{
float *p_data; /* Points to every data value */
for (p_data = p_data_start; (p_data - p_data_start) < quantity;
p_data++)
{
printf("\n Enter data value %d: ", (int)(p_data - p_data_start)
+ 1);
scanf(" %f", p_data);
if(*p_data < 0.0f)
{
printf("\nNegative %.2f ", *p_data);
*p_data = -*p_data;
printf("converted to positive is %.2f", *p_data);
}
}
return;
}
/**********************************************************************/
/* Sorts the data into descending order */
/**********************************************************************/
void sort_data(float *p_data_start, int quantity)
{
float *p_data, /*Points to the data */
*p_greatest, /*Points to greatest data */
*p_sort, /* Points to sorted data */
*p_sort_start; /* Points to start of data */
if((p_sort_start = (float *)malloc(sizeof(*p_data) * quantity))
== NULL)
{
printf("\nError %d in main.", DATA_ALLOC_ERR);
printf("\nCannot allocate memory for the data.");
printf("\nThe program is aborting.");
exit (DATA_SORT_ERR);
}
for(p_sort = p_data_start; (p_sort - p_sort_start) < quantity;
p_sort++)
{
*p_sort = 0.0f;
for(p_data = p_data_start; (p_data - p_data_start) < quantity;
p_data++)
{
if(*p_data > *p_sort)
{
*p_sort = *p_data;
p_greatest = p_data;
}
}
*p_greatest = 0.0f;
}
memcpy(p_data_start, p_sort_start, sizeof(*p_data) * quantity);
free(p_sort_start); /* Release the memory allocated to the data */
return;
}
/**********************************************************************/
/* Print all data values */
/**********************************************************************/
void prints_data(float *p_data_start, int quantity)
{
float *p_data; /* Points to the data */
printf("\n\nThe data in descending order (wiht duplicates noted):");
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - -");
for(p_data = p_data_start; (p_data - p_data_start) < quantity;
p_data++)
{
printf("\n %9.2f", *p_data);
if(p_data != p_data_start)
if(*p_data == *(p_data - 1))
printf(" (duplicate)");
}
return;
}
/**********************************************************************/
/* Sum the data */
/**********************************************************************/
float sum_data(float *p_data_start, int quantity)
{
float *p_data, /* Points to the data */
sum = 0.0f; /* Sum of all data */
for(p_data = p_data_start; (p_data - p_data_start) < quantity;
p_data++)
sum += *p_data;
return sum;
}
/**********************************************************************/
/* Prints the data sum */
/**********************************************************************/
void print_sum(float sum)
{
printf("\n ---------");
printf("\n %9.2f total", sum);
return;
}
for(p_sort = p_data_start; (p_sort - p_sort_start) < quantity;
p_sort++)
p_data_start
在不同的陣列不是點p_sort_start
所以這是錯誤。 而且,您從未真正將數據從main()復制到p_sort_start
數組中。 另外,您可以在排序循環中將內容設置為零,這也沒有意義。
我不確定為什么首先需要分配第二個緩沖區,只需就地對數組進行排序即可。
正如其他答案所指出的那樣,您的排序功能不必要地復雜且容易出錯。
您可以簡單地使您的排序功能如下。
void sort_data(float *p_data_start, int quantity)
{
float temp;
size_t i=0,j=0;
for( i=0; i < quantity; i++)
{
for(j=0; j < quantity-i-1; j++)
{
if(p_data_start[j] > p_data_start[j+1])
{
temp = p_data_start[j];
p_data_start[j] = p_data_start[j+1];
p_data_start[j+1] = temp;
}
}
}
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.