鱼C论坛

 找回密码
 立即注册
查看: 2219|回复: 1

[技术交流] 自己写的 C++ 头文件

[复制链接]
发表于 2020-1-16 09:15:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zltzlt 于 2020-1-16 09:43 编辑

utils.h

  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. const string sep = " ";
  5. const string newl = "\n";
  6. const string empty_str = "";

  7. bool all(int *array, int length)
  8. {
  9.     int i;
  10.     for (i = 0; i < length; i++)
  11.     {
  12.         if (!array[i])
  13.             return false;
  14.     }
  15.     return true;
  16. }

  17. bool any(int *array, int length)
  18. {
  19.     int i;
  20.     for (i = 0; i < length; i++)
  21.     {
  22.         if (array[i])
  23.             return true;
  24.     }
  25.     return false;
  26. }

  27. void print(string val = empty_str, string end = newl)
  28. {
  29.     cout << val << end;
  30. }

  31. void print_num(double num = 0, string end = newl)
  32. {
  33.     cout << num << end;
  34. }

  35. string input(string prompt = empty_str)
  36. {
  37.     cout << prompt;
  38.     string s;
  39.     cin >> s;
  40.     return s;
  41. }

  42. int int_input(string prompt = empty_str)
  43. {
  44.     cout << prompt;
  45.     long long n;
  46.     cin >> n;
  47.     return n;
  48. }

  49. double float_input(string prompt = empty_str)
  50. {
  51.     cout << prompt;
  52.     double n;
  53.     cin >> n;
  54.     return n;
  55. }

  56. int gcd(int x, int y)
  57. {
  58.     int z;
  59.     while (y)
  60.     {
  61.         z = x % y;
  62.         x = y;
  63.         y = z;
  64.     }
  65.     return x;
  66. }
复制代码


test.cpp

  1. #include <iostream>
  2. #include <string>
  3. #include "utils.h"

  4. int main()
  5. {
  6.     print("Hi!", newl);
  7.     print_num(3, sep);
  8.     print_num(3.1415);
  9.     print(input());
  10.     print_num(int_input("Please input a number: "));
  11.     print_num(float_input("Please input a decimal: "));
  12.     print("The greatest common divisor of 18 and 24 is:", sep);
  13.     print_num(gcd(18, 24));
  14.     return 0;
  15. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-1-16 11:37:35 | 显示全部楼层
顶一个。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 20:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表