6-1

#include <iostream>using namespace std;int main(){void swap(int *, int *);int n1, n2, n3;cin >> n1 >> n2 >> n3;if (n1 > n2)swap(&n1, &n2);if (n1 > n3)swap(&n1, &n3);if (n2 > n3)swap(&n2, &n3);cout << n1 << n2 << n3 << endl;system("pause");return 0;}void swap(int *p1, int *p2){int temp = *p1;*p1 = *p2;*p2 = temp;}
6-4

#include <iostream>using namespace std;int main(){void move(int *arr, int n, int m);int arr[10];for (int i = 0; i < 10; i++)arr[i] = i;move(arr, 10, 3);for (int i = 0; i < 10; i++)cout << arr[i];system("pause");return 0;}
循环实现
void move(int *arr, int n, int m){int temp;while (m-- > 0){temp = *(arr + n - 1);for (int i = n - 1; i > 0; i--){*(arr + i) = *(arr + i - 1);}*(arr) = temp;}}
递归实现
void move(int *arr, int n, int m){if (m <= 0)return;int temp;temp = *(arr + n - 1);for (int i = n - 1; i > 0; i--){*(arr + i) = *(arr + i - 1);}*(arr) = temp;move(arr, n, m - 1);}
、
6-5

6-6 统计字符串的长度
#include <iostream>using namespace std;int main(){int length(char *p);char str[20];cin >> str;cout << "length=" << length(str) << endl;system("pause");return 0;}int length(char *p){int count = 0;while (*p != '\0'){count++;p++;}return count;}
6-7
6.13

#include <iostream>#include <cmath>using namespace std;float fsin(float);float fcos(float);float fexp(float);double integral(float (*p)(float), float, float, int);int main(){const int N = 10000;float (*p)(float);p = fsin;cout << "0~1 sinx: \n"<< integral(p, 0, 1, N) << endl;p = fcos;cout << "-1~1 cosx \n"<< integral(p, -1, 1, N) << endl;p = fexp;cout << "0~2 ex \n"<< integral(p, 0, 2, N) << endl;system("pause");return 0;}float fsin(float x){return sin(x);}float fcos(float x){return cos(x);}float fexp(float x){return exp(x);}double integral(float (*p)(float), float a, float b, int n){float sum, d;sum = 0;d = (b - a) / n;for (int i = 1; i <= n; i++){sum += (*p)(a + (d * (i - 1))) * d;// cout << "a + (d * i): " << a + (d * i) << endl;// cout << "(*p)(a + (d * i)): " << (*p)(a + (d * i)) << endl;// cout << "sum: " << sum << endl;}return sum;}

6-14
n个字符按照输入的顺序逆序排列;
#include <iostream>using namespace std;void sort(char *str, int n);int main(){char str[100];cin >> str;int n = 0;while (str[n] != '\0'){n++;}sort(str, n);cout << str;system("pause");return 0;}void sort(char *str, int n){char temp, *p1, *p2;for (int i = 0; i < (n / 2); i++){p1 = str + i;p2 = str + (n - 1 - i);temp = *p1;*p1 = *p2;*p2 = temp;}}

6-16

6-17.cpp
#include <iostream>using namespace std;int strcmp(char *p1, char *p2);int main(){char str1[10] = "test";char str2[10] = "testA";cout << strcmp(str1, str2);system("pause");return 0;}int strcmp(char *p1, char *p2){while (*p1 != '\0' && *p2 != '\0'){if ((*p1 - *p2) != 0){return *p1 - *p2;}p1++;p2++;}if (*p1 == '\0' && *p2 == '\0'){return 0;}else if (*p1 != '\0' && *p2 == '\0'){cout << "*p1 != \0" << endl;return int(*p1);}else if (*p1 == '\0' && *p2 != '\0'){cout << "*p2 != \0" << endl;return -int(*p2);}return 0;}
