用一维数组存储由键盘输入的一组正整数(约20个). 再输入整数x. 如果数组中已经包含x, 则删除它; 否则按次序 20
【要求】1数据的输入输出,排序,查找,插入数据,删除数据等功能必须用函数实现,且每个函数只能实现一个功能2排序必须使用选择排序法3查找必须使用折半查找法4排序,删除,插入...
【要求】
1 数据的输入输出, 排序, 查找, 插入数据, 删除数据等功能必须用函数实现, 且每个函数只能实现一个功能
2 排序必须使用选择排序法
3 查找必须使用折半查找法
4 排序, 删除, 插入等操作前后须输出数组中的内容, 以便比对操作效果
5 不得使用全局变量
6 在输入和输出数据之前必须要有英文的提示信息 展开
1 数据的输入输出, 排序, 查找, 插入数据, 删除数据等功能必须用函数实现, 且每个函数只能实现一个功能
2 排序必须使用选择排序法
3 查找必须使用折半查找法
4 排序, 删除, 插入等操作前后须输出数组中的内容, 以便比对操作效果
5 不得使用全局变量
6 在输入和输出数据之前必须要有英文的提示信息 展开
2个回答
展开全部
已经通过QQ发送
/*
用一维数组存储由键盘输入的一组正整数(约20个). 再输入整数x. 如果数组中已经包含x, 则删除它; 否则按次序将x插入数组.
假设在输入数据(x除外) 时能够保证: 每个数据都不超过100, 数据无序, 且互不相同.
要求】
1 数据的输入输出, 排序, 查找, 插入数据, 删除数据等功能必须用函数实现, 且每个函数只能实现一个功能
2 排序必须使用选择排序法
3 查找必须使用折半查找法
4 排序, 删除, 插入等操作前后须输出数组中的内容, 以便比对操作效果
5 不得使用全局变量
6 在输入和输出数据之前必须要有英文的提示信息
*/
#include
// 输入
void input(int a[], int len)
{
int i = 0;
while (i < len)
{
scanf("%d", &a[i++]);
}
}
// 输出
void output(int a[], int len)
{
printf("[ ");
int i = 0;
while (i < len) printf("%3d ", a[i++]);
printf("]\n");
}
// 排序
void reorder(int a[], int len)
{
int i, j, index, max;
for (i = len - 1; i >= 0; i--)
{
index = 0;
max = 0;
for (j = 0; j <= i; j++)
{
if (a[j] > max)
{
max = a[j];
index = j;
}
}
a[index] = a[i];
a[i] = max;
}
}
// 查找
int search(int a[], int begin, int end, int target)
{
int left = begin;
int right = end;
int mid = (left + right) / 2;
if (left >= right)
return -1;
if (a[mid] == target)
return mid;
else if (a[mid] < target)
return search(a, mid, right, target);
else
return search(a, left, mid, target);
}
// 插入
void insert(int a[], int len, int target)
{
int i = 0;
int index = -1;
if (target <= a[0]) index = 0;
else if (target >= a[len - 1]) index = len;
else
{
for (i = 0; i < len; i++)
{
if (a[i] target && a[i + 1] >= target)
{
index = i + 1;
break;
}
}
}
for (i = len; i > index; i--)
{
a[i] = a[i - 1];
}
a[index] = target;
}
// 删除
void delete(int a[], int len, int target)
{
int index = search(a, 0, len, target);
if (index != -1)
{
int i = 0;
for (i = index; i < len; i++)
{
a[i] = a[i + 1];
}
}
}
int main()
{
int a[30] = {0};
int count = 20;
int index = -1;
int x = -1;
printf("Please input %d integers:", count);
input(a, count);
printf("The numbers you input are:\n");
output(a, count);
printf("Please input the x you want to insert: x = ");
scanf("%d", &x);
printf("Firstly, we reorder these numbers...\n");
reorder(a, 10);
printf("Now we get an array like this:\n");
output(a, 10);
printf("Search %d in the array...\n", x);
index = search(a, 0, count, x);
if (index == -1)
{
printf("We CAN NOT find %d in the array, so we insert it.\n", x);
insert(a, count, x);
count++;
}
else
{
printf("Wow, we found it, so we delete it...\n");
delete(a, count, x);
count--;
}
printf("Now let us check up the content in the array:\n");
output(a, count);
printf("\nThanks for your using!\n");
return 0;
}
/*
用一维数组存储由键盘输入的一组正整数(约20个). 再输入整数x. 如果数组中已经包含x, 则删除它; 否则按次序将x插入数组.
假设在输入数据(x除外) 时能够保证: 每个数据都不超过100, 数据无序, 且互不相同.
要求】
1 数据的输入输出, 排序, 查找, 插入数据, 删除数据等功能必须用函数实现, 且每个函数只能实现一个功能
2 排序必须使用选择排序法
3 查找必须使用折半查找法
4 排序, 删除, 插入等操作前后须输出数组中的内容, 以便比对操作效果
5 不得使用全局变量
6 在输入和输出数据之前必须要有英文的提示信息
*/
#include
// 输入
void input(int a[], int len)
{
int i = 0;
while (i < len)
{
scanf("%d", &a[i++]);
}
}
// 输出
void output(int a[], int len)
{
printf("[ ");
int i = 0;
while (i < len) printf("%3d ", a[i++]);
printf("]\n");
}
// 排序
void reorder(int a[], int len)
{
int i, j, index, max;
for (i = len - 1; i >= 0; i--)
{
index = 0;
max = 0;
for (j = 0; j <= i; j++)
{
if (a[j] > max)
{
max = a[j];
index = j;
}
}
a[index] = a[i];
a[i] = max;
}
}
// 查找
int search(int a[], int begin, int end, int target)
{
int left = begin;
int right = end;
int mid = (left + right) / 2;
if (left >= right)
return -1;
if (a[mid] == target)
return mid;
else if (a[mid] < target)
return search(a, mid, right, target);
else
return search(a, left, mid, target);
}
// 插入
void insert(int a[], int len, int target)
{
int i = 0;
int index = -1;
if (target <= a[0]) index = 0;
else if (target >= a[len - 1]) index = len;
else
{
for (i = 0; i < len; i++)
{
if (a[i] target && a[i + 1] >= target)
{
index = i + 1;
break;
}
}
}
for (i = len; i > index; i--)
{
a[i] = a[i - 1];
}
a[index] = target;
}
// 删除
void delete(int a[], int len, int target)
{
int index = search(a, 0, len, target);
if (index != -1)
{
int i = 0;
for (i = index; i < len; i++)
{
a[i] = a[i + 1];
}
}
}
int main()
{
int a[30] = {0};
int count = 20;
int index = -1;
int x = -1;
printf("Please input %d integers:", count);
input(a, count);
printf("The numbers you input are:\n");
output(a, count);
printf("Please input the x you want to insert: x = ");
scanf("%d", &x);
printf("Firstly, we reorder these numbers...\n");
reorder(a, 10);
printf("Now we get an array like this:\n");
output(a, 10);
printf("Search %d in the array...\n", x);
index = search(a, 0, count, x);
if (index == -1)
{
printf("We CAN NOT find %d in the array, so we insert it.\n", x);
insert(a, count, x);
count++;
}
else
{
printf("Wow, we found it, so we delete it...\n");
delete(a, count, x);
count--;
}
printf("Now let us check up the content in the array:\n");
output(a, count);
printf("\nThanks for your using!\n");
return 0;
}
展开全部
/* HELLO.C -- Hello, world */
#include <stdio.h>
print(int *arr,int len)
{
int i=0;
if(len>0)
{
for(i=0;i<len;i++)
printf("num%d %d\n",i,*(arr+i));
}
}
void selectsort(int *a,int len)
{
int i,j,min,flag;
for(i = 0; i<len-1;i++)
{
min = a[i];
flag = i;
for(j = i+1; j < len; j++)
{
if(a[j] < min)
{
min = a[j];
flag = j;
}
}
if(flag != i)
{
a[flag] = a[i];
a[i] = min;
}
}
}
int binarysearch(int arr[],int len,int number)
{
int mid, start = 0, end = len - 1;
while (start <= end) {
mid = (start + end) / 2;
if (arr[mid] < number)
start = mid + 1;
else if (arr[mid] > number)
end = mid - 1;
else
return mid;
}
return -1;
}
void insert(int arr[],int *len,int pos,int num)
{
int i=*len-2;
if(pos>*len)
{
printf("error\n");
return ;
}
for(;i>=pos;i--)
{
arr[i+1]=arr[i];
}
if(*len<20)
{
*len=(*len)+1;
}
arr[pos]=num;
printf("now,the length of arr is %d\n",*len);
}
void delete_arr(int arr[],int *len,int pos)
{
int i=*len-pos;
for(;i;i--)
{
arr[*len-i]=arr[*len-i+1];
}
*len=*len-1;
printf("now,the length of arr is %d\n",*len);
}
void input(int arr[],int len)
{
int i=0;
for(i=0;i<len;i++)
{
printf("input number %d\n",i);
scanf("%d",arr+i);
}
}
void menu()
{
printf("************************************\n");
printf("print p to print; print I to input \n");
printf("print i to insert; print d to delete\n");
printf("print f to find; print s to sort \n");
printf(" print q to quit \n");
printf("************************************\n");
}
int main()
{
char choice='p';
int arr[20],i,len=20,pos,value;
for(i=0;i<20;i++)
{
arr[i]=i;
}
do
{
menu();
choice=getchar();
switch(choice)
{
case 'I':
printf("intput the length of arry\n");
scanf("%d",&len);
input(arr, len);
printf("after input\n");
print(arr,len);
break ;
case 'i':
printf("befoe insert\n");
print(arr,len);
printf("intput the postion and the vaule \n");
scanf("%d%d",&pos,&value);
insert(arr,&len,pos,value);
printf("after insert\n");
print(arr,len);
break ;
case 'p':
print(arr,len);
break ;
case 'd':
printf("befoe insert\n");
print(arr,len);
printf("intput the postion of arry\n");
scanf("%d",&pos);
delete_arr(arr,&len,pos);
printf("after delete\n");
print(arr,len);
break ;
case 'f':
printf("please intpu the number to find\n");
scanf("%d",&value);
selectsort(arr,len);
pos=binarysearch(arr,len,value);
if(pos<0)
printf("not find it in arry\n");
else
printf("find it at %d\n",pos);
break ;
case 's':
printf("befoe sort\n");
print(arr,len);
selectsort(arr,len);
printf("after sort\n");
print(arr,len);
break ;
default:
break;
}
}while(choice!='q');
return 0;
}
#include <stdio.h>
print(int *arr,int len)
{
int i=0;
if(len>0)
{
for(i=0;i<len;i++)
printf("num%d %d\n",i,*(arr+i));
}
}
void selectsort(int *a,int len)
{
int i,j,min,flag;
for(i = 0; i<len-1;i++)
{
min = a[i];
flag = i;
for(j = i+1; j < len; j++)
{
if(a[j] < min)
{
min = a[j];
flag = j;
}
}
if(flag != i)
{
a[flag] = a[i];
a[i] = min;
}
}
}
int binarysearch(int arr[],int len,int number)
{
int mid, start = 0, end = len - 1;
while (start <= end) {
mid = (start + end) / 2;
if (arr[mid] < number)
start = mid + 1;
else if (arr[mid] > number)
end = mid - 1;
else
return mid;
}
return -1;
}
void insert(int arr[],int *len,int pos,int num)
{
int i=*len-2;
if(pos>*len)
{
printf("error\n");
return ;
}
for(;i>=pos;i--)
{
arr[i+1]=arr[i];
}
if(*len<20)
{
*len=(*len)+1;
}
arr[pos]=num;
printf("now,the length of arr is %d\n",*len);
}
void delete_arr(int arr[],int *len,int pos)
{
int i=*len-pos;
for(;i;i--)
{
arr[*len-i]=arr[*len-i+1];
}
*len=*len-1;
printf("now,the length of arr is %d\n",*len);
}
void input(int arr[],int len)
{
int i=0;
for(i=0;i<len;i++)
{
printf("input number %d\n",i);
scanf("%d",arr+i);
}
}
void menu()
{
printf("************************************\n");
printf("print p to print; print I to input \n");
printf("print i to insert; print d to delete\n");
printf("print f to find; print s to sort \n");
printf(" print q to quit \n");
printf("************************************\n");
}
int main()
{
char choice='p';
int arr[20],i,len=20,pos,value;
for(i=0;i<20;i++)
{
arr[i]=i;
}
do
{
menu();
choice=getchar();
switch(choice)
{
case 'I':
printf("intput the length of arry\n");
scanf("%d",&len);
input(arr, len);
printf("after input\n");
print(arr,len);
break ;
case 'i':
printf("befoe insert\n");
print(arr,len);
printf("intput the postion and the vaule \n");
scanf("%d%d",&pos,&value);
insert(arr,&len,pos,value);
printf("after insert\n");
print(arr,len);
break ;
case 'p':
print(arr,len);
break ;
case 'd':
printf("befoe insert\n");
print(arr,len);
printf("intput the postion of arry\n");
scanf("%d",&pos);
delete_arr(arr,&len,pos);
printf("after delete\n");
print(arr,len);
break ;
case 'f':
printf("please intpu the number to find\n");
scanf("%d",&value);
selectsort(arr,len);
pos=binarysearch(arr,len,value);
if(pos<0)
printf("not find it in arry\n");
else
printf("find it at %d\n",pos);
break ;
case 's':
printf("befoe sort\n");
print(arr,len);
selectsort(arr,len);
printf("after sort\n");
print(arr,len);
break ;
default:
break;
}
}while(choice!='q');
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询