From b1e453a396fb416e46b4066e49955aec39efa65a Mon Sep 17 00:00:00 2001 From: iridiumR Date: Sun, 3 Oct 2021 11:21:32 +0800 Subject: [PATCH] =?UTF-8?q?2=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex2.cpp | 169 ++++++++++++++++++++++++++------------------------------ 1 file changed, 78 insertions(+), 91 deletions(-) diff --git a/ex2.cpp b/ex2.cpp index ba21b2e..8ca66fd 100644 --- a/ex2.cpp +++ b/ex2.cpp @@ -3,54 +3,50 @@ #include #include - class Vec { - private: - - int *v; - int LEN; - int USED; +private: + int *v; + int LEN; + int USED; - void expand(); + void expand(); - void shrink(); + void shrink(); - public: +public: + Vec(int len); - Vec(int len); + int get(int a); - int get(int a); + void put(int a, int value); - void put(int a,int value); + void swap(int a, int b); - void swap(int a, int b); + int insert(int locate, int value); - int insert(int locate,int value); + int del(int locate, int value); - int del(int locate,int value); + int find(int value); - int find(int value); + void printall(); - void printall(); + int getlen(); - int getlen(); - - int getused(); + int getused(); }; - Vec::Vec(int len) { - LEN=len*2; - v=new int[LEN]; - USED=9; + 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); + for (int i = 0; i < 10; i++) + v[i] = i + 1; + + for (int i = 0; i < 10; i++) + swap(i, rand() % 10); } int Vec::get(int a) @@ -58,72 +54,72 @@ int Vec::get(int a) return v[a]; } -void Vec::put(int a,int value) +void Vec::put(int a, int value) { - v[a]=value; + v[a] = value; } void Vec::swap(int a, int b) { - int temp = v[a]; - v[a] = v[b]; - v[b] = temp; + int temp = v[a]; + v[a] = v[b]; + v[b] = temp; } void Vec::expand() { - LEN=LEN*2; - int *p=new int[LEN]; + LEN = LEN * 2; + int *p = new int[LEN]; + + for (int i = 0; i <= USED; i++) + p[i] = v[i]; - for(int i=0;i<=USED;i++) - p[i]=v[i]; - delete[] v; - v=p; + v = p; } void Vec::shrink() { - LEN=LEN>>1; - int *p=new int[LEN]; + LEN = LEN >> 1; + int *p = new int[LEN]; + + for (int i = 0; i <= USED; i++) + p[i] = v[i]; - for(int i=0;i<=USED;i++) - p[i]=v[i]; - delete[] v; - v=p; + v = p; } -int Vec::insert(int locate,int value) +int Vec::insert(int locate, int value) { - if(locate<0||locate>USED) + if (locate < 0 || locate > USED) return 1; USED++; - - if(USED>=LEN) + + if (USED >= LEN) expand(); - for(int i=USED;i>locate;i--) - v[i]=v[i-1]; - - v[locate]=value; + for (int i = USED; i > locate; i--) + v[i] = v[i - 1]; + + v[locate] = value; return 0; } -int Vec::del(int locate,int value) +int Vec::del(int locate, int value) { - if(locate<0||((USED-value)<0)||((locate+value-1)>USED)) + if (locate < 0 || ((USED - value) < 0) || ((locate + value - 1) > USED)) return 1; - USED=USED-value; + USED = USED - value; - for(int i=locate;i<=USED;i++) - v[i]=v[i+value]; + for (int i = locate; i <= USED; i++) + v[i] = v[i + value]; - if( ( (double)USED/(double)LEN )<=0.3 ) + if (((double)USED / (double)LEN) <= 0.3) shrink(); return 0; @@ -132,9 +128,9 @@ int Vec::del(int locate,int value) int Vec::find(int value) { int i = 0; - for(i;i<=USED;i++) + for (i; i <= USED; i++) { - if(v[i]==value) + if (v[i] == value) { return i; } @@ -155,64 +151,55 @@ int Vec::getused() void Vec::printall() { - for(int i=0;i