2代码规范

This commit is contained in:
iridiumR 2021-10-03 11:21:32 +08:00
parent d4ad5301b5
commit b1e453a396

157
ex2.cpp
View file

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