繁体   English   中英

如何在 C++ 中将 cin 输入更改为命令行参数输入

[英]How to Change a cin Input to a Command Line Argument Input in C++

我试图将我的代码从要求 x 和 y 值的 cin 用户输入更改为使用带有文件名的单个命令行参数。 该文件有一系列 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 }

您需要将主要定义更改为

int main(int argc, char ** argv)

argc "Argument count" 是参数的数量,由空格分隔,你传递给你的程序

argv "Argument value" 是一个包含参数的 c 字符串数组。 argv[0]始终是您的调用程序路径。

因此,要执行您想要实现的目标,请更改您的 main 并使用命令行参数来创建一个fstreamifstream对象,该对象的作用与您的标准istream朋友cin非常相似,只是它从文件而不是stdin读取

编辑:我应该说你不能随意超载你的主程序,但这是标准主程序的两个签名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM