Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 7 strings as arrays of chars. #include #include #include using namespace std; int printMenu( ); int hexSymbolValue( char c ); int readBinValue( bool.

Similar presentations


Presentation on theme: "Lab 7 strings as arrays of chars. #include #include #include using namespace std; int printMenu( ); int hexSymbolValue( char c ); int readBinValue( bool."— Presentation transcript:

1 Lab 7 strings as arrays of chars

2 #include #include #include 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() { ……… Let’s concentrate on these first

3 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)

4 int printMenu() { cout > 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; } } strcmp, for char arrays then, let’s focus on how strcmp works first, this is a new way to define a string

5 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 !

6 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 }

7 int hexSymbolValue( 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 function…. receives char, returns int

8 can be used like this: say you have a char…. char myChar = ‘A’; cout << hexSymbolValue( myChar ); will print 10

9 main int main() { while (true) { int choice = printMenu(); switch (choice) { case 1 : …etc…

10 calc decimal from binary case 1 : { bool myArray[ maxInputLength ]; // arbitrarily large int myArraySize; 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

11 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.

12 int readBinValue( bool vect[ ] ) { int actualSize; while(true) { cout > str; for (int i=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; } this is supplied for you, but look how it works …

13 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!

14 calc decimal from binary case 1 : { bool myArray[ maxInputLength ]; // arbitrarily large int myArraySize; 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

15 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] * 2 3 + vect[1] * 2 2 + vect[2] * 2 1 + vect[3] * 2 0

16 int binaryToDecimal( bool vect[ ], 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; }

17 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 )

18 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

19 int readHexValue(char vect[ ] ) { int actualSize; while (true) { cout > str; bool ok = true; for (int i=0; i ='0') && (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; } this is supplied for you, but look how it works …

20 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.

21 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


Download ppt "Lab 7 strings as arrays of chars. #include #include #include using namespace std; int printMenu( ); int hexSymbolValue( char c ); int readBinValue( bool."

Similar presentations


Ads by Google