#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;
}
以上是实验二的完整代码展示
如有不对,非常欢迎批评与指正!!!谢谢!