回复 10楼 TonyDeng
对的对的,是51个,我需要的是数值,按规则将数值存到a[i][j]这个二维数组中就可以了
回复 10楼 TonyDeng
对的对的,是51个点,我需要的是数据,将这些数据按规则保存到a[i][j]这个二维数组中去就可以了!
/* * 我想读入附件中的数据,我想读入Y对应的数据,并将数据存入到a[i][j]中去,其中i表示第i个x,j表示第j个curve; * 例如对应的数据为a[0][0]=3.500000e+002, a[1][0]=3.487281e+002直到把全部Y数据存入到a[i][j]这个50*341二维数组中! */ #pragma comment( compiler ) #include <cstdio> #include <cstdlib> #include <string> #include <vector> #include <conio.h> const size_t PointsPerCurve = 51; struct Point { double Increment; // X double Temperature; // Y }; struct Curve { Point points[PointsPerCurve]; }; void Pause(void) { printf_s("\nPress any key to continue..."); _getch(); } bool Load_Data(std::vector<Curve*>& curves, const char* fileName) { FILE* inputFile; errno_t errorCode = fopen_s(&inputFile, fileName, "rt"); if (errorCode != 0) { printf_s("数据源文件%s打开错误,错误码为%d,请在网上检索错误码对应的信息。\n", fileName, errorCode); Pause(); return false; } char bufferLine[1024]; while (fgets(bufferLine, _countof(bufferLine), inputFile) != NULL) { bool newCurve = false; if (strstr(bufferLine, "Curve") != NULL) { curves.push_back(new Curve); } if (strstr(bufferLine, "------------------------------") != NULL) { newCurve = true; } if (newCurve) { for (size_t index = 0; index < PointsPerCurve; ++index) { Point point; if (fscanf_s(inputFile, "%lf %lf\n", &point.Increment, &point.Temperature) == 2) { curves.back()->points[index] = point; } } newCurve = false; } } fclose(inputFile); return true; } int main(int argc, char* argv[]) { if (argc < 2) { printf_s("请以数据源文件名为参数启动程序\n"); Pause(); return EXIT_SUCCESS; } std::vector<Curve*> curves; // 容器内的元素是指针,每个指针指向从堆分配的1个Curve数据 if (Load_Data(curves, argv[1])) { // 在堆空间中构造所需要的数组,用完后自己释放 double* arrTemperatures = new double[PointsPerCurve * curves.size()]; size_t curveIndex = 0; for (const Curve* curve : curves) { for (size_t index = 0; index < PointsPerCurve; ++index) { // 注意一维数组当二维的下标用法 arrTemperatures[index * curves.size() + curveIndex] = curve->points[index].Temperature; } ++curveIndex; delete curve; } // 输出数据到文件中以便验证正确性 FILE* outputFile; fopen_s(&outputFile, "opt_out.txt", "wt"); double* p = arrTemperatures; for (size_t i = 0; i < PointsPerCurve; ++i) { for (size_t j = 0; j < curveIndex; ++j) { fprintf_s(outputFile, "%e ", *p++); } fputc('\n', outputFile); } fclose(outputFile); } return EXIT_SUCCESS; }