Witam! Mam problem ze zrozumieniem poniższego algorytmu napisanego w języku C. Chodzi mi o wytłumaczenie do czego służy każda zmienna, co realizuje poszczególna funkcja itp. Z góry dzięki!
- include <stdio.h>
- include <math.h>
int n,k;
int tab;
int *str;
void read();
void load_date();
void gen();
void gen_tab();
void sort();
int min(int x, int y);
int max(int x, int y);
void make();
void save();
int main(){
read();
gen();
load_date();
gen_tab();
sort();
make();
save();
printf("Wyniki zapisane do exit.txt");
free(str);
free(tab);
return 0;}
void read(){
char nazwap[]="date.txt";
char help_sign;
FILE fp;
fp=fopen(nazwap,"rt");
n=1;
while((help_sign=fgetc(fp)) != EOF)
{
if (help_sign=='\n')
{
n++;
}
}
fclose(fp);}
void gen(){
int i;
str=(int)malloc(nsizeof(int*));
for (i=0; i<n; i++){
str[i]=(int*)malloc( 4sizeof(int)); }}
void load_date(){
char nazwap[]="date.txt";
int i;
FILE fp;
fp=fopen(nazwap,"rt");
for (i=0; i<n; i++){
fscanf(fp, "%d" , &str[i][0]);
fscanf(fp, "%d" , &str[i][1]);
fscanf(fp, "%d" , &str[i][2]);
str[i][3]=0;}
fclose(fp);}
void gen_tab(){
int i;
k=0;
for (i=0; i<n; i++){
if (str[i][0]>k) k=str[i][0];
if (str[i][1]>k) k=str[i][1];}
tab=(int)malloc(ksizeof(int));
for (i=0; i<k; i++){
tab[i]=i+1;}}
void sort(){
int i,j,tmp;
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++){
if (str[i][2]>str[j][2]){
tmp=str[i][0];
str[i][0]=str[j][0];
str[j][0]=tmp;
tmp=str[i][1];
str[i][1]=str[j][1];
str[j][1]=tmp;
tmp=str[i][2];
str[i][2]=str[j][2];
str[j][2]=tmp;}}}
int min(int x, int y){
if (x>y) return y;
else return x;}
int max(int x, int y){
if (x<y) return y;
else return x;}
void make(){
int i,j,min1,max1,p,q;
for (i=0; i<n; i++){
if (tab[((str[i][0])-1)]!=tab[((str[i][1])-1)]){
str[i][3]=1;
min1=min(tab[((str[i][0])-1)],tab[((str[i][1])-1)]);
max1=max(tab[((str[i][0])-1)],tab[((str[i][1])-1)]);
for (j=0; j<k; j++){
if (tab[j]==max1) tab[j]=min1;}}}}
void save(){
int i;
char nazwap[]="exit.txt";
FILE *fp;
fp=fopen(nazwap,"wt");
for (i=0; i<n; i++){
if (str[i][3]==1){
fprintf(fp, "%d ", str[i][0]);
fprintf(fp, "%d ", str[i][1]);
fprintf(fp, "%d\n", str[i][2]);}}
close(fp);}