运算符重载有关问题,马上给分
运算符重载问题,马上给分!
编写Time类,使得输入时间时可以以hh-mm-ss的方式输入,且h,m,s既可一位输入也可二位输入,输出以hh:mm:ss的方式输出。因此重载> > , < <函数,但却出现不会输出的问题,不知原因何在,请高手指点,谢谢!!在线等,马上给分!!
//13.6.h
class Time
{
friend ostream &operator < <(ostream &,Time &);
friend istream &operator> > (istream &,Time &);
public:
Time(int h=0,int m=0,int s=0);
private:
int hour,minute,second;
};
#include <iostream.h>
#include <string.h>
#include "13.6.h "
istream &operator> > (istream &in,Time &it)
{
char ss[9];
cin> > ss;
ss[strlen(ss)]= '\0 ';
int temp[7],i=0,j=0,m=0;
while(*(ss+i)!= '\0 ')
{
int n=0;
while(*(ss+i)!= '- '&&*(ss+i)!= '\0 ')
{
temp[j++]=(*(ss+i));
++i;
++n;//用来判断小时、分钟等输入的位数
// cout < <temp[0] < <endl;
}
// cout < <temp[0] < <temp[1];
++m;//用来判断给哪个数据成员赋值
if(m==1)
{
if(n%2)
{
it.hour=temp[j-n]-48;
}
else
{
it.hour=(temp[j-n]-48)*10+temp[j-n+1]-48;
// cout < <temp[j-n] < <temp[j-n+1] < <endl;
}
}
else if(m==2)
{
if(n%2)
it.minute=temp[j-n]-48;
else
{
it.minute=(temp[j-n]-48)*10+temp[j-n+1]-48;
// cout < <temp[j-n] < <temp[j-n+1] < <endl;
}
}
else
{
if(n%2)
{
it.second=temp[j-n]-48;
cout < <temp[j-n]-48 < <endl;
}
else
{
it.second=(temp[j-n]-48)*10+temp[j-n+1]-48;
// cout < <temp[j-n]-48 < <temp[j-n+1]-48 < <endl;
}
}
++i;
// cout < <i < <endl;
}
return in;
}
ostream &operator < <(ostream &out,Time &ot)
{
out < <ot.hour < < ": " < <ot.minute < < ": " < <ot.second < <endl;
return out;
}
Time::Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
int main()
{
Time tt;
cout < < "input the time: ";
cin> > tt;
cout < <tt;
return 0;
}
------解决方案--------------------
VC6吧?
class Time
{
friend ostream &operator < <(ostream &,Time &)
{
....
}
....
------解决方案--------------------
友元问题...
------解决方案--------------------
//包含头文件 <stdio.h>
//istream &operator> > () 改为以下形式
istream &operator> > (istream &in,Time &it)
{
scanf( "%d-%d-%d ", &it.hour, &it.minute, &it.second);
return in;
}
试试
------解决方案--------------------
//要么这样,好像对点
istream &operator> > (istream &in,Time &it)
{
in> > it.hour;
getchar();
in> > it.minute;
getchar();
in> > it.second;
return in;
}
编写Time类,使得输入时间时可以以hh-mm-ss的方式输入,且h,m,s既可一位输入也可二位输入,输出以hh:mm:ss的方式输出。因此重载> > , < <函数,但却出现不会输出的问题,不知原因何在,请高手指点,谢谢!!在线等,马上给分!!
//13.6.h
class Time
{
friend ostream &operator < <(ostream &,Time &);
friend istream &operator> > (istream &,Time &);
public:
Time(int h=0,int m=0,int s=0);
private:
int hour,minute,second;
};
#include <iostream.h>
#include <string.h>
#include "13.6.h "
istream &operator> > (istream &in,Time &it)
{
char ss[9];
cin> > ss;
ss[strlen(ss)]= '\0 ';
int temp[7],i=0,j=0,m=0;
while(*(ss+i)!= '\0 ')
{
int n=0;
while(*(ss+i)!= '- '&&*(ss+i)!= '\0 ')
{
temp[j++]=(*(ss+i));
++i;
++n;//用来判断小时、分钟等输入的位数
// cout < <temp[0] < <endl;
}
// cout < <temp[0] < <temp[1];
++m;//用来判断给哪个数据成员赋值
if(m==1)
{
if(n%2)
{
it.hour=temp[j-n]-48;
}
else
{
it.hour=(temp[j-n]-48)*10+temp[j-n+1]-48;
// cout < <temp[j-n] < <temp[j-n+1] < <endl;
}
}
else if(m==2)
{
if(n%2)
it.minute=temp[j-n]-48;
else
{
it.minute=(temp[j-n]-48)*10+temp[j-n+1]-48;
// cout < <temp[j-n] < <temp[j-n+1] < <endl;
}
}
else
{
if(n%2)
{
it.second=temp[j-n]-48;
cout < <temp[j-n]-48 < <endl;
}
else
{
it.second=(temp[j-n]-48)*10+temp[j-n+1]-48;
// cout < <temp[j-n]-48 < <temp[j-n+1]-48 < <endl;
}
}
++i;
// cout < <i < <endl;
}
return in;
}
ostream &operator < <(ostream &out,Time &ot)
{
out < <ot.hour < < ": " < <ot.minute < < ": " < <ot.second < <endl;
return out;
}
Time::Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
int main()
{
Time tt;
cout < < "input the time: ";
cin> > tt;
cout < <tt;
return 0;
}
------解决方案--------------------
VC6吧?
class Time
{
friend ostream &operator < <(ostream &,Time &)
{
....
}
....
------解决方案--------------------
友元问题...
------解决方案--------------------
//包含头文件 <stdio.h>
//istream &operator> > () 改为以下形式
istream &operator> > (istream &in,Time &it)
{
scanf( "%d-%d-%d ", &it.hour, &it.minute, &it.second);
return in;
}
试试
------解决方案--------------------
//要么这样,好像对点
istream &operator> > (istream &in,Time &it)
{
in> > it.hour;
getchar();
in> > it.minute;
getchar();
in> > it.second;
return in;
}