菜鸟的博客

纵有疾风起,人生不言弃。

导航

2023.4.25

  1 //实验五任务三
  2 #include <iostream>
  3 #include<string>
  4 #include <cmath>
  5 using namespace std;
  6 class vector_N
  7 {
  8 private:
  9     int n;
 10     int* p;
 11 public:
 12     vector_N();
 13     vector_N(vector_N &B);
 14     bool check(vector_N B);
 15     int show_N();
 16     friend istream& operator>>(istream& input, vector_N &B);
 17     friend ostream& operator<<(ostream& output, vector_N &B);
 18     friend int neiji(vector_N A, vector_N B);
 19     vector_N operator+(vector_N B);
 20     vector_N operator-(vector_N B);
 21     vector_N operator*(int a);
 22     int operator[](int B);
 23 };
 24 
 25 istream& operator>>(istream& input, vector_N &B)
 26 {
 27     cout << "请输入维数" << endl;
 28     input >> B.n;
 29     if (B.n <= 0)
 30     {
 31         cout << "Error Length!" << endl;
 32         exit(0);//退出程序
 33     }
 34     else
 35     {
 36         B.p = new int[B.n];//动态数组
 37         cout << "请分别输入各个坐标" << endl;
 38         for (int i = 0; i < B.n; i++)
 39         {
 40             input >> B.p[i];
 41         }
 42     }
 43     return input;
 44 }
 45 ostream& operator<<(ostream& output, vector_N &B)
 46 {
 47     for (int i = 0; i < B.n; i++)
 48     {
 49         if (i == 0)
 50         {
 51             output << "(";
 52         }
 53         cout << B.p[i];
 54         if (i != B.n - 1)
 55         {
 56             output << ",";
 57         }
 58         else
 59         {
 60             output << ")" << endl;
 61         }
 62     }
 63     return output;
 64 }
 65 int neiji(vector_N A, vector_N B)
 66 {
 67     int sum = 0;
 68     if (A.n == B.n)
 69     {
 70         vector_N C(A);
 71         for (int i = 0; i < A.n; i++)
 72         {
 73             C.p[i] = A.p[i] * B.p[i];
 74             sum+=C.p[i];
 75         }
 76         return sum;
 77     }
 78     else
 79     {
 80         cout << "error" << endl;
 81     }
 82 }
 83 vector_N::vector_N()
 84 {
 85     n = 0;
 86     p = NULL;
 87 }
 88 vector_N::vector_N(vector_N &B)
 89 {
 90     n = B.n;
 91     p = new int[B.n];
 92     for (int i = 0; i < B.n; i++)
 93     {
 94         p[i] = B.p[i];
 95     }
 96 }
 97 bool vector_N::check(vector_N B)
 98 {
 99     if (n == B.n) return true;
100     else
101     {
102         cout << "Mismatch Length!" << endl;
103         return false;
104     }
105 }
106 int vector_N::show_N()
107 {
108     return n;
109 }
110 vector_N vector_N::operator + (vector_N B)
111 {
112     vector_N A(B);
113     if (check(B))
114     {
115         for (int i = 0; i < n; i++)
116         {
117             A.p[i] = p[i] + B.p[i];
118         }
119         return A;
120     }
121     else
122     {
123         exit(0);
124     }
125 }
126 vector_N vector_N::operator - (vector_N B)
127 {
128     vector_N A(B);
129     cout << B;
130     if (check(B))
131     {
132         for (int i = 0; i < n; i++)
133         {
134             A.p[i] = p[i] - B.p[i];
135         }
136         return A;
137     }
138     else
139     {
140         exit(0);
141     }
142 }
143 vector_N vector_N:: operator*(int a)
144 {
145     for (int i = 0; i < n; i++)
146     {
147         this->p[i] = this->p[i] * a;
148     }
149     return *this;
150 }
151 /////////////////////////////////////////////
152 int vector_N::operator[](int y)
153 {
154     if (y>=0&&y<this->n)
155     {
156         int num = this->p[y];
157         return num;
158     }
159         
160     else
161     {
162         cout << "Error Index" << endl;
163     }
164 }
165 int main()
166 {
167     int x,y;
168     vector_N a, b;
169     cin >> a;
170     cin >> b;
171     cout<<"数乘运算乘数:"<<endl;
172     cin>>x;
173     cout<<"a中对应下标:"<<endl;
174     cin>>y;
175     cout << "a+b=" << a + b;
176     cout << "a-b=" << a - b;
177     cout << "a*b=" << neiji(a,b)<<endl;
178     cout << "a*x = " << a.operator*(x);
179     cout << "a的y下标对应值:"<<b.operator[](y)<<endl;
180     return 0;
181 }

 

posted on 2023-04-25 01:26  hhmzd233  阅读(15)  评论(0编辑  收藏  举报