Yurttas/PL/OOL/Cplusplus/F/02/06/06/A.cpp

From ZCubes Wiki
Revision as of 22:46, 6 November 2013 by MassBot1 (talk | contribs) (Created page with "<syntaxhighlight lang="cpp" line start="1" enclose="div">/* Copyright(C) 2004 All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc.. Perm...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
  1/*
  2   Copyright(C) 2004
  3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
  4
  5   Permission to use, copy, modify, and distribute this
  6   software and its documentation for EDUCATIONAL purposes
  7   and without fee is hereby granted provided that this
  8   copyright notice appears in all copies.
  9
 10   date   : January 1, 2004.
 11   author : Salih Yurttas.
 12
 13   A.cpp
 14*/
 15
 16
 17#include "A.h"
 18
 19A::
 20A() {
 21}
 22
 23A::
 24A(const A& a) : data_a(a.data_a),
 25                data_b(a.data_b) {
 26}
 27
 28A&
 29A::
 30operator=(const A& a) {
 31  if(this==&a) return *this;
 32
 33  data_a = a.data_a;
 34  data_b = a.data_b;
 35
 36  return *this;
 37}
 38
 39int
 40A::
 41get_first_data_a() const {
 42  return data_a.front();
 43}
 44
 45int
 46A::
 47get_last_data_a() const {
 48  return data_a.back();
 49}
 50
 51int
 52A::
 53get_any_data_a(const int i) const {
 54  if(i<0 || i>data_a.size())
 55    throw out_of_range("index i is out_of_range");
 56  else 
 57    return data_a.at(i);
 58}
 59
 60int
 61A::
 62get_first_data_b() const {
 63  return data_b.front();
 64}
 65
 66int
 67A::
 68get_last_data_b() const {
 69  return data_b.back();
 70}
 71
 72int
 73A::
 74get_any_data_b(const int i) const {
 75  if(i<0 || i>data_b.size())
 76    throw out_of_range("index i is out_of_range");
 77  else 
 78    return data_b.at(i);
 79}
 80
 81void
 82A::
 83set_first_data_a(const int a) {
 84  if(data_a.empty())
 85    data_a.push_back(a);
 86  else
 87    data_a.at(0) = a;
 88}
 89
 90void
 91A::
 92set_last_data_a(const int a) {
 93  if(data_a.empty())
 94    data_a.push_back(a);
 95  else
 96    data_a.at(data_a.size()-1) = a;
 97}
 98
 99void
100A::
101set_all_data_a(const int a) {
102  if(data_a.empty())
103    data_a.push_back(a);
104  else {
105    int n = data_a.size();
106    for(int i=0; i<n; i++)
107      data_a.at(i) = a;
108  }
109}
110
111void
112A::
113set_all_data_a(const vector<int>& a) {
114  data_a = a;
115}
116
117void
118A::
119set_any_data_a(const int i,
120               const int a) {
121  if(i<0 || i>data_a.size())
122    throw out_of_range("index i is out_of_range");
123  else 
124    data_a.at(i) = a;
125}
126
127void
128A::
129set_first_data_b(const int a) {
130  if(data_b.empty())
131    data_b.push_back(a);
132  else
133    data_b.at(0) = a;
134}
135
136void
137A::
138set_last_data_b(const int a) {
139  if(data_b.empty())
140    data_b.push_back(a);
141  else
142    data_b.at(data_b.size()-1) = a;
143}
144
145void
146A::
147set_all_data_b(const int a) {
148  if(data_b.empty())
149    data_b.push_back(a);
150  else {
151    int n = data_b.size();
152    for(int i=0; i<n; i++)
153      data_b.at(i) = a;
154  }
155}
156
157void
158A::
159set_all_data_b(const vector<int>& a) {
160  data_b = a;
161}
162
163void
164A::
165set_any_data_b(const int i,
166               const int a) {
167  if(i<0 || i>data_b.size())
168    throw out_of_range("index i is out_of_range");
169  else 
170    data_b.at(i) = a;
171}
172
173A&
174A::
175operator+=(const A& a) {
176  int n = data_a.size();
177  int m = data_b.size();
178
179  if(n!=data_a.size()||
180     m!=data_b.size()) 
181    throw out_of_range("mismatch sizes!");
182
183  for(int i=0; i<n; i++)
184    data_a.at(i) += a.data_a.at(i);
185
186  for(int i=0; i<m; i++)
187    data_b.at(i) += a.data_b.at(i);
188
189  return *this;
190}
191
192A
193operator+(const A& a,
194          const A& b) {
195  int n = a.data_a.size();
196  int m = a.data_b.size();
197
198  if(n!=b.data_a.size()||
199     m!=b.data_b.size())
200    throw out_of_range("mismatch sizes!");
201
202  A c(a);
203
204  for(int i=0; i<n; i++)
205    c.data_a.at(i) = a.data_a.at(i) + b.data_a.at(i);
206
207  for(int i=0; i<m; i++)
208    c.data_b.at(i) = a.data_b.at(i) + b.data_b.at(i);
209
210  return c;
211}
212
213ostream&
214operator<<(ostream& os,
215           const A& a) {
216  os << endl;
217  int n = a.data_a.size();
218  for(int i=0; i<n; i++)
219    os << a.data_a.at(i) << endl;
220  os << endl;
221
222  os << endl;
223  n = a.data_b.size();
224  for(int i=0; i<n; i++)
225    os << a.data_b.at(i) << endl;
226  os << endl;
227
228  return os;
229}