#include <iostream> #include <string> #include <cmath> #include <Windows.h> #include <fstream> using namespace std; const double THRESHOLD=1.5; double sy_check(double arr[],int length,int n) { double XSqrt=0; for(int i=0;i<n;i++) { XSqrt+=pow(arr[length-i],2); } return XSqrt/n; } int main(void) { string filename; ifstream fin; int num,long_window=0,weight_window=0; double time_cr=0,*sensor=NULL,weight_power=0,long_power=0; double ratio; cout<<"Please input your file name:"; cin>>filename; fin.open(filename.c_str()); if(fin.fail()) { cout<<"Enter file of name"<<endl; exit(-1); }else { fin>>num>>time_cr; if(num>=0) { sensor=new double[num]; for(int i=0;i<num;i++) { fin>>sensor[i]; } cout<<"请输入长时间:"<<endl; cin>>long_window; cout<<"请输入短时间:"<<endl; cin>>weight_window; for(int i=long_window-1;i<num;i++) { long_power=sy_check(sensor,i,long_window); weight_power=sy_check(sensor,i,weight_window); ratio=weight_power/long_power; if(ratio>THRESHOLD) { cout<<"Possible event at"<<time_cr*i<<"seconds\n"; } } //这条语句要在for外面 delete[] sensor; } fin.close(); } system("pause"); return 0; }
运行结果: