$(document).ready(function() { $('pre code').each(function(i, block) { hljs.highlightBlock(block); }); });

Sunday, July 11, 2010

Button for graphics program















#define HOLLOW 0
#define FILLED 1
#define INACTIVE 0
#define NORMAL 1
#define ACTIVE 2
#define SETACTIVE 3

//----------------------Button functions----------------------------//
void button(int,int,int,int);
void colorbtn(int,int,int,int);
void mainbody(int,int,int);
void finishkey(int,int);
//------------------------------------------------------------------//


void CircularBar(int x1,int y1,int x2,int y2,int TYPE,int color,int border);

////-----------------------------------------------------------------//
//Save this file as 'buttons.h' in 'bin' directory of 'tc'

#include
#include"buttons.h"
char readfile[50],writefile[50];

//---------------------BUTTON FUNCTIONS--------------------------------------//
void button(int x,int y,int len,int k)
{
int i;
//Creating Buttons
for(i=0;i
{
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x,y,x+len,y+20);
setcolor(BLACK);
rectangle(x,y,x+len,y+20);
x=x-(len+10);
}
}

void colorbtn(int a,int b,int len,int status)
{
//0=Inactive button,1=Normal Button,2=Active button,3=All time active
if(status==INACTIVE)
setcolor(DARKGRAY);
else
setcolor(BLACK);
rectangle(a,b,a+len,b+20);

if(status==SETACTIVE)
setcolor(WHITE);
else if(status==ACTIVE)
setcolor(DARKGRAY);
else
setcolor(LIGHTGRAY);
rectangle(a+1,b+1,a+(len-1),b+19);

if(status==ACTIVE)
setcolor(WHITE);
else
setcolor(LIGHTGRAY);
line(a+2,b+2,a+(len-2),b+2);
line(a+2,b+2,a+2,b+18);
line(a+3,b+3,a+(len-3),b+3);
line(a+3,b+3,a+3,b+18);

if(status==ACTIVE)
setcolor(DARKGRAY);
else
setcolor(LIGHTGRAY);
line(a+3,b+18,a+(len-3),b+18);
line(a+(len-2),b+18,a+(len-2),b+3);
}
//*******************BUTTON FUNCTIONS END HERE*************************//

//---------------------------------------------------------------------//
//*******************Circular-Edge bar function************************//
void CircularBar(int x1,int y1,int x2,int y2,int TYPE,int color,int border)
{
int len,i,j;
int x[4],y[4];
len=(y2-y1)/10;

x[0]=x2-len,y[0]=y1+len;
x[1]=x1+len,y[1]=y1+len;
x[2]=x1+len,y[2]=y2-len;
x[3]=x2-len,y[3]=y2-len;

if(TYPE==HOLLOW)
{
color=getpixel(x1-1,y1-1);
if(color==WHITE)
border=BLACK;
else
border=WHITE;
}

for(i=0;i<4;i++)
{
for(j=0;j<=len;j++)
{
if(j==len)
setcolor(border);
else
setcolor(color);
circle(x[i],y[i],j);
}
}
setcolor(border);
line(x1,y1+len,x1,y2-len);
line(x1+len,y1,x2-len,y1);
line(x2,y1+len,x2,y2-len);
line(x1+len,y2,x2-len,y2);
setfillstyle(SOLID_FILL,color);
bar(x1+1,y1+len,x2-1,y2-len);
bar(x1+len,y1+1,x2-len,y1+len);
bar(x1+len,y2-len,x2-len,y2-1);
}

///Include this file in your programs and call its functions to produce animation effects.
//Remember, u'll hav to create logic to produce animation effects as i hav done in some of my programs.

Addition using recursion

#include
#include
int add(int);
void main()
{
int num,sum=0;
clrscr();
printf("How many no.s do u wanna add:");
scanf("%d",&num);
printf("\nEnter the no.s here:");
sum=add(num);
printf("The sum of the no.s is %d",sum);
getch();
}

int add(int x)
{
int n;
scanf("%d",&n);
if(x==0)
return(0);
else if(x==1)
return(n);
else
return(n+add(x-1));
}

cofactor value of matrix

#include
#include
#include
int calculate(int);
int cofactor();
void main()
{
unsigned int m[25][25],t[24][25][25],c[25][25];
int i,j,p,q,x,y,or;
unsigned int value;
printf("Enter the order of the matrix:");
scanf("%d",&or);
printf("Enter the elements of the matrix:");
for(i=0;i for(j=0;j {
printf("\nEnter element[%d][%d]:",i+1,j+1);
scanf("%d",&m[i][j]);
}

if(or==0)
value=0;
else if(or==1)
value=m[0][0];
else if(or==2)
value+=calculate(0);
else
value+=cofactor();
printf("\nCofactor of the determinant is:");
for(i=0;i {
printf("\n|");
for(j=0;j printf("%d ",m[i][j]);
printf("|");
}
}

calculate(int docalctn)
{
if(docalctn==0)
return 0;
}

cofactor()
{
return 0;
}Cofa

Determinant of a matrix

#include
#include
#include
static int count=0;
int mat[25][25][25];
int det(int order,int mat);
int cofactor(int mat,int order,int i,int j);
void main()
{
int i,j,order,val=0;
clrscr();

printf("Enter the order of the Matrix(Square matrix only):");
scanf("%d",&order);
printf("\n");
for(i=0;i for(j=0;j {
printf("Enter element [%d][%d]:",i+1,j+1);
scanf("%d",&mat[0][i][j]);
}
if(order==0)
{
printf("Sorry, A matrix must have atleast one element.");
getch();
main();
}
else if(order==1)
val=mat[0][0][0];
else
val=det(order,count);
printf("\nDeterminant of the Matrix is: %d",val);
getch();
}

int det(int order,int term)
{
int i,j,val=0;

if(order==2)
val=mat[term][0][0]*mat[term][1][1]-(mat[term][0][1]*mat[term][1][0]);
else
for(i=0;i val+=pow(-1,i+j+2)*(mat[term][i][0]*cofactor(term,order,i,0));
return(val);
}

int cofactor(int term,int order,int i,int j)
{
int value=0;
int p,q,x,y;
int neworder;
neworder=order-1;
count++;

for(x=0,p=0;p {
if(p==i)
x++;
for(y=0,q=0;q {
if(q==j)
y++;
mat[count][p][q]=mat[loc][x][y];
}
}
value=det(neworder,count);
return(value);
}

Character counting program

#include
#include
#include
void main()
{
FILE *fp;
int count_space=0,count_chars=0,count_tab=0,count_line=0;
char name[20],fname[20],str;
clrscr();
printf("\nEnter the name of text file to be created:\n");
gets(name);
strcpy(fname,name);
strcat(fname,".txt");

if((fp=fopen(fname,"w"))==NULL)
{
printf("\n\tSorry, file could not be created.");
printf("\n\tProgram will exit now.");
delay(2000);
exit(0);
}
else
{
printf("\nA file with name '%s' has been created.",fname);
printf("Now enter the string please.");
printf("\n(Press 'Esc' to quit entering string):\n");
do
{
if((str=getch())!='\r')
{
printf("%c",str);
if(str==' ')
str='-';
}
else
{
printf("\n");
str='|';
}
putc(str,fp);
}while(str!=27);
///Esc key
fclose(fp);
}
fp=fopen(fname,"r");
if(fp==NULL)
{
printf("\nSorry, File Read Error.The program will exit now.");
delay(2000);
exit(0);
}
else
{
while(!feof(fp))
{
str=getc(fp);
count_chars++;
if(str=='-')
count_space++;
else if(str=='\t')
count_tab++;
else if(str=='|')
count_line++;
}
count_chars-=count_space;
count_chars-=count_tab;
count_chars-=count_line;
count_chars-=2;
printf("\n\t-------------------------------");
printf("\n\tDetails");
printf("\n\t-------------------------------");
printf("\n\tFilename: %s",fname);
printf("\n\tNo. of Characters in file: %d characters (alphabets+other characters),",count_chars);
printf("\n\tNo. of Spaces in file: %d spaces,",count_space);
printf("\n\tNo. of Tabs in file: %d tabs,",count_tab);
printf("\n\tNo. of Newlines in file: %d newlines.",count_line);
}
getch();
}

Data copying program

#include
#include
void main()
{
char oldfname[50],newfname[50],x,ch;
FILE *fp1,*fp2;
clrscr();

printf("------------Program to copy data from one file to another------------");
printf("\nEnter the source/name of source file:");
gets(oldfname);
fp1=fopen(oldfname,"r");
if(fp1==NULL)
{
printf("\nSorry, No such files exist.The program will exit now.");
getch();
exit(0);
}
else
{
printf("\nEnter the name of destination file:");
gets(newfname);
fp2=fopen(newfname,"w");
while(!feof(fp1))
{
x=getc(fp1);
putc(x,fp2);
}
printf("\nData from '%s' has been copied to '%s'.",oldfname,newfname);
printf("\nDo you want to see it?(y/n):");
ch=getch();
printf("%c\n",ch);
if(ch=='Y'||ch=='y')
{
rewind(fp2);
while(!feof(fp2))
printf("%c",getc(fp2));
}
else
printf("\nOk, the program will exit now.");
}
getch();
}

File creation program

#include
#include
void main()
{
FILE *fp;
char ch,fname[20];
clrscr();

printf("Enter the name of the file to be created:");
gets(fname);
fp=fopen(fname,"w");
printf("Now hit any key to store it in file.");
printf("(Press 'Esc' to break):\n");
do
{
printf("%c",(ch=getch()));
if(ch==' ')
ch='-';
if(ch=='\r')
printf("\n");
putc(ch,fp);
}while(ch!=27);
fclose(fp);
printf("\nData has been saved into file.Do you want to see it?(y/n):");
printf("%c",(ch=getch()));
if(ch=='Y'||ch=='y')
{
printf("\n");
fp=fopen(fname,"r");
while(!feof(fp))
{
ch=getc(fp);
if(ch=='-')
ch=' ';
printf("%c",ch);
}
}
else
printf("\nOk Thanks! The program will exit now.");
getch();
}

Matrix Transpose (using array)

#include
#include
void main()
{
int i,j,m,n,mat[9][9];
clrscr();
printf("Enter the order of matrix:\n");
scanf("%d%d",&m,&n);
printf("Enter the matrix terms:\n\n");
for(i=0;i{
for(j=0;j{
gotoxy(10+4*j,5+2*i);
scanf("%d",&mat[i][j]);
}}
printf("\nThe original matrix is:\n\n");
for(i=0;i{
printf("\t|");
{
for(j=0;jprintf("%3d",mat[i][j]);
}
printf("|\n");
}
printf("\nThe Transpose of the above matrix is:\n\n");
for(j=0;j{
printf("|");
for(i=0;i{
printf("%3d",mat[i][j]);
}
printf(" |\n");
}
getch();
}

Simple Structure program

#include
#include

struct classdata{
char name[20],rollno[15];
int age,height;
};

void main()
{
int i=1,j;
char choice;
struct classdata db[30];
clrscr();

printf("\n-----Data Entry for Students -----");
do
{
printf("\n\n\t\tEnter the Student's name: ");
scanf("%s",db[i].name);
printf("\t\tEnter Roll Number: ");
scanf("%s",db[i].rollno);
printf("\t\tEnter age: ");
scanf("%d",&db[i].age);
printf("\t\tEnter height: ");
scanf("%d",&db[i].height);
printf("\n\tEntry for Mr. %s has been saved.",db[i].name);
printf("\nDo you want to enter data for someone else? (Press Y/y for Yes):");
scanf("%s",&choice);
i++;
}while(choice=='Y'||choice=='y');

printf("\n\n\t\t----------------------------------------------");
printf("\n\t\tName\t\tRoll No.\tAge\tHeight");
printf("\n\t\t----------------------------------------------\n");

for(j=1;j{
printf("\n\t\t%s",db[j].name);
printf("\t\t%s",db[j].rollno);
printf("\t%d Yrs",db[j].age);
printf("\t%d cms",db[j].height);
}
printf("\n\t\t----------------------------------------------");
getch();
}

Contact Me

Name

Email *

Message *