Romanizer V3 - Codename: Aquädukt
Übersetzt 1-20 arabische Zahlenwerte in die römische Schreibweise unter Nutzung einer named pipe
, ohne die Subtraktionsregel zu verwenden (Bonusaufgabe?). Abbruch durch eingabe von '0'
/**************************************************************** * Aquaedukt - Roemische Zahlen durch Pipe uebersetzen * * (c) 2009 Manuel Krischer fuer das Betriebssystempraktikum * ****************************************************************/ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> /* Pipe Name und Berechtigung festlegen */ #define PIPEFILE "romanize" #define PIPEPERM 0777 /*Wieviele Eingaben erlauben*/ #define EINGABEN 20 /*zuordnung roemische/arabische ziffern festlegen*/ #define U 5000 #define M 1000 #define D 500 #define C 100 #define L 50 #define X 10 #define V 5 #define I 1 /*anzahl roemische zahlzeichen*/ #define r_zif 8 int roman[r_zif] = { U, M, D, C, L, X, V, I }; char roman_z[r_zif] = { 'U', 'M', 'D', 'C', 'L', 'X', 'V', 'I' }; int count[r_zif] = { 0, 0, 0, 0, 0, 0, 0, 0 }; /* Named Pipe loeschen und neu erstellen */ int make_pipe() { unlink(PIPEFILE); if(0 != mkfifo(PIPEFILE, PIPEPERM)) { printf("Pipe anlegen fehlgeschlagen!"); } } int max = EINGABEN; /*wieviel eingaben erlauben*/ /* Funktion fuer den Eingabe, wird im Sohnprozess 1 ausgefuehrt*/ int araber() { int i=0, h=1; int wert; /* Named Pipe zum schreiben oeffnen und Betraege einlesen*/ FILE *input = fopen(PIPEFILE, "w"); do { printf("Betrag %i: ",h++); scanf("%i",&wert); /*in pipe schreiben*/ if ( wert > 0 ) { fprintf(input, "%i\n", wert); } else { printf("Nur positive, ganze Werte erlaubt!"); } i++; /*Wenn EINGABEN erreicht oder 0, dann eingabevorgang abbrechen*/ } while (i != max && wert != 0 ); fflush(input); fclose(input); return; } /* Funktion fuer den Roemer, wird im zweiten Kindprozess ausgefuehrt*/ int roemer() { int werte[max], auslesen, o=0, e=0, i=0, j=0, k=0, tmp_zahl; char worte[max][128], tmp_wort[128]; FILE *input; /* Datei/Named Pipe oeffnen */ if ((input = fopen(PIPEFILE, "r")) != NULL) { while (!feof(input)) { /*arabische zahlen aus pipe lesen*/ fscanf(input, "%i", &tmp_zahl); /*aktuelle arabische zahl in einem array sichern*/ werte[j] = tmp_zahl; /*anzahl der roemischen zahlwerte ermitteln*/ for (o=0; o<r_zif; o++) { /*Hier ansetzen, wenn Subtraktionsregeln erwuenscht sind*/ if (tmp_zahl >= roman[o]) { count[o] = tmp_zahl / roman[o]; tmp_zahl = tmp_zahl - roman[o] * count[o]; } } /*in dem array count steckt jetzt die anzahl der jeweiligen buchstaben, die benoetigt werden*/ /*ermittelte roemische werte als buchstaben in chararray schreiben*/ for (k=0; k<r_zif; k++) { while (count[k] != 0 ) { tmp_wort[e++] = roman_z[k]; count[k]-=1; } } /*nullbyte anhaengen*/ tmp_wort[e] = '\0'; e=0; /*aktuelles wort in wortarray kopieren*/ strcpy(worte[j], tmp_wort); j++; } fclose(input); /*ausgabe des wert und wortarrays als gegenueberstellung*/ printf("\nUmrechnungstabelle\n"); printf("*******************************************\n"); for (i=0; i<=j; i++) { if (werte[i] == 0) break; printf("Arabisch: %i\t\t | Roemisch: %s\n", werte[i], worte[i]); } printf("*******************************************\n"); } return; } /*Hauptprogramm*/ int main(int argc,char *argv[]) { make_pipe(); if (fork() == 0) { araber(); } else if (fork() == 0) { roemer(); } else { wait(0); wait(0); } unlink(PIPEFILE); return; }