c++建立返回值为成员指针的成员函数的问题 求大神解答
发布网友
发布时间:2023-09-01 19:21
我来回答
共1个回答
热心网友
时间:2024-08-17 15:13
#include <iostream>
using namespace std;
int const MAXLEN = 10; // Time的字符存储长度
class Time {
public:
Time(int h = 0,int m = 0,int s = 0) {
hour = h;
minute = m;
second = s;
}
void Seth(int h) { hour = h; }
void Setm(int m) { minute = m; }
void Sets(int s) { second = s; }
void Show() const {printf("%02d:%02d:%02d\n",hour,minute,second); }
int Geth() const { return hour; }
int Getm() const { return minute; }
int Gets() const { return second; }
private:
int hour;
int minute;
int second;
};
class DynamicTimeArray {
public:
explicit DynamicTimeArray(int n = 0) {
if(n > 0) {
point = new Time[n];
size = n;
}
else {
point = NULL;
size = 0;
}
}
DynamicTimeArray(char *tarr[],int n);
void sort();
int search(const Time &t) const;
bool edit(const Time &t);
bool modify(int index);
void display();
~DynamicTimeArray() {delete [] point;}
private:
Time *point;
int size;
};
DynamicTimeArray::DynamicTimeArray(char *tarr[],int n) {
point = new Time[n];
int h,m,s;
for(int i = 0; i < n; ++i) {
sscanf(tarr[i],"%d:%d:%d",&h,&m,&s);
point[i].Seth(h);
point[i].Setm(m);
point[i].Sets(s);
}
size = n;
}
void DynamicTimeArray::sort() {
int i,j,k;
char front[MAXLEN],next[MAXLEN];
Time t;
for(i = 0; i < size - 1; ++i) {
k = i;
sprintf(front,"%02d:%02d:%02d",point[i].Geth(),point[i].Getm(),point[i].Gets());
for(j = i + 1; j < size; ++j) {
sprintf(next,"%02d:%02d:%02d",point[j].Geth(),point[j].Getm(),point[j].Gets());
if(strcmp(front,next) > 0) {
k = j;
sprintf(front,"%02d:%02d:%02d",point[k].Geth(),point[k].Getm(),point[k].Gets());
}
}
if(k != i) {
t = point[k];
point[k] = point[i];
point[i] = t;
}
}
}
int DynamicTimeArray::search(const Time &t) const {
int low = 0,high = size - 1,mid,cmpres;
char dest[MAXLEN],tmp[MAXLEN];
sprintf(dest,"%02d:%02d:%02d",t.Geth(),t.Getm(),t.Gets());
while(low <= high) {
mid = (low + high) / 2;
sprintf(tmp,"%02d:%02d:%02d",point[mid].Geth(),point[mid].Getm(),point[mid].Gets());
cmpres = strcmp(dest,tmp);
if(cmpres < 0) high = mid - 1;
else if(cmpres == 0) return mid;
else low = mid + 1;
}
return -1;
}
bool DynamicTimeArray::edit(const Time &t) {
int n,res = search(t);
if(res < 0) {
cout << "没找到:";
t.Show();
return false;
}
t.Show();
cout << point[res].Geth() << " 改为 :";
cin >> n;
point[res].Seth(n);
cout << point[res].Getm() << " 改为 :";
cin >> n;
point[res].Setm(n);
cout << point[res].Gets() << " 改为 :";
cin >> n;
point[res].Sets(n);
return true;
}
bool DynamicTimeArray::modify(int index) {
int n;
if(index < 0 || index > size - 1) {
cout << "数组索引越界。\n";
return false;
}
point[index].Show();
cout << point[index].Geth() << " 改为 :";
cin >> n;
point[index].Seth(n);
cout << point[index].Getm() << " 改为 :";
cin >> n;
point[index].Setm(n);
cout << point[index].Gets() << " 改为 :";
cin >> n;
point[index].Sets(n);
return true;
}
void DynamicTimeArray::display() {
for(int i = 0; i < size; ++i)
point[i].Show();
}
int main() {
char *st[MAXLEN] = {"18:56:32","25:01:01","31:10:31","10:03:20","12:12:31","21:03:08"};
int h,m,s;
DynamicTimeArray ta(st,6);
Time t;
cout << "排序前:\n";
ta.display();
ta.sort();
cout << "排序后:\n";
ta.display();
sscanf(st[1],"%d:%d:%d",&h,&m,&s);
t.Seth(h);
t.Setm(m);
t.Sets(s);
ta.edit(t);
ta.modify(5);
cout << "修改后:\n";
ta.sort();
ta.display();
return 0;
}