#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main()
{
int eventnumber = 100 ;//设置粒子个数
ifstream infile_sy; //定义输入类变量infile_sy
ofstream outfile_sy; //定义输出类变量outfile_sy
infile_sy.open("2-1000mu1000MeVCH1mm.txt");//读取文件
//检验文件是否打开成功
if(!infile_sy)
{
cout<<"文件打开失败"<<endl;
}
//将读取的数据存入数组num
int i,j,datalen = 0;//i是代表粒子序数,从0开始计数
double num[10000];//输入文件数组
double ene[eventnumber]={0};//要计算的能来沉积数组
while(!infile_sy.eof())
infile_sy>>num[datalen++]; //循环将文件数据存入数组中
//使用指针查找数组中的数
double *ptr_n;
double *ptr_e;
ptr_n = num;
ptr_e = ene;
//相同粒子沉积能量累加
for(i=0;i<eventnumber;i++)
{
while(*ptr_n == i)
{
*(ptr_e + i) = *(ptr_e + i) + *(ptr_n + 1);
ptr_n = ptr_n + 2;
}
cout<<*(ptr_e+i)<<endl;
}
/*
//挨个输出ene数组
for(
C++ 读取txt文件中数据并存入数组中
于 2022-04-05 09:52:06 首次发布