本文只是确定插补位置,插补计算没写。
插补文件的原始数据,是已经从小到大排列好的,数据设置为double类型。现在按顺序查找,寻找插入位置。包含小数判断大小。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
using namespace std;
const double esp = 1e-6; //精度
int Order_selection(vector<vector<double> > silly, double testnum);
int main()
{
cout << "Hi,doge" << endl;
double arr[][2] = { {1,34}, {3,25}, {6,43} ,{9,78} ,{12,89} ,{15,114} ,{18,136} };
vector<vector<double> > Silly(sizeof(arr)/ sizeof(arr[0]), vector<double>(2, 0));
int m, n;
for (m = 0; m < Silly.size(); m++)
{
for (n = 0; n < Silly[m].size(); n++)
{
Silly[m][n] = arr[m][n];
cout << Silly[m][n] << " ";
}
cout << "\n";
}
cout << "数组行数为:" << Silly.size() << endl;
cout << "数组列数为:" << Silly[0].size() << endl;
double setnum1 = 7.67;
double setnum2 = 9;
double setnum3 = 15;
int test_num1;//即使是保护代码块运行的数据,也可以被接过来,不接过的,就没了。
try {
test_num1 = Order_selection(Silly,setnum1);
Order_selection(Silly, setnum2);
Order_selection(Silly, setnum3);
Order_selection(Silly, 1);
Order_selection(Silly, 12.001);
Order_selection(Silly, 20);//捕获第一个异常以后,保护代码区域,下面的不再运行
Order_selection(Silly, 0);
}
catch (const char* msg) {
cerr << msg << endl;
}
cout << "\ntest_num1 = " << test_num1 << endl;
return 0;
}
int Order_selection(vector<vector<double> > tem_ErrorTab, double T_num)
{
int Row_num = tem_ErrorTab.size();//行数
//int Col_num = tem_ErrorTab[0].size();//列数
double min_num = tem_ErrorTab[0][0];
double max_num = tem_ErrorTab[Row_num - 1][0];
//抛出异常
if (T_num < min_num && fabs(T_num - min_num) > esp)//小数判断大小
{
cout << "输入的插补值为:" << T_num << endl;
cout << "可计算最小值为:" << min_num << endl;
throw "异常:输入坐标小于可以补偿的最小值";
}
if (T_num > max_num && fabs(T_num - max_num) > esp)
{
cout << "输入的插补值为:" << T_num << endl;
cout << "可计算最大值为:" << max_num << endl;
throw "异常:输入坐标大于可以补偿的最大值";
}
int i;
for (i = 0; i < Row_num; i++)
{
double c_num = tem_ErrorTab[i][0];
if (c_num > T_num && fabs(c_num - T_num) > esp || fabs(c_num - T_num) < esp)
break;
}
cout << "\n参与比较的数是:" << T_num << endl;
cout << "第一个不比它小的数是:" << tem_ErrorTab[i][0] << endl;
cout << "第一个不比它小的数下标是:" << i << endl;
cout << "这是第 " << i + 1 << " 个数"<< endl; //或者用C语言的 printf("第d%个数",i+1)
cout << endl;
return i;
}