1060 Are They Equal (25分)

在这里插入图片描述
在这里插入图片描述
是我太菜,这题做的好麻烦,标注里的案例挨个试

#include<iostream>
#include<string>
using namespace std;
int n;
string init(string a,int &s)
{
	int i=0;
	int l=0;
	int len=a.length();
	while(i<len&&a[0]=='0')
	//因while(i<a.length()&&a[i]=='0')出现错误
	{
		a.erase(a.begin());
		//因写成a.erase(a.begin()+i);出现错误
		i++;
	}
	if(i<len&&a[0]=='.')
	{
		a.erase(a.begin());
		i++;
		while(i<len&&a[0]=='0')
		{	
			a.erase(a.begin());
			i++;
			s--;
		}
		if(i>=len)
			s=0;
	}
	else
	{
		i=0;
		while(i<a.length()&&a[i]!='.')
		{l++;i++;}
		s=l;
		if(i<a.length()&&a[i]=='.')
			a.erase(a.begin()+i);

	//	cout<<a.length()<<endl;

	}
	while(a.length()<n)
	{
		a+="0";
	}
	return a.substr(0,n);
}
int main()
{
	string a,b;
	string stra,strb;
	while(cin>>n)
	{
	cin>>a>>b;
	int as=0,bs=0;
	stra=init(a,as);
	strb=init(b,bs);
	if(stra[0]=='0'&&strb[0]=='0')
	{
		as=as<bs?as:bs;
		cout<<"YES 0."<<stra<<"*10^"<<as<<endl;
		continue;
	}
	if(stra==strb&&as==bs)
		cout<<"YES 0."<<stra<<"*10^"<<as<<endl;
	else
		cout<<"NO 0."<<stra<<"*10^"<<as<<" 0."<<strb<<"*10^"<<bs<<endl;
	}
	return 0;
}
/*
4 0000 0000.00
4 00123.5678 0001235
3 0.0520 0.0521
4 00000.000000123 0.0000001230
4 00100.00000012 100.00000013
5 0010.013 10.012
4 123.5678 123.5
3 123.5678 123
4 123.0678 123
3 0.000 0
12 123456789012345 123456789012300
12 123456789012345 123456789010000
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Here is the function template for isEqualTo: ``` template<typename T> bool isEqualTo(const T& a, const T& b) { return a == b; } ``` Here is an example program that uses isEqualTo with built-in types: ``` #include <iostream> int main() { std::cout << std::boolalpha; std::cout << isEqualTo(5, 5) << std::endl; std::cout << isEqualTo(5, 7) << std::endl; std::cout << isEqualTo('a', 'a') << std::endl; std::cout << isEqualTo('a', 'b') << std::endl; std::cout << isEqualTo(true, true) << std::endl; std::cout << isEqualTo(true, false) << std::endl; std::cout << isEqualTo(3.14, 3.14) << std::endl; std::cout << isEqualTo(3.14, 2.71) << std::endl; return 0; } ``` When we attempt to run this program with the user-defined class type Complex, we get a compilation error because the compiler doesn't know how to compare two Complex objects. To fix this, we can overload the equality operator in the Complex class: ``` class Complex { public: Complex(double real, double imag) : m_real(real), m_imag(imag) {} double real() const { return m_real; } double imag() const { return m_imag; } private: double m_real; double m_imag; }; bool operator==(const Complex& lhs, const Complex& rhs) { return lhs.real() == rhs.real() && lhs.imag() == rhs.imag(); } ``` Now when we attempt to run the program with Complex objects, it works as expected: ``` #include <iostream> class Complex { public: Complex(double real, double imag) : m_real(real), m_imag(imag) {} double real() const { return m_real; } double imag() const { return m_imag; } private: double m_real; double m_imag; }; bool operator==(const Complex& lhs, const Complex& rhs) { return lhs.real() == rhs.real() && lhs.imag() == rhs.imag(); } template<typename T> bool isEqualTo(const T& a, const T& b) { return a == b; } int main() { std::cout << std::boolalpha; std::cout << isEqualTo(Complex(1.0, 2.0), Complex(1.0, 2.0)) << std::endl; std::cout << isEqualTo(Complex(1.0, 2.0), Complex(3.0, 4.0)) << std::endl; return 0; } ``` Output: ``` true false ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茅蒐Rubia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值