#include #include #include struct table_line { double temperature; double pressure; double u_sub_f; double u_sub_g; }; void pause (void) { cout << "Hit any key to continue...\n"; getch (); } bool read_in_table (int capacity, table_line table[], int &table_size) { ifstream fin; char file_name[60]; double temperature_value; for (;;) { cout << "Enter name of input file: "; cin >> file_name; fin.open (file_name); if (!fin.fail()) { break; } cout << "Unable to open input file - please try again.\n"; } table_size = 0; // no values so far for (;;) { fin >> temperature_value; if (fin.fail()) { if (!fin.eof()) { cout << "\nThe input file contains a bad temperature value.\n"; return false; } if (table_size < 2) { cout << "The table contains fewer than two lines.\n"; return false; } return true; } if (table_size == capacity) { cout << "\nThe table is too large to be read..\n"; return false; } table[table_size].temperature = temperature_value; fin >> table[table_size].pressure >> table[table_size].u_sub_f >> table[table_size].u_sub_g; if (fin.fail()) { cout << "\nThe input files contains bad data or a partial line.\n"; return false; } table_size++; } } void output_results (double T, double pressure, double u_sub_f, double u_sub_g) { cout << "\nAt " << T << " degrees C:\n" << " Pressure = " << pressure << " bar\n" << " u sub f = " << u_sub_f << " KJ/kg\n" << " u sub f = " << u_sub_g << " KJ/kg\n"; } void calculate_and_output (int table_size, table_line table[], double T) { int low, high, mid; double k; if ((T < table[0].temperature) || (T > table[table_size - 1].temperature)) { cout << "Temperature is not within the range covered by the table.\n"; return; } low = 0; high = table_size - 1; for (;;) { if (high == (low + 1)) { k = (T - table[low].temperature) / (table[high].temperature - table[low].temperature); output_results (T, table[low].pressure + (k * (table[high].pressure - table[low].pressure)), table[low].u_sub_f + (k * (table[high].u_sub_f - table[low].u_sub_f)), table[low].u_sub_g + (k * (table[high].u_sub_g - table[low].u_sub_g))); return; } mid = (low + high) / 2; if (table[mid].temperature == T) { output_results (T, table[mid].pressure, table[mid].u_sub_f, table[mid].u_sub_g); return; } if (table[mid].temperature > T) { high = mid; } else { low = mid; } } } void main (void) { const int capacity = 100; table_line table[capacity]; double T; int table_size; if (!read_in_table(capacity, table, table_size)) { cout << "\nUnable for read table - program execution terminated.\n"; pause (); return; } cout << "\nTable successfully read.\n" << "The table contains " << table_size << " entries.\n" << "Temperatures run from " << table[0].temperature << " degrees C to " << table[table_size - 1].temperature << " degrees C.\n"; for (;;) { cout << "\nEnter temperature (-999 to end): "; cin >> T; if (T == -999) { return; } calculate_and_output (table_size, table, T); } }