2021-10-03 02:28:37 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Vec
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
int *v;
|
|
|
|
|
int LEN;
|
|
|
|
|
int USED;
|
|
|
|
|
|
2021-10-03 02:38:04 +00:00
|
|
|
|
void expand();
|
|
|
|
|
|
|
|
|
|
void shrink();
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
Vec(int len)
|
|
|
|
|
{
|
|
|
|
|
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:29:24 +00:00
|
|
|
|
int get(int a);
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
void put(int a,int value);
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
void swap(int a, int b);
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
int insert(int locate,int value);
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
int del(int locate,int value);
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
int find(int value);
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
void printall();
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
int getlen();
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
int getused();
|
|
|
|
|
};
|
2021-10-03 02:28:37 +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 02:29:24 +00:00
|
|
|
|
void Vec::put(int a,int value)
|
|
|
|
|
{
|
|
|
|
|
v[a]=value;
|
|
|
|
|
}
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
void Vec::swap(int a, int b)
|
|
|
|
|
{
|
|
|
|
|
int temp = v[a];
|
|
|
|
|
v[a] = v[b];
|
|
|
|
|
v[b] = temp;
|
|
|
|
|
}
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
void Vec::expand()
|
|
|
|
|
{
|
|
|
|
|
LEN=LEN*2;
|
|
|
|
|
int *p=new int[LEN];
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
for(int i=0;i<=USED;i++)
|
|
|
|
|
p[i]=v[i];
|
|
|
|
|
|
|
|
|
|
delete[] v;
|
|
|
|
|
v=p;
|
|
|
|
|
}
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
void Vec::shrink()
|
|
|
|
|
{
|
|
|
|
|
LEN=LEN>>1;
|
|
|
|
|
int *p=new int[LEN];
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
for(int i=0;i<=USED;i++)
|
|
|
|
|
p[i]=v[i];
|
|
|
|
|
|
|
|
|
|
delete[] v;
|
|
|
|
|
v=p;
|
|
|
|
|
}
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
int Vec::insert(int locate,int value)
|
|
|
|
|
{
|
|
|
|
|
if(locate<0||locate>USED)
|
|
|
|
|
return 1;
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
2021-10-03 02:29:24 +00:00
|
|
|
|
USED++;
|
|
|
|
|
|
|
|
|
|
if(USED>=LEN)
|
|
|
|
|
expand();
|
|
|
|
|
|
|
|
|
|
for(int i=USED;i>locate;i--)
|
|
|
|
|
v[i]=v[i-1];
|
|
|
|
|
|
|
|
|
|
v[locate]=value;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Vec::del(int locate,int value)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(locate<0||((USED-value)<0)||((locate+value-1)>USED))
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
USED=USED-value;
|
|
|
|
|
|
|
|
|
|
for(int i=locate;i<=USED;i++)
|
|
|
|
|
v[i]=v[i+value];
|
|
|
|
|
|
|
|
|
|
if( ( (double)USED/(double)LEN )<=0.3 )
|
|
|
|
|
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;
|
|
|
|
|
for(i;i<=USED;i++)
|
|
|
|
|
{
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
for(int i=0;i<USED;i++)
|
|
|
|
|
printf("%d\n",v[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-10-03 02:28:37 +00:00
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
|
|
Vec v(10);
|
|
|
|
|
int temp[10]={0};
|
|
|
|
|
int flag;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
|
|
|
|
|
v.printall();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
printf("ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n1.<2E><><EFBFBD><EFBFBD>\n2.ɾ<><C9BE>\n3.<2E><><EFBFBD><EFBFBD>\n");
|
|
|
|
|
scanf("%d",&flag);
|
|
|
|
|
|
|
|
|
|
switch(flag)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ\n");
|
|
|
|
|
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"));
|
|
|
|
|
v.printall();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
|
|
|
|
|
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"));
|
|
|
|
|
v.printall();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>ֵ\n");
|
|
|
|
|
scanf("%d",&temp[0]);
|
|
|
|
|
temp[2]=v.find(temp[0]);
|
|
|
|
|
|
|
|
|
|
(temp[2]==-1)?(printf("<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>\n")):(printf("<EFBFBD><EFBFBD>Ϊ%d \n",temp[2]));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD>ز˵<EFBFBD>\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
flag=0;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|