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;
}
09-26