2个回答
展开全部
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout << "请输入3个数字:"<< endl;
cin >> a >> b >> c;
cout << a << "+" << b << "+" << c << "=" << a + b + c << endl;
return 0;
}
using namespace std;
int main()
{
int a,b,c;
cout << "请输入3个数字:"<< endl;
cin >> a >> b >> c;
cout << a << "+" << b << "+" << c << "=" << a + b + c << endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <vector>
#include <iostream>
#include <limits>
using namespace std;
class Polynomial
{
public:
friend ostream& operator<< (ostream&, const Polynomial& p);
public:
Polynomial() {}
Polynomial(const double* c, size_t s) { Coffs.assign(c, c+s); }
Polynomial operator+ (Polynomial& other)
{
Polynomial p1, p2;
if (Coffs.size() < other.Coffs.size())
{
p1 = *this;
p2 = other;
}
else
{
p1 = other;
p2 = *this;
}
Polynomial result = p2;
for (int i=0; i<p1.Coffs.size(); ++i)
{
result.Coffs[i] += p1.Coffs[i];
}
result.adjust();
return result;
}
private:
void adjust()
{
size_t lenth = Coffs.size();
for (int i=lenth-1; i>=0; --i)
{
static double epsilon = numeric_limits<double>::epsilon();
if (Coffs[i]<=-epsilon || Coffs[i]>=epsilon)
{
lenth = i+1;
break;
}
}
if (lenth != Coffs.size())
{
Polynomial tmp;
tmp.Coffs.assign(Coffs.begin(), Coffs.begin()+lenth);
*this = tmp;
}
}
vector<double> Coffs;
};
ostream& operator<< (ostream& os, const Polynomial& p)
{
for (int i=0; i<p.Coffs.size(); ++i)
{
os << p.Coffs[i] << " ";
}
os << endl;
return os;
}
int main()
{
double poly1[] = {0, 2, 3, 5};
double poly2[] = {1, -2, 3, -5};
Polynomial p1(poly1, sizeof(poly1) / sizeof(poly1[0]));
Polynomial p2(poly2, sizeof(poly2) / sizeof(poly2[0]));
Polynomial p3 = p1 + p2;
cout << p3;
return 0;
}
#include <iostream>
#include <limits>
using namespace std;
class Polynomial
{
public:
friend ostream& operator<< (ostream&, const Polynomial& p);
public:
Polynomial() {}
Polynomial(const double* c, size_t s) { Coffs.assign(c, c+s); }
Polynomial operator+ (Polynomial& other)
{
Polynomial p1, p2;
if (Coffs.size() < other.Coffs.size())
{
p1 = *this;
p2 = other;
}
else
{
p1 = other;
p2 = *this;
}
Polynomial result = p2;
for (int i=0; i<p1.Coffs.size(); ++i)
{
result.Coffs[i] += p1.Coffs[i];
}
result.adjust();
return result;
}
private:
void adjust()
{
size_t lenth = Coffs.size();
for (int i=lenth-1; i>=0; --i)
{
static double epsilon = numeric_limits<double>::epsilon();
if (Coffs[i]<=-epsilon || Coffs[i]>=epsilon)
{
lenth = i+1;
break;
}
}
if (lenth != Coffs.size())
{
Polynomial tmp;
tmp.Coffs.assign(Coffs.begin(), Coffs.begin()+lenth);
*this = tmp;
}
}
vector<double> Coffs;
};
ostream& operator<< (ostream& os, const Polynomial& p)
{
for (int i=0; i<p.Coffs.size(); ++i)
{
os << p.Coffs[i] << " ";
}
os << endl;
return os;
}
int main()
{
double poly1[] = {0, 2, 3, 5};
double poly2[] = {1, -2, 3, -5};
Polynomial p1(poly1, sizeof(poly1) / sizeof(poly1[0]));
Polynomial p2(poly2, sizeof(poly2) / sizeof(poly2[0]));
Polynomial p3 = p1 + p2;
cout << p3;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询