Commit e71e4ee2 by raliis

Upload New File

parent d55a2b92
Showing with 106 additions and 0 deletions
#include <stdio.h>
#include <stdio.h>
typedef struct Kandidaat
{
char perenimi[31];
char erakond[21];
int haaled;
} Kandidaat;
int howManyLines(char *failiNimi);
void sisse (char *failiNimi, Kandidaat *nimekiri);
void sort (int N, Kandidaat *nimekiri);
void tulemus (int N, Kandidaat *nimekiri);
int main ()
{
int N = 0;
N = howManyLines ("kandidaadid.txt");
Kandidaat nimekiri[N];
sisse ("kandidaadid.txt", nimekiri);
sort (N, nimekiri);
tulemus (N, nimekiri);
return 0;
}
int howManyLines(char *failiNimi)
{
FILE *data;
data = fopen (failiNimi, "r");
int c = 0;
int read = 0;
if (data == NULL)
return 0;
while ((c = fgetc(data)) != EOF)
{
if (c == '\n')
read++;
}
fclose(data);
return read;
}
void sisse (char *failiNimi, Kandidaat *nimekiri)
{
int i = 0;
int j;
FILE *data;
data = fopen (failiNimi, "r");
while (fscanf (data, "%s %s %d", nimekiri[i].perenimi, nimekiri[i].erakond, &nimekiri[i].haaled) != EOF)
{
i++;
}
fclose (data);
for (j = 0; j < i; j++)
{
printf ("%s %s %d\n", nimekiri[j].perenimi, nimekiri[j].erakond, nimekiri[j].haaled);
}
}
void sort (int N, Kandidaat *nimekiri)
{
int k;
int j;
for (j = 0; j < N-1; j++)
{
int min = nimekiri[j].haaled;
int indeks = j;
for (k = j+1; k < N; k++)
{
if (nimekiri[k].haaled < min)
{
min = nimekiri[k].haaled;
indeks = k;
}
Kandidaat abi = nimekiri[indeks];
nimekiri[indeks] = nimekiri [j];
nimekiri[j] = abi;
}
}
}
void tulemus (int N, Kandidaat *nimekiri)
{
FILE *data;
data = fopen ("tul.txt", "w");
int i;
for (i = 0; i < N; i++)
{
printf ("perenimi = %s erakond = %s hääli: %d\n", nimekiri[i].perenimi, nimekiri[i].erakond, nimekiri[i].haaled);
fprintf (data, "perenimi = %s erakond = %s hääli: %d\n", nimekiri[i].perenimi, nimekiri[i].erakond, nimekiri[i].haaled);
}
fclose (data);
}
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