我有一段C++函数,是关于生成随机数的,如何更改它输出值的小数点位置,要求精确到小数点后两位

#include "stdafx.h"
#include <iostream>
#include <list>
#include <iterator>
#include <math.h>
#include <TIME.H>
using namespace std;
#define M_PI 3.14

class randNbs
{
public:
void Display()
{
cout<< "***X coordinates***" << endl;
for(list<double>::iterator itList=(myXs.begin()); itList!=(myXs.end()); itList++)
{
cout<< (*itList) << endl;
}

cout<< "***Y coordinates***" << endl;
for(list<double>::iterator itList=(myYs.begin()); itList!=(myYs.end()); itList++)
{
cout<< (*itList) << endl;
}
}

randNbs (int dimension, double miu, double sigma, double min, double max)
{
srand ( time(NULL) );
for (int i = 0; i<dimension; i++){
double temp = NormalRandom(miu, sigma, min, max);
myXs.push_back(temp);
myYs.push_back(Normal(temp, miu, sigma));
}
}
protected:

double AverageRandom(double min,double max)
{
int nbRand = rand() % 10001;
return (min + nbRand*(max-min)/10000);
}

double Normal(double x,double miu,double sigma)
{
return 1.0/sqrt(2*M_PI*sigma) * exp(-1*(x-miu)*(x-miu)/(2*sigma*sigma));
}

double NormalRandom(double miu, double sigma,double min,double max)
{
double x;
double dScope;
double y;
do
{
x = AverageRandom(min,max);
y = Normal(x, miu, sigma);
dScope = AverageRandom(0, Normal(miu,miu,sigma));
}while( dScope > y);
return x;
}

list <double> myXs, myYs;
};

int main() {
randNbs *myNbs = new randNbs(100, 0, 1, -3, 3);
myNbs->Display();
return 0;
}

示例:
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
double a= 12.3456789;
cout<<a<<endl;//输出为12.3457,因为C++默认输出6位
cout<<setprecision(5)<<a<<endl;//输出为12.234,总的输出位数为5
cout<<setiosflags(ios::fixed)
<<setprecision(5)<<a<<endl;//输出为12.34568,小数位数是5位,不够补0;
cout<<endl;
cout<<resetiosflags(ios::fixed); //取消精度的设置

double b=12.34;
cout<<b<<endl;//输出为12.34,
cout<<setprecision(5)<<b<<endl;//输出为12.23,原来的数位不够也不补0;
cout<<setiosflags(ios::fixed)
<<setprecision(5)<<b<<endl;//输出为12.34000,数位数是5位,不够补0;

system("pause");
}

输出如下:

12.3457

12.346

12.34568

12.34

12.34

12.34000

==========================

void Display()
{
cout<< "***X coordinates***" << endl;
for(list<double>::iterator itList=(myXs.begin()); itList!=(myXs.end()); itList++)
{
cout<<setiosflags(ios::fixed)
<<setprecision(2)<< (*itList) << endl;
}
试试。
需要注意的问题:
第一:用setiosflags 和 setprecision 两个函数之前必须 #include<iomanip>
第二:楼主的display函数里面重复定义 itList ,输出X的时候 定义了一次,输入Y的时候也定义了一次,编译没通过。

这些改过来之后,运行成功。
最后提醒楼主,不要把代码写在类的定义里面. 不直观。
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-04-14
这样改就行了
#include <iostream>
#include <list>
#include <fstream>
#include <iterator>
#include <iomanip>
#include <math.h>
#include <TIME.H>
using namespace std;
#define M_PI 3.14

class randNbs
{
public:
void Display()
{
ofstream fout;
fout.open("output.txt");
fout<< "***X coordinates***" << endl;
for(list<double>::iterator itList=(myXs.begin()); itList!=(myXs.end()); itList++)
{
fout<< setiosflags(ios::fixed)<<setprecision(2)<< (*itList) << endl;
}

fout<< "***Y coordinates***" << endl;
for(list<double>::iterator itList=(myYs.begin()); itList!=(myYs.end()); itList++)
{
fout<< setiosflags(ios::fixed)<<setprecision(2)<< (*itList) << endl;
}
fout.close();
}

randNbs (int dimension, double miu, double sigma, double min, double max)
{
srand ( time(NULL) );
for (int i = 0; i<dimension; i++){
double temp = NormalRandom(miu, sigma, min, max);
myXs.push_back(temp);
myYs.push_back(Normal(temp, miu, sigma));
}
}
protected:

double AverageRandom(double min,double max)
{
int nbRand = rand() % 10001;
return (min + nbRand*(max-min)/10000);
}

double Normal(double x,double miu,double sigma)
{
return 1.0/sqrt(2*M_PI*sigma) * exp(-1*(x-miu)*(x-miu)/(2*sigma*sigma));
}

double NormalRandom(double miu, double sigma,double min,double max)
{
double x;
double dScope;
double y;
do
{
x = AverageRandom(min,max);
y = Normal(x, miu, sigma);
dScope = AverageRandom(0, Normal(miu,miu,sigma));
}while( dScope > y);
return x;
}

list <double> myXs, myYs;
};

int main() {
randNbs *myNbs = new randNbs(100, 0, 1, -3, 3);
myNbs->Display();
return 0;
}
会在你的主文件所在的目录下,生成一个output.txt文件,依次输出X和Y坐标,其中均保留两位小数
另外,团IDC网上有许多产品团购,便宜有口碑本回答被提问者采纳
相似回答