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
91118ff3
authored
Mar 15, 2018
by
raliis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
dd727167
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
137 additions
and
0 deletions
KT1/KT12.c
KT1/KT12.c
0 → 100644
View file @
91118ff3
#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
);
}
}
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