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.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;
printf("<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
v.printall();
2021-10-03 03:21:32 +00:00
while (1)
2021-10-03 02:28:37 +00:00
{
2021-10-03 03:21:32 +00:00
printf("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:1.<2E><><EFBFBD><EFBFBD> 2.ɾ<><C9BE> 3.<2E><><EFBFBD><EFBFBD>\n");
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:
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ\n");
2021-10-03 03:21:32 +00:00
scanf("%d %d", &temp[0], &temp[1]);
(v.insert(temp[0], temp[1])) ? (printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n")) : (printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n"));
2021-10-03 02:28:37 +00:00
v.printall();
break;
case 2:
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
2021-10-03 03:21:32 +00:00
scanf("%d %d", &temp[0], &temp[1]);
(v.del(temp[0], temp[1])) ? (printf("<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n")) : (printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n"));
2021-10-03 02:28:37 +00:00
v.printall();
break;
case 3:
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>ֵ\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-03 03:21:32 +00:00
(temp[2] == -1) ? (printf("<EFBFBD>޴<EFBFBD>Ԫ<EFBFBD><EFBFBD>\n")) : (printf("<EFBFBD><EFBFBD>Ϊ%d \n", temp[2]));
break;
2021-10-03 02:28:37 +00:00
2021-10-03 03:21:32 +00:00
default:
printf("<EFBFBD><EFBFBD><EFBFBD>ز˵<EFBFBD>\n");
}
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;
}