Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
raliis
/
IAX0584
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e71e4ee2
authored
Mar 05, 2018
by
raliis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
d55a2b92
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
106 additions
and
0 deletions
hea näide/hea_naide.c
hea näide/hea_naide.c
0 → 100644
View file @
e71e4ee2
#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
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment