modified: makefile

modified:   sudokuloeser.c
main
Thomas Ba. 14 years ago
parent 87f2a4b7c2
commit f61b07685f

@ -3,3 +3,9 @@ sudokuloeser: sudokuloeser.c
windows: sudokuloeser.c windows: sudokuloeser.c
sh -c '[ -x /usr/bin/i486-mingw32-gcc ] && i486-mingw32-gcc -Wall -pedantic sudokuloeser.c -o sudokuloeser.exe || [ -x /usr/bin/i586-mingw32msvc-gcc ] && i586-mingw32msvc-gcc -Wall -pedantic sudokuloeser.c -o sudokuloeser.exe || echo "Compiler not found"' sh -c '[ -x /usr/bin/i486-mingw32-gcc ] && i486-mingw32-gcc -Wall -pedantic sudokuloeser.c -o sudokuloeser.exe || [ -x /usr/bin/i586-mingw32msvc-gcc ] && i586-mingw32msvc-gcc -Wall -pedantic sudokuloeser.c -o sudokuloeser.exe || echo "Compiler not found"'
install: sudokuloeser
install -vDm755 sudokuloeser /usr/bin/sudokuloeser
uninstall:
rm /usr/bin/sudokuloeser

@ -24,6 +24,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include <ctype.h>
#ifdef linux #ifdef linux
#include <regex.h> #include <regex.h>
#endif #endif
@ -37,7 +39,8 @@ typedef struct sudoku {
/* Allgemeine Funktionen */ /* Allgemeine Funktionen */
int s_einlesen(char*, sudoku*); /* Einlesen aus Datei*/ int s_einlesen(char*, sudoku*); /* Einlesen aus Datei*/
void s_ausgabe(sudoku*); /* Auf dem bildschirm ausgeben */ void s_ausgabe(sudoku*,int); /* Auf dem bildschirm ausgeben */
void s_ausgabe_unicode(sudoku*,int); /* Auf dem bildschirm ausgeben, Unicode Rahmenelemente */
void print_help(int,char**); /* "Hilfe" ausgeben */ void print_help(int,char**); /* "Hilfe" ausgeben */
/* Loeserfunktionen */ /* Loeserfunktionen */
/* Funktion fuer Backtracking */ /* Funktion fuer Backtracking */
@ -59,32 +62,64 @@ const int OUTPUT_COLOR=0;
int main(int argc, char **argv) { int main(int argc, char **argv) {
sudoku s={ {{0}}, {{0}}, {{{1}}}, 81 }; sudoku s={ {{0}}, {{0}}, {{{1}}}, 81 };
char* outfile = NULL;
int st=0,sl=0,ret=0; int st=0,sl=0,ret=0;
int c,unicode=0,color=0,outfilev=0,solve=1;
#ifdef linux #ifdef linux
struct timespec ts,te,l_ts,l_te; struct timespec ts,te,l_ts,l_te;
long double t; long double t;
clock_gettime(CLOCK_REALTIME, &ts); clock_gettime(CLOCK_REALTIME, &ts);
color = 1;
#endif #endif
/* Hilfe ausgeben, wenn angefordert oder zuwenig Parameter */ /* Argumente auslesen */
if(argc < 2 || strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0) { while ((c = getopt (argc, argv, "cho:uUn")) != -1)
switch(c) {
case 'h':
print_help(argc,argv); print_help(argc,argv);
exit(0); exit(0);
break;
case 'u':
case 'U':
unicode = 1;
break;
case 'c':
color = 0;
break;
case 'o':
if(outfilev==0) outfile = ((strcmp(optarg,"") == 0) ? "sudoku_geloest.txt" : optarg);
outfilev = 1;
break;
case 'n':
solve = 0;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
exit(1);
break;
} }
/* Aus Datei einlesen und Fehler auswerten */ /* Aus Datei einlesen und Fehler auswerten */
st = s_einlesen(argv[1],&s); st = s_einlesen(argv[optind],&s);
if(st == 0) { if(st == 0) {
fprintf(stderr,"Ungueltiges Dateiformat!\n"); fprintf(stderr,"Ungueltiges Dateiformat!\n");
s_write_error( (argc == 3) ? argv[2] : "sudoku_geloest.txt",0 ); if(outfilev) s_write_error( outfile ,0 );
return 2; return 2;
}else if(st == -1) { }else if(st == -1) {
#ifdef linux if(color)
fprintf(stderr,"\033[31;1mDas Sudoku ist nicht loesbar!!\033[0m\n\n"); fprintf(stderr,"\033[31;1mDas Sudoku ist nicht loesbar!!\033[0m\n\n");
#else else
fprintf(stderr,"Das Sudoku ist nicht loesbar!!\n\n"); fprintf(stderr,"Das Sudoku ist nicht loesbar!!\n\n");
#endif if(outfilev) s_write_error( outfile ,1 );
s_write_error( (argc == 3) ? argv[2] : "sudoku_geloest.txt",1 );
return 1; return 1;
} }
if(solve == 0) {
(unicode == 1) ? s_ausgabe_unicode(&s,color) : s_ausgabe(&s,color);
exit(0);
}
/* Sudoku Loesen, Loseung ausgeben und in Datei schreiben */ /* Sudoku Loesen, Loseung ausgeben und in Datei schreiben */
printf("Suche...\nProbiere es mit Logik... "); printf("Suche...\nProbiere es mit Logik... ");
#ifdef linux #ifdef linux
@ -102,21 +137,19 @@ int main(int argc, char **argv) {
clock_gettime(CLOCK_REALTIME, &l_te); clock_gettime(CLOCK_REALTIME, &l_te);
#endif #endif
if(sl == 0) { if(sl == 0) {
#ifdef linux if(color)
printf("\033[31;1mDas Sudoku ist nicht loesbar!!\033[0m\n"); printf("\033[31;1mDas Sudoku ist nicht loesbar!!\033[0m\n");
#else else
fprintf(stderr,"Das Sudoku ist nicht loesbar!!\n"); fprintf(stderr,"Das Sudoku ist nicht loesbar!!\n");
#endif if(outfilev) s_write_error( outfile , 2 );
s_write_error( (argc == 3) ? argv[2] : "sudoku_geloest.txt",2 );
ret = 1; ret = 1;
}else{ }else{
#ifdef linux if(color)
printf("\033[32;1mLoesung gefunden:\033[0m\n\n"); printf("\033[32;1mLoesung gefunden:\033[0m\n\n");
#else else
printf("Loesung gefunden:\n\n"); printf("Loesung gefunden:\n\n");
#endif (unicode == 1) ? s_ausgabe_unicode(&s,color) : s_ausgabe(&s,color);
s_ausgabe(&s); if(outfilev) s_write( outfile ,&s );
s_write( (argc == 3) ? argv[2] : "sudoku_geloest.txt",&s );
} }
#ifdef linux #ifdef linux
clock_gettime(CLOCK_REALTIME, &te); clock_gettime(CLOCK_REALTIME, &te);
@ -184,13 +217,13 @@ int s_einlesen(char * path, sudoku * sf) {
return 1; return 1;
} }
void s_ausgabe(sudoku* s) { void s_ausgabe(sudoku* s,int color) {
int i,j; int i,j;
for(i=0;i<9;i++) { for(i=0;i<9;i++) {
if(i%3==0) printf("+-------+-------+-------+\n"); if(i%3==0) printf("+-------+-------+-------+\n");
for(j=0;j<9;j++) { for(j=0;j<9;j++) {
if(j%3==0) printf("| "); if(j%3==0) printf("| ");
#ifdef linux if(color) {
if(s->feld[i][j] == 0) { if(s->feld[i][j] == 0) {
printf("\033[30;1m?\033[0m "); printf("\033[30;1m?\033[0m ");
}else if(s->vorgabe[i][j] == 1){ }else if(s->vorgabe[i][j] == 1){
@ -200,9 +233,8 @@ void s_ausgabe(sudoku* s) {
}else{ }else{
printf("%d ",s->feld[i][j]); printf("%d ",s->feld[i][j]);
} }
#else }else
printf("%d ",s->feld[i][j]); printf("%d ",s->feld[i][j]);
#endif
} }
printf("|\n"); printf("|\n");
@ -210,10 +242,51 @@ void s_ausgabe(sudoku* s) {
printf("+-------+-------+-------+\n"); printf("+-------+-------+-------+\n");
} }
void s_ausgabe_unicode(sudoku* s,int color) {
int i,j;
printf("┏━━━┯━━━┯━━━┳━━━┯━━━┯━━━┳━━━┯━━━┯━━━┓\n");
for(i=0;i<9;i++) {
if(i%3==0 && i!=0) printf("┣━━━┿━━━┿━━━╋━━━┿━━━┿━━━╋━━━┿━━━┿━━━┫\n");
else if (i!=0) printf("┠───┼───┼───╂───┼───┼───╂───┼───┼───┨\n");
for(j=0;j<9;j++) {
if(j%3==0) printf("");
else printf("");
if(color) {
if(s->feld[i][j] == 0) {
/* printf("\033[30;1m?\033[0m "); */
printf(" ");
}else if(s->vorgabe[i][j] == 1){
printf("\033[32;1m%d\033[0m ",s->feld[i][j]);
}else if(s->vorgabe[i][j] == 2){
printf("\033[33;1m%d\033[0m ",s->feld[i][j]);
}else{
printf("%d ",s->feld[i][j]);
}
}else{
if(s->feld[i][j] == 0) {
printf(" ");
}else{
printf("%d ",s->feld[i][j]);
}
}
}
printf("\n");
}
printf("┗━━━┷━━━┷━━━┻━━━┷━━━┷━━━┻━━━┷━━━┷━━━┛\n");
}
void print_help(int argc, char **argv) { void print_help(int argc, char **argv) {
#ifdef linux #ifdef linux
printf("\033[0;1mUsage:\033[0m\n"); printf("\033[0;1mUsage:\033[0m\n");
printf(" %s <input_file> [<output_file>]\n",argv[0]); printf(" %s [optionen] <input_file> \n",argv[0]);
printf("\033[0;1mOptionen\033[0m\n");
printf(" -U Unicode Rahmenelemente\n");
printf(" -h Diese Hilfe\n");
printf(" -o <file> Ausgabedatei\n");
printf(" -c Keine Farbe\n");
printf(" -n Nicht loesen, nur ausgeben\n");
printf("\033[0;1mAusgabe:\033[0m\n"); printf("\033[0;1mAusgabe:\033[0m\n");
printf(" \033[32;1mgruen:\033[0m Vorgegebene Werte\n"); printf(" \033[32;1mgruen:\033[0m Vorgegebene Werte\n");
printf(" \033[33;1mgelb:\033[0m Mit Logik gefundene Werte\n"); printf(" \033[33;1mgelb:\033[0m Mit Logik gefundene Werte\n");
@ -221,8 +294,14 @@ void print_help(int argc, char **argv) {
printf("By Thomas Battermann\n"); printf("By Thomas Battermann\n");
#else #else
printf("Usage:\n"); printf("Usage:\n");
printf(" %s <input_file> [<output_file>]\n\n",argv[0]); printf(" %s [optionen] <input_file> \n",argv[0]);
printf("Optionen:\n");
printf(" -U Unicode Rahmenelemente (Probleme unter Windows!)\n");
printf(" -h Diese Hilfe\n");
printf(" -o <file> Ausgabedatei\n");
printf(" -n Nicht loesen, nur ausgeben\n");
printf("By Thomas Battermann\n"); printf("By Thomas Battermann\n");
#endif #endif
} }
@ -262,6 +341,7 @@ int s_write(char* fn,sudoku* s) {
int i,j; int i,j;
FILE * fp = fopen(fn,"w"); FILE * fp = fopen(fn,"w");
if(fp == NULL) return 0; if(fp == NULL) return 0;
printf("Schreibe in Datei `%s'...\n",fn);
for(i=0;i<9;i++) { for(i=0;i<9;i++) {
for(j=0;j<9;j++) { for(j=0;j<9;j++) {
fputc(s->feld[i][j]+48,fp); fputc(s->feld[i][j]+48,fp);
@ -276,6 +356,7 @@ int s_write_error(char* fn,int err) {
/* Fehler in Datei schreiben */ /* Fehler in Datei schreiben */
FILE * fp = fopen(fn,"w+"); FILE * fp = fopen(fn,"w+");
if(fp == NULL) return 0; if(fp == NULL) return 0;
printf("Schreibe Fehler in Datei `%s'...\n",fn);
switch(err) { switch(err) {
case 0: case 0:
fputs("Eingabefehler\nEingabedatei ist Fehlerhaft!\n",fp); fputs("Eingabefehler\nEingabedatei ist Fehlerhaft!\n",fp);

Loading…
Cancel
Save