C++learn3
数据类型
数字
short s; //定义
int i;
long l;
float f;
double d;
s=10; //赋值
i=1000;
l=1000000;
f=230.67;
d=302.22922;
cout <<“short s :”<<s<<endl;
cout <<“int i :”<<s<<endl;
cout <<“long l :”<<s<<endl;
cout <<“float f :”<<s<<endl;
cout <<“double d :”<<s<<endl;
// C++learn3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <ctime>
#include <random>
#define random(x)(rand()%x)
int main()
{
//数字
short s; //定义
int i;
long l;
float f;
double d;
s = 10; //赋值
i = 1000;
l = 1000000;
f = 230.67;
d = -302.22922;
std::cout << "short s :" << s << std::endl;
std::cout << "int i :" << i << std::endl;
std::cout << "long l :" << l << std::endl;
std::cout << "float f :" << f << std::endl;
std::cout << "double d :" << d << std::endl; //这里的输出为-302.229 只有三位小数
double abs_double =abs(d); //绝对值函数
std::cout <<"abs_double绝对值的值: "<< abs_double << std::endl;
//随机数
srand((unsigned)time(NULL)); //随机数发生器的初始化函数
int k, j;
for ( k = 0; k < 10; k++)
{
//生成实际的随机数
//j = rand();
j = random(100); //100以内的随机数,配合上面的定义
std::cout << "随机数的值: " << j << std::endl;
}
//也可以使用random库生成真随机数 #include <random>
system("pause");
return 0;
}