C++小数插补计算,顺序查找插补位置,并抛出异常

本文只是确定插补位置,插补计算没写。

插补文件的原始数据,是已经从小到大排列好的,数据设置为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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值