NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/*
* Meine Ausgabedatei heisst ausgabe_loesung.txt
*/
typedef struct plateau {
int counter; //anzahl bruecken
int nummer; //nummer des plateaus
struct plateau *next;
} plateau;

#include <stdlib.h>
#include <stdio.h>
#include "bruecken.h"

/* Alles freigeben */
void freigeben(plateau *liste) {
plateau *i = liste;
while (liste != NULL) {
i = liste;
liste = liste->next;
free(i);
}
}

/* Schauen ob Plateau bereits in Liste */
plateau *suchen(int a, plateau *liste) {
while (liste != NULL) {
if (a == liste->nummer) { // gefunden
return liste;
} else { // schaue naechstes an
liste = liste->next;
}
}

return NULL;
}

/* Plateau vorne anhaengen oder counter erhoehen */
plateau *erstellen(int a, plateau *liste) {
plateau *erg = suchen(a, liste);
plateau *pl = liste;

if (erg == NULL) { // noch nicht in Liste
pl = malloc(sizeof(plateau));
pl->next = liste;
pl->nummer = a;
pl->counter = 1;
} else { // bereits in Liste, also counter erhoehen
erg->counter = erg->counter + 1;
}

return pl;
}

/* Werte aus Datei einlesen */
int einlesen(char *argv[], plateau **liste) {
int a, b; // Werte in Eingabedatei

FILE *input = fopen(argv[1], "r"); // Eingabedaetei oeffnen

if (input == NULL) { // Datei kann nicht geoeffnet werden
puts("Eingabedatei konnte nicht geöffnet werden.");
return 2;
}

// solange einlesen bis Datei zu Ende
while (fscanf(input, "%i %i", &a, &b) == 2) {
*liste = erstellen(a, *liste);
*liste = erstellen(b, *liste);
}

fclose(input);

return 0; // alles ok
}
/* Loesungsweg suchen */
int weg_finden(plateau *liste, char *argv[]) {
int ungerade = 0; // ungerade Anz Bruecken
int start_ziel[3]; // nur zwei Werte benoetigt, aber 3 Wert falls Schleife noch eine ungerade Anz findet, bevor abgebrochen wird
int i = 0;
int start, ziel;

while (liste != NULL) {
if (liste->counter % 2 != 0) {
ungerade++;
start_ziel[i] = liste->nummer;
i++;
}
if (ungerade > 2) {
puts("Keinen Weg gefunden.");
return 3; // genau 2 oder keine ungerade Anz gewollt
}

liste = liste->next;
}

if (ungerade == 0) { // beliebige Wege moeglich
start = -1;
ziel = -1;

return ausgabe(argv, start, ziel);
} else if (ungerade == 2) { // nur ein Start- und Zielplateau vorhanden
if (start_ziel[0] < start_ziel[1]) { // Start muss kleiner als Ziel sein
start = start_ziel[0];
ziel = start_ziel[1];
} else {
start = start_ziel[1];
ziel = start_ziel[0];
}

return ausgabe(argv, start, ziel);
} else { // nur eine ungerade
puts("Keinen Weg gefunden.");
return 3;
}
}

/* In Ausgabedatei schreiben */
int ausgabe(char *argv[], int start, int ziel) {
FILE *output = fopen(argv[2], "w"); // Ausgabedaetei oeffnen
if (output == NULL) {
puts("Ausgabedatei konnte nicht geöffnet/geschrieben werden.");
return 2;
}

puts("Lösung gefunden.");

if (start == -1 && ziel == -1) {
fprintf(output, "beliebign");
} else {
fprintf(output, "%in%in", start, ziel);
}

fclose(output);

return 0;
}

int main(int argc, char *argv[]) {
plateau *liste = NULL; // Liste mit Plateaus

if (argc != 3) {
puts("Es müssen genau 3 Argumente übergeben werden.");
return 1;
}

int eingabe = einlesen(argv, &liste);

if (eingabe == 2) {
return 2;
}

int weg = weg_finden(liste, argv);

freigeben(liste);

return weg;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.