The document provides an overview of C++ programming concepts including variable types, control structures like do-while and switch statements, string manipulation, and array declarations. It includes examples of type casting, string to integer conversion, and comparisons between C-style strings and C++ strings. Additionally, it discusses scoping rules for variables and functions, as well as basic operations on arrays.
The document provides an overview of C++ programming concepts including variable types, control structures like do-while and switch statements, string manipulation, and array declarations. It includes examples of type casting, string to integer conversion, and comparisons between C-style strings and C++ strings. Additionally, it discusses scoping rules for variables and functions, as well as basic operations on arrays.
int x=5 ==> double y = x/2; // equals 2.0 First iteration always run!! do { statement 2;} double z = x/2.0; // equals 2.5 while (condition1); // Don’t forget the semicolon cout.setf(ios::fixed); cout.precision(2); //把小数精确到两位 && “and”; || “or” Scoping De Morgan’s laws 所有的变量只存在在属于他们的{ }里 ! (A||B) ==> !A && !B Variables declared outside the function do not exist ! (A&&B) ==> !A || !B inside the function unless they are global variables static_cast<double>(total)/nscores ==> 两个 int 转 换为 double **Convert String to Int string number = “125”; int result =0; int multiplier = 1; String for (int i = number.size()-1; i>=0; i—) { #include <cstring> result += (number[i] - ‘0’)*multiplier; string s = “Hello”; multiplier*=10;} cout<<s.size(); ==> writes 5 return result; cout<<s[4]; ==>writes o s[6]; ==> Undefined behavior Array cin.ignore(10000, ‘\n’) [前一个 cin numbers,现在 Declare an array: 想 input string] const int NUMBER_OF_MONTH = 12; Substring: string s = “duplicate”; int daysInMonth[NUMBER_OF_MONTH]; [array size s.substr(5,3); //writes cat has to be a constant] int arr[] = {1,2,3}; #include <cctype> isdigit (char xx) //true if it is a digit CString islower (ch) // ‘a’ ‘b’ #define _CRT_SECURE_NO_WARNINGS //can use isupper (ch)// ‘A’ ‘B’ both cstring and c++string isalpha (ch) //是否是 2 字母 不论大小写 #include <cstring> toupper (ch) //转换成大写 不是字母就什么都不改变 Declare CString: char t[10]= “Ghost”; // 'G' 'h' 'o' 's' 't' '\0' Compare String char s[100] =“”; Ghostwriter < Ghoul (s has a lower value than u, b/c s is early than u) //the length of string is irrelevant; only read in a c string: cin.getline(s, 100); //tells the find the first mismatch and compare the alphabetical number of characters the array can store order strlen(t); // outputs 5 Switch Statement strcpy(s,t) //copy t to s The value tested must be an integral type (e.g. int, strcat(s, “!!!”); // now s is “Ghost!!!" char, short, long, etc.) not string strcmp(a,b); //returns a int == negative if a comes switch (number) { before b; 0 if they are equal; positive if a comes after case 0: b alphabetically case 2: string s2 = t; // initialize a c++string with a c string value = “Good”; string s = “hello”; break; f(s.c_str()); //look c++string in a c string’s way case 3: value = “Bad”; Is a equal to b? break; wrong: if (strcmp(a,b)) //actually yields the opposite default: result right: if (strcomp(a,b)==0); {int total = 0; string s=“Hello"; for (int k=0; k<n; k++) f(s);//won't compile! s is a C++ string, not a C string {if (strlen(a[1]) == targetLength) f(s.c_str());//OK total++;} return total;} char t[10]=“Ghost"; s=t;//s is now Ghost void eraseChar(char str[], char c){ s=“Wow"; int x = 0; r=s;//won't compile while(str[x] != ‘\0’){ t=s.c_str();//won't compile if(str[x]==c){ for(int y = x;str[y]!=0;y++) strcpy(t,s.c_str());//t is now Wow str[y] = str[y + 1]; else N-dimensional array x++;} double meanForADay(const a[][NWEEKS], int nRows, } int dayNumber) // except for the first array, it has to know how many entries are in the following arrays