Lectures 7 and 8 of C++mooc in Tongji University
Lecture 7
Topic 1
Use the character pointer to convert the uppercase and lowercase letters in an input string to each other and output the contents of the converted string.For example, assuming the content of the input string is "How are you", the converted content is "hOW ARE YOU"
Code slice.
// #include<iostream> #include<stdio.h> using namespace std; int main() { char s[100], *p = s; gets_s(s); while (*p != '\0') { if (*p != ' ') { if (*p <= 'z'&&*p >= 'a') *p = *p + 'A' - 'a'; else if(*p <= 'Z'&&*p >= 'A')*p = *p - 'A' + 'a'; } *p++; } puts(s); system("pause"); } }
Question 2
Copy from the nth character in string s to string t using a character pointer
Code slice.
// #include<iostream> #include<stdio.h> using namespace std; int main() { char s[100],a[100],*p; gets_s(s); int n,i=0; cout << "From which character do you copy?"; cin >> n; p = &s[n-1]; while (*p!='\0') { a[i++] = *p++; } a[i] = '\0'; i = 0; while (a[i] != '\0') { cout << a[i++]; } system("pause"); }
Question 3
Use the pointer to swap the largest and smallest elements of an array containing 10 integers and output the swapped contents.Ten integers are randomly generated numbers between 0 and 100.
Code slice.
// #include<iostream> #include<stdio.h> using namespace std; int main() { int s[10], *p,i,max,min,m,n; for ( i = 0; i < 10; i++) { s[i] = rand() % 101; } max = s[0]; min = s[0]; for (i = 0; i < 10; i++) { cout<<s[i]<<' ' ; } int *p1=&min , *p2=&max, *p3=&i; cout << endl; for (i = 0; i < 10; i++) { if (s[i] < min) { p1=&s[i]; } if (s[i]> max) { p2 =&s[i]; } } *p3 = *p1; *p1 = *p2; *p2 = *p3; for (i = 0; i < 10; i++) { cout << s[i] << ' '; } cout << endl; system("pause"); }
Lecture 8
Topic 1
Write a function to determine whether m is a prime number and use it in the main function to output the ten smallest pairs of twin prime numbers.Twin prime numbers are two prime numbers that differ by two, such as 3 and 5, 11 and 13.The results of the program are shown in the following figure.
The form of the function is:
bool isprime(int m)
Code slice.
// #include<iostream> #include<stdio.h> using namespace std; bool isprime(int m) { int i = 2; for (i =2; i <m; i++) { if (m%i == 0) { return 0; break; } } return 1; } int main() { int i, cnt=0; for (i = 2;; i++) { if (isprime(i)) { if (isprime(i + 2)) { cout << "(" << i << "," << i + 2 << ")"<<endl; cnt++; } } if (cnt == 10) break; } system("pause"); }
Question 2
A function that determines whether a string is a palindrome or returns 1 if it is a palindrome or 0 if it is not.Palindromes refer to strings that are read along as well as read back, such as "deed" and "level" being palindromes.The input string is called in the main function.
The form of the function is int huiwen(char s[])
Code slice.
// #include<iostream> #include<stdio.h> using namespace std; int huiwen(char s[]) { int t = strlen(s); for (int i = 0;i<t/2; i++) { if (s[i] != s[t - i - 1]) return 0; break; } return 1; } int main() { char s[100]; gets_s(s); if (huiwen(s)) cout << s << "Is palindrome"; else cout << s << "Not palindrome"; system("pause"); }
Question 3
The function is to sort students'scores from high to low, and to count the number of excellent and failed students.Use the following two methods:
(1) Function form is: int fun1(int s[],int n,int *x)
Require excellent returns and failed returns with pointer parameters.
(2) The function form is: void fun2 (int s[], int n, int &x, int &y)
Require the number of EXCELLENTS and failures to return results by referencing parameters.
Make two functions, and the number of students is entered from the keyboard.
Code slice.
// #include<iostream> #include<stdio.h> using namespace std; int fun1(int s[], int n, int *x) { int cnt=0; for (int i = 0; i < n; i++) { if (s[i] > 90) cnt++; if (s[i] < 60) (*x)++; } return cnt; } void fun2(int s[], int n, int &x, int &y) { for (int i = 0; i < n; i++) { if (s[i] > 90) x++; if (s[i] < 60) y++; } } void sort(int *s, int low, int high) { if (low < high) { int m = low, temp; int q = s[high]; int i = low - 1; for (m; m <= high; m++) { if (s[m] >= q) { i++; temp = s[m]; s[m] = s[i]; s[i] = temp; } } sort(s, low, i-1); sort(s, i+1,high); } } int main() { int p=0,q=0; int n,i,a[100]; cout << "Number of students:" ; cin >> n; cout << "Before sorting:"; for (i = 0; i < n; i++) { a[i] = rand() % 101; cout << a[i] << " "; } sort(a, 0, n-1); cout << "After sorting:"; for (i = 0; i < n; i++) { cout << a[i] << " "; } a[n] = 0; int *x=&a[n]; int num = fun1(a,n,x); cout << endl; cout << "The number of outstanding people is:" << num<<endl; cout << "The number of failures is:" << *x; fun2(a, n, p, q); cout << endl; cout << "The number of outstanding people is:" <<p << endl; cout << "The number of failures is:" <<q; system("pause"); }
Question 4
A function that counts the frequency of each letter in a string (not distinguishing between upper and lower case) and also finds the letter with the highest frequency and the number of occurrences, assuming that there is only one letter with the most occurrences.The form of the function is:
void freq(char s[],int p[],char &chmax,int &max)
The program runs as follows:
Code slice.
// using namespace std; #include<iostream>; void freq(char s[], int p[], char &chmax, int &max) { int m = strlen(s); for (int i = 0; i < m; i++) { if (s[i] >= 'A'&&s[i] <= 'Z') s[i] = s[i] - 'A' + 'a'; } for (int i = 0; i < m; i++) { if (s[i] >= 'a'&&s[i] <= 'z') { int x = int(s[i]); p[x - 97]++; } } max = p[0]; for (int i = 0; i < 26; i++) { if (p[i] > max) { max = p[i]; chmax = char(97 + i); } } } int main() { char s[100],chmax='a'; int a[26],max=0; gets_s(s); int i; for ( i= 0; i < 26; i++) { a[i] = 0; } freq(s, a, chmax, max); for (i = 0; i < 26; i++) { if (a[i] != 0) { cout << char(97 + i) << "--" << a[i]<<endl; } } cout << "The letter that appears most frequently is" << chmax << "--" << max; system("pause"); }
Topic 5
A function that constructs the inverse ordinal of a positive integer x.Edit another main function to output 10 smallest palindromes greater than 10,000.Palindrome number refers to the number that is the same in both reading and reading, such as 5, 151, 3553, etc.(Note that the inverse ordinal number here is either the inverse ordinal number in algebra or literally)
The form of the function is: int reverse (int x)
Code slice.
// #include<iostream> using namespace std; int reverse(int y) { int i, cnt = 0; int x = y; for (i = 1;; i++)//Judging how many digits y is { if (x / 10 == 0) break; else x = x / 10; } int a[100], b = 0; for (int m = 0; m < i; m++) { a[m] = y % 10;//Store from bits y = y / 10; } int u = 1; for (int m = i - 1; m >= 0; m--) { b += a[m] * u; u = u * 10; } return b; } int main() { int cnt = 0, i, a[100], flag; for (int x = 10000;; x++) { if (reverse(x) == x) { cout << x<<endl; cnt++; } if (cnt == 10) break; } system("pause"); }
Sixth topic
Write a function to determine if the substring subis a substring of the string s. If so, return the position of the first occurrence of the Subin s. Otherwise, return -1.
Code slice.
// #include "iostream" using namespace std; int judge(char s[],char sub[]) { int m= strlen(s),n=strlen(sub); int x = 0,k,flag; for (int i = 0; i <= m; i++) { flag = 1; k = i; for (x = 0; x < n; x++) { if (sub[x] != s[k++]) { flag = 0; break; } } if(flag==1) return k-1; } return -1; } int main() { char a[100],sub[100]; gets_s(a); gets_s(sub); cout<< judge(a, sub); system("pause"); }