001.重载DataLength函数
题目描述
定义一个Test类,该类有两个重载的成员函数DataLength,一个接收int型的参数,另一个接收string型的参数
第一个DataLength函数用于计算一个整型值的位数并返回
第二个DataLength函数用于计算string变量字符串的实际长度并返回
main函数已经写好,请根据下面的main函数完成该类的设计:
int main(){
int a;
string s;
getline(cin,s);
cin>>a;
Test t;
cout<<t.DataLength(s)<<endl;
cout<<t.DataLength(a)<<endl;
return 0;
}
输入描述
第一行输入一行字符串
第二行输入一个整数
输出描述
分两行分别输出字符串的实际长度和整数的位数
提示
你需要提交除main函数之外的其他代码
样例输入
Hello World
12345678
样例输出
11
8
允许最长运行时间
1000ms
允许使用最大内存
64KB
#include<iostream>
using namespace std;
#include<string>
class Test
{
public:
int DataLength(int n)
{
int i = 0;
if ( n == 0)
{
return 0;
}
else
{
while (n)
{
n = n / 10;
i++;
}
return i;
}
}
int DataLength(string s)
{
return s.length();
}
};
int main(){
int a;
string s;
getline(cin,s);
cin>>a;
Test t;
cout<<t.DataLength(s)<<endl;
cout<<t.DataLength(a)<<endl;
return 0;
}
运行结果: