210 likes | 351 Views
Lab 7. strings as arrays of chars. Let’s concentrate on these first.
E N D
Lab 7 strings as arrays of chars
Let’s concentrate on these first #include <iostream>#include <cstring>#include <math.h>using namespace std;int printMenu( );int hexSymbolValue( char c );int readBinValue( bool vect[ ] );int readHexValue( char vect[ ] );int binaryToDecimal( bool vect[ ], int size );int hexidecimalToDecimal( char vect[ ], int size );const int maxInputLength = 255;int main(){ ………
functions int printMenu( ); - takes no parameters (nothing in the parenthesis) - returns an int to the caller int hexSymbolValue( char c ); - takes a char parameter - returns an int to the caller - clearly, this converts a hex char(0-9 and A-F) to an integer (0-15)
strcmp, for char arrays intprintMenu(){cout << "Please choose a command" << endl;cout << "0) Exit " << endl;cout << "1) Convert bin to decimal" << endl;cout << "2) Convert hex to decimal" << endl;cout << endl; char str[ maxInputLength ]; cin >> str; if (strcmp(str,"0")== 0) { return 0; } else if (strcmp(str,"1")== 0) { return 1; } else if (strcmp(str,"2")== 0) { return 2; } else { cout << "Invalid input." << endl; } } first, this is a new way to define a string then, let’s focus on how strcmp works
arrays of chars? string Str = “Hello”; or char *Str = {“Hello”}; or char Str[5] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’}; // this one offers the most utility // because it allows Str[ x ], strcmp, strlen // and cin !
strcmp • can only compare strings, even if they are one character long • returns a zero if strings match e.g. strcmp( str, “2” ) compares the contents of str with “2” and is == 0 if they match if ( strcmp(str,“2") == 0 ) { return 2; // returns an int 2 back to main }
function…. receives char, returns int inthexSymbolValue( char c) { switch ( c ) { case '0' : return 0; case '1' : return 1; case '2' : return 2; case '3' : return 3; case '4' : return 4; case '5' : return 5; case '6' : return 6; case '7' : return 7; case '8' : return 8; case '9' : return 9; case 'A' : return 10; case 'B' : return 11; case 'C' : return 12; case 'D' : return 13; case 'E' : return 14; case 'F' : return 15; } // end switch} // end function
can be used like this: say you have a char…. char myChar = ‘A’; cout << hexSymbolValue( myChar ); will print 10
main int main() { while (true) { int choice = printMenu(); switch (choice) { case 1 : …etc…
calc decimal from binary case 1 : {boolmyArray[ maxInputLength ]; // arbitrarily largeintmyArraySize; myArraySize = readBinValue( myArray );int result = binaryToDecimal( myArray, myArraySize ); cout << "result = " << result << endl; break; } first, get an array of bools from the user then, calculate the decimal equivalent
myArraySize = readBinValue( myArray ); • my Array is an array of bools, which is empty before the call, but filled with ones and zeros after myArray before: [ ] myArray after: [1, 0, 1, 0] myArraySize is thus 4. • within the function readBinaryValue, vect[ ] is used to hold myArray.
this is supplied for you, but look how it works … intreadBinValue( boolvect[ ] ){ intactualSize; while(true) {cout << "Please enter a binary number: " << endl; char str[maxInputLength];cin >> str; for (inti=0; i<strlen(str); i++) { if (str[i]=='0') vect[i] = false; else if (str[i]=='1') vect[i] = true; else {cout << "Invalid input." << endl; ok = false; continue; } } // end for if (!ok) continue;actualSize = strlen(str); break; } // end while (true)return actualSize;}
returns size, but also • in main: myArraySize = readBinValue( myArray ); • in the function int readBinValue( bool vect[ ] ) BOTH myArraySize and myArray are CHANGED BY THE FUNCTION!
calc decimal from binary case 1 : {boolmyArray[ maxInputLength ]; // arbitrarily largeintmyArraySize; myArraySize = readBinValue( myArray ); int result = 0; // define and init result result = binaryToDecimal( myArray, myArraySize ); cout << "result = " << result << endl; break; } first, get an array of bools from the user then, calculate the decimal equivalent
result = binaryToDecimal( myArray, myArraySize ); think of it as: result = binaryToDecimal( [ 1, 0 , 1, 0 ] , 4 ); within the function binaryToDecimal, vect[ ] is used to hold myArray. therefore, result = vect[0] * 23 + vect[1] * 22 + vect[2] * 21 + vect[3] * 20
intbinaryToDecimal( boolvect[ ], int size ){int result = 0;for (int x=0; x<size; x++) // x = 0, 1, 2, 3 { result = result + ( vect[x] * (int)pow(2,size-1-x) ); } // pow(2 to the 3, 2, 1, 0 )return result;}
result = result + ( vect[x] * (int)pow(2,size-1-x) ); • since pow returns a double, it must be cast to an int • (int) takes the double from pow, and truncates it to an integer for (int x=0; x<size; x++) // x = 0, 1, 2, 3 { result = result + ( vect[x] * (int)pow(2,size-1-x) ); } // pow(2 to the 3, 2, 1, 0 )
calc decimal from hex case 2 : { char myArray[maxInputLength]; int myArraySize; myArraySize = readHexValue( myArray ); int result = 0; // define and init result int result = hexidecimalToDecimal(myArray, myArraySize ); cout << "result = " << result << endl; break; } first, get an array of CHARs from the user then, calculate the decimal equivalent
this is supplied for you, but look how it works … int readHexValue(char vect[ ] ){int actualSize;while (true) { cout << "Please enter an hex number: " << endl; char str[ maxInputLength ];cin >> str; bool ok = true; for (int i=0; i<strlen(str); i++) { if (((str[i]>='0') && (str[i]<='9')) || ((str[i]>='A')&&(str[i]<='F'))) {vect[ I ] = str[ I ]; } else { cout << "Invalid input." << endl; ok = false; break; } } if (!ok) continue; actualSize = strlen(str); break; } return actualSize;}
myArraySize = readHexValue( myArray ); • my Array is an array of chars, which is empty before the call, but filled with characters after myArray before: [ ] myArray after: [‘A’, ‘E’, ‘F’, ‘6’] myArraySize is thus 4. • within the function readHexValue, vect[ ] is used to hold myArray.
int hexidecimalToDecimal( char vect[], int size ){int result = 0;for (int x=0; x<size; x++) { result = result + (hexSymbolValue(vect[x] ) * (int)pow(16, size-1-x) ); } return result;} needs ints not chars