gctl_toolkits/archive/addnoise/main.cpp
2024-09-10 20:25:18 +08:00

89 lines
2.4 KiB
C++

#include "addnoise.h"
void disp_help(char* proname)
{
cout << proname << " - 0.1 add Gaussian noise to a 1D, 2D or 3D data set" << endl
<< "Author: zhangyi.cugwuhan@gmail.com" << endl << endl
<< "usage: " << proname << " -i<input-file> -o<output-file> -n<mean>/<deviation> [-d<x-col>[,<y-col>,<z-col>],<val-col>] [-h]" << endl
<< "-i\tinput file (ASCII)" << endl
<< "-o\toutput file" << endl
<< "-n\tnoise parameters, mean value and standard deviation of the Gaussian noise" << endl
<< "-d\tdata columns, the default is 0,1,2 for a 2D data set. The program uses the last column as input data, all others will be simply copied to the output" << endl
<< "-h\tshow this info" << endl;
}
int main(int argc, char** argv)
{
char infilename[1024] = "NULL";
char outfilename[1024] = "NULL";
char cols[1024] = "0,1,2";
char noise[1024] = "NULL";
opterr = 0; //内置参数 若不为0则会在发生遭遇错误时输出一条信息到屏幕
if (argc == 1)
{
cout << "use -h option for help information" << endl;
return 0;
}
int curr;
/*循环拾取参数 最后一个参数为-1 需要变量的参数后跟一个冒号 可有可无参数跟两个冒号*/
while((curr = getopt(argc,argv,"hi:o:n:d:")) != -1)
{
/*匹配命令*/
switch (curr)
{
case 'h': //显示帮助信息
disp_help(argv[0]);
break;
case 'i':
if (1!=sscanf(optarg,"%s",infilename))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 'o':
if (1!=sscanf(optarg,"%s",outfilename))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 'n':
if (1!=sscanf(optarg,"%s",noise))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 'd':
if (1!=sscanf(optarg,"%s",cols))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case '?': //处理未定义或错误参数
if (optopt == 'i' || optopt == 'o' || optopt == 'n' || optopt == 'd')
{
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
return -1;
}
else if (isprint(optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
return -1;
}
else
{
fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
return -1;
}
break;
default:
abort();
}
}
addnoise as;
as.runtine(infilename,outfilename,noise,cols);
return 0;
}