大家好
我是一個C++的新手
這裡有個題目..說要用C++跑出規定的東西
附上我的程式碼
可以請高手幫我看看哪裡有問題嗎
感謝
程式碼如下
using std::cout;
using std::endl;
using std::cerr;
using std::ios;
#include
#include
using std::ofstream;
#include
#include
#include // generate array of uniform distribution on [0,1]
void uniform( double rand_uni [] , int size ){
srand( time(0) );
double max = RAND_MAX;
double random;
for( int i = 0; i < size; ++i ){
random = rand();
rand_uni[i] = random/max;
}
}// end uniform
// generates binomial distribution
void binomial( int n, double p ){
ofstream output_binomial("binomial.txt", ios::out);
double rand_uni[n*1000];
int binomial_out[1000];
for( int i = 0; i < 1000; ++i ){
binomial_out[i] = 0; binomial_out[i] = 0;
}
uniform( rand_uni, n*1000 );
for( int i = 0; i < 1000; ++i ){
for( int j = 0; j < n; ++j ){
if( rand_uni[n*i+j] <= p )
++binomial_out[i];
}
}// end outer for
for( int i = 0; i < 1000; ++i ){
output_binomial << binomial_out[i] << " ";
if( ((i+1))==0 )
output_binomial << endl;
}
output_binomial.close();
}// end binomial
// generate exponential distributions
// generate exponential distributions
void exponential( double lambda ){
ofstream output_exponential("exponential.txt", ios::out);
double rand_uni[1000];
uniform( rand_uni, 1000 );
double exponential_out[1000];
for( int i = 0; i < 1000; ++i ){
exponential_out[i] = ((-1)/(lambda))*log(rand_uni[i]);
}
for( int i = 0; i < 1000; ++i){
output_exponential << exponential_out[i] << " ";
if( ((i+1)%5)==0 )
output_exponential << endl;
}
output_exponential.close();
}// end exponential
// generate normal distribution
void normal( double mean, double variance ){
ofstream output_normal("normal.txt", ios::out);
double rand_uni_1[1000];
double rand_uni_2[1000];
uniform( rand_uni_1, 1000 );
uniform( rand_uni_2, 1000 );
double normal_out[1000];
for( int i = 0; i < 1000; ++i ){
normal_out[i] = ( sqrt(variance) * cos(2*3.1415926*rand_uni_1[i]) *
sqrt( (-2)*(log(rand_uni_2[i])) ) ) + mean;
}
for( int i = 0; i < 1000; ++i ){
output_normal << normal_out[i] << " ";
if( ((i+1)%5)==0 )
output_normal << endl;
}
output_normal.close();
}// end normal
int main( int argc, char* argv[] )
{
if( argc > 1 ){
if( (!strcmp( argv[1], "binomial" )) && (argc == 4) ){
int n;
double p;
char* test1;
char* test2;
n = strtol( argv[2], &test1, 10 );
p = strtod( argv[3], &test2 );
if( *test1 != 0 ){
cout << "Wrong Parameter!";
exit(1);
}
}
else if( n <= 0 ){
cout << "Wrong Parameter!";
exit(1);
}
if( *test2 != 0 ){
cout << "Wrong Parameter!";
exit(1);
}
else if( (p<0) || (p>1) ){
cout << "Wrong Parameter!";
exit(1);
}
binomial( n , p );
}
else if( (!strcmp( argv[1], "exponential" )) && (argc == 3) ){
double lambda;
char* test;
lambda = strtod( argv[2], &test );
if( *test != 0 ){
cout << "Wrong Parameter!";
exit(1);
}
else if( lambda <= 0 ){
cout << "Wrong Parameter!";
exit(1);
}
exponential( lambda );
}
else if( (!strcmp( argv[1], "normal" )) && (argc == 4) ){
double mean;
double variance;
char* test1;
char* test2;
mean = strtod( argv[2], &test1 );
variance = strtod( argv[3], &test2 );
if( *test1 != 0 ){
cout << "Wrong Parameter!";
cout << "Wrong Parameter!";
exit(1);
}
if( *test2 != 0 ){
cout << "Wrong Parameter!";
exit(1);
}
else if( variance <= 0 ){
cout << "Wrong Parameter!";
exit(1);
}
normal( mean, variance );
}
else{
cout << "Wrong Input!";
exit(1);
}
}// end of outmost if
return 0;
return 0;
} // end main
發表人 - zargminidisc 於 2005/06/07 09:53:55