Also versuche ich, mich selbst zu unterrichten, und ich habe Online-Laborübungen gemacht, um es zu lernen. Ich habe ein Programm geschrieben, das ziemlich detailliert auf Arrays und Strukturen eingeht. Es verwendet Zufallszahlen, um auf einen Spike von 100 psi zu überwachen, und druckt das dann als 0-Punkt und druckt die vorherigen 10 Sekunden des Arrays und die nächsten 10, als ob ich Daten sammeln würde. Der nächste Teil dieser Übung besteht darin, die print-Anweisung des Programms zu übernehmen, sie in eine externe Datei schreiben zu lassen und sie dort auszudrucken. Mein Denkprozess besteht darin, ein Array in der Ausdruckfunktion zu füllen, das die aus der Datei gelesenen Werte enthält und dann von dem Array auf den Bildschirm druckt. Aber ich bin mir nicht sicher, wie das aussehen würde oder wie ich es wirklich erreichen könnte. Wenn jemand mir in die richtige Richtung zeigen oder eine gute Erklärung geben könnte, würde es sehr geschätzt werden!Exportieren von Druckanweisungen in eine externe Datei
Mein Code so weit:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define MAX_CHANGE 100
#define ARRAY_SIZE 21
typedef struct data_slice
{
int t; // -> Time
float tp; // -> Valve pressure
float tf; // -> Sodium flow
float tt; // -> Sodium temp in Celsius
} data_slice;
// Function Declarations
void get_values(float * pressure, float * flow, float * temp);
void printIt(data_slice * data);
void initializeArray(data_slice * data);
bool spikeValueRecorded(data_slice * data, int outputIndex);
int main()
{
srand((unsigned int)time(NULL));
data_slice data[ARRAY_SIZE];
int index = -1;
while (1)
{
// Initialize the entire array
initializeArray(data);
// If there's a spike.....
if (spikeValueRecorded(data, index))
{
// Set the previous "time" in array to negatives
int temp = index;
for (int i = 0; i >= -10; --i)
{
data[temp].t = i;
temp = temp - 1;
if (temp < 0)
temp = temp + ARRAY_SIZE;
}
// Record for 10 more seconds
for (int i = 0; i <= 10; ++i)
{
data[index].t = i;
index = (index + 1) % ARRAY_SIZE; // Increment the index of the circular array
get_values(&data[index].tp, &data[index].tf, &data[index].tt); // "Record" the values
}
break;
}
}
// Print the finished recording
printIt(data);
}
// Return: void
// in - Values of the data_slice struct
//
// Description: The three values of the struct (data_slice) to be filled in
void get_values(float * pressure, float * flow, float * temp)
{
*pressure = (float)(rand() % (700 - 500 + 1) + 500); // Range: 500 - 700
*flow = (float)(rand() % (20 - 10 + 1) + 10); // Range: 10 - 20
*temp = (float)(rand() % (200 - 100 + 1) + 100); // Range: 100 - 200
}
// Return: void
// in - The array of data_slice
//
// Description: Prints the entire array being passed in
void printIt(data_slice * data)
{
// Find the indice holding the time value of -10
int indice = 0;
for (int i = 0; i < ARRAY_SIZE; ++i)
{
if (data[i].t == -10)
{
indice = i;
break;
}
}
for (int i = 0; i < ARRAY_SIZE; ++i)
{
printf("%i\t %f\t %f\t %f\n", data[indice].t, data[indice].tp, data[indice].tf, data[indice].tt);
indice = (indice + 1) % ARRAY_SIZE;
}
}
// Return: void
// in - The array of data_slice
//
// Description: Initializes the entire array to random values and their times to 0
void initializeArray(data_slice * data)
{
for (int i = 0; i < ARRAY_SIZE; ++i)
{
data[i].t = 0;
get_values(&data[i].tp, &data[i].tf, &data[i].tt);
}
}
// Return: boolean
// in - The array of data_slice
// out - Indice of the pressure spike
//
// Description: Returns true if a positive spike in pressure has been recorded.
// outputIndex will hold the 0-indice of the pressure spike, else -1
bool spikeValueRecorded(data_slice * data, int outputIndex)
{
float oldValue = data[0].tp;
for (int i = 0; i < ARRAY_SIZE; ++i)
{
if (data[i].tp - oldValue < MAX_CHANGE)
{
outputIndex = i;
return true;
}
}
outputIndex = -1;
return false;
}
[Holen Sie sich ein paar gute Anfänger in Datei schreiben Bücher zu lesen] (http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), sollten sie alle Informationen enthalten, die Sie benötigen (und vieles mehr). –
Korrigieren Sie mich, wenn sie falsch verstanden werden. Sie wollten Array in Datei schreiben und dann aus Datei lesen und auf dem Bildschirm anzeigen? –
Mehr wie nehme ich meine Druckfunktion, lege sie in eine andere Datei im Projekt und lasse sie dann von dort drucken? Hier ist das Beispielproblem, das ich befolge: Die Ausdruckfunktion sollte nun in output-data.txt ausgedruckt werden. Entnehmen Sie den gesamten Code, der auf dem Bildschirm ausgegeben wird Schreiben Sie ein zweites Programm (d. H. KEINE Funktion im ersten Programm!), Die die Daten aus der Datei output-data.txt liest und auf dem Bildschirm ausgibt. –