April 17th Friday (四月 十七日 金曜日)

   The detail logic of the program dattrcol is so complex that I must modify servel place in order to adjust the control flow.  The original design
is so bad.  The source is difficult to be maintained.

  This morning I must think the control flow again according to the source.  I found that some controls we added can be removed because they
are surplus.

//bettersleep.c
#include <errno.h>
#include <time.h>

int better_sleep(double sleep_time){
  struct timespec tv;
  tv.tv_sec = (time_t)sleep_time;
  tv.tv_nsec = (long) ((sleep_time - tv.tv_sec) * 1e+9;

  while(1){
   int rval = nanosleep(&tv, &tv);

   if (rval == 0)
     return 0;
   else if (errno == EINTR)
     continue;
   else
     return rval;
  }

  return 0;
}

  There is a simple program to make union set.

// unitedSet.cpp
#include <iostream>

using namespace std;

class Set {
private:
int id;
int cnt;
int capacity;
int *pSet;
public:
Set(int id, int capacity) {
pSet = new int[capacity];
this->capacity = capacity;
cnt = 0;
this->id = id;
}

~Set() {
delete [] pSet;
}

inline int getCount() { return cnt; }
inline int getId() { return id; }

int operator[] (int index) const {
if ((index < 0) || (index >= cnt))
return 0;
return pSet[index];
}

void add(int e) {
if (cnt < capacity) {
pSet[cnt] = e;
cnt++;
}
}
};

void printSet(Set& s) {
cout<<s.getId()<<": (";
for (int i = 0; i < s.getCount() - 1; i++)
cout<<s[i]<<",";
cout<<s[s.getCount() - 1]<<")"<<endl;
}

int inSet(int e, Set& s) {
for (int i = 0; i < s.getCount(); i++)
if (e == s[i]) return 1;
return 0;
}

void uniteSet(Set& src_set, Set& dst_set) {
int e = 0;

for (int i = 0; i <src_set.getCount(); i++){
e = src_set[i];
if (!inSet(e, dst_set))
dst_set.add(e);
}
}

int main() {

Set s1(1, 5);
Set s2(2, 8);

for (int i = 1; i < 6; i++)
s1.add(i);
printSet(s1);

for (int n = 4; n < 10; n++)
s2.add(n);
printSet(s2);

uniteSet(s1, s2);
printSet(s2);

return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值