בתרגיל זה תוכלו לתרגל את נושא המחרוזות והפונקציות שלהן וקצת קומבינות 🙂
העמסת אופרטורים למחרוזות כגון: <,>,= ,>= ועוד ..
תרגיל 3 שאלה 1 סדנא ב ++C
/****************************** targil: 3/1 lecturer: david Cohen discription: work with strings *******************************/ #include <iostream> #include <cstring> #include "String1.h" using namespace std; int main() { char str1[50]; char str2[50]; int n; cout<<"Enter strins\n"; int index; cin>>str1; cin>>str2; cin>>n; String A(str1); String B(str2); A>B; A<B; A=B; String C=B.insert(n,str1); if (n<=strlen(str1)) { C.print_str(); } //system("pause"); return 0; } /*****output*********** Enter strins hello hello 2 a=b hehellollo ************************ Enter strins hello world 3 a<b worhellold ************************/
מימוש המחלקה
#include "String1.h" #include <cstring> #include <string> #include <iostream> using namespace std; String::String() { } String::String(char*string) { str= new char [strlen(string)+1]; // creayte arrary for the length of this sting +1 for \0 strcpy(str,string); } char* String::get_str() { return str; } void String::print_str() { cout<<str<<endl; } String String::operator+(String s) { char*temp=new char[strlen(str)+strlen(s.str)+1]; //creayte arrary for the length of two string +1 for \0 strcpy(temp,str); strcat(temp,s.str); // add the second string to the first one String Temp(temp); // creayte new string that contine the two stings return Temp; } String String:: insert( int index, const char* s ) { if (index<=strlen(str)) { char*temp=new char[strlen(str)+strlen(s)+1]; //creayte arrary for the length of two string +1 for \0 strncpy(temp,str,index); //copy the first string but only form the begining to the index temp[index]='\0'; // end the first word (not complete word) strcat(temp,s); // add the second string strcat(temp,str+index); //add the first word (the left letters(from the index)) String Temp(temp); return Temp; } else { cout<<"ERROR\n"; str=NULL; } } void String::operator>(String s) { if(strcmp(str,s.get_str())>1) //its mean: the first word is biger from the second word { cout<<"a>b\n"; } } void String::operator<(String s) { if(strcmp(str,s.get_str())<0) //its mean: the first word is smaller from the second word { cout<<"a<b\n"; } } void String::operator=(String s) { if(strcmp(str,s.get_str())==0) //its mean: the two words is even { cout<<"a=b\n"; } } String::~String() { }
המחלקה
#pragma once class String { char* str; public: String(); //defolt c-tor String(char*); //c-tor void print_str(); char*get_str(); String operator+(String str); void operator>(String str); void operator<(String str); void operator=(String str); String insert( int index, const char* str ); ~String(void); };