关于数组的一点操作实现,完成一下作业
#include<bits/stdc++.h>
#define rep(i, aa, bb) for(int i = aa; i <= bb; i ++)
#define per(i, aa, bb) for(int i = aa; i >= bb; i --)
#define int long long
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], n;
void menu()
{
printf("操作菜单如下:\n");
printf("1: 输入一个数组\n");
printf("2: 对数组进行排序\n");
printf("3: 查询数组中是否出现某一元素\n");
printf("4: 删除数组中某一元素\n");
printf("5: 插入一个元素在指定位置\n");
printf("6: 销毁这个数组\n");
printf("7: 结束程序");
}
void _creat()
{
printf("请输入数组大小:");
scanf("%d", &n);
printf("\n");
rep(i, 1, n){
a[i] = 0;
}
rep(i, 1, n){
scanf("%d", &a[i]);
}
}
void quick_sort(int l, int r, int a[])
{
if(l >= r) return;
int i = l - 1, j = r + 1, x = a[l + r >> 1];
while(i < j){
do i ++ ; while(a[i] < x);
do j -- ; while(a[j] > x);
if(i < j) swap(a[i], a[j]);
}
quick_sort(l, j, a);
quick_sort(j + 1, r, a);
}
void _find()
{
printf("请输入要查询的元素:");
int tmp;
scanf("%d", &tmp);
printf("\n");
bool f = false;
rep(i, 1, n){
if(tmp == a[i]){
f = true;
break;
}
}
cout << f << endl;
}
void _delete()
{
printf("请输入要删除的元素:");
int tmp;
scanf("%d", &tmp);
bool f = false;
rep(i, 1, n){
if(tmp == a[i]){
rep(j, i, n - 1){
a[j] = a[j + 1];
}
n --;
f = true;
break;
}
}
if(f){
rep(i, 1, n){
printf("%d ", a[i]);
}
} else {
printf("未查询到此元素");
}
printf("\n");
}
void _insert()
{
printf("请输入要插入元素的数值和位置");
int pos, val;
scanf("%d%d", &val, &pos);
a[n + 1] = 0;
per(i, n, pos){
a[i + 1] = a[i];
}
a[pos] = val;
n ++;
rep(i, 1, n){
printf("%d ", a[i]);
}
printf("\n");
}
signed main(void)
{
bool f = true;
menu();
while(f){
printf("请输入你要进行的操作:");
int op;
scanf("%d", &op);
if(op == 1) {
_creat();
} else if(op == 2) {
quick_sort(1, n, a);
rep(i, 1, n){
printf("%d ", a[i]);
}
printf("\n");
} else if(op == 3) {
_find();
} else if(op == 4) {
_delete();
} else if(op == 5) {
_insert();
} else if(op == 6) {
// _destory();
} else {
return 0;
}
}
}