Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunrisepeak committed Mar 28, 2024
1 parent b96dc8a commit de9d9c9
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ In file included from /usr/include/c++/11/cassert:44,
from ./tests/common.hpp:6,
from tests/dslings.0.cpp:14:
tests/dslings.0.cpp:22:12: error: ‘mVal’ was not declared in this scope
22 | assert(mVal.get() == 2);
22 | d2ds_assert_eq(mVal.get(), 2);
| ^~~~
> in tests/dslings.0.cpp
Expand Down Expand Up @@ -83,7 +83,7 @@ int main() {

d2ds::MaxValue mVal(2);

assert(mVal.get() == 2);
d2ds_assert_eq(mVal.get(), 2);

return 0;
}
Expand Down
15 changes: 8 additions & 7 deletions exercises/dslings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ namespace d2ds {




/*
class MaxValue {
public:
MaxValue(int val) {
__mVal = val;
__mMaxVal = val;
}
int get() {
return __mVal;
return __mMaxVal;
}
void set(int val) {
if (val > __mVal)
__mVal = val;
if (val > __mMaxVal) {
__mMaxVal = val;
}
}
private:
int __mVal;
int __mMaxVal;
};

*/

}

Expand Down
133 changes: 102 additions & 31 deletions src/Instroduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,135 @@

#### 代码示例
```cpp
#include <tests/common.hpp>
#include <chrono>

#include <exercises/dslings.hpp>

int main() {
d2ds:MyType myType;
myType.push(1);
assert(myType.size() == 1);
myType.pop();

d2ds::MaxValue mVal(2);

d2ds_assert_eq(mVal.get(), 2);

mVal.set(-1);
d2ds_assert_eq(mVal.get(), 2);

mVal.set(100);
d2ds_assert_eq(mVal.get(), 100);

// random test
dstruct::Array<int, 10> data;
d2ds::randomDataGenerator(data, 0, 200);
d2ds::ds_print(data);

int maxVal = 0;
for (int i = 0; i < data.size(); i++) {
mVal.set(data[i]);
if (data[i] > maxVal) {
maxVal = data[i];
}
}

d2ds_assert_eq(mVal.get(), maxVal);

return 0;
}
```

#### 接口描述
#### 代码介绍/描述

- MyType: 需要有默认构造
- push: 放置一个int值到数据结构
- size: 获得当前大小
- pop: 删除最新放置的数据
MaxValue一个最大值数据检查器

**MaxValue**构造函数设置默认值

### 数据结构接口实现
```cpp
d2ds::MaxValue mVal(2);
```
根据代码示例和接口描述来实现这个数据结构
**get函数获取当前最大值**
#### 类型定义
```cpp
d2ds_assert_eq(mVal.get(), 2);
```

**set函数设置一个值**

> 如果当前最大值小于这个值则需要进行替换
```cpp
struct MyType {
// ...
};
mVal.set(-1);
d2ds_assert_eq(mVal.get(), 2);

mVal.set(100);
d2ds_assert_eq(mVal.get(), 100);
```
#### push接口实现
**MaxVal的应用测试 - 获取最大数组中最大值**
```cpp
struct MyType {
void push(int val) {
// ...
// random test
dstruct::Array<int, 10> data;
d2ds::randomDataGenerator(data, 0, 200);
d2ds::ds_print(data);
int maxVal = 0;
for (int i = 0; i < data.size(); i++) {
mVal.set(data[i]);
if (data[i] > maxVal) {
maxVal = data[i];
}
}
d2ds_assert_eq(mVal.get(), maxVal);
```

### 数据结构接口实现

根据代码示例和接口描述来实现这个数据结构

#### 类型定义

```cpp
class MaxValue {
public:
MaxValue(int val) : __mMaxVal { val } { }

private:
int __mMaxVal;
};
```
#### size接口实现
#### get接口实现
```cpp
struct MyType {
// ...
void size() {
//...
class MaxValue {
public:
//...
int get() {
return __mMaxVal;
}
private:
int __mMaxVal;
};
```

#### pop接口实现
#### set接口实现

```cpp
struct MyType {
// ...
void pop() {
//...
class MaxValue {
public:
//...
void set(int val) {
if (val > __mMaxVal) {
__mMaxVal = val;
}
}

private:
int __mMaxVal;
};
```
Expand Down Expand Up @@ -116,7 +187,7 @@ In file included from /usr/include/c++/11/cassert:44,
from ./tests/common.hpp:6,
from tests/dslings.0.cpp:14:
tests/dslings.0.cpp:22:12: error: ‘mVal’ was not declared in this scope
22 | assert(mVal.get() == 2);
22 | d2ds_assert_eq(mVal.get(), 2);
| ^~~~
> in tests/dslings.0.cpp
Expand Down Expand Up @@ -151,7 +222,7 @@ int main() {
d2ds::MaxValue mVal(2);
assert(mVal.get() == 2);
d2ds_assert_eq(mVal.get(), 2);
return 0;
}
Expand Down
35 changes: 34 additions & 1 deletion tests/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,40 @@
#include <iostream>
#include <random>
#include <cassert>
#include <thread>

#define DeleteTheAssertToContinue() assert(false)
#include <dstruct.hpp>

#define d2ds_assert_eq(a, b) \
if (a != b) \
std::cerr << #a << " == " << #b << " (" << a << " == " << b << ")" << std::endl; \
assert((a) == (b))

namespace d2ds {

template <typename DSVType>
static void randomDataGenerator(DSVType &dsv, int rangeL, int rangeR) {
// test: random data
// Choose a random mean between 1 and 2 * ARR_SIZE
// https://en.cppreference.com/w/cpp/numeric/random
std::random_device r;
std::default_random_engine e1(r());
std::uniform_int_distribution<int> uniform_dist(rangeL, rangeR);

for (auto it = dsv.begin(); it != dsv.end(); it++) {
*it = uniform_dist(e1);
}
}

template <typename DSVType>
static void ds_print(DSVType &dsv) {
std::cerr << "[ ";
for (auto it = dsv.begin(); it != dsv.end(); it++) {
std::cerr << *it << " ";
}
std::cerr << "]" << std::endl;
}

}

#endif
2 changes: 1 addition & 1 deletion tests/dslings.0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main() {

d2ds::MaxValue mVal(2);

assert(mVal.get() == 2);
d2ds_assert_eq(mVal.get(), 2);

return 0;
}
6 changes: 3 additions & 3 deletions tests/dslings.1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ int main() {

d2ds::MaxValue mVal(2);

assert(mVal.get() == 2);
d2ds_assert_eq(mVal.get(), 2);

mVal.set(-1);
assert(mVal.get() == 2);
d2ds_assert_eq(mVal.get(), 2);

mVal.set(100);
assert(mVal.get() == 100);
d2ds_assert_eq(mVal.get(), 100);

return 0;
}
32 changes: 15 additions & 17 deletions tests/dslings.2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,36 @@
//

#include <tests/common.hpp>
#include <chrono>

#include <exercises/dslings.hpp>

int main() {

d2ds::MaxValue mVal(2);

assert(mVal.get() == 2);
d2ds_assert_eq(mVal.get(), 2);

mVal.set(-1);
assert(mVal.get() == 2);
d2ds_assert_eq(mVal.get(), 2);

mVal.set(100);
assert(mVal.get() == 100);
d2ds_assert_eq(mVal.get(), 100);

// random test
std::random_device rd;
std::mt19937 gen(rd()); // 使用随机设备作为种子
std::uniform_int_distribution<int> dis(0, 200); // 定义范围为 [0, 200] 的均匀分布

int currMaxVal = mVal.get();
for (int i = 0; i < 10; i++) {
int randomNum = dis(gen);
int newMaxVal = currMaxVal > randomNum ? currMaxVal : randomNum;
mVal.set(randomNum);
assert(mVal.get() == newMaxVal);
currMaxVal = mVal.get();
dstruct::Array<int, 10> data;
d2ds::randomDataGenerator(data, 0, 200);
d2ds::ds_print(data);

int maxVal = 0;
for (int i = 0; i < data.size(); i++) {
mVal.set(data[i]);
if (data[i] > maxVal) {
maxVal = data[i];
}
}

std::cout << "hello, dslings..." << std::endl;

DeleteTheAssertToContinue();
d2ds_assert_eq(mVal.get(), maxVal);

return 0;
}
2 changes: 1 addition & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ task("dslings")
if build_success then
try {
function ()
output, errs = os.iorunv("xmake", {"r", name})
os.iorunv("xmake", {"r", name})
end,
catch
{
Expand Down

0 comments on commit de9d9c9

Please sign in to comment.