C++生成随机数以及cout输出大数字

随机数

random.h

#ifndef XY_RANDOM
#define XY_RANDOM


class Random {
public:
	Random(int = 0, int = 0);
private:
	int seed;
public:
	int random(int = 0, int = 100);
};


#endif

random.cpp

#include 
#include 
#include "random.h"


Random::Random(int seed, int iv) {
	this->seed = seed ? seed : static_cast(time(NULL));
	if (iv) {
		this->seed ^= iv;
	}
	srand(this->seed);
}

int Random::random(int start, int end) {
	return start < end ? rand() % (end - start + 1) + start : rand() % (start - end + 1) + end;
}

main.cpp

#include 
#include "random.h"


void run() {
	Random* r = new Random();
	std::cout << r->random() << std::endl;
	std::cout << r->random(0, 1) << std::endl;
	std::cout << r->random(0, 100) << std::endl;
	std::cout << r->random(-100, 100) << std::endl;
	std::cout << r->random(100, -100) << std::endl;
}


int main() {
	run();
	return 0;
}

运行结果:

C++生成随机数以及cout输出大数字_第1张图片

C++生成随机数以及cout输出大数字_第2张图片

C++生成随机数以及cout输出大数字_第3张图片

 

cout输出大数字

#include 
#include 


int main() {
	/*** 第一种方法 ***/
	std::cout.setf(std::ios::fixed);
	std::cout << pow(2, 8) << std::endl;
	std::cout << pow(2, 16) << std::endl;
	std::cout << pow(2, 32) << std::endl;
	std::cout << pow(2, 64) << std::endl;
	
	/*** 第二种方法 ***/
	// std::cout << std::fixed << std::setprecision(6) << pow(2, 8) << std::endl;
	// std::cout << std::fixed << std::setprecision(6) << pow(2, 16) << std::endl;
	// std::cout << std::fixed << std::setprecision(6) << pow(2, 32) << std::endl;
	// std::cout << std::fixed << std::setprecision(6) << pow(2, 64) << std::endl;
	
	return 0;
}

运行结果:

C++生成随机数以及cout输出大数字_第4张图片

 

你可能感兴趣的:(C++)