程序的输入是名为map.txt的数据文件和用于计算短时间能量和长时间能量的 取样值的数目。输出是给出关于潜在的地震事件次数的报告
#include <iostream>
#include <fstream>
using namespace std;
//定义一个指标用来判断是否发生了地震
#define INDEX 1.5
double power(double arr[11], int n, int window){
double xsqure = 0;
for (int i = 0; i < window; i++){
xsqure = pow(arr[n - i], 2);//这个pow函数是取次方的意思
}
return xsqure / window;
}
int main(void) {
string filename;
int num = 0;
double senior = 0, long_power = 0, short_power = 0, finall = 0;
int long_window = 0;
int short_window = 0;
double *senior_num = NULL;
cout << "请输入文件名:" << endl;
cin >> filename;
ifstream file;
file.open(filename);
if (file.fail()) {
cerr << "输入文件名错误:"<<strerror(errno) << endl;
}
file >> num >> senior;
cout << num <<" " << senior << endl;
if (num>0){
senior_num = new double[num];//按需分配空间
for (int i = 0; i < num; i++){
file >> senior_num[i];
}
}
cout << "请输入长期判断的数据:" << endl;
cin >> long_window;
cout << "请输入短期判断的数据:" << endl;
cin >> short_window;
for (int i = long_window-1; i < num; i++){
long_power = power(senior_num, i, long_window);
short_power = power(senior_num, i, short_window);
cout << long_power << endl;
cout << short_power << endl;
finall = short_power / long_power;
cout << finall << endl;
if (finall > INDEX) {
cout << "地震可能发生在:" << senior * i << " " << endl;
}
}
delete[] senior_num;//释放空间
}
?
?
#include <iostream>
#include <fstream>
using namespace std;
/*
编写一个程序,使用 cin 从标准输入输入 3 段文字,保存到一段动态分配的
内存中,每一段文字输入后,必须要即时保存到动态内存中
*/
int main(void) {
string* p = NULL;
string str;
int i = 0;
p = new string[3];
while (i<3){
str.clear();//每次循环将字符串清空
cout << "请输入"<< i+1 <<"第个字符串:" << endl;
cin >> str;
*(p + i) = str;
cout << "此时动态内存中p的值为:" << endl;
for (int k = 0; k <= i; k++){
cout << *(p+k) <<"";
}
cout << endl;
i++;
}
delete[] p;//释放内存
}
?
#include <iostream>
#include <fstream>
using namespace std;
/*
编写一个程序,链接两个字符串字面常量,将结果保存在一个动态分配的 char
数组中。重写这个程序,连接两个标准 string 对象
*/
int main(void) {
string str1 = "name";
string str2 = "我的";
string str3 = str1 + str2;
string* str = new string[2];
for (int i = 0; i < str3.length(); i++){
*(str + i) = str3[i];
}
for (int i = 0; i < str3.length(); i++) {
cout << *(str + i);
}
delete[] str;//释放内存
}
#include<iostream>
#include <stdlib.h>
using namespace std;
int main(void) {
int* p = NULL;
int row, col;
int i, j, k = 1;
cout << "Input number of row : \n";
cin >> row;
cout << "Input number of column\n";
cin >> col;
p = new int[row * col];
if ((row * col)==0) {
cout << "Not allocate memory!\n";
exit(1);
}
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) p[i*col+j] = k++;//根据行列下标,计算下标值
}
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
cout << p[i * col + j] << '\t';//根据行列下标,计算下表值
cout << endl;
}
delete[] p;
return 0;
}
?