2: 类的写法规范化

This commit is contained in:
iridiumR 2021-10-03 10:29:24 +08:00
parent 1059058706
commit c8cc19778e

99
ex2.cpp
View file

@ -28,25 +28,48 @@ class Vec
swap(i,rand()%10); swap(i,rand()%10);
} }
int get(int a) int get(int a);
{
void put(int a,int value);
void swap(int a, int b);
void expand();
void shrink();
int insert(int locate,int value);
int del(int locate,int value);
int find(int value);
void printall();
int getlen();
int getused();
};
int Vec::get(int a)
{
return v[a]; return v[a];
} }
void put(int a,int value) void Vec::put(int a,int value)
{ {
v[a]=value; v[a]=value;
} }
void swap(int a, int b) void Vec::swap(int a, int b)
{ {
int temp = v[a]; int temp = v[a];
v[a] = v[b]; v[a] = v[b];
v[b] = temp; v[b] = temp;
} }
void expand() void Vec::expand()
{ {
LEN=LEN*2; LEN=LEN*2;
int *p=new int[LEN]; int *p=new int[LEN];
@ -55,10 +78,10 @@ class Vec
delete[] v; delete[] v;
v=p; v=p;
} }
void shrink() void Vec::shrink()
{ {
LEN=LEN>>1; LEN=LEN>>1;
int *p=new int[LEN]; int *p=new int[LEN];
@ -67,14 +90,15 @@ class Vec
delete[] v; delete[] v;
v=p; v=p;
} }
int insert(int locate,int value) int Vec::insert(int locate,int value)
{ {
if(locate<0||locate>USED) if(locate<0||locate>USED)
return 1; return 1;
USED++; USED++;
if(USED>=LEN) if(USED>=LEN)
expand(); expand();
@ -84,10 +108,10 @@ class Vec
v[locate]=value; v[locate]=value;
return 0; return 0;
} }
int 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; return 1;
@ -101,10 +125,10 @@ class Vec
shrink(); shrink();
return 0; return 0;
} }
int find(int value) int Vec::find(int value)
{ {
int i = 0; int i = 0;
for(i;i<=USED;i++) for(i;i<=USED;i++)
{ {
@ -115,24 +139,27 @@ class Vec
} }
return -1; return -1;
} }
void printall() int Vec::getlen()
{ {
return LEN;
}
int Vec::getused()
{
return USED;
}
void Vec::printall()
{
for(int i=0;i<USED;i++) for(int i=0;i<USED;i++)
printf("%d\n",v[i]); printf("%d\n",v[i]);
} }
int getlen()
{
return LEN;
}
int getused()
{
return USED;
}
};
int main() int main()
{ {