自己写的 C++ 头文件
本帖最后由 zltzlt 于 2020-1-16 09:43 编辑utils.h
#include <iostream>
#include <string>
using namespace std;
const string sep = " ";
const string newl = "\n";
const string empty_str = "";
bool all(int *array, int length)
{
int i;
for (i = 0; i < length; i++)
{
if (!array)
return false;
}
return true;
}
bool any(int *array, int length)
{
int i;
for (i = 0; i < length; i++)
{
if (array)
return true;
}
return false;
}
void print(string val = empty_str, string end = newl)
{
cout << val << end;
}
void print_num(double num = 0, string end = newl)
{
cout << num << end;
}
string input(string prompt = empty_str)
{
cout << prompt;
string s;
cin >> s;
return s;
}
int int_input(string prompt = empty_str)
{
cout << prompt;
long long n;
cin >> n;
return n;
}
double float_input(string prompt = empty_str)
{
cout << prompt;
double n;
cin >> n;
return n;
}
int gcd(int x, int y)
{
int z;
while (y)
{
z = x % y;
x = y;
y = z;
}
return x;
}
test.cpp
#include <iostream>
#include <string>
#include "utils.h"
int main()
{
print("Hi!", newl);
print_num(3, sep);
print_num(3.1415);
print(input());
print_num(int_input("Please input a number: "));
print_num(float_input("Please input a decimal: "));
print("The greatest common divisor of 18 and 24 is:", sep);
print_num(gcd(18, 24));
return 0;
} 顶一个。
页:
[1]