00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <ctype.h>
00004 #include <string.h>
00005 #include <stdbool.h>
00006
00007 #include "set.h"
00008
00009 const char * CmdFin = "FIN";
00010 const char * CmdAyuda = "?";
00011 const char * CmdCrear = "C";
00012 const char * CmdFijar = "S";
00013 const char * CmdPreguntar = "Q";
00014 const char * CmdDatos = "D";
00015 const char * CmdLimpiar = "L";
00016
00017
00018 const char * TxtAyuda = "? - Esta ayuda.\n"
00019 "c Crear conjunto\n"
00020 "s Fijar posición\n"
00021 "l Limpiar posición\n"
00022 "q preguntar posición\n"
00023 "d datos del conjunto\n"
00024 ;
00025
00026 const char * MsgErrorInesperado = "Error inesperado";
00027 const char * MsgRangoIncorrecto = "Rango incorrecto";
00028 const char * MsgPosicionIncorrecta = "Posición incorrecta";
00029 const char * MsgSinMemoria = "Sin memoria";
00030 const char * MsgOk = "Ok";
00031
00032 const char * interpretarError(SetStatus st)
00033 {
00034 const char * toret = MsgErrorInesperado;
00035
00036 switch( st ) {
00037 case SetInvalidRange:
00038 toret = MsgRangoIncorrecto;
00039 break;
00040 case SetInvalidPosition:
00041 toret = MsgPosicionIncorrecta;
00042 break;
00043 case SetOk:
00044 toret = MsgOk;
00045 break;
00046 case SetNoMemory:
00047 toret = MsgSinMemoria;
00048 break;
00049
00050 }
00051
00052 return toret;
00053 }
00054
00055 char * maysCnvt(char * buffer)
00056 {
00057 char * ptr = buffer;
00058
00059 for(; *ptr != 0; ++ptr) {
00060 *ptr = toupper( *ptr );
00061 }
00062
00063 return buffer;
00064 }
00065
00066 char * trimCnvt(char * buffer)
00067 {
00068 static const char * Espacios = " \t\n\r";
00069 char * fin = buffer + strlen( buffer );
00070 char * inicioReal;
00071 char * finReal;
00072
00073
00074 for(inicioReal = buffer; strchr( Espacios, *inicioReal ) != NULL; ++inicioReal);
00075
00076
00077 for(finReal = fin - 1; strchr( Espacios, *finReal ) != NULL; --finReal);
00078
00079
00080 *( finReal + 1 ) = 0;
00081 strcpy( buffer, inicioReal );
00082
00083 return buffer;
00084 }
00085
00086 void crearConjunto(Set * conjunto)
00087 {
00088 int l;
00089 int h;
00090
00091 if ( *conjunto != NULL ) {
00092 destroySet( *conjunto );
00093 }
00094
00095 printf( "Introduzca lim. inferior del conjunto: " );
00096 scanf( "%d", &l );
00097 printf( "\nIntroduzca lim. superior del conjunto: " );
00098 scanf( "%d", &h );
00099
00100 *conjunto = createSet( l, h );
00101 }
00102
00103 void fijarPos(Set conjunto)
00104 {
00105 int pos;
00106
00107 if ( conjunto != NULL ) {
00108 printf( "Introduzca la posición a fijar: " );
00109 scanf( "%d", &pos );
00110
00111 setSetPos( conjunto, pos );
00112 }
00113 else printf( "Conjunto inexistente." );
00114 }
00115
00116 void limpiarPos(Set conjunto)
00117 {
00118 int pos;
00119
00120 if ( conjunto != NULL ) {
00121 printf( "Introduzca la posición a fijar: " );
00122 scanf( "%d", &pos );
00123
00124 clearSetPos( conjunto, pos );
00125 }
00126 else printf( "Conjunto inexistente." );
00127 }
00128
00129
00130 void preguntarPos(Set conjunto)
00131 {
00132 int pos;
00133 bool fijada;
00134
00135 if ( conjunto != NULL ) {
00136 printf( "Introduzca la posición a consultar: " );
00137 scanf( "%d", &pos );
00138
00139 fijada = querySetPos( conjunto, pos );
00140
00141 if ( fijada )
00142 printf( "Pos %d es fijada.\n", pos );
00143 else printf( "Pos %d NO es fijada.\n", pos );
00144 }
00145 else printf( "Conjunto inexistente." );
00146 }
00147
00148 int main()
00149 {
00150 const unsigned int MaxBuffer = 1024;
00151 char buffer[MaxBuffer];
00152 Set conjunto = NULL;
00153 bool orden;
00154
00155 *buffer = 0;
00156 do {
00157 orden = true;
00158 printf( "Introduzca orden (? = ayuda): " );
00159 scanf( "%s", buffer );
00160
00161 maysCnvt( trimCnvt( buffer ) );
00162
00163 if ( !strcmp( buffer, CmdAyuda ) ) {
00164 printf( "\n%s\n", TxtAyuda );
00165 }
00166 else
00167 if ( !strcmp( buffer, CmdCrear ) ) {
00168 crearConjunto( &conjunto );
00169 }
00170 else
00171 if ( !strcmp( buffer, CmdFijar ) ) {
00172 fijarPos( conjunto );
00173 }
00174 else
00175 if ( !strcmp( buffer, CmdPreguntar ) ) {
00176 preguntarPos( conjunto );
00177 }
00178 else
00179 if ( !strcmp( buffer, CmdDatos ) ) {
00180 if ( conjunto != NULL ) {
00181 int pos;
00182 printf( "Conjunto:\nInf %d,\nSup %d.\n",
00183 getLowestPos( conjunto ),
00184 getHighestPos( conjunto )
00185 );
00186
00187 for(pos = 1; pos < 9; ++pos) {
00188 printf( "1234567890" );
00189 }
00190 printf( "\n" );
00191
00192 for(pos = getLowestPos( conjunto );
00193 pos <= getHighestPos( conjunto );
00194 ++pos)
00195 {
00196 if ( querySetPos( conjunto, pos ) )
00197 printf( "1" );
00198 else printf( "0" );
00199 }
00200
00201 printf( "\n\n" );
00202 }
00203 else printf( "Conjunto indefinido\n" );
00204 }
00205 else
00206 if ( !strcmp( buffer, CmdFin ) ) {
00207 printf( "Saliendo ...\n" );
00208
00209 }
00210 else orden = false;
00211
00212 if ( orden )
00213 printf( "\n%s\n", interpretarError( getSetStatus() ) );
00214 else printf( "%s\n", "Comando incorrecto" );
00215
00216 } while( strcmp( buffer, CmdFin ) );
00217
00218
00219 return 0;
00220 }