【SEU程序设计课笔记】 09 - 2020/11/09 - Discussion - Solution for Boeing 737 MAX / 数据筛选

背景

Boeing 737 MAX 的软件 MCAS 系统

程序思路

  • 增加传感器(由2提升至4)
  • 写出一个函数data_filter,给出合适的输出结果

程序

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <random>
#include <Windows.h>
#include <conio.h>
using namespace std;

clock_t time_set = 100; // 0.1 second
vector<double> vec; // to store information

double average(vector<double> rec)
{
	double ret = 0;
	for (auto c : rec)
	{
		ret += c;
	}
	return ret / rec.size();
}

double variance(vector<double> rec)
{
	double ave = average(rec);
	double ret = 0;
	for (auto c : rec)
	{
		ret += (c - ave) * (c - ave);
	}
	return ret / rec.size();
}

vector<double> random() // generate test data
{
	vec.clear();
	static default_random_engine e(static_cast<unsigned>(time(NULL)));
	static uniform_int_distribution<int> u1(-2000000, 15000000); // ok
	static uniform_int_distribution<int> u2(-90000000, 90000000); // error
	static uniform_int_distribution<int> u3(0, 5000000); // normal
	static uniform_int_distribution<unsigned> choice(1, 100);
	if (choice(e) % 5 == 0)
	{
		if (choice(e) % 10 == 0)
		{
			if (choice(e) % 40 == 0)
			{
				vec.push_back(u3(e) / 1000000.0);
				vec.push_back(u1(e) / 1000000.0);
				vec.push_back(u2(e) / 1000000.0);
				vec.push_back(u3(e) / 1000000.0);
			}
			else
			{
				for (int i = 0; i != 4; i++)
					vec.push_back(((choice(e) + i) % 4 == 1) ? u1(e) / 1000000.0 : u3(e) / 1000000.0);
			}
		}
		else
		{
			for (int i = 0; i != 4; i++)
				vec.push_back(((choice(e) + i) % 4 == 2) ? u2(e) / 1000000.0 : u3(e) / 1000000.0);
		}
	}
	else
	{
		for (int i = 0; i != 4; i++)
			vec.push_back(u3(e) / 1000000.0);
	}
	return vec;
}

vector<string> data_filter(vector<double>& data)
{
	vector<string> vec_str;
	vector<double> temp;
	for (int i = 0; i != 4; i++)
	{
		if (fabs(data[i] - average(data)) > 1 && fabs(data[i] - average(data)) > 1.5 * sqrt(variance(data)))
		{
			vec_str.push_back(to_string(data[i]) + "*");
		}
		else
		{
			vec_str.push_back(to_string(data[i]) + " ");
			temp.push_back(data[i]);
		}
	}
	string ret = to_string(average(temp));
	if (average(temp) < 0) vec_str.push_back(ret + " ");
	else vec_str.push_back(ret+"");
	return vec_str;
}

void print(vector<string> final_data)
{
	for (int i = 0; i != 5; i++)
		cout << "├─────────────";
	cout << "┤" << endl;
	cout << "│";
	for (int i = 0; i != 5; i++)
	{
		cout << " " << final_data[i];
		for (int j = 0; j != 12 - final_data[i].length(); j++)
			cout << " ";
		cout << "│";
	}
	cout << endl;
}

int main()
{
	// hide the Console Cursor
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	GetConsoleCursorInfo(hOut, &cci);
	cci.bVisible = FALSE;
	SetConsoleCursorInfo(hOut, &cci);

	// change the colour into green
	system("color 02");

	// The title of the table
	cout << "   This is a sipmle data filter program"
		<< " designed by Teddy van Jerry\n" << endl;

	// the front of the table
	for (int i = 0; i != 5; i++)
		cout << "┌─────────────";
	cout << "┐" << endl;
	cout << "│      S1     │      S2     │      S3     │      S4     │      RE     │" << endl;

	// do the test
	while (1)
	{
		if (!_kbhit()) // if there is not a hit on the keyboard
		{
			random();
			vector<string> output = data_filter(vec);
			print(output);
			Sleep(time_set);
		}
		else // if there is a hit on the keyboard, end the test
		{
			for (int i = 0; i != 5; i++)
				cout << "└─────────────"; // the bottom of the table
			cout << "┘" << endl;
			cout << " ALL RIGHTS RESERVED (c) 2020 Teddy van Jerry" << endl;
			break; // over
		}
	}

	return 0;
}

// Copyright: 2020 Teddy van Jerry

输出示例

开头
结尾
改进:
要使小数点对齐,则将print函数改成:

void print(vector<string> final_data)
{
	for (int i = 0; i != 5; i++)
		cout << "├─────────────";
	cout << "┤" << endl;
	cout << "│";
	for (int i = 0; i != 5; i++)
	{
		int alr = 2;
		if (final_data[i][0] == '-')
		{
			--alr;
			if (final_data[i][3] == '.') --alr;
		}
		else
		{
			if (final_data[i][2] == '.') --alr;
		}
		for (int i = 0; i != alr; i++)
			cout << " ";
		cout << " " << final_data[i];
		for (int j = 0; j != 12 - alr - final_data[i].length(); j++)
			cout << " ";
		cout << "│";
	}
	cout << endl;
}

就有这样的效果
Output

分析

  • 关键步骤代码中已有注释。
  • 该程序对同时多个相差较远的数据无能为力。不明白究竟谁才是正确的(比如非常离散或者 2 VS 2)
  • 试验数据为随机生成。
  • 此程序能够做到0.1秒输出一组试验数据,并在按下键盘上任意键时结束。
  • 千万注意对string的加法(否则炸掉)以及string长度的计算!我在这上面差点死过去!!!
  • 有时程序会炸掉,还因为表格那么长放不下!
  • 另一个与数据筛选有关的程序详见我的博客 关于 C++中 处理偏差变量的方法的思考

ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。


See also

Teddy van Jerry 的导航页

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值