//*********************************************************************************
// ICS 211 Data Structures 2001 Spr'ng Semester
// Project : HW 3
//
// File : hw3.cpp
// Date : 16/04/2001
// Author: Seniz Demir
//
// Description: Operator overloading processes on String objects
//
// -------------------------------------------------------------------------------
//  Copyright (C) 2001 Yeditepe University 
//  		       Information and Computer Science Department
//  		       All rights reserved
// -------------------------------------------------------------------------------

#include<iostream.h>
#include<math.h>
#include<assert.h>
#include<string.h>

//-------------------------------------------------------------------------------
// String
// Implementation Notes: Implementation of String class
//-------------------------------------------------------------------------------


class String {

	public:
	 String(const char *s) ;
	 String(){}; 
	~String();
	const String &operator=(const String &);// comparison
	const String &operator+=(const String &);//concatenation
	char  operator[](int number ) const  ;//length
	void operator*(const String &);//reverse 
	void setString(const char *string2);
        void showString();
	int getlength();
	String &operator^(int number);
	private:	
      	int length;
        char *sPtr;	  
};
void String::operator*(const String &s)
{
	for (int j=0;j<strlen(s.sPtr);j++)
        {
	     sPtr[j]=s.sPtr[strlen(s.sPtr)-1-j];
        }
}
void String::showString()
{
  cout<<sPtr;
}	
char String::operator[](int number) const
{
return(sPtr[number]);
}
// Overloaded = operator; avoids selff assignment
const String &String::operator=(const String &right)
{
	strcpy(sPtr,right.sPtr);
	return *this; // enables cascaded assignments
}

// Concatenate right operand to this object and
// store in this object

const String &String::operator+=(const String &right)
{
	strcat(sPtr,right.sPtr);
	return *this; // enables cascaded calls
}
int String::getlength()
{
	return length;
}
String::String(const char *s)
{
   sPtr=new char[strlen(s)];
   strcpy(sPtr,s);
   length=strlen(sPtr);
}
String::~String()//bak
{
delete []sPtr;
}
void String::setString(const char *string2)
{ 
	sPtr=new char[length+1];
	assert(sPtr!=0);
	strcpy(sPtr,string2);
}
 String &String::operator^(int number)
{
  char *ptr;	
  ptr=new char[200]; 
  for (int y=0;y<number;y++) 
	  strcat(ptr,sPtr);
         strcpy(sPtr,ptr);
  return *this;
}

class Menu
{
	public:
		int displayMenu();
};

int Menu::displayMenu()
{
	int d1 = 1;

	cout << "\nChoose one of the options from the menu:\n" << endl;

	cout << "[1]. CONCATENATE TWO STRINGS\n";
	cout << "[2]. ASSIGN A STRING TO ANOTHER\n";
	cout << "[3]. ASSIGN THE REVERSE OF A STRING TO ANOTHER\n";
	cout << "[4]. GET THE POWER OF A STRING\n";
	cout << "[5]. EXIT\n\n";

        do
	{
		if ( (d1<1) || (d1>5) )
		{
			cout << "Wrong Choice!!\n";
		}

		cout << "Enter your choice: ";
		cin >> d1;
	} while (d1<1 || d1>6);	

	return d1;
}	


int main()

{
    String *array1,*array2;
    Menu mainMenu;
    int option,exp,index1,index2;

    array1 = new String[3];
    array2 = new String[3];
    
    array1[0].setString("Ali");
    array1[1].setString("Ahmet");
    array1[2].setString("Mehmet");

    array2[0].setString("Merhaba");
    array2[1].setString("Televole");
    array2[2].setString("Selam");
    
    do
    {
	 option = mainMenu.displayMenu();

	 switch(option)
	 {
		case 1:
			cout << "\nEnter the index number for the first array[1-3]";
			cin >> index1;
			index1--;

			cout << "\nEnter the index number for the second array[1-3]";
			cin >> index2;
			index2--;
			
		        cout << "\nThe string you have chosen from the first array is:";
		        array1[index1].showString();	
			
		        cout << "\nThe string you have chosen from the second array is:";
		        array2[index2].showString();	
			
			array1[index1] += array2[index2];	
	
			cout << "\nThe changed string on first array is:";
		        array1[index1].showString();	
		        cout << endl;

			break;
		case 2:
			cout << "\nEnter the index number for the first array[1-3]";
			cin >> index1;
			index1--;

			cout << "\nEnter the index number for the second array[1-3](to be assigned)";
			cin >> index2;
			index2--;
			
		        cout << "\nThe string you have chosen from the first array is:";
		        array1[index1].showString();	
			
		        cout << "\nThe string you have chosen from the second array is:";
		        array2[index2].showString();	
			
			array1[index1] = array2[index2];	
			
			cout << "\nThe changed string on first array is:";
		        array1[index1].showString();	
		        cout << endl;
			break;
		case 3:
			cout << "\nEnter the index number for the first array[1-3]";
			cin >> index1;
			index1--;

			cout << "\nEnter the index number for the second array[1-3](to be assigned)";
			cin >> index2;
			index2--;
			
		        cout << "\nThe string you have chosen from the first array is:";
		        array1[index1].showString();	
			
		        cout << "\nThe string you have chosen from the second array is:";
		        array2[index2].showString();	
			
			
			array1[index1]*(array2[index2]);	
			
			cout << "\nThe changed string on first array is:";
		        array1[index1].showString();	
		        cout << endl;
			
			
			break;
		case 4: 
			cout << "\nEnter the index number for the first array[1-3]";
			cin >> index1;
			index1--;
		        cout << "\nEnter the exponent: ";
		        cin >> exp;	
			
			cout << "\nThe string you have chosen from the first array is:";
		        array1[index1].showString();	
			
			array1[index1]^exp;	
			
			cout << "\nThe changed string on first array is:";
		        array1[index1].showString();	
		        cout << endl;
			
			break;
	 }
     } while (option != 5);
     
    return 0;
};
		

