Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. // hallo.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
  2. //
  3.  
  4. #include "stdafx.h"
  5. using namespace std;
  6. #include <iostream>
  7. int *array=NULL;
  8.  
  9. int *temparray= NULL;
  10. class bisearch
  11. {
  12. public:
  13.     int *array;
  14.     int key;
  15.     int arraygroesse;
  16.     bisearch();
  17.     ~bisearch();
  18.     bool insert(int key);
  19.     bool remove (int key);
  20.     int search(int key);
  21.     void enlarge();
  22.     void shrink();
  23.     int search_llb(int key);
  24.  
  25. };
  26.  
  27.  
  28. bisearch::bisearch()
  29. {
  30.     int arraygroesse;
  31.     int *array=NULL;
  32.     array = new int[0];
  33.     int *temparray=NULL;
  34.    
  35. }
  36.     bisearch::~bisearch()
  37.         {
  38.             delete []array;
  39. }
  40.     bool bisearch::insert(int key)
  41.         {
  42.             if(search(key)<0)
  43.             {
  44.                 bisearch::enlarge();
  45.                 int i = search_llb(key);
  46.                 for (int j= arraygroesse;j>i+1;j--) array[j]=array[j-1];
  47.                 array[i+1]=key;
  48.                 arraygroesse++;
  49.                 return 1;
  50.             }
  51.             else return 0;
  52.  
  53. }
  54.     bool bisearch::remove (int key)
  55.         {
  56.             int i = search(key);
  57.             if(i>-1)
  58.             {
  59.  
  60.                 for(int j = i; j<arraygroesse-1;j++) array[j]=array[j+1];
  61.                 shrink();
  62.                 return 1;
  63.             }
  64.             else return 0;
  65. }
  66.     int bisearch::search(int key)
  67.         {
  68.             int links = 0;
  69.             int rechts = arraygroesse-1;
  70.             while ( rechts >= links)
  71.             {
  72.             int mid = (links + rechts)/2;
  73.             if(key == array[mid]) return mid;
  74.             if(key < array[mid]) rechts = mid-1;
  75.             else links = mid+1;
  76.             }
  77.     return -1;
  78.  
  79.  
  80. }
  81.  
  82.     int bisearch::search_llb(int key)
  83.     {
  84.             int links = 0;
  85.             int rechts = arraygroesse-1;
  86.             while ( rechts >= links)
  87.             {
  88.             int mid = (links + rechts)/2;
  89.             if(links == rechts) return mid;
  90.             if(key < array[mid]) rechts = mid;
  91.             else links = mid+1;
  92.             }
  93.  
  94.  
  95.  
  96. }
  97.     void bisearch::enlarge()
  98.         {
  99.             arraygroesse++;
  100.             temparray = new int [arraygroesse];
  101.             for(int i = 0; i<arraygroesse; i++)
  102.             {
  103.                 temparray[i]=array[i];
  104.             }
  105.  
  106.             delete []array;
  107.             array = new int [arraygroesse];
  108.             for(int i = 0; i<arraygroesse; i++)
  109.             {
  110.                 array[i]=temparray[i];
  111.             }
  112.  
  113.            
  114.             delete []temparray;
  115.  
  116.  
  117.  
  118. }
  119.     void bisearch::shrink()
  120.         {
  121.             arraygroesse--;
  122.             temparray = new int [arraygroesse];
  123.             for( int i = 0; i<arraygroesse;i++)
  124.             {
  125.                 temparray[i]=array[i];
  126.             }
  127.  
  128.             delete []array;
  129.             array = new int [arraygroesse];
  130.             for(int i = 0; i<arraygroesse; i++)
  131.             {
  132.                 array[i]=temparray[i];
  133.             }
  134.  
  135.            
  136.             delete []temparray;
  137.  
  138. }
  139.  
  140.  
  141.  
  142. int main()
  143. {
  144.     bisearch array;
  145.     char schleife;
  146.      int key = 5;
  147.     int arraygroesse = 0;
  148.     cout<<"geben Sie die zahlen ein die Sie im Array wollen"<<endl;
  149.     cout<<"mit a brechen sie ab"<<endl;
  150.     array.insert(key);
  151.  
  152.  
  153.  
  154.        
  155.    
  156.     for(int i = 0; i<arraygroesse;i++)
  157.     {
  158.         cout<<array[i];
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement