1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
/*This program finds averages for concentrations of compounds at certain
temperatures, and then organizes the data in a matrix that is sent to an output
file.*/
#include<iostream> //Allows for the use of cout/cin commands.
#include<fstream>//Enables one to load data from files and output data to files.
#include<cstdlib> /*Needed for checking that the file was loaded correctly
and also to convert strings to integers.*/
#include<cstring> //Needed to find string lengths.
#include<cctype> /*Allows for the use of isdigit function to check that an
array element is a digit*/
#include<sstream> //Needed to insert and extract data from a stringstream.
using namespace std; //Allows for basic C++ functions.
const int NCOLSA=22,NCOLSB=9;
int NROWSA=-1,i,check=0,MaxTemp,MinTemp; //Declare global variables as integers.
double A[1000][NCOLSA],B[15][NCOLSB]; /*Declare global variables as doubles.*/
int main() //Begins main function.
{
string InputFileName,OutputFileName,line; //Declare variable as a string.
int NROWSB,j,T; //Declare variables as integers.
int Check(char a[]); /*Declare an integer function with a strings as the
only input.*/
void data(); //Declare a void function with no input or output.
ifstream infile; //Declare variable.
ofstream outfile; //Declare variable.
while(1==1) //Always true, therefore infinite loop.
{
cout<<"Enter input file name (save excel sheet as a .txt file): ";
getline(cin,InputFileName); //Take in whatever the user might type.
infile.open(InputFileName.c_str()); //Opens input file.
if(!infile) //If the file cannot be opened...
{
cout<<"\nFile does not exists.\n\n";
infile.clear(); //Clears error state.
}
else //Otherwise...
break; //End the loop.
}
cout<<"\nEnter output file name (use a .csv extension): ";
getline(cin,OutputFileName);
outfile.open(OutputFileName.c_str()); //Creates output file.
DoOver:
MaxTemp=Check("maximum"); //Calls function and assigns output to MaxTemp.
MinTemp=Check("minimum"); //Calls function and assigns output to MinTemp.
if(MinTemp>MaxTemp)
{
cout<<"\n Error: minimum temperature cannot be higher than maximum temperature.\n";
goto DoOver; //Jump to another point in the program.
}
NROWSB=(MaxTemp-MinTemp)/50+1;
for(i=0;i<19;i++)
getline(infile,line);
A[0][0]=1;
for(i=1;A[i-1][0]==1;i++)
{
NROWSA=NROWSA+1;
for(j=0;j<NCOLSA;j++)
infile>>A[i][j];
}
cin.sync();
for(i=MaxTemp;i>=MinTemp;i=i-50) /*For every 50 degree increment of the
temperature starting at MaxTemp and ending at MinTemp...*/
data();
outfile<<"Temp (°C),CH4 (ppm),CO (ppm),NO (ppm),NO2 (ppm),C2H6 (ppm),C3H8 (ppm),C3H6 (ppm),HCHO (ppm),CH3OH (ppm)"<<endl;
for(i=0;i<NROWSB;i++) //For each successive row...
{
T=MinTemp+50*i;
outfile<<T;
for(j=0;j<NCOLSB;j++) //For each successive column in a given row...
outfile<<","<<B[i][j]; /*Send the element in that position to
the output file.*/
outfile<<endl;
}
cout<<endl;
infile.close(); //Close input file.
|