实验---完整代码展示

#include<iostream>
#include<math.h>
#include<string>
using namespace std;
#define  S(a,b,c)  (a+b+c)/2
#define  area(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c))

bool infer_triangel(double a, double b, double c)
{
	return (a + b > c && a + c > b && b + c > a);
}

int main()
{
	double a, b, c;
	cout << "Please input the numbers !" << endl;
	cin >> a;
	cin >> b;
	cin >> c;
	if (infer_triangel(a, b, c))
	{
		cout << "The area that we require is :" << endl;
		cout << area(a, b, c) << endl;
	}
	else
		cout << "Don’t meet the requirements !" << endl;
	return 0;
}

以上是实验一的完整代码展示 


#include<iostream>
#include<string>
using namespace std;
void bubble_sort(int* array, int length)
{
	for (int i = 0; i < length - 1; i++)
	{
		for (int j = 0; j < length - i - 1; j++)
		{
			int temp = 0;
			temp = array[j];
			if (array[j] < array[j + 1])
			{
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}
		}
	}
}
void sort_array(int* array)
{
	cout << "Please input 15 different numbers" << endl;
	for (int i = 0; i < 15; i++)
	{
		cin >> array[i];
	}
}
void select_sort(int* array, int length)
{
	for (int i = 0; i < length - 1; i++)
	{
		int temp = 0;
		temp = array[i];
		int max_loc = i;
		for (int j = i; j < length; j++)
		{
			if (array[j] > array[max_loc])
			{
				max_loc = j;
			}
		}
		array[i] = array[max_loc];
		array[max_loc] = temp;
	}
}
void printarray(int* array)
{
	for (int i = 0; i < 15; i++)
	{
		cout << array[i] << " ";
	}
}
void binary_search(int* array, int num, int length)
{
	int left = 0;
	int right = length;
	int mid = 0;
	while (left <= right)
	{
		int mid = (left + right) / 2;
		if (array[mid] == num)
		{
			cout << "The number you been found, its serial number is : " << endl;
			cout << mid + 1 << endl;
			break;
		}

		else if (array[mid] > num)
		{
			left = mid + 1;
		}
		else
		{
			right = mid - 1;
		}
	}
	if (left > right)
	{
		cout << "无此数" << endl;
	}
}
int main()
{
	int array[15] = {};
	cout << "Please input 15 different numbers !!!" << endl;
	for (int i = 0; i < 15; i++)
	{
		cin >> array[i];
	}
	int length = 0;
	length = sizeof(array) / sizeof(array[0]);
	select_sort(array, length);
	cout << endl;
	cout << "Arrays have been arranged in descending order !!!" << endl;
	cout << endl;
	int target = 0;
	cout << "Please enter the number that you need to query for !!!" << endl;
	cout << endl;
	cin >> target;
	binary_search(array, target, length);
	return 0;
}

以上是实验二的完整代码展示


如有不对,非常欢迎批评与指正!!!谢谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值