This commit is contained in:
iridiumR 2021-10-08 17:17:16 +08:00
parent 684d027719
commit e826d9d44f
3 changed files with 292 additions and 1 deletions

View file

@ -1,8 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include "vec.cpp"
int main()
{
printf("测试");
int flag = 0;
int temp[5];
srand(time(NULL));
int len;
printf("输入生成数量\n");
scanf("%d", &len);
Vec v(len);
v.printall();
system("pause");
printf("排序\n");
v.sort();
v.printall();
while (1)
{
printf("选择操作:1.插入 2.删除 3.统计\n");
scanf("%d", &flag);
switch (flag)
{
case 1:
printf("输入待插入数值\n");
scanf("%d", &temp[0]);
(v.insert(v.search(temp[0]), temp[0]) != -1) ? (printf("输出数组\n")) : (printf("无此元素或超出范围\n"));
v.printall();
break;
case 2:
printf("输入待删除元素\n");
scanf("%d", &temp[0]);
(v.remove_sorted(temp[0]) != -1) ? (printf("输出数组\n")) : (printf("无此元素或超出范围\n"));
v.printall();
break;
case 3:
printf("输入统计的数值\n");
scanf("%d", &temp[0]);
temp[1] = v.count(temp[0]);
(temp[1] != -1) ? (printf("一共%d个\n", temp[1])) : (printf("无此元素或超出范围\n"));
break;
default:
printf("返回菜单\n");
}
flag = 0;
}
return 0;
}

191
ex3/vec.cpp Normal file
View file

@ -0,0 +1,191 @@
#include "vec.h"
Vec::Vec(int len)
{
_used = len - 1;
_len = len * 2;
_v = new int[_len];
// for (int i = 0; i <= _used; i++)
// _v[i] = i + 1;
for (int i = 0; i <= _used; i++)
_v[i] = rand() % (_used + 1);
for (int i = 0; i <= _used; i++)
swap(i, rand() % (_used + 1));
}
int Vec::get(int a)
{
return _v[a];
}
void Vec::put(int a, int value)
{
_v[a] = value;
}
void Vec::swap(int a, int b)
{
int temp = _v[a];
_v[a] = _v[b];
_v[b] = temp;
}
void Vec::expand()
{
_len = _len * 2;
int *p = new int[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
void Vec::shrink()
{
if (((double)_used / (double)_len) >= 0.25)
return;
_len = _len >> 1;
int *p = new int[_len];
for (int i = 0; i <= _used; i++)
p[i] = _v[i];
delete[] _v;
_v = p;
}
//位置 数值
int Vec::insert(int locate, int value)
{
if (locate < 0 || locate > _used)
return -1;
_used++;
if (_used >= _len)
expand();
for (int i = _used; i > locate; i--)
_v[i] = _v[i - 1];
_v[locate] = value;
return 0;
}
//位置 删除个数
int Vec::remove(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];
shrink();
return value;
}
//删除某一元素
int Vec::remove_sorted(int value)
{
int j, i;
for (i = 0; i <= _used && _v[i] != value; i++)
;
for (j = 1; _v[i + j] == value; j++)
;
return remove(i, j);
}
int Vec::count(int value)
{
int j, i;
for (i = 0; i <= _used && _v[i] != value; i++)
;
for (j = 0; _v[i + j] == value && i + j <= _used; j++)
;
if (j == 0)
return -1;
return j;
}
int Vec::find(int value)
{
int i = 0;
for (i; i <= _used; i++)
{
if (_v[i] == value)
{
return i;
}
}
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]);
}
void Vec::sort()
{
int n = _used;
bool sorted = false;
while (!sorted)
{
sorted = true;
for (int i = 1; i < n; i++)
{
if (_v[i - 1] > _v[i])
{
swap(i - 1, i);
sorted = false;
}
}
n--;
}
_sorted = true;
}
int Vec::search(int e)
{
if (_sorted == false)
return -1;
int i = 0;
for (i; i <= _used && _v[i] <= e; i++)
;
return i - 1;
}

54
ex3/vec.h Normal file
View file

@ -0,0 +1,54 @@
#ifndef _INC_STDIO
#include <stdio.h>
#endif
#ifndef _GLIBCXX_STDLIB_H
#include <stdlib.h>
#endif
#include <time.h>
class Vec
{
private:
int *_v;
int _len;
int _used;
bool _sorted = false;
void expand();
void shrink();
public:
Vec(int _len);
int get(int a);
int search(int value);
void put(int a, int value);
void swap(int a, int b);
int insert(int locate, int value);
int remove(int locate, int value);
int remove_sorted(int value);
int find(int value);
void sort();
void printall();
int getlen();
int getused();
int count(int value);
};