Monday, December 28, 2009

Line editor

Simple line editor print characters and manipulate using arrows also home and end


#include
#include
#include
#include
#define size 20
#define ENTER 13
#define ESC 27
#define RIGHT 77
#define LEFT 75
#define HOME 71
#define END 79

void main (void)
{
int flag=1;
char *pcur;
char *plast;
char ch;
int first=1,last=1,curr=1; //screen

char line[size];
textattr(0x79);
pcur=line;
plast=line;
do
{
gotoxy(curr,1);
ch=getch();
switch(ch)
{
case NULL:
ch=getch();
switch(ch)
{
case RIGHT: //right ..check for last then increment
if(curr!=last)
{
curr++;
pcur++;
}
break;

case LEFT: // left check for first then decrement
if(curr!=first)
{
curr--;
pcur--;
}
break;

case HOME: //home cur = first
curr=first;
pcur=line;
break;

case END: // end cur = last
curr=last;
pcur=plast;
break;
}

break;

case ENTER:
*plast='\0';
flag=0;
break;

case ESC:
*line='\0';
flag=0;
break;

default:
if(curr == last)
{
if(isprint(ch)&&last {
*pcur=ch;
plast++;
pcur++;
cprintf("%c",ch);
curr++;
last++;

}
}
else
{
*pcur=ch;
pcur++;
cprintf("%c",ch);
curr++;
}
//check if current == last
// check is printable
// if printable ( put char in array ,
// increment plast pcurrent
//cprintf for char
// increment current last

// current != last
// *cur = ch
// pcur ++; // memory
// cprintf("%c",ch) // screen
//cur ++ //for screen
break;

}

}while(flag!=0);
textattr(0x3f);
clrscr();
puts(line);
getch();
}

Sunday, December 27, 2009

Simple highlight menu using array of structure

A simple highlight menu to enter data in an array of structure and the size of the array is dynamically determined by the user also the index of the data to be entered will be determined by the user


#include
#include
#include
#include
#define size 2


struct Employee
{
int flag;
char name[30];
int age;
float salary;
float bonus;
float deduction;
};

void Display(struct Employee emps[]);
void NewEmployee(struct Employee emps[]);

void main(void)
{
struct Employee emps [size];
int pos=0,term=0,i;
char mych;
char menu[3][10]={"New","Display","Exit"};
for (i=0;i {
emps[i].flag=0;
}
do
{
textattr(0x0f);
clrscr();
printf("Menu");
for (i=0;i<3;i++)
{
textattr(0x0f);
if(pos==i)
textattr(0x79);
printf("\n");
cprintf("%s",menu[i]);
}
mych=getch();
switch(mych)
{
case 27://esc
exit(0);
case 9: //tab
pos ++;
if(pos>2)
pos= 0;
break;
case 13: //enter
switch(pos)
{
case 0:
clrscr();
//printf("you pressed new");
NewEmployee(emps);
getch();
break;
case 1:
clrscr();
//printf("you pressed display");
Display(emps);
break;
case 2:
exit(0);
}
break;
case NULL:
mych=getch();
switch(mych)
{
case 72:
pos--;
if(pos<0)
pos=2;
break;
case 80:
pos++;
if(pos>2)
pos=0;
break;
case 71:
pos=0;
break;
case 79:
pos=2;
break;
}
break;
}
}while(term==0);
getch();
}
void NewEmployee(Employee emps[])
{
int index;
printf("Enter index you want: ");
scanf("%d",&index);
if (index >size || index < 0)
{
printf("Enter valid number !!");
}
else if(emps[index-1].flag != 0)
{
printf("Cannot override data!! ") ;
}
else
{
emps[index-1].flag = 1; // Entering data
printf("\nEnter Employee name: ");
scanf("%s",emps[index-1].name);
printf("\nEnter Employee age: ");
scanf("%d",&emps[index-1].age);
printf("\nEnter Employee salary: ");
scanf("%f",&emps[index-1].salary);
printf("\nEnter Employee bonus: ");
scanf("%f",&emps[index-1].bonus);
printf("\nEnter Employee deduction: ");
scanf("%f",&emps[index-1].deduction);
}
}
void Display(Employee emps[])
{
int i;
for(i=0;i {
if(emps[i].flag!=0)
{
printf("\nEmployee name is: %s",emps[i].name);
printf("\nEmployee age is: %d",emps[i].age);
printf("\nEmployee salary is: %f",emps[i].salary);
printf("\nEmployee bonus is: %f",emps[i].bonus);
printf("\nEmployee deduction is: %f",emps[i].deduction);
}
}
getch();
}

Magic box

My first post ... small C program written on borland C to make magic box ...!
(Enter the size then it will create the magic box as the sum of any of its columns,rows and diagonal will be the same )


#include
#include
#include
void main(void)
{
int col=0,row=0,size=0,counter=1;
clrscr();
printf("Enter the size of the matrix: ");
scanf("%d",&size);
col =(size + 1) / 2;
row = 1;
gotoxy(col*3,row*3);
printf("%d",counter);
while (counter < size * size )
{
if (counter % size != 0)
{
col --;
row --;
if (col == 0) col = size;
if (row == 0) row = size;
gotoxy(col*3,row*3);
counter ++;
printf("%d",counter);


}
else
{
row ++;
if (row > size ) row = 1;
gotoxy(col*3,row*3);
counter ++;
printf("%d",counter);

}


}
getch();
}

About the Blog

This blog will be about various things, not for specific one. It may contain real life situations and it will have a great number of codes written in C , C++,Java,c# as it is my career. Also the new in software technology; programming, database, operating systems, cloud computing, and other technologies ....!
Hope all to enjoy this blog