If it isn't too big, people is sometimes happy to help.
You could post it here or start a new topic. It's up to you. I don't think there's harm in either.
Printable View
If it isn't too big, people is sometimes happy to help.
You could post it here or start a new topic. It's up to you. I don't think there's harm in either.
Ok then. Here's my program. Does it look reliable?
There's also one more thing I want to add, but haven't thought of a way yet. I want to count the number of systems written to each file, any ideas?
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);
}
Maybe too long after all? :)
No worries, this was just in case anyone had some spare time :p.
When dealing with objects that have important characteristics, I like to use structs in my program. It just simplifies the logic and makes it all easier.
As a practical matter, I suggest shorter names for structs and struct members.Code:
struct star {
char name[20];
char type[20];
int numberr;
//etc.
};
struct star stars[1000]; //makes an array of 1000 structs of type star
//you access the struct member using the dot operator:
printf("\nthe first stars name is:%s ", stars[0].name);
Although I find OOP programming nigh onto madness, using structs in C makes good sense, to me.
this part of your code, should be in a loop:
To print on the same line, in a certain position, google "SetConsoleCursorPosition" it's like the old goto(x,y) in conio.h DOS days.Code: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");
for(i = 0; i < 19; i++)
printf("type %d = %s\n", i+1, Startypes[i]);
"Oh Be A Fine Girl, Kiss Me" :)
Ah, you know it too :p.
Thanks for the struct tip. I'll try using that.
I hope I can make that SetConsoleCursorPosition work. It's not really an important thing for my program, but it's fun :).
Right now I've implemented a function that uses windows.h to clear the screen after every step in the loop. It works, but it looks crappy, bacause the tekst on screen flickers (that's a word right?) in stead of showing a steady line with a changing number.
Here's a sample program for you: (MS VS C ver. 6.0)
Code:#include <stdio.h>
#include <windows.h>
#include <wincon.h>
void Gotoxy(int, int);
int main(void) {
int x, y;
char *starlight = "Oh Be a Fine Girl Kiss Me";
char *ilike = "I like blue stars";
for(x = 0, y = 1; x < 41;) {
Gotoxy(x,y);
printf("%s", ilike);
x += strlen(ilike)+1;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_BLUE |BACKGROUND_GREEN);
printf("\n%s\n ", ilike);
for(y = 4, x = 10; y < 18; y+=5) {
Gotoxy(x, y);
printf("%s", starlight);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | BACKGROUND_RED);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | BACKGROUND_BLUE);
printf("\n\n\n%s", starlight);
printf("\n\n");
return 0;
}
void Gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
Haven't I pointed this out before, that string literals should be const char*?
I gave them up for Lent. It was either wine, women, song, or const char*. :)
It doesn't matter where functions are placed as long as you use proper prototypes.
I also noticed silly Adak failed to include the names of the arguments in the prototype. It shall be:
void Gotoxy(int x, int y);
not
void Gotoxy(int, int);
Gotoxy() has a prototype above main: void Gotoxy(int int); ??
Oh, you mean why isn't it spelled out, (int x, int y)?
It's not necessary, at least not for my compilers. Does yours require the variable names, not just the data types of the parameters being passed to a function, in the prototype?
No it compiled fine, that's not why I asked. I'm just used to defining functions before the main. This may actually be a nice way to put all the function details behind the program so that I won't have to scroll through all the functions to get to the main. I may tidy up some stuff.