1.使用牛顿迭代法求方程

牛顿迭代法的公式: xn+1 = xn-f(x1)/f’(x1)

  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. int main()
  5. {
  6. // 牛顿迭代法的公式: xn+1 = xn-f(x1)/f'(x1)
  7. // double sqrt(double x); x^2
  8. double x0, x1, f, f1;
  9. x1 = 1.5;
  10. do
  11. {
  12. x0 = x1;
  13. f = 2 * x0 * x0 * x0 - 4 * x0 * x0 + 3 * x0 - 6;
  14. f1 = 6 * x0 * x0 - 8 * x0 + 3;
  15. x1 = x0 - f / f1;
  16. } while (fabs(x0 - x1) >= 1e-6);
  17. cout << "结果为:" << x1 << endl;
  18. system("pause");
  19. return 0;
  20. }

image.png

2.实现字符串拷贝功能

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. //实现字符串的拷贝功能
  5. //1.string
  6. void scopy(string &a, string &b)
  7. {
  8. a = b;
  9. }
  10. //2字符数组
  11. void scopy(char *a, char *b)
  12. {
  13. while (*b != '\0')
  14. {
  15. *a = *b;
  16. a++;
  17. b++;
  18. }
  19. *a = '\0';
  20. }

第一种方法脱裤子放屁,没事找事。
第二种方法要重点学习。

3.编写一个函数检测输入的字符是大写还是小写或者不是26个英文字母

  1. #include <iostream>
  2. using namespace std;
  3. //编写一个函数检测输入的字符是大写还是小写或者不是26个英文字母
  4. void identify(char s)
  5. {
  6. if (s > 'a' && s < 'z')
  7. {
  8. cout << "小写" << endl;
  9. }
  10. else if (s > 'A' && s < 'Z')
  11. {
  12. cout << "大写" << endl;
  13. }
  14. else if (s > '0' && s < '9')
  15. {
  16. cout << "数字" << endl;
  17. }
  18. else
  19. {
  20. cout << "特殊字符" << endl;
  21. }
  22. }
  23. int main()
  24. {
  25. char c;
  26. do
  27. {
  28. cin >> c;
  29. identify(c);
  30. } while (c != 'q');
  31. system("pause");
  32. return 0;
  33. }

image.png

(补充)统计字符串中大小写字母和数字的个数

  1. #include <iostream>
  2. using namespace std;
  3. void identify(char s, int &a, int &b, int &c, int &d)
  4. {
  5. if (s > 'a' && s < 'z')
  6. {
  7. a++;
  8. }
  9. else if (s > 'A' && s < 'Z')
  10. {
  11. b++;
  12. }
  13. else if (s > '0' && s < '9')
  14. {
  15. c++;
  16. }
  17. else
  18. {
  19. d++;
  20. }
  21. }
  22. void count(char *s)
  23. {
  24. int a, b, c, d;
  25. a = b = c = d = 0;
  26. while (*s != '\0')
  27. {
  28. identify(*s, a, b, c, d);
  29. s++;
  30. }
  31. cout << "小写:" << a << endl;
  32. cout << "大写:" << b << endl;
  33. cout << "数字:" << c << endl;
  34. cout << "特殊字符:" << d << endl;
  35. }
  36. int main()
  37. {
  38. char a[] = "hh21ERRR2dd}}{}{fdf}";
  39. count(a);
  40. system("pause");
  41. return 0;
  42. }

image.png

4.密码编译

  1. A->Z a->z;
  2. B->Y b->Y

只改变字母,数字不发生改变第i个字母变成