#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int sort(int n,int a[]);
int sort(int n,int a[]) // 因第 6、7 段程序的需要,定义排序函数
{
int i, j, p, temp;
for(i=0; i < n; i++)
{
p = i;
for(j = i + 1; j < n; j++) //找出数组中的最大值
if(a[p] > a[j]) // 由小至大排序
p = j;
if(p != i) //将最大值与a[0]调换
{
temp = a[i];
a[i] = a[p];
a[p] = temp;
}
}
return 0;
}
int main()
{
int n;
printf("请选择:1、三个排序;2、闰年识别;3、三角形面积计算;4、四则运算;");
printf("5、10个数的最大值与最小值;6、选择法排序;7、二分法查找;8、退出程序;");
scanf("%d", &n);
switch (n)
{
case 1:
{
int a, b, c,max, mid, min;
printf("请输入3个数: ");
scanf("%d%d%d", &a, &b, &c);
max = a;
min = a;
if (max < b)
max = b;
if (max < c)
max = c;
if (min > b)
min = b;
if (min > c)
min = c;
mid = a + b + c - max - min;
printf("%d %d %d\n", max,mid,min);
break;
}
case 2:
{
int year;
printf("请输入待查询的年份:");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
printf("输入的年份 %d 是闰年\n", year);
}
else
printf("输入的年份 %d 不是闰年\n", year);
break;
}
case 3:
{
double a, b, c, s, area;
printf("输入三角形的三条边:");
scanf("%lf%lf%lf", &a, &b, &c);
s = (a + b + c) / 2.0;
area = sqrt(s*(s - a)*(s - b)*(s - c));
printf("面积 = %.2lf\n", area);
break;
}
case 4:
{
int i;
double a, b;
char c, str[4];
printf("请输入因数及运算符:");
scanf("%lf%lf%s", &a, &b, str);
for (i = 0;i < 4;i++)
{
if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
{
c = str[i];
}
}
if (c == '+')
{
printf("%.2f\n", a + b);
}
else if (c == '-')
{
printf("%.2f\n", a - b);
}
else if (c == '*')
{
printf("%.2f\n", a*b);
}
else if (c == '/')
{
if (b != 0)
{
printf("%.2f\n", a / b);
}
else
{
printf("除数不能为0 !\n");
}
}
break;
}
case 5:
{
int i, j, min = 0, max = 0;
int a[2][5] = {0};
printf("请输入10个数:");
for (i = 0;i < 2;i++)
{
for (j = 0;j < 5;j++)
{
scanf("%d", &a[i][j]);
}
}
max = a[0][0];
min = a[0][0];
for (i = 0;i < 2;i++)
{
for (j = 0;j < 5;j++)
{
if (max < a[i][j])
{
max = a[i][j];
}
if (min > a[i][j])
{
min = a[i][j];
}
}
}
printf("最大的值是:%d ", max);
printf("最小的值是:%d ", min);
break;
}
case 6:
{
int i, n, a[11];
printf("enter data: ");
for (i = 0;i < 10;i++)
{
scanf("%d", &a[i]);
}
n = sizeof(a) / sizeof(int);
sort(n,a); // 调用函数,实现选择法排序
printf("The sorted numbers: ");
for (i = 0;i < 10;i++)
printf("%d ", a[i]);
printf("\n");
break;
}
case 7:
{
int a[] = { 1,5,66,8,55,9,1,32,5,65,4,8,5,15,64,156,1564,15,1,8,9,7,215,
16,45,5,6,164,15,236,2,5,55,6,4,1,59,23,4,5,314,56,15,3,54,
1,54,54,2,4,4,5,15,698,486,56,26,98,78,456,1894,564,26,56,5 };
int left = 0;
int right = 9;
int mid;
int n; //存放数组a中元素的个数
int m; //查找的数字
int i; //循环变量
n = sizeof(a) / sizeof(int); //求出数组中所有元素的个数
sort(n,a);
printf("请输入待查找的数字: ");
scanf("%d", &m);
while (left <= right) // 二分法查找
{
mid = (left + right) / 2;
if (a[mid] > 1.5)
{
right = mid-1;
}
else if (a[mid] < 1.5)
{
left = mid + 1;
}
else
{
printf("在位于:%d 找到了所输入的数\n", mid);
break;
}
}
if (left > right)
{
printf("找不到所输入的数");
}
return 0;
break;
}
case 8:
{
// 退出控制台程序
}
}
}