Informatika3-2024/Megoldasok7

A MathWikiből
(Változatok közti eltérés)
(Új oldal, tartalma: „<c> #include<iostream> using namespace std; class String { private: char *str; int length; int c_string_length(const char* s); public: String(); String(co…”)
 
 
13. sor: 13. sor:
 
   String(const char* s);
 
   String(const char* s);
 
   String(const String& other);
 
   String(const String& other);
  String& operator=(String& other);
 
 
   
 
   
 
   void print();
 
   void print();
  int getLength();
 
 
   
 
   
 
   String operator+(String other);
 
   String operator+(String other);
+
  bool operator==(String other);
   friend String operator+(const char* left, String right);
+
   String& operator=(String& other);
 +
 
 
   friend ostream& operator<<(ostream& os, String s);
 
   friend ostream& operator<<(ostream& os, String s);
 
   
 
   
55. sor: 54. sor:
 
   this->str[this->length] = '\0';
 
   this->str[this->length] = '\0';
 
}
 
}
+
 
String& String::operator=(String& other) {
+
String String::operator+(String other) {
   if(this == &other) {
+
   char* s = new char[this->length + other.length + 1];
     return *this;
+
  for(int i = 0; i < this->length; i++) {
 +
     s[i] = this->str[i];
 
   }
 
   }
  delete[] this->str;
+
   for(int i = 0; i < other.length; i++) {
  this->str = new char[other.length];
+
     s[i + this->length] = other.str[i];
  this->length = other.length;
+
   for(int i = 0; other.str[i] != '\0'; i++) {
+
     this->str[i] = other.str[i];
+
 
   }
 
   }
   this->str[this->length] = '\0';
+
   s[this->length + other.length] = '\0';
   return *this;
+
  String ret = String(s);
 +
  delete[] s;
 +
   return ret;
 
}
 
}
+
 
 
void String::print() {
 
void String::print() {
 
   cout << str << endl;
 
   cout << str << endl;
 
}
 
}
+
 
int String::getLength() {
+
bool String::operator==(String other) {
   return length;
+
   /*
 +
  if(this->length != other.length) {
 +
    return false;
 +
  }
 +
  */
 +
  for(int i = 0; i < this->length + 1; i++) {
 +
    if (this->str[i] != other.str[i]) {
 +
      return false;
 +
    }
 +
  }
 +
  return true;
 
}
 
}
 
   
 
   
String String::operator+(String other) {
+
String& String::operator=(String& other) {
   int new_length = this->length + other.length + 1;
+
   if(this == &other) {
  char s[new_length];
+
     return *this;
  for(int i = 0; i < this->length; i++) {
+
     s[i] = this->str[i];
+
 
   }
 
   }
 +
  delete[] this->str;
 +
  this->str = new char[other.length + 1];
 
   for(int i = 0; i < other.length; i++) {
 
   for(int i = 0; i < other.length; i++) {
     s[i + this->length] = other.str[i];
+
     this->str[i] = other.str[i];
 
   }
 
   }
   s[new_length] = '\0';
+
   this->str[other.length] = '\0';
   return String(s);
+
  this->length = other.length;
 +
   return *this;
 
}
 
}
+
 
 
String::~String() {
 
String::~String() {
 
   delete[] str;
 
   delete[] str;
 
}
 
}
+
 
String operator+(const char* left, String right) {
+
  int j;
+
  for(j = 0; left[j] != '\0'; j++) {}
+
  int new_length = j + right.length + 1;
+
  char s[new_length];
+
  for(int i = 0; i < j; i++) {
+
    s[i] = left[i];
+
  }
+
  for(int i = 0; i < right.length; i++) {
+
    s[i + j] = right.str[i];
+
  }
+
  s[new_length] = '\0';
+
  return String(s);
+
}
+
+
 
ostream& operator<<(ostream& os, String s) {
 
ostream& operator<<(ostream& os, String s) {
   os << s.str;
+
   cout << s.str;
 
   return os;
 
   return os;
 
}
 
}
+
 
+
String& String::operator=(const char* str) {
 +
}
 +
 
 
int main(void) {
 
int main(void) {
   String s1 = String("batman");
+
   String a;
   String s2 = String("catman");
+
  String b("batman");
   cout << s1 << endl;
+
   String c(b);
   cout << s2 << endl;
+
  String d("catman");
   //cout << (s1 == s2) << endl;
+
   String e = b + d;
   s1 = s2;
+
  //String e = b.operator+(d);
  //cout << (s1 == s2) << endl;
+
   cout << e << endl;
   cout << s1 << endl;
+
   //operator<<(operator<<(cout, e), endl);
   cout << s2 << endl;
+
   cout << (a == b) << endl;
 +
   a = b;
 +
  a = a;
 +
   cout << (a == b) << endl;
 
   return 0;
 
   return 0;
 
}
 
}
 
</c>
 
</c>

A lap jelenlegi, 2024. május 2., 14:43-kori változata

#include<iostream>
 
using namespace std;
 
class String {
private:
  char *str;
  int length;
  int c_string_length(const char* s);
public:
  String();
  String(const char* s);
  String(const String& other);
 
  void print();
 
  String operator+(String other);
  bool operator==(String other);
  String& operator=(String& other);
 
  friend ostream& operator<<(ostream& os, String s);
 
  ~String();
};
 
int String::c_string_length(const char* s) {
  int i;
  for(i = 0; s[i] != '\0'; i++) {}
  return i;
}
 
String::String() {
  str = new char[1];
  str[0] = '\0';
  length = 0;
}
 
String::String(const char* s) {
  length = c_string_length(s);
  str = new char[length + 1];
  for(int i = 0; s[i] != '\0'; i++) {
    str[i] = s[i];
  }
  str[length] = '\0';
}
 
String::String(const String& other) {
  this->length = other.length;
  str = new char[this->length + 1];
  for(int i = 0; other.str[i] != '\0'; i++) {
    this->str[i] = other.str[i];
  }
  this->str[this->length] = '\0';
}
 
String String::operator+(String other) {
  char* s = new char[this->length + other.length + 1];
  for(int i = 0; i < this->length; i++) {
    s[i] = this->str[i];
  }
  for(int i = 0; i < other.length; i++) {
    s[i + this->length] = other.str[i];
  }
  s[this->length + other.length] = '\0';
  String ret = String(s);
  delete[] s;
  return ret;
}
 
void String::print() {
  cout << str << endl;
}
 
bool String::operator==(String other) {
  /*
  if(this->length != other.length) {
    return false;
  }
  */
  for(int i = 0; i < this->length + 1; i++) {
    if (this->str[i] != other.str[i]) {
      return false;
    }
  }
  return true;
}
 
String& String::operator=(String& other) {
  if(this == &other) {
    return *this;
  }
  delete[] this->str;
  this->str = new char[other.length + 1];
  for(int i = 0; i < other.length; i++) {
    this->str[i] = other.str[i];
  }
  this->str[other.length] = '\0';
  this->length = other.length;
  return *this;
}
 
String::~String() {
  delete[] str;
}
 
ostream& operator<<(ostream& os, String s) {
  cout << s.str;
  return os;
}
 
String& String::operator=(const char* str) {
}
 
int main(void) {
  String a;
  String b("batman");
  String c(b);
  String d("catman");
  String e = b + d;
  //String e = b.operator+(d);
  cout << e << endl;
  //operator<<(operator<<(cout, e), endl);
  cout << (a == b) << endl;
  a = b;
  a = a;
  cout << (a == b) << endl;
  return 0;
}
Személyes eszközök