RSS

Network Monitor in C#

Ever thought about checking the internet uploading/downloading speed via C#? Well this network monitor can show you how!

Include using System.Net.NetworkInformation; in the .cs file
Drag 5 Labels onto the form.
Drag a timer and change the Enabled property to True and the Interval to 1000.
Intialize the following global variables:

NetworkInterface networkInterface;
long lngBytesSend = 0;
long lngBtyesReceived = 0;

Write the following code in the constructor:

foreach (NetworkInterface currentNetworkInterface in NetworkInterface.GetAllNetworkInterfaces())
if (currentNetworkInterface.Name.ToLower() == “local area connection”)
{
networkInterface = currentNetworkInterface;
break;
}
ShowNetworkInterfaceTraffice();

In the timer’s tick event call the function for showing the network traffic “ShowNetworkInterfaceTraffice()“.

Write the following code in the ShowNetworkInterfaceTraffice() function:

private void ShowNetworkInterfaceTraffice()
{
IPv4InterfaceStatistics interfaceStatistic = networkInterface.GetIPv4Statistics();
double bytesSentSpeed = (double)(interfaceStatistic.BytesSent – lngBytesSend) / 1024;
double bytesReceivedSpeed = (double)(interfaceStatistic.BytesReceived – lngBtyesReceived) / 1024;
labelSpeed.Text = (networkInterface.Speed / 1000000).ToString() + ” Mbps”;
labelReceived.Text = interfaceStatistic.UnicastPacketsReceived.ToString();
labelSent.Text = interfaceStatistic.UnicastPacketsSent.ToString();
labelUploading.Text = bytesSentSpeed.ToString() + ” KB/s”;
labelDownloading.Text = bytesReceivedSpeed.ToString() + ” KB/s “;
lngBytesSend = interfaceStatistic.BytesSent;
lngBtyesReceived = interfaceStatistic.BytesReceived;
}

You can download the project from the Box on the right!

 
Leave a comment

Posted by on June 29, 2011 in C#

 

Sending Emails Through C#

Here’s how you can develop your own Email sender in C# Windows Application with the inbuilt SMTP protocol using System.Net.Mail


3 Prerequisites:
  • Search for your Email Service Provider’s SMTP Server Address.
  • The Server Port Number
  • Configure your account settings to enable POP service so that the application is able to send mails via your account.

(POP is for outgoing mails and IMAP is for incoming mails. Since we are going to create an email sender and not an email receiver we need only POP enabled.)


Here we wil take the example of GMAIL,
  • Server Address: smtp.gmail.com
  • Server Port: 587
  • For account configuration login to Gmail, goto Settings, goto Forwarding and POP/IMAP, and Check on “Enable POP for all mail” and save the settings.




1. Now create a new windows application
2. Drag and drop the following controls from the toolbar:
  • 7 Text Boxes
  • 1 Rich Text Box
  • 2 Buttons
  • 8 Labels

3. Text boxes are renamed as:
  • textBoxId
  • textBoxPassword
  • textBoxSendTo
  • textBoxSubject
  • textBoxAttachment
  • textBoxAddr
  • textBoxPort

4. Buttons are renamed as:
  • buttonBrowse
  • buttonSend

5. Rich Text Box is renamed as richTextBoxMessage.
6. Provide a Password Char “*” (or any other) for the textBoxPassword.

7. Move the controls to give your form the following look:



8. Include the following line on the top of the .cs file:
using System.Net.Mail

9. At the buttonBrowse click event place the following code:
openFileDialog1.ShowDialog();
textBoxAttachment.Text = openFileDialog1.FileName;


This will open up file browser dialogue for selecting an attachment, and the location of selected file is put into the textbox.

10. At the buttonSend click event place the following code:

try{
SmtpClient client = new SmtpClient(textBoxAddr.Text);
MailMessage message = new MailMessage(textBoxId.Text, textBoxSendTo.Text);
message.Body = richTextBoxMessage.Text;
message.Subject = textBoxSubject.Text;
if (textBoxAttachment.Text != “”)
{ message.Attachments.Add(new Attachment(textBoxAttachment.Text)); }
client.Credentials = new System.Net.NetworkCredential(textBoxId.Text, textBoxPassword.Text);
client.Port = Convert.ToInt32(textBoxPort.Text);
client.Send(message);
MessageBox.Show(“Message delivered successfully!!!”, “ashKAP Mail Sender”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (Exception ex)
{
MessageBox.Show(“Message could not be sent.\nThe following error was returned:\n'” + ex.Message + “‘”, “ashKAP Mail Sender”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}



Run the project and start mailing!!!!!!!!
Download the ashKAP Mail Sender Project from the Box on the right side.
(The project is made in Visual Studio 2010 and will not open in lower versions)

Here’s the software I made “ashKAP Prism”

 
Leave a comment

Posted by on March 29, 2011 in C#

 

Sending SMS via C# using Way2Sms

Create a new windows application.
Drag and drop 3 text boxes, 1 rich text box, 4 label, 1 button onto the form.
Rename the textboxes as following:
  • textbox1—>textBoxId
  • textbox2—>textBoxPassword
  • textbox3—>textBoxSendTo
  • richTextbox1—>richTextBoxText
  • button1—->buttonSend

In the solution explorer right cick on the Reference and add the following reference to the project:




Include the following lines:
using System;
using SMSClientLib;
using System.Net;


Double click on the button and add the following snippet to the click event:
string status = “”;
CookieContainer cookie = Login.Connect(textBoxId.Text, textBoxPassword.Text, out status);
string[] siteParameters = Login.GetSiteParameters(cookie);
string messgeSentResult = SendSMS.Send_Processing(textBoxSendTo.Text, richTextBoxText.Text, cookie, siteParameters);

 

Here’s the ashKAP Texter:

 
11 Comments

Posted by on March 26, 2011 in C#

 

C Sharp: How to make your own Battery Monitor

My laptop’s battery is totally screwed, it gets shut down within a minute once the power is off… often when i’m working the charger plug gets disturbed and come out a bit and the power is cut off and I don’t even get to know!

Unfortunately the computer suddenly shuts down while I’m working and all my data and progress is lost 😦

I thus tried to make my own Battery Monitor to tackle this problem. Now a popup window appears as soon as my laptop comes on battery!!!

Here’s the way… try out making one for your PC too…



C# Battery Monitor:
1). Create a new C# windows application project in Visual Studio.
2). Resize the form to 500×280 px
3). From the properties box make the values of MaximizeBox and MinimizeBox equal to false.
4). Drag the following controls from the toolbox(press Ctrl+Alt+X to show toolbox):
a). 1 Button
b). 1 Check Box
c). 1 Timer
d). 1 Progress Bar
e). 5 Labels
g). 1 Group Box
h). 2 Picture Box
5). Apply two different images to your picture box one showing a charging battery and another a discharging battery.
6). Rename the controls as follows:
label1—>BatteryStatus
label2—>ChargeRemaining
label3—>labelAlert
progressBar1—>BatteryIndicator
button1—>buttonOK
checkBox1—>chkRun
pictureBox1—>pictureBoxBattery
pictureBox2—>pictureBoxCharging

7). Arrange all the controls to give your form the following look:

8). Note the the 2 picture boxes overlap each other.
9). Write the following code:
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Media;

namespace Battery
{
public partial class Battery : Form
{
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(“SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run”, true);
PowerStatus power = SystemInformation.PowerStatus;
int x,y;
public Battery()
{
InitializeComponent();
if (rkApp.GetValue(“MyApp”) == null)
{chkRun.Checked = false;}
else
{chkRun.Checked = true;}
float secondsRemaining = power.BatteryLifePercent;
if (secondsRemaining >= 0)
{
ChargeRemaining.Text = (secondsRemaining * 100).ToString() + “% available.”;
}
else
{
ChargeRemaining.Text = string.Empty;
}
}

private void Battery_Load(object sender, EventArgs e)
{
RefreshStatus();
timer1.Enabled = true;
y = this.Left;
}

private void RefreshStatus()
{
int powerPercent = (int)(power.BatteryLifePercent * 100);
if (powerPercent <= 100) BatteryIndicator.Value = powerPercent; else BatteryIndicator.Value = 0; switch (power.PowerLineStatus)
{
case PowerLineStatus.Online: ;
groupBox1.Text = “Running On Mains”;
x = 1;
labelAlert.ForeColor = Color.Chartreuse;
labelAlert.Text=”Plugged In”;
pictureBoxCharging.Visible = true;
pictureBoxBattery.Visible = false;
float secondsRemaining = power.BatteryLifePercent;
if (secondsRemaining >= 0)
{
ChargeRemaining.Text = (secondsRemaining * 100).ToString() + “% available.”;
}
else
{
ChargeRemaining.Text = string.Empty;
}
BatteryStatus.Text = power.BatteryChargeStatus.ToString();
break;

case PowerLineStatus.Offline:


groupBox1.Text = “Running On Battery”;
if (x == 1)
{
SoundPlayer simpleSound = new SoundPlayer(@”c:\Windows\Media\Windows Exclamation.wav”);
simpleSound.Play();
labelAlert.ForeColor = Color.Red;
labelAlert.Text = “On Battery”;
pictureBoxCharging.Visible = false;
pictureBoxBattery.Visible = true;
this.WindowState = FormWindowState.Normal;
this.Left = y;
BatteryStatus.Text = power.BatteryChargeStatus.ToString();
}
x = 0;
break;

case PowerLineStatus.Unknown:

break;
}

}

private void timer1_Tick(object sender, EventArgs e)
{
RefreshStatus();
}

private void buttonOK_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}

private void chkRun_CheckedChanged(object sender, EventArgs e)
{
if (chkRun.Checked)
{
rkApp.SetValue(“MyApp”, Application.ExecutablePath.ToString());
}
else
{
rkApp.DeleteValue(“MyApp”, false);
}
}
}
}

 

Run your application! 🙂
While charging:

While running on battery:


 
Leave a comment

Posted by on March 8, 2011 in C#

 

Binary Search Tree (BST)

Binary Search Tree (BST)

#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int info;
struct node *left;
struct node *right;
struct node *p;
};
node *root,*deep,*x;
int n,lr;
void insert();
void op();
void kl();
void in(struct node*);
void pre(struct node*);
void post(struct node*);
void main()
{
root=NULL;
op();
}
void op()
{
int f;
printf(“\npress 1 for enter element\npress 2 for left rotation\npress 3 for print preorder\n press 4 for exit”);
scanf(“%d”,&f);
switch(f)
{
case 1:kl();
break;
case 2:leftrotate();
break;
case 3:pre(root);
break;
case 4:exit(1);
default : op();
break;
}
}
void kl()
{
printf(“enter element”);
scanf(“%d”,&n);
if(root==NULL)
{
root=(node*)malloc(sizeof(node));
root->info=n;
root->left=NULL;
root->right=NULL;
root->p=NULL;
}
else
{
deep=root;
insert();
}
op();
}
void insert()
{
node *ptr,*rtf;
ptr=(node*)malloc(sizeof(node));
ptr->info=n;
if(n<deep->info)
{
while(deep->left!=NULL)
{
rtf=deep->left;
if(rtf->info<ptr->info)
break;
deep=deep->left;
}
if(deep->left==NULL)
{
ptr->left=NULL;
ptr->right=NULL;
deep->left=ptr;
}
else if(rtf->info<ptr->info)
{
deep=deep->left;
insert();
}
}
else if(n>deep->info)
{
while(deep->right!=NULL)
{
rtf=deep->right;
if(rtf->info>ptr->info)
break;
deep=deep->right;
}
if(deep->right==NULL)
{
ptr->left=NULL;
ptr->right=NULL;
deep->right=ptr;
ptr->p=deep;}
else if(rtf->info>ptr->info)
{
deep=deep->right;
insert();
}
}
op();
}
void pre(struct node *root)
{
if(root!=NULL)
{
printf(“%d\t”,root->info);
pre(root->left);
pre(root->right);
}
}
 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

AVL Trees

Program for insertion in AVL tree


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<process.h>
#include<malloc.h>

typedef enum { FALSE,TRUE } bool;
struct node
{
int info;
int balance;
struct node *lchild;
struct node *rchild;
bool ht_inc;
};

struct node *insert (int , struct node *);
struct node* search(struct node *,int);

main()
{

int info ;
int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;

while(1)
{
printf(“1.Insert\n”);
printf(“2.Display\n”);
printf(“3.Quit\n”);
printf(“Enter your choice : “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
printf(“Enter the value to be inserted : “);
scanf(“%d”, &info);
if( search(root,info) == NULL )
root = insert(info, root);
else
printf(“Duplicate value ignored\n”);
break;
case 2:
if(root==NULL)
{
printf(“Tree is empty\n”);
continue;
}
printf(“Tree is :\n”);
display(root, 1);
printf(“\n\n”);
printf(“Inorder Traversal is: “);
inorder(root);
printf(“\n”);
break;
case 3:
exit(1);
default:
printf(“Wrong choice\n”);
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

struct node* search(struct node *ptr,int info)
{
if(ptr!=NULL)
if(info < ptr->info)
ptr=search(ptr->lchild,info);
else if( info > ptr->info)
ptr=search(ptr->rchild,info);
return(ptr);
}/*End of search()*/

struct node *insert (int info, struct node *pptr, int *ht_inc)
{
struct node *aptr;
struct node *bptr;

if(pptr==NULL)
{
pptr = (struct node *) malloc(sizeof(struct node));
pptr->info = info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
*ht_inc = TRUE;
return (pptr);
}

if(info < pptr->info)
{
pptr->lchild = insert(info, pptr->lchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case -1: /* Right heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = 1;
break;
case 1: /* Left heavy */
aptr = pptr->lchild;
if(aptr->balance == 1)
{
printf(“Left to Left Rotation\n”);
pptr->lchild= aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf(“Left to right rotation\n”);
bptr = aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild = aptr;
pptr->lchild = bptr->rchild;
bptr->rchild = pptr;

if(bptr->balance == 1 )
pptr->balance = -1;
else
pptr->balance = 0;
if(bptr->balance == -1)
aptr->balance = 1;
else
aptr->balance = 0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc = FALSE;
}/*End of switch */
}/*End of if */
}/*End of if*/

if(info > pptr->info)
{
pptr->rchild = insert(info, pptr->rchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case 1: /* Left heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = -1;
break;
case -1: /* Right heavy */
aptr = pptr->rchild;
if(aptr->balance == -1)
{
printf(“Right to Right Rotation\n”);
pptr->rchild= aptr->lchild;
aptr->lchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf(“Right to Left Rotation\n”);
bptr = aptr->lchild;
aptr->lchild = bptr->rchild;
bptr->rchild = aptr;
pptr->rchild = bptr->lchild;
bptr->lchild = pptr;

if(bptr->balance == -1)
pptr->balance = 1;
else
pptr->balance = 0;
if(bptr->balance == 1)
aptr->balance = -1;
else
aptr->balance = 0;
bptr->balance=0;
pptr = bptr;
}/*End of else*/
*ht_inc = FALSE;
}/*End of switch */
}/*End of if*/
}/*End of if*/

return(pptr);
}/*End of insert()*/

void display(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf(“\n”);
for (i = 0; i < level; i++)
printf(” “);
printf(“%d”, ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/

void inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf(“%d “,ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/
 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

B-Tree of Degree 3

Program which maintains a B-tree of degree 3.


#include <iostream.h>
#include <stdlib.h>
#include <alloc.h>
const int MAX = 5 ;
const int MIN = 3 ;
struct btnode
{int count ;
int value[MAX + 1] ;
btnode *child[MAX + 1] ;
} ;

class btree
{private :
btnode *root ;
public :
btree( ) ;
void insert ( int val ) ;
int setval ( int val, btnode *n, int *p, btnode **c ) ;
static btnode * search ( int val, btnode *root, int *pos ) ;
static int searchnode ( int val, btnode *n, int *pos ) ;
void fillnode ( int val, btnode *c, btnode *n, int k ) ;
void split ( int val, btnode *c, btnode *n,
int k, int *y, btnode **newnode ) ;
void del ( int val ) ;
int delhelp ( int val, btnode *root ) ;
void clear ( btnode *root, int k ) ;
void copysucc ( btnode *root, int i ) ;
void restore ( btnode *root, int i ) ;
void rightshift ( int k ) ;
void leftshift ( int k ) ;
void merge ( int k ) ;
void show( ) ;
static void display ( btnode *root ) ;
static void deltree ( btnode *root ) ;
~btree( ) ;
} ;

// initialises data member
btree :: btree( )
{root = NULL ;
}
// inserts a value in the B-tree
void btree :: insert ( int val )
{
int i ;
btnode *c, *n ;
int flag ;
flag = setval ( val, root, &i, &c ) ;
if ( flag )
{
n = new btnode ;
n -> count = 1 ;
n -> value[1] = i ;
n -> child[0] = root ;
n -> child[1] = c ;
root = n ;
}
}
// sets the value in the node
int btree :: setval ( int val, btnode *n, int *p, btnode **c )
{
int k ;
if ( n == NULL )
{
*p = val ;
*c = NULL ;
return 1 ;
}
else
{
if ( searchnode ( val, n, &k ) )
cout << endl << “Key value already exists.” << endl ;
if ( setval ( val, n -> child[k], p, c ) )
{
if ( n -> count < MAX )
{
fillnode ( *p, *c, n, k ) ;
return 0 ;
}
else
{split ( *p, *c, n, k, p, c ) ;
return 1 ;
}
}
return 0 ;
}
}

// searches value in the node
btnode * btree :: search ( int val, btnode *root, int *pos )
{
if ( root == NULL )
return NULL ;
else
{
if ( searchnode ( val, root, pos ) )
return root ;
else
return search ( val, root -> child[*pos], pos ) ;
}
}
// searches for the node
int btree :: searchnode ( int val, btnode *n, int *pos )
{
if ( val < n -> value[1] )
{
*pos = 0 ;
return 0 ;
}
else
{
*pos = n -> count ;
while ( ( val < n -> value[*pos] ) && *pos > 1 )
( *pos )– ;
if ( val == n -> value[*pos] )
return 1 ;
else
return 0 ;
}
}

// adjusts the value of the node
void btree :: fillnode ( int val, btnode *c, btnode *n, int k )
{
int i ;
for ( i = n -> count ; i > k ; i– )
{
n -> value[i + 1] = n -> value[i] ;
n -> child[i + 1] = n -> child[i] ;
}
n -> value[k + 1] = val ;
n -> child[k + 1] = c ;
n -> count++ ;
}

// splits the node
void btree :: split ( int val, btnode *c, btnode *n,
int k, int *y, btnode **newnode )
{
int i, mid ;

mid=MIN;
*newnode = new btnode ;
for ( i = mid + 1 ; i <= MAX ; i++ )
{
( *newnode ) -> value[i – mid] = n -> value[i] ;
( *newnode ) -> child[i – mid] = n -> child[i] ;
}
( *newnode ) -> count = MAX – mid ;
n -> count = mid ;
if ( k <= MIN )
fillnode ( val, c, n, k ) ;
else
fillnode ( val, c, *newnode, k – mid ) ;
*y = n -> value[n -> count] ;
( *newnode ) -> child[0] = n -> child[n -> count] ;
n -> count– ;
}

// deletes value from the node
void btree :: del ( int val )
{
btnode * temp ;
if ( ! delhelp ( val, root ) )
cout << endl << “Value ” << val << ” not found.” ;
else
{
if ( root -> count == 0 )
{
temp = root ;
root = root -> child[0] ;
delete temp ;
}
}
}
// helper function for del( )
int btree :: delhelp ( int val, btnode *root )
{
int i ;
int flag ;
if ( root == NULL )
return 0 ;
else
{
flag = searchnode ( val, root, &i ) ;
if ( flag )
{
if ( root -> child[i – 1] )
{
copysucc ( root, i ) ;
flag = delhelp ( root -> value[i], root -> child[i] ) ;
if ( !flag )
cout << endl << “Value ” << val << ” not found.” ;
}
else
clear ( root, i ) ;
}
else
flag = delhelp ( val, root -> child[i] ) ;
if ( root -> child[i] != NULL )
{
if ( root -> child[i] -> count < MIN )
restore ( root, i ) ;
}
return flag ;
}
}
// removes the value from the node and adjusts the values
void btree :: clear ( btnode *root, int k )
{
int i ;
for ( i = k + 1 ; i <= root -> count ; i++ )
{
root -> value[i – 1] = root -> value[i] ;
root -> child[i – 1] = root -> child[i] ;
}
root -> count– ;
}
// copies the successor of the value that is to be deleted
void btree :: copysucc ( btnode *root, int i )
{btnode *temp = root -> child[i] ;
while ( temp -> child[0] )
temp = temp -> child[0] ;
root -> value[i] = temp -> value[1] ;
}
// adjusts the node
void btree :: restore ( btnode *root, int i )
{
if ( i == 0 )
{
if ( root -> child [1] -> count > MIN )
leftshift ( 1 ) ;
else
merge ( 1 ) ;
}
else
{
if ( i == root -> count )
{
if ( root -> child[i – 1] -> count > MIN )
rightshift ( i ) ;
else
merge ( i ) ;
}
else
{
if ( root -> child[i – 1] -> count > MIN )
rightshift ( i ) ;
else
{
if ( root -> child[i + 1] -> count > MIN )
leftshift ( i + 1 ) ;
else
merge ( i ) ;
}
}
}
}
// adjusts the values and children while shifting the value from parent to right child
void btree :: rightshift ( int k )
{
int i ;
btnode *temp ;
temp = root -> child[k] ;
for ( i = temp -> count ; i > 0 ; i– )
{
temp -> value[i + 1] = temp -> value[i] ;
temp -> child[i + 1] = temp -> child[i] ;
}
temp -> child[1] = temp -> child[0] ;
temp -> count++ ;
temp -> value[1] = root -> value[k] ;
temp = root -> child[k – 1] ;
root -> value[k] = temp -> value[temp -> count] ;
root -> child[k] -> child [0] = temp -> child[temp -> count] ;
temp -> count– ;
}
// adjusts the values and children while shifting the value from parent to left child
void btree :: leftshift ( int k )
{btnode *temp ;
temp = root -> child[k – 1] ;
temp -> count++ ;
temp -> value[temp -> count] = root -> value[k] ;
temp -> child[temp -> count] = root -> child[k] -> child[0] ;
temp = root -> child[k] ;
root -> value[k] = temp -> value[1] ;
temp -> child[0] = temp -> child[1] ;
temp -> count– ;
for ( int i = 1 ; i <= temp -> count ; i++ )
{temp -> value[i] = temp -> value[i + 1] ;
temp -> child[i] = temp -> child[i + 1] ;
}
}
// merges two nodes
void btree :: merge ( int k )
{btnode *temp1, *temp2 ;
temp1 = root -> child[k] ;
temp2 = root -> child[k – 1] ;
temp2 -> count++ ;
temp2 -> value[temp2 -> count] = root -> value[k] ;
temp2 -> child[temp2 -> count] = root -> child[0] ;
for ( int i = 1 ; i <= temp1 -> count ; i++ )
{
temp2 -> count++ ;
temp2 -> value[temp2 -> count] = temp1 -> value[i] ;
temp2 -> child[temp2 -> count] = temp1 -> child[i] ;
}
for ( i = k ; i < root -> count ; i++ )
{
root -> value[i] = root -> value[i + 1] ;
root -> child[i] = root -> child[i + 1] ;
}
root -> count– ;
delete temp1 ;
}
// calls display( )
void btree :: show( )
{
display ( root ) ;
}
// displays the B-tree
void btree :: display ( btnode *root )
{
if ( root != NULL )
{
for ( int i = 0 ; i < root -> count ; i++ )
{
display ( root -> child[i] ) ;
cout << root -> value[i + 1] << “\t” ;
}
display ( root -> child[i] ) ;
}
}
// deallocates memory
void btree :: deltree ( btnode *root )
{if ( root != NULL )
{
for ( int i = 0 ; i < root -> count ; i++ )
{
deltree ( root -> child[i] ) ;
delete ( root -> child[i] ) ;
}
deltree ( root -> child[i] ) ;
delete ( root -> child[i] ) ;
}
}
btree :: ~btree( )
{
deltree ( root ) ;
}

void main( )
{
btree b ;
int n,m;
while(1)
{
cout<<“\nwhat u want\ninsertion\n2-deletion\n3-display”;
cin>>n;
if(n==1)
{cout<<“\nenter the no to insert”;
cin>>m;
b.insert(m);
}
if(n==2)
{cout<<“enter the no to delete:”;
cin>>m;
b.del(m);
}
if(n==3)
b.show();

}


}
 
Leave a comment

Posted by on March 8, 2011 in C/C++