Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dsprog
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Manuel Bucher
dsprog
Commits
26e2b52e
There was an error fetching the commit references. Please try again later.
Commit
26e2b52e
authored
6 years ago
by
Manuel Bucher
Browse files
Options
Downloads
Patches
Plain Diff
sorted tiles in array
parent
fcf4690e
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
loesung.c
+56
-4
56 additions, 4 deletions
loesung.c
with
56 additions
and
4 deletions
loesung.c
+
56
−
4
View file @
26e2b52e
...
@@ -12,6 +12,23 @@ typedef struct Tile {
...
@@ -12,6 +12,23 @@ typedef struct Tile {
}
Tile
;
}
Tile
;
int
tile_compare
(
const
void
*
p
,
const
void
*
q
)
{
const
Tile
*
s
=
*
(
const
Tile
**
)
p
;
const
Tile
*
t
=
*
(
const
Tile
**
)
q
;
// als erstes nach x sortieren
if
(
s
->
x
<
t
->
x
)
return
-
1
;
if
(
s
->
x
>
t
->
x
)
return
1
;
// danach nach y sortieren
if
(
s
->
y
<
t
->
y
)
return
-
1
;
if
(
s
->
y
>
t
->
y
)
return
1
;
return
0
;
// Elemente sind gleich
}
/*************** List von Tiles *********************/
/*************** List von Tiles *********************/
/** List von Tiles */
/** List von Tiles */
typedef
struct
List
{
typedef
struct
List
{
...
@@ -32,6 +49,9 @@ void list_init(List *s) {
...
@@ -32,6 +49,9 @@ void list_init(List *s) {
*/
*/
Tile
*
list_push
(
List
*
s
)
{
Tile
*
list_push
(
List
*
s
)
{
Tile
*
p_tile
=
malloc
(
sizeof
(
Tile
));
Tile
*
p_tile
=
malloc
(
sizeof
(
Tile
));
if
(
p_tile
==
NULL
)
{
// failed to allocate memory
return
NULL
;
}
p_tile
->
x
=
0
;
p_tile
->
x
=
0
;
p_tile
->
y
=
0
;
p_tile
->
y
=
0
;
p_tile
->
p_next
=
s
->
p_top
;
p_tile
->
p_next
=
s
->
p_top
;
...
@@ -76,6 +96,7 @@ typedef enum ReadResult {
...
@@ -76,6 +96,7 @@ typedef enum ReadResult {
ReadErrNonDigitCharacter
,
ReadErrNonDigitCharacter
,
ReadErrTooFewNumbers
,
ReadErrTooFewNumbers
,
ReadErrTooManyNumbers
,
ReadErrTooManyNumbers
,
ReadErrOutOfMemory
,
}
ReadResult
;
}
ReadResult
;
typedef
struct
Field
{
typedef
struct
Field
{
...
@@ -161,9 +182,14 @@ void field_init(Field *f) {
...
@@ -161,9 +182,14 @@ void field_init(Field *f) {
/** Freigeben des im Heap reservierten speichers des fields f */
/** Freigeben des im Heap reservierten speichers des fields f */
void
field_drop
(
Field
*
f
)
{
void
field_drop
(
Field
*
f
)
{
list_free
(
&
f
->
tile_list
);
uint32_t
num_tiles
=
f
->
tile_list
.
size
;
if
(
f
->
tile_array
)
{
if
(
f
->
tile_array
!=
NULL
)
{
for
(
uint32_t
i
=
0
;
i
<
num_tiles
;
i
++
)
{
free
(
f
->
tile_array
[
i
]);
}
free
(
f
->
tile_array
);
free
(
f
->
tile_array
);
}
else
{
list_free
(
&
f
->
tile_list
);
}
}
}
}
...
@@ -187,6 +213,9 @@ void field_drop(Field *f) {
...
@@ -187,6 +213,9 @@ void field_drop(Field *f) {
ReadResult
field_init_tile_list
(
Field
*
f
)
{
ReadResult
field_init_tile_list
(
Field
*
f
)
{
while
(
1
)
{
while
(
1
)
{
Tile
*
t
=
list_push
(
&
f
->
tile_list
);
// neues Element in die Liste einfuegen
Tile
*
t
=
list_push
(
&
f
->
tile_list
);
// neues Element in die Liste einfuegen
if
(
t
==
NULL
)
{
return
ReadErrOutOfMemory
;
}
ReadResult
result
=
read_line
(
t
);
ReadResult
result
=
read_line
(
t
);
switch
(
result
)
{
switch
(
result
)
{
case
ReadOk
:
case
ReadOk
:
...
@@ -209,18 +238,33 @@ ReadResult field_init_tile_list(Field *f) {
...
@@ -209,18 +238,33 @@ ReadResult field_init_tile_list(Field *f) {
}
}
}
}
typedef
enum
MallocResult
{
MallocFailed
,
MallocOk
,
}
MallocResult
;
/** Initialisieren des Tile-Arrays
/** Initialisieren des Tile-Arrays
*
*
* Laufzeit: O(n)
* Laufzeit: O(n)
*/
*/
void
field_init_tile_array
(
Field
*
f
)
{
MallocResult
field_init_tile_array
(
Field
*
f
)
{
f
->
tile_array
=
malloc
(
f
->
tile_list
.
size
*
sizeof
(
Tile
*
));
f
->
tile_array
=
malloc
(
f
->
tile_list
.
size
*
sizeof
(
Tile
*
));
if
(
f
->
tile_array
==
NULL
)
{
return
MallocFailed
;
}
Tile
*
cur
=
f
->
tile_list
.
p_top
;
Tile
*
cur
=
f
->
tile_list
.
p_top
;
for
(
uint32_t
i
=
0
;
i
<
f
->
tile_list
.
size
;
i
++
)
{
for
(
uint32_t
i
=
0
;
i
<
f
->
tile_list
.
size
;
i
++
)
{
f
->
tile_array
[
i
]
=
cur
;
f
->
tile_array
[
i
]
=
cur
;
cur
=
cur
->
p_next
;
cur
=
cur
->
p_next
;
}
}
return
MallocOk
;
}
void
field_print
(
Field
*
f
)
{
printf
(
"# Array
\n
"
);
for
(
uint32_t
i
=
0
;
i
<
f
->
tile_list
.
size
;
i
++
)
{
printf
(
"%d %d
\n
"
,
f
->
tile_array
[
i
]
->
x
,
f
->
tile_array
[
i
]
->
y
);
}
}
}
int
main
(
void
)
int
main
(
void
)
...
@@ -231,7 +275,6 @@ int main (void)
...
@@ -231,7 +275,6 @@ int main (void)
ReadResult
result
=
field_init_tile_list
(
&
field
);
// lesen des Inputs
ReadResult
result
=
field_init_tile_list
(
&
field
);
// lesen des Inputs
switch
(
result
)
{
switch
(
result
)
{
case
ReadOk
:
case
ReadOk
:
field_init_tile_array
(
&
field
);
break
;
break
;
case
ReadEof
:
case
ReadEof
:
// bei leerer Eingabe nichts ausgeben
// bei leerer Eingabe nichts ausgeben
...
@@ -254,7 +297,16 @@ int main (void)
...
@@ -254,7 +297,16 @@ int main (void)
field
.
tile_list
.
size
);
field
.
tile_list
.
size
);
field_drop
(
&
field
);
field_drop
(
&
field
);
return
EXIT_FAILURE
;
return
EXIT_FAILURE
;
case
ReadErrOutOfMemory
:
fprintf
(
stderr
,
"Error in line %d: Failed to allocate more memory"
,
field
.
tile_list
.
size
);
return
EXIT_FAILURE
;
}
// turn the list into an array
if
(
field_init_tile_array
(
&
field
)
==
MallocFailed
)
{
fprintf
(
stderr
,
"Error: Failed to allocate enough memory"
);
}
}
qsort
(
field
.
tile_array
,
field
.
tile_list
.
size
,
sizeof
(
Tile
*
),
tile_compare
);
field_drop
(
&
field
);
field_drop
(
&
field
);
return
EXIT_SUCCESS
;
return
EXIT_SUCCESS
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment