This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/ex2/ex2.cpp

204 lines
2.8 KiB
C++
Raw Normal View History

2021-10-03 02:28:37 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
class Vec
{
2021-10-03 03:21:32 +00:00
private:
int *v;
int LEN;
int USED;
2021-10-03 03:21:32 +00:00
void expand();
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
void shrink();
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
public:
Vec(int len);
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
int get(int a);
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
void put(int a, int value);
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
void swap(int a, int b);
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
int insert(int locate, int value);
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
int del(int locate, int value);
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
int find(int value);
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
void printall();
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
int getlen();
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
int getused();
2021-10-03 02:29:24 +00:00
};
2021-10-03 02:28:37 +00:00
2021-10-03 02:58:05 +00:00
Vec::Vec(int len)
{
2021-10-03 03:21:32 +00:00
LEN = len * 2;
v = new int[LEN];
USED = 9;
for (int i = 0; i < 10; i++)
v[i] = i + 1;
for (int i = 0; i < 10; i++)
swap(i, rand() % 10);
2021-10-03 02:58:05 +00:00
}
2021-10-03 02:29:24 +00:00
int Vec::get(int a)
{
return v[a];
}
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
void Vec::put(int a, int value)
2021-10-03 02:29:24 +00:00
{
2021-10-03 03:21:32 +00:00
v[a] = value;
2021-10-03 02:29:24 +00:00
}
2021-10-03 02:28:37 +00:00
2021-10-03 02:29:24 +00:00
void Vec::swap(int a, int b)
{
2021-10-03 03:21:32 +00:00
int temp = v[a];
v[a] = v[b];
v[b] = temp;
2021-10-03 02:29:24 +00:00
}
2021-10-03 02:28:37 +00:00
2021-10-03 02:29:24 +00:00
void Vec::expand()
{
2021-10-03 03:21:32 +00:00
LEN = LEN * 2;
int *p = new int[LEN];
for (int i = 0; i <= USED; i++)
p[i] = v[i];
2021-10-03 02:28:37 +00:00
2021-10-03 02:29:24 +00:00
delete[] v;
2021-10-03 03:21:32 +00:00
v = p;
2021-10-03 02:29:24 +00:00
}
2021-10-03 02:28:37 +00:00
2021-10-03 02:29:24 +00:00
void Vec::shrink()
{
2021-10-03 03:21:32 +00:00
LEN = LEN >> 1;
int *p = new int[LEN];
for (int i = 0; i <= USED; i++)
p[i] = v[i];
2021-10-03 02:28:37 +00:00
2021-10-03 02:29:24 +00:00
delete[] v;
2021-10-03 03:21:32 +00:00
v = p;
2021-10-03 02:29:24 +00:00
}
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
int Vec::insert(int locate, int value)
2021-10-03 02:29:24 +00:00
{
2021-10-03 03:21:32 +00:00
if (locate < 0 || locate > USED)
2021-10-03 02:29:24 +00:00
return 1;
2021-10-03 02:28:37 +00:00
2021-10-03 02:29:24 +00:00
USED++;
2021-10-03 03:21:32 +00:00
if (USED >= LEN)
2021-10-03 02:29:24 +00:00
expand();
2021-10-03 03:21:32 +00:00
for (int i = USED; i > locate; i--)
v[i] = v[i - 1];
v[locate] = value;
2021-10-03 02:29:24 +00:00
return 0;
}
2021-10-03 03:21:32 +00:00
int Vec::del(int locate, int value)
2021-10-03 02:29:24 +00:00
{
2021-10-03 03:21:32 +00:00
if (locate < 0 || ((USED - value) < 0) || ((locate + value - 1) > USED))
2021-10-03 02:29:24 +00:00
return 1;
2021-10-03 03:21:32 +00:00
USED = USED - value;
2021-10-03 02:29:24 +00:00
2021-10-03 03:21:32 +00:00
for (int i = locate; i <= USED; i++)
v[i] = v[i + value];
2021-10-03 02:29:24 +00:00
2021-10-03 03:21:32 +00:00
if (((double)USED / (double)LEN) <= 0.3)
2021-10-03 02:29:24 +00:00
shrink();
return 0;
}
2021-10-03 02:28:37 +00:00
2021-10-03 02:29:24 +00:00
int Vec::find(int value)
{
int i = 0;
2021-10-03 03:21:32 +00:00
for (i; i <= USED; i++)
2021-10-03 02:29:24 +00:00
{
2021-10-03 03:21:32 +00:00
if (v[i] == value)
2021-10-03 02:28:37 +00:00
{
2021-10-03 02:29:24 +00:00
return i;
2021-10-03 02:28:37 +00:00
}
2021-10-03 02:29:24 +00:00
}
return -1;
}
int Vec::getlen()
{
return LEN;
}
int Vec::getused()
{
return USED;
}
void Vec::printall()
{
2021-10-03 03:21:32 +00:00
for (int i = 0; i < USED; i++)
printf("%d\n", v[i]);
2021-10-03 02:29:24 +00:00
}
2021-10-03 02:28:37 +00:00
int main()
{
srand(time(NULL));
Vec v(10);
2021-10-03 03:21:32 +00:00
int temp[10] = {0};
2021-10-03 02:28:37 +00:00
int flag;
2021-10-29 08:51:54 +00:00
printf("初始化,输出数组\n");
2021-10-03 02:28:37 +00:00
v.printall();
2021-10-03 03:21:32 +00:00
while (1)
2021-10-03 02:28:37 +00:00
{
2021-10-29 08:51:54 +00:00
printf("选择操作:1.插入 2.删除 3.查找\n");
2021-10-03 03:21:32 +00:00
scanf("%d", &flag);
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
switch (flag)
{
2021-10-03 02:28:37 +00:00
case 1:
2021-10-29 08:51:54 +00:00
printf("输入秩与待插入数值\n");
2021-10-03 03:21:32 +00:00
scanf("%d %d", &temp[0], &temp[1]);
2021-10-29 08:51:54 +00:00
(v.insert(temp[0], temp[1])) ? (printf("非法输入\n")) : (printf("输出数组\n"));
2021-10-03 02:28:37 +00:00
v.printall();
break;
case 2:
2021-10-29 08:51:54 +00:00
printf("输入秩与待删除个数\n");
2021-10-03 03:21:32 +00:00
scanf("%d %d", &temp[0], &temp[1]);
2021-10-29 08:51:54 +00:00
(v.del(temp[0], temp[1])) ? (printf("非法输入\n")) : (printf("输出数组\n"));
2021-10-03 02:28:37 +00:00
v.printall();
break;
case 3:
2021-10-29 08:51:54 +00:00
printf("输入要查找的数值\n");
2021-10-03 03:21:32 +00:00
scanf("%d", &temp[0]);
temp[2] = v.find(temp[0]);
2021-10-03 02:28:37 +00:00
2021-10-29 08:51:54 +00:00
(temp[2] == -1) ? (printf("无此元素\n")) : (printf("秩为%d \n", temp[2]));
2021-10-03 03:21:32 +00:00
break;
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
default:
2021-10-29 08:51:54 +00:00
printf("返回菜单\n");
2021-10-03 03:21:32 +00:00
}
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
flag = 0;
2021-10-03 02:28:37 +00:00
}
return 0;
}