Commit 91118ff3 by raliis

Upload New File

parent dd727167
Showing with 137 additions and 0 deletions
#include <stdio.h>
#include <string.h>
typedef struct
{
char i[2];
int number;
} Kirje;
int fileExist (char *filename); // kontrollib kas fail on olemas
int howManyLines(char *filename); // loeb ära read failis
int kirjedSisse (Kirje *nimekiri, char *filename); // loeb andmed sisse
void sort (Kirje *nimekiri, int counter); // sorteerib andmed
void valjastus (Kirje *nimekiri, int counter); // väljastab töödeldud andmed
int main ()
{
if (fileExist ("f11.txt") != 0)
{
printf ("Faili ei asu programmiga samas kaustas");
return -1;
}
int T;
T = howManyLines ("f11.txt");
Kirje nimekiri[T];
int counter;
counter = kirjedSisse (nimekiri, "f11.txt");
sort (nimekiri, counter);
valjastus (nimekiri, counter);
return 0;
}
int fileExist (char *filename) // kontrollib kas fail on olemas
{
FILE *fp;
fp = fopen (filename, "r");
if (fp == NULL)
{
return 1;
}
else
{
fclose (fp);
return 0;
}
}
int howManyLines(char *filename)
{
FILE *data;
data = fopen (filename, "r");
int c;
int read = 0;
while ((c = fgetc(data)) != EOF)
{
if (c == '\n')
{
read++;
}
}
fclose(data);
if (read == 0)
{
printf ("Fail %s on tühi", filename);
return 1;
}
else
{
return read;
}
}
int kirjedSisse (Kirje *nimekiri, char *filename)
{
int i = 0; // loeb ära mitu arvu sisse loetakse
int j;
FILE *fp;
fp = fopen (filename, "r");
while (fscanf (fp, "%s %d", nimekiri[i].i, &nimekiri[i].number) != EOF)
{
if (nimekiri[i].number == 0)
break;
i++;
}
fclose (fp);
for (j = 0; j < i; j++)
{
printf ("%s %d\n", nimekiri[j].i, nimekiri[j].number);
}
printf ("\n");
return i;
}
void sort (Kirje *nimekiri, int counter)
{
int i, j;
Kirje temp;
for (i = 0; i < counter; i++)
{
for (j = 1; j < counter; j++)
{
if (nimekiri[j-1].number < nimekiri[j].number)
{
temp = nimekiri[j];
nimekiri[j] = nimekiri[j-1];
nimekiri[j-1] = temp;
}
}
}
}
void valjastus (Kirje *nimekiri, int counter)
{
int i;
for (i = 0; i < counter; i++)
{
printf ("%s %d\n", nimekiri[i].i, nimekiri[i].number);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment