1 / 21

Lab 7

Lab 7. strings as arrays of chars. Let’s concentrate on these first.

urit
Download Presentation

Lab 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 7 strings as arrays of chars

  2. 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(){ ………

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

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

  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 : {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

  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. 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;}

  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 : {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

  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] * 23 + vect[1] * 22 + vect[2] * 21 + vect[3] * 20

  16. 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;}

  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. 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;}

  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

More Related