c_cpp operations_on _unordered_array_using_class.cpp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp operations_on _unordered_array_using_class.cpp相关的知识,希望对你有一定的参考价值。

// author:  dipankar pal (c) copyright 2017
// assignment 01 : choice no: 2
// 11 - sep -2017

#include <iostream>
#include<cstdlib>
using namespace std;

const int size=100;
/////////////////////////////// CLASS /////////////////////////
class array
{
  int arr[size];
  int n; //user defined dynamic size of the array
public:
  void set_values();
  void show_values();
  friend int search(array temp, int x);
  friend int insert(array &temp, int data);
  friend void shift_right(array &temp, int start, int end);
  friend void shift_left(array &temp, int start, int end);
  friend int remove(array &temp,int data);
  friend int ascending(array temp);
  friend void reverse(array &temp);
  friend array copy_array(array &source);
};
////////////////////// SET ///////////////////////////////////
void array :: set_values()
{
  cout<<"enter the number of entries you want : ";
  cin>>n;
  int i;
  cout<<"enter in ascending or descending order : "<<endl;
  for( i=0;i<n;i++)
  {
    cout<<"value ? :";
    cin>>arr[i];
  }
}
  ///////////////////// SHOW ////////////////////////////////////
  void array :: show_values()
  {
    int i;
    cout<<"[ ";
    for (i = 0; i <n; i++)
     {
       cout<<arr[i]<<",";
     }
     cout<<" ]"<<endl;
  }
  //////////////////////// BINARY SEARCH ////////////////////////////////////
  int search(array temp, int x)
  {

    int size=(temp.n)-1;
    int upper_bound=size;
    int lower_bound=0;
  int mid=(upper_bound - lower_bound)/2;

// if ascending oredred array
    if(ascending(temp))
    {
      if (x==temp.arr[mid]) //if found at middle
      {
        cout<<"found at position : "<<mid<<endl;
        return mid;
      }
      else if( x> temp.arr[mid]) lower_bound=mid+1;
      else upper_bound=(mid -1 );

      for(lower_bound;lower_bound<=upper_bound;lower_bound++)
      {
        if(x==temp.arr[lower_bound]) // if found return
        {
          cout<<"found at position: "<<lower_bound<<endl;
          return lower_bound;
        }
        // else loop
      }

// if descending ordered array
    }
    else
    {
      if (x==temp.arr[mid]) //if found at middle
      {
        cout<<"found at position : "<<mid<<endl;
        return mid;
      }
      else if( x< temp.arr[mid]) lower_bound=mid+1;
      else upper_bound=(mid -1 );

      for(lower_bound;lower_bound<=upper_bound;lower_bound++)
      {
        if(x==temp.arr[lower_bound]) // if found return
        {
          cout<<"found at position: "<<lower_bound<<endl;
          return lower_bound;
        }
        // else loop
      }
    }




    cout<<"not found !!"<<endl;
    return (-1);
  }
  ////////////////////////////////////// INSERT /////////////////////////////

  int insert(array &temp, int data)
  {
/*    cout<<"n"<<temp.arr[(temp.n)-1]<<endl;
    cout<<"arr[0]: "<<temp.arr[0]<<endl;
    cout<<"arr[n]: "<<temp.arr[temp.n]<<endl;*/
    //int test;
    int last=(temp.n)-1;

/*    if(temp.arr[0] > temp.arr[last])
    {
      test =0; //descending
    }
    else if(temp.arr[0] <temp.arr[last])
    {
            test=1; //ascending
    }*/

    int start =0;
    if((temp.n) == size -1 )
    {
      cout<<" maximum limit reached !! can not insert further !!"<<endl;
      return (-1);
    }

    bool choice;
    int flag = search(temp,data);

    // if exists already, promt user
    if(flag != (-1))
    {
      cout<<"already exists "<<endl;
      cout<<"insert anyway ? : no (0) / yes(1) : ";
      cin>> choice;
      if (choice == 0) return -2;
      else
      {
        shift_right(temp,flag+1,temp.n); // because flag holds the address
        temp.arr[flag+1]=data;
        cout<<"insertion done"<<endl;
        return 0;
      }

    }
  cout<<"insertion will be performed"<<endl;
  //int test=ascending(temp);


  if(ascending(temp)) // if it is an array of ascending order then do this
  {
    if(temp.arr[0]> data)  // if less than first data write at first
    {
      shift_right(temp,0,last);
      temp.arr[0]=data;
      cout<<"done.."<<endl;
      return 0;
    }
     if(data > temp.arr[last]) // if greater than last value write at last
    {
      last++; //update local last
      temp.n++; //update original value
      temp.arr[last]=data;
      cout<<"done !!"<<endl;
      return 0;
    }

    while (start <= last)
    {
      cout<<" asc loop"<<endl;
      if (data > temp.arr[start] && data < temp.arr[start + 1])
      {
        shift_right(temp,start+1,last);
        temp.arr[start +1]=data;
        cout<<"insertion done: "<<endl;
        return 0;
      }
        start++;
    }
  }
  else          // if desecnding ordered array
  {
    if(temp.arr[0]< data)  // if greater than first data write at first
    {
      shift_right(temp,0,last);
      temp.arr[0]=data;
      cout<<"done.."<<endl;
      return 0;
    }
     if(data < temp.arr[last]) //if less than last data write last
    {
      last++; //update local last
      temp.n++; //update original value
      temp.arr[last]=data;
      cout<<"done !!"<<endl;
      return 0;
    }
    while (start <= last)
    {
      cout<<" desc loop"<<endl;
      if (data < temp.arr[start] && data > temp.arr[start + 1] )
      {
        shift_right(temp,start,last);
        temp.arr[start+ 1]=data;
        cout<<"insertion done: "<<endl;
        return 0;
      }
      start++;
    }
  }

  cout<<"something error !!"<<endl;
  return -1;
    }

  ///////////////////////////////////////// shift_right //////////////////////////////////
  void shift_right(array &temp, int start, int end)
  {
    end++;
    temp.n++;
    while(end>start)
    {
      temp.arr[end]=temp.arr[end - 1];
      end--;
    }

  }

  ///////////////////////////////// ascending check ///////////////////////
 int ascending(array temp)
  {
    {
      if ( temp.arr[0]>temp.arr[(temp.n)-1]) return 0; // descending order
      else return 1;
    }
  }

/////////////////////////////////////// delete /////////////////////////

int remove(array &temp, int data)
{
  int end=(temp.n)-1;
  int loc=search(temp,data);
  if(loc==-1)
  {
    cout<<"can not delete the value , does not exist !!"<<endl;
    return -1;
  }
  shift_left(temp,loc,end);
  cout<<"deleted : "<<endl;
  return 0;

}



  /////////////////////////////// left shift ////////////////////////////

  void shift_left(array &temp, int start,int end)
  {
    while(start<=end)
    {
      temp.arr[start]=temp.arr[start+1];
      start++;
    }
    temp.n--;
  }
///////////////////////////////////// REVERSE /////////////////////
void reverse(array &temp) //reverse the order of the array
{
  int buff;
  int start=0;
  int last=(temp.n)-1;
  while(start<=last)
  {
    int buff=temp.arr[start]; //temporary buffered
    temp.arr[start]=temp.arr[last];
    temp.arr[last]=buff;  //swap with the buffer value
    start++;last--;
  }
}
////////////////////////////////////////////////////////////

array copy_array(array &source)
{
  array duplicate;
  duplicate=source;
  return duplicate;
}

int menu()
{
  int choice;
  cout<<"<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
  cout<<"00. EXIT"<<endl
      <<"01. CREATE AN ARRAY"<<endl
      <<"02. SHOW THE CONTENTS"<<endl
      <<"03. SERACH A VALUE"<<endl
      <<"04. INSERT A VALUE"<<endl
      <<"05. DELETE A VALUE"<<endl
      <<"06. REVERESE THE ARRAY"<<endl
      <<"07. CREATE ANOTHER ARRAY WITH SAME CONTENTS"<<endl;
      cout<<"<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;

  cout<<endl<<endl<<" now enter the value assigned to the function you want to run : ";
  cin>>choice;
  return choice;
}
///////////////////////////////////////////////////////////////////////////

  int main()
  {
    int choice;

    do {
      choice=menu();
      switch(choice)
      {
        case 00:
              exit(0);
              break;
        case 01:
              array arr1;
              arr1.set_values();
              break;
        case 02:
              arr1.show_values();
              break;
        case 03:
              cout << "enter the data to be searched: ";
              int data;
              cin>>data;
              search(arr1,data);
              break;
        case  04:
              cout<<"enter the data to be inserted: ";
              cin>>data;
              insert(arr1,data);
              break;
        case 05:
              cout<<"enter the data to be deleted :";
              cin>>data;
              remove(arr1,data);
              break;
        case 06:
              reverse(arr1);
              arr1.show_values();
              break;
        case 07:
              array arr2;
              arr2=copy_array(arr1); //returns an object of type array class
              arr2.show_values();
              break;
        default :
                cout<<"enter correct choice !!"<<endl;
      }
    } while(1); //choice !=0

    return 0;
  }

以上是关于c_cpp operations_on _unordered_array_using_class.cpp的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp _strlwr&_strupr

c_cpp 垫_

c_cpp -_-公元前

c_cpp uv__next_timeout.c

c_cpp uv__handle_unref.c

c_cpp uv__handle_ref.c