我试图将代码从要求cin用户输入x和y值更改为使用带有文件名的单个命令行参数。该文件具有x和y的一系列双精度值,每行一对由空格隔开。我了解如何像在这里一样编写代码,但是当涉及到命令行参数时,我会感到困惑。如果有人可以提供任何技巧或帮助转换代码,我将不胜感激。
1 #include <iostream>
2 #include <vector>
3 #include <cmath>
4
5 using namespace std;
6
7 double correlation_r(const vector<double> &x, const vector<double> &y);
8 double mean(const vector<double> &x_obs);
9 double st_dev(const vector<double> &x_obs);
10 void least_squares_regression(const vector<double> &x, const vector<double> &y);
11
12 int main(){
13
14 double x;
15 double y;
16 vector<double> x_obs;
17 vector<double> y_obs;
18
19 cout << "Please enter two real number values for x and y: ";
20
21 while(cin >> x >> y){
22 if (!cin.good()){
23 break;
24 } else {
25 x_obs.push_back(x);
26 y_obs.push_back(y);
27
28 cout << "Please enter two real number values for x and y or a string to terminate: ";
29 }
30 }
31
32 if(x_obs.size()==0||x_obs.size()==1){
33 cout << "The sample was too small to be useful.";
34 return 0;
35 }
36
37 cout << "The sample mean of x is " << mean(x_obs) << endl;
38 cout << "The sample mean of y is " << mean(y_obs) << endl;
39
40 cout << "The sample standard deviation of x is " << st_dev(x_obs) << endl;
41 cout << "The sample standard deviation of y is " << st_dev(y_obs) << endl;
42
43 if(st_dev(x_obs)==0.0){
44 cout << "The correlation is undefined." << endl;
45 } else if(st_dev(y_obs)==0.0&& st_dev(x_obs)!=0.0){
46 cout << "The correlation is undefined." << endl;
47 } else {
48 cout << "The sample correlation is " << correlation_r(x_obs, y_obs) << endl;
49 }
50
51
52 least_squares_regression(x_obs, y_obs);
53
54 return 0;
55 }
56 void least_squares_regression(const vector<double> &x, const vector<double> &y){
57 double m;
58 double b;
59 if(st_dev(x)!=0 && st_dev(y)!=0){
60 m=correlation_r(x,y)*(st_dev(y)/st_dev(x));
61 b= mean(y)-(m*mean(x));
62 cout << "The least squares regression line is y=" << m << "x+ " << b << endl;
63 } else if(st_dev(x)==0){
64 cout<< "The least squares linear regression line is undefined."<< endl;
65 } else if(st_dev(y)==0 && st_dev(x)!=0){
66 cout<< "The least squares linear regression line is y=" << mean(y) << endl;
67 }
68 return;
69 }
70
71
72 double correlation_r(const vector<double> &x, const vector<double> &y){
73 double r = 0.0;
74 for(int i = 0; i < x.size(); i++){
75 r += (x[i] - mean(x))*(y[i] - mean(y))/st_dev(x)/st_dev(y);
76 }
77 return r = r / (x.size() - 1.0);
78 }
79
80
81 double mean(const vector<double> &x_obs){
82 double total = 0.0;
83 for (int i = 0; i < x_obs.size(); i++){
84 total += x_obs[i];
85 }
86 return total/x_obs.size();
87 }
88
89 double st_dev(const vector<double> &x_obs){
90 double x_bar = mean(x_obs);
91 double total = 0.0;
92 for (double elem : x_obs){
93 total += pow(elem - x_bar, 2);
94 }
95 return pow( total/(x_obs.size() - 1.0), .5);
96 }
答案 0 :(得分:1)
您需要将主要定义更改为
int main(int argc, char ** argv)
argc
“自变量计数”是传递给程序的自变量数量(以空格分隔)
argv
“参数值”是包含您的参数的c字符串数组。 argv[0]
始终是您调用程序的路径。
因此,要完成您想实现的目标,请更改主要内容并使用命令行参数来创建一个fstream
或ifstream
对象,该对象的行为与您的标准istream
朋友{ {1}}只是从文件而不是cin
编辑: 我应该说您不能随心所欲地重载主程序,但是这是标准主程序的两个签名。