我想知道如何打印出这个巨大的圆形缓冲区。前 3 个块是结构,而 while 循环在 main 中,这只是我的代码的一部分。我现在不关注格式或输出。这只是我应该得到的一个想法。这条线是我关注的地方:
// copies from and to destination of the string of the timestamp and the value.
strcpy(chart.buffer[asci].reading[chart.buffer[asci].end].timestamp,
current.field[1]);
我的输入是:
5、08:00:00、2、60 5、08:00:01、5、15 5, 08:00:10, 1, 986 5, 08:05:24, 2, 61 5、08:07:21、2、58 5, 08:10:11, 6, 0
我的输出应该是:
患者 ID = 5 的读数为: 温度: 08:00:10: 98.6 心率: 08:00:00:60 08:05:24:61 08:07:21:58 收缩压 舒张压 呼吸频率 08:00:11:15
/*
* Health Monitoring System
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define MAXPATIENTS 5
#define MAXREADINGS 10
#define MAXTYPES 5
#define MAXTIME 8
/* One health type reading: timestamp + actual value */
typedef struct{
char timestamp[MAXTIME+1];
int value;
}Element;
/* Circular buffer of health type readings */
typedef struct{
int start; /* index of oldest reading */
int end; /* index of most current reading */
Element reading[MAXREADINGS];
}CircularBuffer;
/* Patient's health chart: ID + multiple health type readings */
typedef struct{
int id;
CircularBuffer buffer[MAXTYPES];
}Chart;
/*
* Health records for all patients defined here.
* The variable record is visible to all functions
* in this file, i.e. it is global.
*/
Chart record[MAXPATIENTS];
typedef enum{false,true} bool;
typedef char f_string[30] ;
typedef struct{
int nfields ;
f_string field[30] ;
}health_line;
bool is_end_of_field(char ch){
return (ch == ',') || (ch == '\n') || (ch == EOF);
}
int min(int x, int y){
return x < y ? x : y;
}
//read the next field from standard input
int get_field(f_string field){
int nextIndex;
int next_char;
next_char = getchar();
for(nextIndex = 0; ! is_end_of_field(next_char); nextIndex++){
field[nextIndex] = next_char;
next_char = getchar();
}
field[nextIndex] = '\0';
return next_char;
}
// reads in a csv line and fields is filled with data
health_line get_line(){
health_line line;
int currentChar = 0;
int lastChar;
lastChar = get_field(line.field[currentChar++]);
while(lastChar == ','){
lastChar = get_field(line.field[currentChar++]);
}
line.nfields = (lastChar == EOF) ? 0: currentChar;
return line;
}
void print_health(health_line data){
int i =0;
for(i = 0; i <data.nfields; i++){
printf("%s \n", data.field[i]);
}
}
void print_line(health_line data){
int type = atoi(data.field[2]);
double tempType = atoi(data.field[3]);
int patientID = atoi(data.field[0]);
int value = atoi(data.field[3]);
if(type == 1){
tempType = tempType/10.0;
printf("%s Temperature for Patient ID = %d is %.1f \n",data.field[1],patientID,tempType );
}
else if(type == 2){
printf("%s Heart Rate for Patient ID = %d is %d \n",data.field[1],patientID,value );
}
else if(type == 3){
printf("%s Systolic Pressure for Patient ID = %d is %d \n",data.field[1],patientID,value );
}
else if(type == 4){
printf("%s Diastolic Pressure for Patient ID = %d is %d \n",data.field[1],patientID,value );
}
else if(type == 5){
printf("%s Respiration Rate for Patient ID = %d is %d \n",data.field[1],patientID, value );
}
else if(type == 6){
printf("%s Print command entered for Patient ID = %d \n", data.field[1], patientID);
}
}
void main(health_line data){
int i, j;
/* initialize health data records for each patient */
for( i=0; i < MAXPATIENTS; i++ ){
record[i].id = i + 1;
for( j=0; j < MAXTYPES; j++ ){
record[i].buffer[j].start = 0;
record[i].buffer[j].end = 0;
}
}
printf("Welcome to the Health Monitoring System\n\n");
/*
* YOUR CODE GOES HERE:
* (1) Read a csv line of health data from stdin
* (2) Parse csv line into appropriate fields
* (3) Store health data in patient record or print if requested
* (4) Continue (1)-(3) until EOF
*
*/
health_line arrayTotal[30];
health_line current;
current = get_line();
arrayTotal[0] = current;
int k = 0;
int Index = 0;
int printValue = atoi(data.field[2]);
health_line temp;
int asci;
asci = atoi(current.field[2]);
int printAsci;
printAsci = atoi(current.field[2]);
double tempValue = 0.0;
tempValue = atof(current.field[3]);
int patientID;
Chart chart;
int valueType;
//stores the current line read in to a temp array
while(asci != 6){
current = get_line();
asci = atoi(current.field[2]);
patientID = atoi(current.field[0]);
arrayTotal[asci - 1] = current;
//copies from and to destination of the string of the timestamp and the value.
strcpy(chart.buffer[asci].reading[chart.buffer[asci].end].timestamp, current.field[1]);
//why is this line giving me seg fault
// chart.buffer[asci].reading[chart.buffer[asci].end].value = atoi(current.field[3]);
if(chart.buffer[asci].start == chart.buffer[asci].end){
chart.buffer[asci].start++;
}
chart.buffer[asci].end++;
}
printf("\nReading for Patient ID = %d\n",patientID);
//attempt to print out the result of the strcpy
//printf("%s",chart.buffer[asci]);
/*
//checks the second field within an array to compare it to a condition to print out a certain string for a category
int p;
int l;
for(p = 0; p < 8; p++){
health_line temp;
temp = arrayTotal[p];
int type;
type = atoi(temp.field[2]);
if(type == 1){
printf("Temperature:\n%s: %.1f\n",temp.field[1],tempValue/10.0);
}
else if(type == 2){
printf("Heart Rate:\n%s: %s\n", temp.field[1],temp.field[3]);
}
else if(type == 3){
printf("Systolic Pressure:\n%s %s\n", temp.field[1],temp.field[3]);
}
else if(type == 4){
printf("Diastolic Pressure:\n%s: %s\n", temp.field[1], temp.field[3]);
}
else if(type == 5){
printf("Respiration Rate:\n%s: %s\n", temp.field[1], temp.field[3]);
}
}*/
}