6-1

image.png

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. void swap(int *, int *);
  6. int n1, n2, n3;
  7. cin >> n1 >> n2 >> n3;
  8. if (n1 > n2)
  9. swap(&n1, &n2);
  10. if (n1 > n3)
  11. swap(&n1, &n3);
  12. if (n2 > n3)
  13. swap(&n2, &n3);
  14. cout << n1 << n2 << n3 << endl;
  15. system("pause");
  16. return 0;
  17. }
  18. void swap(int *p1, int *p2)
  19. {
  20. int temp = *p1;
  21. *p1 = *p2;
  22. *p2 = temp;
  23. }

6-4

image.png

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. void move(int *arr, int n, int m);
  6. int arr[10];
  7. for (int i = 0; i < 10; i++)
  8. arr[i] = i;
  9. move(arr, 10, 3);
  10. for (int i = 0; i < 10; i++)
  11. cout << arr[i];
  12. system("pause");
  13. return 0;
  14. }

循环实现

  1. void move(int *arr, int n, int m)
  2. {
  3. int temp;
  4. while (m-- > 0)
  5. {
  6. temp = *(arr + n - 1);
  7. for (int i = n - 1; i > 0; i--)
  8. {
  9. *(arr + i) = *(arr + i - 1);
  10. }
  11. *(arr) = temp;
  12. }
  13. }

递归实现

  1. void move(int *arr, int n, int m)
  2. {
  3. if (m <= 0)
  4. return;
  5. int temp;
  6. temp = *(arr + n - 1);
  7. for (int i = n - 1; i > 0; i--)
  8. {
  9. *(arr + i) = *(arr + i - 1);
  10. }
  11. *(arr) = temp;
  12. move(arr, n, m - 1);
  13. }

image.png、

6-5

image.png

6-6 统计字符串的长度

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int length(char *p);
  6. char str[20];
  7. cin >> str;
  8. cout << "length=" << length(str) << endl;
  9. system("pause");
  10. return 0;
  11. }
  12. int length(char *p)
  13. {
  14. int count = 0;
  15. while (*p != '\0')
  16. {
  17. count++;
  18. p++;
  19. }
  20. return count;
  21. }

6-7

image.png

6.13

image.png

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. float fsin(float);
  5. float fcos(float);
  6. float fexp(float);
  7. double integral(float (*p)(float), float, float, int);
  8. int main()
  9. {
  10. const int N = 10000;
  11. float (*p)(float);
  12. p = fsin;
  13. cout << "0~1 sinx: \n"
  14. << integral(p, 0, 1, N) << endl;
  15. p = fcos;
  16. cout << "-1~1 cosx \n"
  17. << integral(p, -1, 1, N) << endl;
  18. p = fexp;
  19. cout << "0~2 ex \n"
  20. << integral(p, 0, 2, N) << endl;
  21. system("pause");
  22. return 0;
  23. }
  24. float fsin(float x)
  25. {
  26. return sin(x);
  27. }
  28. float fcos(float x)
  29. {
  30. return cos(x);
  31. }
  32. float fexp(float x)
  33. {
  34. return exp(x);
  35. }
  36. double integral(float (*p)(float), float a, float b, int n)
  37. {
  38. float sum, d;
  39. sum = 0;
  40. d = (b - a) / n;
  41. for (int i = 1; i <= n; i++)
  42. {
  43. sum += (*p)(a + (d * (i - 1))) * d;
  44. // cout << "a + (d * i): " << a + (d * i) << endl;
  45. // cout << "(*p)(a + (d * i)): " << (*p)(a + (d * i)) << endl;
  46. // cout << "sum: " << sum << endl;
  47. }
  48. return sum;
  49. }

image.png

6-14

n个字符按照输入的顺序逆序排列;

  1. #include <iostream>
  2. using namespace std;
  3. void sort(char *str, int n);
  4. int main()
  5. {
  6. char str[100];
  7. cin >> str;
  8. int n = 0;
  9. while (str[n] != '\0')
  10. {
  11. n++;
  12. }
  13. sort(str, n);
  14. cout << str;
  15. system("pause");
  16. return 0;
  17. }
  18. void sort(char *str, int n)
  19. {
  20. char temp, *p1, *p2;
  21. for (int i = 0; i < (n / 2); i++)
  22. {
  23. p1 = str + i;
  24. p2 = str + (n - 1 - i);
  25. temp = *p1;
  26. *p1 = *p2;
  27. *p2 = temp;
  28. }
  29. }

image.png

6-16

image.png

6-17.cpp

  1. #include <iostream>
  2. using namespace std;
  3. int strcmp(char *p1, char *p2);
  4. int main()
  5. {
  6. char str1[10] = "test";
  7. char str2[10] = "testA";
  8. cout << strcmp(str1, str2);
  9. system("pause");
  10. return 0;
  11. }
  12. int strcmp(char *p1, char *p2)
  13. {
  14. while (*p1 != '\0' && *p2 != '\0')
  15. {
  16. if ((*p1 - *p2) != 0)
  17. {
  18. return *p1 - *p2;
  19. }
  20. p1++;
  21. p2++;
  22. }
  23. if (*p1 == '\0' && *p2 == '\0')
  24. {
  25. return 0;
  26. }
  27. else if (*p1 != '\0' && *p2 == '\0')
  28. {
  29. cout << "*p1 != \0" << endl;
  30. return int(*p1);
  31. }
  32. else if (*p1 == '\0' && *p2 != '\0')
  33. {
  34. cout << "*p2 != \0" << endl;
  35. return -int(*p2);
  36. }
  37. return 0;
  38. }