ایجاد منو برای تغییر رنگ پس زمینه
یک منو رسم میکنه که 3تا گزینه داره که هر کدوم یه رنگه و با زدن هر گزینه رنگ پس زمینه عوض میشه.....
[PHP]
#include <stdio.h>
#include <conio.h>
f_lines();
up_key();
down_key();
void main()
{
int i;
char c1,c2;
textbackground(0);
gotoxy(wherex(),11);
f_lines();
do
{
c1=getch();
/*******if insert ENTER key*******/
if (c1==13)
{
if (wherey()==19)
{
textbackground(2);
f_lines();
}
if (wherey()==15)
{
textbackground(1);
f_lines();
}
if (wherey()==11)
{
textbackground(4);
f_lines();
}
}
/*******if insert UP key*******/
if (c1==0)
{
c2=getch();
if (((c1==0) && (c2==72)))
{
up_key();
}
/*******if insert DOWN key*******/
if (((c1==0) && (c2==80)))
{
down_key();
}
}
}while(c1!=27);
}
/***********( FUNCTION )**********/
f_lines()
{
int w,i;
w=wherey();
textcolor(15);
clrscr();
printf("%c",201);
for (i=0; i<78; i++)
{
printf("%c",205);
}
printf("%c",187);
for (i=0; i<23; i++)
{ printf("%c\n",186); }
printf("%c",200);
for (i=0; i<77; i++)
{
printf("%c",205);
}
printf("%c",188);
for (i=2; i<25; i++)
{ gotoxy(80,i);
printf("%c",186); }
/*************** Drow KEY ****************/
/******* Red key *******/
gotoxy(34,9);
printf("%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205, 205,205,205,205,205,205,205,187);
gotoxy(34,10);
printf("%c RED %c",186,186);
gotoxy(34,11);
printf("%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205, 205,205,205,205,205,205,205,188);
/******* Blue key *******/
gotoxy(34,13);
printf("%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205, 205,205,205,205,205,205,205,187);
gotoxy(34,14);
printf("%c Blue %c",186,186);
gotoxy(34,15);
printf("%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205, 205,205,205,205,205,205,205,188);
/******* Green key *******/
gotoxy(34,17);
printf("%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205, 205,205,205,205,205,205,205,187);
gotoxy(34,18);
printf("%c Green %c",186,186);
gotoxy(34,19);
printf("%c%c%c%c%c%c%c%c%c%c%c%c",200,205,205,205, 205,205,205,205,205,205,205,188);
gotoxy(wherex(),w);
}
up_key()
{
if ((wherey()==19) || (wherey()==15))
{
int k;
k=wherey()-4;
gotoxy(wherex(),k);
}
}
down_key()
{
if ((wherey()==15) || (wherey()==11))
{
int k;
k=wherey()+4;
gotoxy(wherex(),k);
}
} [/PHP]
برنامه ساده برای کار با کلیدهای مکان نما
این برنامه تحت ++Turbo c اجرا میشه و به صورت خیلی ساده کار کردن با کلیدهای مکان نما را در ++ c برای حرکت دادن اشیا نشون میده.
[PHP]
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#define RIGHT 0x4d /* define keyboard arrow keys */
#define LEFT 0x4b
#define DOWN 0x50
#define UP 0x48
#define ENTER 13
void draw( int, int);
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "C:\\tc\\bgi\\");
int x, y;
char c;
x = 50;
y = 300;
setfillstyle(1,1);
circle(x,y,10);
floodfill(x+1,y+1, getmaxcolor());
do
{
c = getch();
if ( c == UP )
{
y-=1;
draw(x, y);
}
if ( c == DOWN )
{
y+=1;
draw(x, y);
}
if ( c == RIGHT )
{
x+=1;
draw(x, y);
}
if ( c == LEFT )
{
x-=1;
draw(x, y);
}
if ( c == ENTER )
{
for ( int i = x; i < getmaxx()+10; i++)
draw( i, y);
}
}while( c != ENTER );
getch();
closegraph();
return 0;
}
void draw( int x , int y)
{
cleardevice();
setfillstyle(SOLID_FILL,BLUE);
circle(x,y,10);
floodfill(x+1,y+1,getmaxcolor());
}
[/PHP]
گرافیک در توربو سی پلاس پلاس
یک برنامه کوچک و جالب برای تمرین توابع گرافیکی در Turbo c++
[PHP]
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "C:\\tc\\bgi\\");
int x, y, maxx, maxy, color;
maxx = getmaxx();
maxy = getmaxy();
srand( time ( 0 ) );
for ( int i = 0; i < 500; i++ )
{
if(kbhit()) break;
x = rand() % maxx;
y = rand() % maxy;
color = rand() % 12;
setcolor(color);
for (int xx = 0, yy = 10; xx <= 360; xx+= 5, yy += 5 )
{
pieslice ( x, y, xx, yy, 10);
delay(10);
}
}
getch();
closegraph();
return 0;
}[/PHP]
دترمینان یک ماتریس n در n
برنامه ای برای بدست آوردن دترمینان یک ماتریس n در n
[PHP]
#include <iostream.h>
#include <conio.h>
int calc(int [],int dim);
void revmatrix( int [],int dim);
void main()
{
int matrix[1000];
int dim,temp;
double leftsum,rightsum;
cout<<" PLEASE ENTER MATRIX DIMENSION : ";
cin>>dim;
cout<<"\n\n\n";
for( int i = 0;i<( dim*dim );i ++ )
{
cout<<"ENTER ELEMAN : ";
cin>>temp;
matrix[i] = temp;
clrscr();
cout<<" PLEASE ENTER MATRIX DIMANTION : "<<dim;
cout<<"\n\n\n";
}//for i
if ( dim > 2 )
{
leftsum = calc( matrix , dim );
cout<<"LEFTSUM of the matrix = "<< leftsum <<"\n\n";
revmatrix( matrix , dim );
rightsum = calc( matrix ,dim );
cout<<"RIGHTSUM of the matrix = "<< rightsum <<"\n\n\n\n\n\n" ;
cout<<" ( DETERMINAN OF THE MATRIX = "<< leftsum - rightsum<<" )";
}
else
cout<<" ( DETERMINAN OF THE MATRIX = "<<(matrix[0] * matrix[3] - matrix[1] * matrix[2])<<" )";
getch();
}//end main
/////////////////calc function//////////
int calc( int matrix[ ], int dim )
{
int sum = 0, bul, x = 1;
for( int l = 0; l<( dim*dim );l += ( dim+1 ) )//ghotr asli
x *= matrix[l];
sum = x;
x = 1;
for( int c = 1;c<dim;c ++ )
{
bul=c;
for( int m = 0;m<dim;m ++ )
{
if( ( bul+1 )%dim != 0 )
{
x *= matrix[bul];
bul +=( dim + 1 );
}
else
{
x = x * matrix[bul];
bul += 1;
}
}//for m
sum += x;
x = 1;
}//for c
return sum;
}
////////////////////revmatriv determinan
void revmatrix(int matrix[ ],int dim)
{
int end,temp,counter;
for( int t = dim-1;t <= dim*dim;t = t + dim )
{
end=t;
counter = end -( dim-1 );
while( end > counter )
{
temp = matrix[end];
matrix[end] = matrix[counter];
matrix[counter] = temp;
++ counter;
-- end;
}
}
}
[/PHP]
برنامه پشته به صورت گرافیکی و متنی
بصورت متنی:
[php]
#include <stdio.h>
#include<process.h>
#include <dos.h>
#include<conio.h>
#include<stdio.h>
#define SIZE 5
static struct stack{
int top;
int items[SIZE];
}s1;
void push(struct stack *,int);
int pop(struct stack*);
int stacktop(struct stack*);
int empty(struct stack*);
int search(struct stack *,int);
void insert(struct stack *,int,int);
void replace(struct stack *,int,int);
void del(struct stack *,int);
void printline(void);
void main()
{
char c;
int i,temp;
s1.top=-1;
mark:
clrscr();
gotoxy(28,2);
printf("BASIC STACK OPERATIONS");
gotoxy(34,4); printf("(1) Push");
gotoxy(34,6); printf("(2) Pop");
gotoxy(34,8); printf("(3) Search");
gotoxy(34,10); printf("(4) Insert");
gotoxy(34,12); printf("(5) Replace");
gotoxy(34,14); printf("(6) Delete\n\n");
gotoxy(0,18); printline();
c=getch();
switch (c)
{
case '1': printf("\nWhat element do you wish to push?");
scanf("%d",&temp);
push(&s1,temp);
break;
case '2': pop(&s1);
break;
case '3': printf("\nEnter the element you wish to search?");
scanf("%d",&temp);
printf("\nThe element is at index %d\n",search(&s1,temp));
break;
case '4': printf("\nEnter the POSTION and ELEMENT you wish to insert?");
scanf("%d %d",&i,&temp);
insert(&s1,i,temp);
break;
case '5': printf("\nEnter the POSTION and ELEMENT you wish to replace?");
scanf("%d %d",&i,&temp);
replace(&s1,i,temp);
break;
case '6': printf("\nEnter the POSTION you wish to delete from stack?");
scanf("%d",&temp);
del(&s1,temp);
break;
default : printf("\nInvalid entry! Try again!");
break;
}
for (i=s1.top;i>=0;i--)
printf("\nThe element at position (%d) is %4d",i,s1.items[i]);
printf("\n\nDo you wish to continue?");
c=getch();
if (c=='y')
goto mark;
}
void push(struct stack *sx,int x)
{
if (sx->top==SIZE-1)
{
printf("\n\tSTACT OVERFLOW\n");
getch();
exit(1);
}
sx->items[++sx->top]=x;
}
int pop(struct stack *sx)
{
if (empty(sx))
{
printf("\n\t STACK UNDERFLOW\n");
getch();
exit(1);
}
return(sx->items[sx->top--]);
}
int stacktop(struct stack *sx)
{
return(sx->items[sx->top]);
}
int empty(struct stack *sx)
{
return((sx->top==-1));
}
int search(struct stack *sx,int n)
{
int arr[20];
int i=0,j,result=-1;
for (j=sx->top;!(empty(sx));j--)
{
if (n==stacktop(sx))
{
result=sx->top;
break;
}
arr[i++]=pop(sx);
}
for (j=i-1;j>=0;j--)
push(sx,arr[j]);
return(result);
}
void insert(struct stack *sx,int pos,int ele)
{
int arr[20];
int i=0,j;
for (j=sx->top;j>=pos;j--)
arr[i++]=pop(sx);
push(sx,ele);
for (j=i-1;j>=0;j--)
push(sx,arr[j]);
}
void replace(struct stack *sx,int pos,int ele)
{
int arr[20];
int i=0,j;
for (j=sx->top;j>=pos;j--)
arr[i++]=pop(sx);
push(sx,ele);
for (j=i-2;j>=0;j--)
push(sx,arr[j]);
}
void del(struct stack *sx,int pos)
{
int arr[20];
int i=0,j;
for (j=sx->top;j>pos;j--)
arr[i++]=pop(sx);
pop(sx);
for (j=i-1;j>=0;j--)
push(sx,arr[j]);
}
void printline(void)
{
int i;
for (i=0;i<40;i++)
printf("®¯");
}
[/php]
بصورت گرافیکی:
[php]
#include <stdio.h>
#include <graphics.h>
#include <dos.h>
#include<conio.h>
#include<process.h>
#define SIZE 6
static struct stack{
int top;
int items[SIZE];
}s1;
void push(struct stack *,int);
int pop(struct stack*);
int stacktop(struct stack*);
int empty(struct stack*);
int search(struct stack *,int);
void insert(struct stack *,int,int);
void replace(struct stack *,int,int);
void del(struct stack *,int);
void printline(void);
void startscreen(int,int);
void shell(int,int);
void close(void);
void start_screen();
void press_esc();
int gd=DETECT,gm,maxx,maxy;
void main()
{
char c;
int i,temp;
s1.top=-1;
initgraph(&gd,&gm,"f:\\tc\\bgi");
maxx=getmaxx();
maxy=getmaxy();
startscreen(maxx,maxy);
start_screen();
shell(maxx,maxy);
START:
c=getch();
switch (c)
{
case '1': gotoxy(3,17);
printf("What element do you wish to push?");
scanf("%d",&temp);
push(&s1,temp);
break;
case '2': gotoxy(3,17);
pop(&s1);
break;
case '3': gotoxy(3,17);
printf("Enter the element you wish to search?");
scanf("%d",&temp);
temp=search(&s1,temp);
gotoxy(5,19);
if (temp==-1)
printf("Sorry! No results Found!");
else
printf("The element is at index %d",temp);
getch();
break;
case '4': gotoxy(3,17);
printf("Enter the POSTION and ELEMENT you wish to insert?");
scanf("%d %d",&i,&temp);
insert(&s1,i,temp);
break;
case '5': gotoxy(3,17);
printf("Enter the POSTION and ELEMENT you wish to replace?");
scanf("%d %d",&i,&temp);
replace(&s1,i,temp);
break;
case '6': gotoxy(3,17);
printf("Enter the POSTION you wish to delete from stack?");
scanf("%d",&temp);
del(&s1,temp);
break;
default : gotoxy(3,17);
printf("Invalid entry! Try again!");
break;
}
for (i=0;i<=6;i++)
{
gotoxy(3,17+i);
printf("\t\t\t\t\t\t\t\t\t");
}
gotoxy(3,17);
printf("Do You wish to contine?");
c=getch();
if (c=='y')
{
gotoxy(3,17); printf("\t\t\t\t\t");
goto START;
}
closegraph();
close();
restorecrtmode();
}
void push(struct stack *sx,int x)
{
if (sx->top==SIZE-1)
{
gotoxy(5,22);
printf("\tSTACT OVERFLOW\n");
getch();
return;
}
sx->items[++sx->top]=x;
gotoxy(66,14-2*sx->top);
printf("%d",x);
}
int pop(struct stack *sx)
{
if (empty(sx))
{
gotoxy(5,22);
printf("\t STACK UNDERFLOW\n");
getch();
return(0);
}
gotoxy(66,14-2*sx->top);
printf(" ");
return(sx->items[sx->top--]);
}
int stacktop(struct stack *sx)
{
return(sx->items[sx->top]);
}
int empty(struct stack *sx)
{
return((sx->top==-1));
}
int search(struct stack *sx,int n)
{
int arr[20];
int i=0,j,result=-1;
for (j=sx->top;!(empty(sx));j--)
{
if (n==stacktop(sx))
{
result=sx->top;
break;
}
arr[i++]=pop(sx);
}
for (j=i-1;j>=0;j--)
push(sx,arr[j]);
return(result);
}
void insert(struct stack *sx,int pos,int ele)
{
int arr[20];
int i=0,j;
if (pos>sx->top)
{
gotoxy(3,19);
printf("Invalid Position!");
getch();
return;
}
for (j=sx->top;j>=pos;j--)
arr[i++]=pop(sx);
push(sx,ele);
for (j=i-1;j>=0;j--)
push(sx,arr[j]);
}
void replace(struct stack *sx,int pos,int ele)
{
int arr[20];
int i=0,j;
if (pos>sx->top)
{
gotoxy(3,19);
printf("Invalid Position!");
getch();
return;
}
for (j=sx->top;j>=pos;j--)
arr[i++]=pop(sx);
push(sx,ele);
for (j=i-2;j>=0;j--)
push(sx,arr[j]);
}
void del(struct stack *sx,int pos)
{
int arr[20];
int i=0,j;
if (pos>sx->top)
{
gotoxy(3,19);
printf("Invalid Position!");
getch();
return;
}
for (j=sx->top;j>pos;j--)
arr[i++]=pop(sx);
pop(sx);
for (j=i-1;j>=0;j--)
push(sx,arr[j]);
}
void printline(void)
{
int i;
for (i=0;i<40;i++)
printf("®¯");
}
void startscreen(int x,int y)
{
int i;
sound(1);
setbkcolor(1);
setlinestyle(5,0,2);
rectangle(0,0,x,y);
rectangle(2,2,x-2,y-2);
settextstyle(10,HORIZ_DIR,3);
setcolor(YELLOW);
outtextxy(x/10,y/3,"Loading ....... Please Wait......");
setcolor(LIGHTGRAY);
rectangle(x/8,2*y/3,7*x/8,2*y/3+20);
setcolor(WHITE);
bar(x/8+1,2*y/3+1,7*x/8-1,2*y/3+20-1);
setfillstyle(1,LIGHTBLUE);
delay(2000);
for (i=4;i>=1;i--)
{
bar(x/8+1,2*y/3+1,7*x/8/i-1,2*y/3+20-1);
delay(1000);
}
delay(500);
nosound();
//closegraph();
initgraph(&gd,&gm,"f:\tc\bgi");
}
void shell(int x,int y)
{
int i;
setbkcolor(BLACK);
rectangle(0,0,x,y);
rectangle(2,2,x-2,y-2);
settextstyle(0,HORIZ_DIR,2);
gotoxy(18,2);
setcolor(WHITE);
outtextxy(x/5,y/30,"BASIC STACK OPERATIONS");
textcolor(DARKGRAY);
gotoxy(24,15); printf("Enter (1--6) for Stack Operations");
gotoxy(24,4); printf("(1) Push");
gotoxy(24,6); printf("(2) Pop");
gotoxy(24,8); printf("(3) Search");
gotoxy(24,10); printf("(4) Insert");
gotoxy(24,12); printf("(5) Replace");
gotoxy(24,14); printf("(6) Delete\n\n");
bar(0,y/2+4,x,y/2+6);
setcolor(MAGENTA);
setlinestyle(0,0,3);
line(3*x/4,y/12-7,3*x/4,y/2-7);
line(3*x/4+100,y/12-7,3*x/4+100,y/2-7);
for (i=0;i<7;i++)
{
line(3*x/4,y/2-i*33-7,3*x/4+100,y/2-i*33-7);
gotoxy(55,14-2*i);
if (i<6)
printf("® %d ¯",i);
}
press_esc();
}
void close(void)
{
int mx, my;
/* request auto detection */
int gd= DETECT, gm;
initgraph(&gd,&gm,"f:\\tc\\bgi");
mx = (getmaxx() / 2);
my = (getmaxy() / 2);
//SET baqckground color
setfillstyle(9, 1);
bar(0,0,getmaxx(),getmaxy());
//DRAW a bar, and make it look like a 3d bar
setfillstyle(1,7);
//bar(50,20,600,400);
//DRAW lines for the top and left side
setcolor(15);
line(50,20,600,20);
line(51,21,599,21);
line(50,20,50,400);
line(51,21,51,399);
//DRAW lines for the bottom and right side
setcolor(8);
line(600,20,600,400);
line(599,21,599,400);
line(50,400,600,400);
line(51,399,600,399);
//DRAW two 3D bars for the left and right side
setfillstyle(9,8);
bar(70,40,100,380);
bar(70,40,550,70);
bar(70,350,550,379);
//getch();
bar(545,40,575,380);
//PRINT 3D text CALENDAR
settextstyle(1, 0, 4);
settextjustify(1,1);
setcolor(LIGHTGREEN);
outtextxy(mx+2, my - 46, "Thank You!");
setcolor(LIGHTGREEN);
outtextxy(mx + 1, my - 45, "Thank You!");
setcolor(GREEN);
outtextxy(mx + 2, my - 44, "Thank You!");
//PRINT
settextstyle(2, 0, 5);
setcolor(WHITE);
outtextxy(mx + 1, my + 150, "Copyright www.forum.daneshjuha.org");
getch(); //PAUSE for a while
closegraph();
//initgraph(&gd,&gm,"e:\tc\bgi");
}
//Start
void start_screen()
{
int mx, my;
/* request auto detection */
int gd= DETECT, gm;
initgraph(&gd,&gm,"f:\\tc\\bgi");
mx = (getmaxx() / 2);
my = (getmaxy() / 2);
//SET baqckground color
setfillstyle(9, 1);
bar(0,0,getmaxx(),getmaxy());
//DRAW a bar, and make it look like a 3d bar
setfillstyle(1,7);
//bar(50,20,600,400);
//DRAW lines for the top and left side
setcolor(15);
line(50,20,600,20);
line(51,21,599,21);
line(50,20,50,400);
line(51,21,51,399);
//DRAW lines for the bottom and right side
setcolor(8);
line(600,20,600,400);
line(599,21,599,400);
line(50,400,600,400);
line(51,399,600,399);
//DRAW two 3D bars for the left and right side
setfillstyle(9,8);
bar(70,40,100,380);
bar(70,40,550,70);
bar(70,350,550,379);
//getch();
bar(545,40,575,380);
//PRINT 3D text CALENDAR 2002
settextstyle(1, 0, 4);
settextjustify(1,1);
setcolor(LIGHTGREEN);
outtextxy(mx+2, my - 150, "!...Wel Come...!");
setcolor(LIGHTGREEN);
outtextxy(mx + 1, my - 149, "!...Wel Come...!");
setcolor(GREEN);
outtextxy(mx + 2, my - 148, "!...Wel Come...!");
setcolor(GREEN);
outtextxy(mx + 2, my - 100, "Stack Operations");
//Print 3D Text
setcolor(WHITE);
outtextxy(mx, my-50, "BY");
setcolor(DARKGRAY);
outtextxy(mx, my + 5, "hossein azarpevand");
setcolor(DARKGRAY);
outtextxy(mx + 1, my + 6, "hossein azarpevand");
setcolor(LIGHTGRAY);
outtextxy(mx+1, my + 7, "hossein azarpevand");
//PRINT copyright notice
settextstyle(2, 0, 5);
setcolor(WHITE);
outtextxy(mx + 1, my + 150, "Copyright www.forum.daneshjuha.org");
getch(); //PAUSE for a while
closegraph();
initgraph(&gd,&gm,"e:\tc\bgi");
}
void press_esc()
{
settextstyle(2, 1, 5);
setcolor(RED);
outtextxy(600, 60,"..Press Esc to Exit..");
}[/php]