如何输入向量? [英] How to make an input of vector?
问题描述
你好,请帮助我.
我写了一个简单的程序来创建和编辑矢量.有必要对这两个向量进行估算.如您所见,在我的代码中,默认情况下设置了向量:
vect x(5,6,7,8,9),y(4,3,2,1,7);
Hello, please, help me someone.
I wrote a simple program for creating and editing vector. It is necessary to make an imput of these two vectors. As you see, in my code, vectors are set by default:
vect x(5,6,7,8,9),y(4,3,2,1,7);
#include <conio.h>
#include <iostream.h>
class vect {
public:
vect(int=0,int=0,int=0,int=0,int=0);
vect operator+(const vect &) const;
vect operator-(const vect &) const;
vect operator*(const vect &);
vect operator*(const int &);
void print() const;
private:
int a,b,c,d,e;
};
vect::vect(int a1,int b1,int c1,int d1,int e1)
{
a = a1;
b = b1;
c = c1;
d = d1;
e = e1;
}
vect vect::operator+(const vect &operand2) const
{
vect sum;
sum.a = a + operand2.a;
sum.b = b + operand2.b;
sum.c = c + operand2.c;
sum.d = d + operand2.d;
sum.e = e + operand2.e;
return sum;
}
vect vect::operator-(const vect &operand2) const
{
vect diff;
diff.a = a - operand2.a;
diff.b = b - operand2.b;
diff.c = c - operand2.c;
diff.d = d - operand2.d;
diff.e = e - operand2.e;
return diff;
}
vect vect::operator*(const vect &operand2)
{
vect proizv;
proizv.a = a * operand2.a;
proizv.b = b * operand2.b;
proizv.c = c * operand2.c;
proizv.d = d * operand2.d;
proizv.e = e * operand2.e;
return proizv;
}
vect vect::operator*(const int &operand2)
{
vect proizv;
proizv.a = a * operand2;
proizv.b = b * operand2;
proizv.c = c * operand2;
proizv.d = d * operand2;
proizv.e = e * operand2;
return proizv;
}
void vect::print() const
{
cout<<''(''<<a<<", "<<b<<", "<<c<<", "<<d<<", "<<e<<'')'';
}
int main(int argc, char* argv[])
{
vect x(5,6,7,8,9),y(4,3,2,1,7);
vect z;
int scl = 8;
cout<<"\n Vector 1: ";
x.print();
cout<<"\n Vector 2: ";
y.print();
z = x + y;
cout<<"\n Slozhenie: ";
z.print();
z = x - y;
cout<<"\n Vichitanie: ";
z.print();
z = x * y;
cout<<"\n Vector*Vector: ";
z.print();
z = x * scl;
cout<<"\n Scalar1: ";
z.print();
z = y * scl;
cout<<"\n Scalar2: ";
z.print();
getch();
return 0;
}
推荐答案
我猜你的意思是:
I guess you mean something like:
integer a,b,c,d,e;
cout << "Enter vector 1 values: " << endl;
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
vect x(a, b, c, d, e);
// repeat for vector y
显然不是很复杂,但是我相信您会明白.
Obviously not very sophisticated but I trust you get the idea.
看看如何实现插入和提取运算符,它将使您的代码更容易阅读.例如,如果您实现operator<<和运算符>>您可能最终会写出类似以下内容的内容:
Have a look at how you implement the insertion and extraction operators, it''ll make your code a lot easier to read. For example if you implement operator<< and operator>> you could end up writing something like:
int main()
{
std::cout << "Please enter 10 integers, white space delimited to initialise my vectors: ";
vect a, b;
std::cin >> a >> b;
std::cout << "Sum of your vectors is: " << a + b << std::endl;
std::cout << "Difference of your vectors is: " << a - b << std::endl;
}
现在那里没有很多错误处理,但是它表明您的代码可以简单得多.
无论您用来学习C ++的文本是什么,都应包含有关如何重载运算符的更多详细信息.呆呆的去吧,如果您有问题,请提出更具体的问题.
干杯,
灰
Now there''s not a lot of error handling there but it shows how much simpler your code could be.
Whatever text you''re using to learn C++ from should have some more details on how to overload operators. Have a gawp there give it a go and if you''re having problems ask a more specific questions.
Cheers,
Ash
这篇关于如何输入向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!