|
Salt Lake City Saturday, 2013.05.25 09:48 MDT [GMT-7] |
![]() |
Condition: Partly Cloudy Temperature: 21°C/70°F Barometer: 1014.9 mb and rising |
| IT | |
|---|---|
| MIX | |
| News |
/**
* @title: assign2.cpp
* @date: 2004.09.13 - 17:19
* @author: vinnie
* @source for assign2.exe
*/
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// ========================================
// GLOBAL VARs
// init the hex set
const char mySet[17] = "0123456789ABCDEF\0";
// ========================================
// user error check
void userError( int exitCode)
{
switch ( exitCode)
{
case -1:
printf( "\nWrong lenght in set A\nExit code: %d\n", exitCode);
exit( exitCode);
case -2:
printf( "\nWrong lenght in set B\nExit code: %d\n", exitCode);
exit( exitCode);
case -3:
printf( "\nWrong character in set A\nExit code: %d\n", exitCode);
exit( exitCode);
case -4:
printf( "\nWrong character in set B\nExit code: %d\n", exitCode);
exit( exitCode);
}
}
// ==============================================
// given a char and a string, returns true if the
// char is present in the string
bool isContained( char c, char *p)
{
int iLen = strlen( p);
for ( int i = 0; i < iLen; i++)
if ( c == *(p+i) )
return true;
return false;
}
// =================
// *** M A I N ***
// =================
int main( )
{
// declare the strings used
char sub_a[17], sub_b[17];
// BEGIN of the app
APPINIT:
system("color 4e");
system("cls");
memset( sub_a, 0, 17);
memset( sub_b, 0, 17);
// print menu
printf(" Given the set of the hexadecimal numbers\n S = {");
for ( int i = 0; i < 15; i++)
printf( "%c, ", *(mySet+i) );
printf( "%c}\n\n", *(mySet+15) );
printf("1) the user can choose 2 subset, called A and B\n");
printf("2) each subset can be 1-16 characters long;\n");
printf("3) the subset must be input as a string, i.e.:\n");
printf(" a) no spaces\n");
printf(" b) no tabs\n");
printf(" c) no punctuation\n\n");
printf(" VALID examples: aB2cF345 aaffcc123 AAFFCC123 A2b3c4D5e6\n");
printf(" NOT valid: a,f ,4,c, B . 3; ce;\n\n");
printf("4) now input the subset A\n\n");
printf("A = ");
// user input
gets( sub_a);
printf("\n5) now input subset B\n\nB = ");
gets( sub_b);
// toUpper the strings and check the length
int iLen = strlen( sub_a);
if ( iLen > 16 ) userError(-1);
for ( int i = 0; i < iLen; i++)
if ( isalpha( sub_a[i]))
sub_a[i] = toupper( sub_a[i]);
iLen = strlen( sub_b);
if ( iLen > 16 ) userError(-2);
for ( int i = 0; i < iLen; i++)
if ( isalpha( sub_b[i]))
sub_b[i] = toupper( sub_b[i]);
// check subset A
iLen = strlen( sub_a);
for ( int i = 0; i < iLen; i++)
if ( !( isdigit( sub_a[i])))
if ( sub_a[i] != 0x41 && sub_a[i] != 0x42 && sub_a[i] != 0x43 &&
sub_a[i] != 0x44 && sub_a[i] != 0x45 && sub_a[i] != 0x46
)
userError( -3);
// check subset B
iLen = strlen( sub_b);
for ( int i = 0; i < iLen; i++)
if ( !( isdigit( sub_b[i])))
if ( sub_b[i] != 0x41 && sub_b[i] != 0x42 && sub_b[i] != 0x43 &&
sub_b[i] != 0x44 && sub_b[i] != 0x45 && sub_b[i] != 0x46
)
userError( -4);
// clearScreen and
system("color 1f");
system("cls");
// print the headers
printf(" Set H = {");
for ( int i = 0; i < 15; i++)
printf( "%c, ", *(mySet+i) );
printf( "%c}\n", *(mySet+15) );
printf(" Input Set A = %s\n", sub_a);
printf(" Input Set B = %s\n", sub_b);
// order the subsets and check for repeated chars in the set
// order subset A
char ar[17];
memset( ar, 0 , 17);
for ( int i = 0; i < 16; i++)
if ( isContained( mySet[i], sub_a) )
{
char stub[2] = "\0\0";
stub[0] = mySet[i];
strcat( ar, stub);
}
iLen = strlen( ar);
memset( sub_a, 0, 17);
for ( int i = 0; i < iLen; i++)
sub_a[i] = ar[i];
// order subset B
memset( ar, 0 , 17);
for ( int i = 0; i < 16; i++)
if ( isContained( mySet[i], sub_b) )
{
char stub[2] = "\0\0";
stub[0] = mySet[i];
strcat( ar, stub);
}
iLen = strlen( ar);
memset( sub_b, 0, 17);
for ( int i = 0; i < iLen; i++)
sub_b[i] = ar[i];
// ==================================
// let's begin. we need a result var
char strResult[50];
// init it
memset( strResult, 0, 50);
// ===================
// C O M P L E M E N T
// ===================
strcat( strResult, "{");
for ( int i = 0; i < 16; i++)
{
if ( ! isContained( mySet[i], sub_a))
{
char stub[4];
stub[0]=mySet[i];stub[1]=',';stub[2]=' ';stub[3]='\0';
strcat( strResult, stub);
}
}
// umph, kill the last comma + space
iLen = strlen( strResult);
strResult[iLen-2] = '\0';
// append close curl bracket
strcat( strResult, "}");
// --------------------
printf(" Ordered set A = %s\n", sub_a);
printf(" Ordered set B = %s\n", sub_b);
// print result
printf("\n Complement of A = %s\n", strResult);
// =========
// U N I O N
// =========
memset( strResult, 0, 50);
strcat( strResult, "{");
for ( int i = 0; i < 16; i++)
{
if ( isContained( mySet[i], sub_a) || isContained( mySet[i], sub_b) )
{
char stub[4];
stub[0]=mySet[i];stub[1]=',';stub[2]=' ';stub[3]='\0';
strcat( strResult, stub);
}
}
iLen = strlen( strResult);
strResult[iLen-2] = '\0';
strcat( strResult, "}");
printf(" Union of A, B = %s\n", strResult);
// =======================
// I N T E R S E C T I O N
// =======================
memset( strResult, 0, 50);
strcat( strResult, "{");
for ( int i = 0; i < 16; i++)
{
if ( isContained( mySet[i], sub_a) && isContained( mySet[i], sub_b) )
{
char stub[4];
stub[0]=mySet[i];stub[1]=',';stub[2]=' ';stub[3]='\0';
strcat( strResult, stub);
}
}
iLen = strlen( strResult);
strResult[iLen-2] = '\0';
strcat( strResult, "}");
printf(" Intersection of A, B = %s\n", strResult);
// ===================
// D I F F E R E N C E
// ===================
memset( strResult, 0, 50);
strcat( strResult, "{");
for ( int i = 0; i < 16; i++)
{
if ( isContained( mySet[i], sub_a) && ! isContained( mySet[i], sub_b) )
{
char stub[4];
stub[0]=mySet[i];stub[1]=',';stub[2]=' ';stub[3]='\0';
strcat( strResult, stub);
}
}
iLen = strlen( strResult);
strResult[iLen-2] = '\0';
strcat( strResult, "}");
printf(" Difference of A - B = %s\n", strResult);
// =======================
// E X C L U S I V E O R
// =======================
memset( strResult, 0, 50);
strcat( strResult, "{");
for ( int i = 0; i < 16; i++)
{
if
(
( isContained( mySet[i], sub_a) && ! isContained( mySet[i], sub_b) )
||
( isContained( mySet[i], sub_b) && ! isContained( mySet[i], sub_a) )
)
{
char stub[4];
stub[0]=mySet[i];stub[1]=',';stub[2]=' ';stub[3]='\0';
strcat( strResult, stub);
}
}
iLen = strlen( strResult);
strResult[iLen-2] = '\0';
strcat( strResult, "}");
printf(" Exclusive Or of A, B = %s\n", strResult);
// =====================
// ask for an other trip
char c;
printf("\n Another one [Y/y/N/n]? ");
while ( !kbhit() );
c = getchar( );
if ( c == 'Y' || c == 'y' ) goto APPINIT;
// =====================================
// ''' The End '''
system("color 0f");
printf("\n Thanks for using assign2 !!\n");
printf("\n Vincenzo Maggio CodeŠ - released under GPL\n\n");
} // niam
Vincenzo Maggio