如何实现把这些很相同的代码写成一个函数或者一个类?
程序代码:const int NYEAR_HIST = 3; //总共模拟几年,必须接CONST哟
const int START_YEAR_HIST = 1990;//模拟起始年
//
//读温度的方法(原始数据是摄氏度)
//************************************************************************************
//确保数据格式如下:
// 经度 纬度 起始年份 1990年1月数据 1990年2月数据 .........XX年11月数据 XX年12月数据
// -36.5 93 1990 12.4 -12.5 .................-12.6 23
// 100 -92 1990 -12.1 0 34 13
double lon = gridlist.getobj().lon;
double lat = gridlist.getobj().lat;
FILE* in; double dlon, dlat, dyear;//从文件里读出来的数据****
double dmtemp[12 * NYEAR_HIST];//从文件里读取月(温度)
double mtemp[NYEAR_HIST][12];//给予赋值,在模型里计算的月(温度)
//********************************
//打开文件
in = fopen(file_temp, "r");
if (!in) fail("readenv: could not open %s for input", (char*)file_temp);
//********************************
//循环或许当前STAND的数据(温度!!!!)
bool foundgrid;//找到了这个格子的数据
foundgrid = false;//一开始是处于没有找到格子的状态
while (!feof(in) && !foundgrid) { //while(真)则继续循环,while(到文件尾 && 未找到)停止遍历
// Read next record in file,下面的((36))要改!!!!!!!重要
readfor(in, "f,f,i,36f", &dlon, &dlat, &dyear, dmtemp);//读取数据,其中36要改 36=12*模拟的年份,即NYEAR_HIST!!!
// f表示浮点数 i表示整数 36f表示有36个月就读36个数据
if (equal(lon, dlon) && equal(lat, dlat) && equal((START_YEAR_HIST), dyear)) foundgrid = true;//等于ture时就停止遍历了
}
if (!foundgrid) fail("readenv: could not find record for (%g,%g) in %s",
gridlist.getobj().lon, gridlist.getobj().lat, (char*)file_temp);//如果没找到格子的话if(!假)=if(真)就会执行这个语句,输出错误结果
//*********************************
//将提取的月数据赋值给要进行计算的数据
int y, m;
for (y = 0; y < NYEAR_HIST; y++) {
for (m = 0; m < 12; m++) {
mtemp[y][m] = dmtemp[y * 12 + m]; // now deg mtemp[0][11]即起始年12月份的数据 mtemp[1][0]即第二年1月份数据
}
}
fclose(in);//关闭打开的文件
//*************************************************************************************
//循环读取当前stand温度完毕
//
//读降雨的方法
//************************************************************************************
//确保数据格式如下:
// 经度 纬度 起始年份 1990年1月数据 1990年2月数据 .........XX年11月数据 XX年12月数据
// -36.5 93 1990 124 125 ..................126 23
// 100 -92 1990 121 0 34 13
double lon = gridlist.getobj().lon;
double lat = gridlist.getobj().lat;
FILE* in; double dlon, dlat, dyear;//从文件里读出来的数据****
double dmprec[12 * NYEAR_HIST];//从文件里读取月(降水)
double mprec[NYEAR_HIST][12];//给予赋值,在模型里计算的月(降水)
//********************************
//打开文件
in = fopen(file_prec, "r");
if (!in) fail("readenv: could not open %s for input", (char*)file_prec);
//********************************
//循环或许当前STAND的数据(温度!!!!)
bool foundgrid;//找到了这个格子的数据
foundgrid = false;//一开始是处于没有找到格子的状态
while (!feof(in) && !foundgrid) { //while(真)则继续循环,while(到文件尾 && 未找到)停止遍历
// Read next record in file,下面的((36))要改!!!!!!!重要
readfor(in, "f,f,i,36f", &dlon, &dlat, &dyear, dprec);//读取数据,其中36要改 36=12*模拟的年份,即NYEAR_HIST!!!
// f表示浮点数 i表示整数 36f表示有36个月就读36个数据
if (equal(lon, dlon) && equal(lat, dlat) && equal((START_YEAR_HIST), dyear)) foundgrid = true;//等于ture时就停止遍历了
}
if (!foundgrid) fail("readenv: could not find record for (%g,%g) in %s",
gridlist.getobj().lon, gridlist.getobj().lat, (char*)file_prec);//如果没找到格子的话if(!假)=if(真)就会执行这个语句,输出错误结果
//*********************************
//将提取的月数据赋值给要进行计算的数据
int y, m;
for (y = 0; y < NYEAR_HIST; y++) {
for (m = 0; m < 12; m++) {
mprec[y][m] = dmprec[y * 12 + m]; // now mm mtemp[0][11]即起始年12月份的数据 mtemp[1][0]即第二年1月份数据
// Limit very low precip amounts because negligible precipitation causes problems
// in the prdaily function (infinite loops).
if (mprec[y][m] <= 1) mprec[y][m] = 0;
}
}
fclose(in);//关闭打开的文件
//*************************************************************************************
//循环读取当前stand降雨完毕
//
//读光照百分比的方法
//************************************************************************************
//自己的方法!!!!!!!自己的方法!!!!!!!,其中readfor()中读月数据量需要根据实际数据修改
//确保数据格式如下:
// 经度 纬度 起始年份 1990年1月数据 1990年2月数据 .........XX年11月数据 XX年12月数据
// -36.5 93 1990 56 60 ..................80 23
// 100 -92 1990 12 0 34 13
double lon = gridlist.getobj().lon;
double lat = gridlist.getobj().lat;
FILE* in; double dlon, dlat, dyear;//从文件里读出来的数据****
double dmsun[12 * NYEAR_HIST];//从文件里读取月(光照)
double msun[NYEAR_HIST][12];//给予赋值,在模型里计算的月(光照)
//********************************
//打开文件
in = fopen(file_sun, "r");
if (!in) fail("readenv: could not open %s for input", (char*)file_sun);
//********************************
//循环或许当前STAND的数据(光照!!!!)
bool foundgrid;//找到了这个格子的数据
foundgrid = false;//一开始是处于没有找到格子的状态
while (!feof(in) && !foundgrid) { //while(真)则继续循环,while(到文件尾 && 未找到)停止遍历
// Read next record in file,下面的((36))要改!!!!!!!重要
readfor(in, "f,f,i,36f", &dlon, &dlat, &dyear, dmsun);//读取数据,其中36要改 36=12*模拟的年份,即NYEAR_HIST!!!
// f表示浮点数 i表示整数 36f表示有36个月就读36个数据
if (equal(lon, dlon) && equal(lat, dlat) && equal((START_YEAR_HIST), dyear)) foundgrid = true;//等于ture时就停止遍历了
}
if (!foundgrid) fail("readenv: could not find record for (%g,%g) in %s",
gridlist.getobj().lon, gridlist.getobj().lat, (char*)file_sun);//如果没找到格子的话if(!假)=if(真)就会执行这个语句,输出错误结果
//*********************************
//将提取的月数据赋值给要进行计算的数据
int y, m;
for (y = 0; y < NYEAR_HIST; y++) {
for (m = 0; m < 12; m++) {
msun[y][m] = dmsun[y * 12 + m] ; // % sun mtemp[0][11]即起始年12月份的数据 mtemp[1][0]即第二年1月份数据
}
}
fclose(in);//关闭打开的文件
//*************************************************************************************
//循环读取当前stand光照百分比完毕如何实现把这些很相同的代码写成一个函数或者一个类?
另外问一下,定义的dlon,dlat等好多变量,都重新定义了好多遍,每次重新定义对跟在定义后的代码有什么影响吗?






