Code:
/*
Name: Mergersystems Analyzer
Description: Uses lists of mergersystems and devides those into
seperate textfiles for each type of system.
Format lists:
line1: Initial binary system
line2: Binary system just before final merger
line3: Merger product
Collum01: ID
Collum02: Iime [Myr]
Collum03: Distance [R_O]
Collum04: Eccentricity
Collum05: Type of star1
Collum06: Mass of star1 [M_O]
Collum07: -
Collum08: Type of star2
Collum09: Mass of star2 [M_O]
Collum10: -
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
//******************************************************************************
// Types of Stars:
//******************************************************************************
int comb_array_pointer = 0;
int file_counter = 0;
const char *Startypes[] =
{
"Planet",
"Brown Dwarf",
"Main Sequence",
"Wolf Rayet",
"Herzsprung Gap",
"Sub Giant",
"Horizontal Branch",
"Super Giant",
"Carbon Star",
"Helium Star",
"Helium Giant",
"Helium Dwarf",
"Carbon Dwarf",
"Oxygen Dwarf",
"Thorn Zytkow",
"Xray Pulsar",
"Radio Pulsar",
"Neutron Star",
"Black Hole"
};
//******************************************************************************
// Function for counting lines in a file:
//******************************************************************************
int Linecount(char filename_array[][1024],int n)
{
char filename[100];
int no_lines = 0;
sprintf(filename, "%s", filename_array[n]); //- Construct filename out
// of the filename array.
FILE* file = fopen(filename,"r"); //- Open that file for reading
if (file != NULL)
{
char dummyline[1000];
while ( fgets(dummyline, 1000, file) != NULL) no_lines++;
printf("Just counted lines in: %s\n", filename); //- Testing filename
fclose(file);
return no_lines;
}
else
{
printf("!! %s Is not available.\n", filename);
return no_lines = 0;
}
}
//******************************************************************************
// Function for checking array for two given strings:
//******************************************************************************
int stringcheck2(const char* string1_to_check_for,const char* string2_to_check_for,char array_to_check[][8],int length_of_array){
int i = 0;
for(i = 0; i < length_of_array; i++)
{
if (strcmp(string1_to_check_for, array_to_check[i]) == 0 ||
strcmp(string2_to_check_for, array_to_check[i]) == 0)
{
return 1;
}
}
return 0;
}
//******************************************************************************
// Function for sorting the systems into file constructed by type_array:
// It also counts the number of systems added. (When I'm done)
//******************************************************************************
void Analyse3(FILE* filetoread, const char* type_array[])
{
int startype1 = 0;
int startype2 = 0;
int id = 0;
int max_comb = 500;
int i = 0;
int result = 0;
char current_comb1[8];
char current_comb2[8];
char Binarysystem_filename[1000];
char* line1 = (char*) malloc (1000);
char* line2 = (char*) malloc (1000);
char* line3 = (char*) malloc (1000);
char comb_array[max_comb][8];
//read the three lines and remember them:
fgets(line1, 1000, filetoread);
fgets(line2, 1000, filetoread);
fgets(line3, 1000, filetoread);
//divide the second line into three integers, startype1, startype2 and id (Might need it at some point):
sscanf(line2, "%d %*s %*s %*s %d %*s %*s %d %*s %*s", &id, &startype1, &startype2);
//Now look at the combination of the types and make a new file to write them into,
//unless the file has already been made.
//The order of the combination does not matter, so I just need to look for either one.
sprintf(current_comb1, "%d%d", startype1, startype2);
sprintf(current_comb2, "%d%d", startype2, startype1);
result = stringcheck2(current_comb1, current_comb2, comb_array, comb_array_pointer);
if(result == 0) //This combination has not occurred yet.
{
sprintf(Binarysystem_filename, "%s - %s.txt", type_array[startype1 - 1], type_array[startype2 - 1]);
FILE* file = fopen(Binarysystem_filename, "w");
strcpy(comb_array[comb_array_pointer], current_comb1);
comb_array_pointer++;
fprintf(file, "%s", line1);
fprintf(file, "%s", line2);
fprintf(file, "%s", line3);
file_counter++;
fclose(file);
}
else //The combination already has an existing file.
{
sprintf(Binarysystem_filename, "%s - %s.txt", type_array[startype1 - 1], type_array[startype2 - 1]);
FILE* file = fopen(Binarysystem_filename, "a"); //append the lines
fprintf(file, "\n");
fprintf(file, "%s", line1);
fprintf(file, "%s", line2);
fprintf(file, "%s", line3);
fclose(file);
}
sprintf(Binarysystem_filename, "%s - %s.txt", type_array[startype1 - 1], type_array[startype2 - 1]);
free(line1);
free(line2);
free(line3);
}
//******************************************************************************
// Main Program:
//******************************************************************************
int main ()
{
float percent = 0;
int i = 0;
int lines_in_file0 = 0;
int filefound = 0;
char filenames[10][1024];
printf("type01 = %s\n", Startypes[0]);
printf("type02 = %s\n", Startypes[1]);
printf("type03 = %s\n", Startypes[2]);
printf("type04 = %s\n", Startypes[3]);
printf("type05 = %s\n", Startypes[4]);
printf("type06 = %s\n", Startypes[5]);
printf("type07 = %s\n", Startypes[6]);
printf("type08 = %s\n", Startypes[7]);
printf("type09 = %s\n", Startypes[8]);
printf("type10 = %s\n", Startypes[9]);
printf("type11 = %s\n", Startypes[10]);
printf("type12 = %s\n", Startypes[11]);
printf("type13 = %s\n", Startypes[12]);
printf("type14 = %s\n", Startypes[13]);
printf("type15 = %s\n", Startypes[14]);
printf("type16 = %s\n", Startypes[15]);
printf("type17 = %s\n", Startypes[16]);
printf("type18 = %s\n", Startypes[17]);
printf("type19 = %s\n", Startypes[18]);
printf("\n");
printf("Give the name of the file you wish to analyze.\n");
printf("-> ");
fgets(filenames[0], 1024, stdin);
filenames[0][strlen(filenames[0])-1] = '\0';
printf("\n");
FILE* file0 = fopen(filenames[0], "r");
if (file0 == NULL)
{
while (filefound == 0)
{
printf("File '%s' not found...\n", filenames[0]);
printf("Give the name of the file you wish to analyze.\n");
printf("-> ");
fgets(filenames[0], 1024, stdin);
filenames[0][strlen(filenames[0])-1] = '\0';
printf("\n");
file0 = fopen(filenames[0], "r");
if (file0 == NULL)
{
filefound = 0;
}
else
{
printf("File exists\n");
filefound = 1;
}
}
}
lines_in_file0 = Linecount(filenames, 0);
printf("No of lines in %s: %d \n", filenames[0], lines_in_file0);
rewind(file0);
printf("tot hier");
for (i=0;i<(lines_in_file0/3);i++)
{
Analyse3(file0, Startypes);
//printf("Lines analyzed: %d/%d \n", ((i + 1) * 3), lines_in_file0);
percent = ( ( ( (float)i + 1 ) * 3) / (float)lines_in_file0 )*100;
//I just want one line that changes
//system("cls"); WAY too slow
printf("Precentage done: %2.2f\n", percent);
}
printf("Files counted: %d\n", file_counter);
fclose(file0);
//**************************************************************************
// The usual:
//**************************************************************************
printf("\n");
//to make sure window doesn't close right away.
#ifdef _WIN32
system("PAUSE");
#endif
return (0);
}