PDA

Tüm Versiyonu Göster : Örnekler....


MeRT&SwLY
26-12-05, 21:43
Tic Tac Toe Oyun Kodu C++


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* WWW tic tac toe hack */

/* cgi-bin util routines */
void unescape_url(char *url);
void plustospace(char *str);

int vals[19683]; /* 3**9 -- not all positions are accesible, however */

enum { Xwins=1, Owins=-1, Tie=2, None=0 };
enum { X=1, O=-1 };

int diff = 0, player = 0;
char board[10];

#define FHEAD "Linki görüntüleyebilmek için <a href="%2$s"><strong>Üye</strong></a> olmanız gerekiyor."



int board_to_int(board)
char *board;
{
int i, out =0, exp=1;

for (i=0; i<9; i++, exp *= 3) {
switch(board[i]) {
case '-': /* zero */
break;
case 'X': /* one */
out += exp * 1;
break;
case 'O': /* two */
out += exp * 2;
}
}

return out;
}



int test_won()
{
int i, flag=0;
char *b = board;

if ((b[0] == 'X' && b[1] == 'X' && b[2] == 'X') ||
(b[3] == 'X' && b[4] == 'X' && b[5] == 'X') ||
(b[6] == 'X' && b[7] == 'X' && b[8] == 'X') ||
(b[0] == 'X' && b[3] == 'X' && b[6] == 'X') ||
(b[1] == 'X' && b[4] == 'X' && b[7] == 'X') ||
(b[2] == 'X' && b[5] == 'X' && b[8] == 'X') ||
(b[0] == 'X' && b[4] == 'X' && b[8] == 'X') ||
(b[2] == 'X' && b[4] == 'X' && b[6] == 'X'))
return Xwins;

if ((b[0] == 'O' && b[1] == 'O' && b[2] == 'O') ||
(b[3] == 'O' && b[4] == 'O' && b[5] == 'O') ||
(b[6] == 'O' && b[7] == 'O' && b[8] == 'O') ||
(b[0] == 'O' && b[3] == 'O' && b[6] == 'O') ||
(b[1] == 'O' && b[4] == 'O' && b[7] == 'O') ||
(b[2] == 'O' && b[5] == 'O' && b[8] == 'O') ||
(b[0] == 'O' && b[4] == 'O' && b[8] == 'O') ||
(b[2] == 'O' && b[4] == 'O' && b[6] == 'O'))
return Owins;

/* test for tie */
for (i=0; i<9; i++) {
if (b[i] == '-')
flag=1;
}
if (!flag)
return Tie;

return None; /* game's not over */
}



int minimax(pl, depth)
int pl, depth;
{
int best, val;
int i, index;

index = board_to_int(board);
if ((vals[index] % 10) >= depth)
return vals[index] - (vals[index] % 10); /* since 0 is a valid value */
/* we don't want use the memoized value if it is shallower than
* we're allowed to go--in fact, this doesn't matter since the memoization
* goes away with each move, but otherwise it would.
*/

if (depth > diff)
return 0;

if ((val = test_won(board)) != None) {
switch(val) {
case Xwins:
case Owins:
return 1000 * val;
case Tie:
return 0;
}
}

best = -pl * 1000000;
for (i=0; i<9; i++) {
if (board[i] == '-') {
board[i] = (pl == X) ? 'X' : 'O';
val = minimax(-pl, depth+1);
if (val * pl > best * pl)
best = val;
board[i] = '-';
}
}

vals[index] = best + depth;
return best;
}



find_move()
{
int best, val, besti[9], i, bestcount = 0;
int pl = -player; /* find the computer's move, not the player's */

best = -pl * 1000000;
for (i=0; i<9; i++) {
if (board[i] == '-') {
board[i] = (pl == X) ? 'X' : 'O';
val = minimax(-pl, 1);
if (val == best) {
best = val;
besti[bestcount++] = i;
}
else if (val * pl > best * pl) {
best = val;
bestcount = 0;
besti[bestcount++] = i;
}
board[i] = '-';
}
}
i = random()%bestcount;

board[besti[i]] = (pl == X) ? 'X' : 'O';
}



output(won)
int won;
{
int i, j;

printf("<TITLE>Tic Tac Toe</TITLE>\n<center><H1>Tic Tac Toe</H1>\n");
printf("<p>You are %s.\n", (player == X) ? "X's" : "O's");
printf("<p><table border>");

for (j=0; j<3; j++) {
printf("<TR>");
for (i=0; i<3; i++) {
printf("<TD>");
if (board[j*3+i] == '-') {
if (won == None) {
board[j*3 + i] = (player == O) ? 'O' : 'X';
printf("<a href = \"ttt?d=%c&p=%c&b=%s\">",
diff+'0', (player == O) ? 'O' : 'X', board);
printf("<img src = \"%se.gif\" border=0></a>\n", FHEAD);
board[j*3 + i] = '-';
}
else {
printf("<img src = \"%se.gif\" border=0>\n", FHEAD);
}
}
else {
printf("<img src = \"%s%c.gif\" border=0>\n",
FHEAD,
board[j*3+i] == 'X' ? 'x' : 'o');
}
printf("</TD>");
}
printf("</TR>");
}

printf("</table>\n");

if (won != None) {
printf("<form method=\"get\" action=\"/~donham/ttt.html\">\n");
printf("<p>%s won.\n",
won == Tie ? "Nobody" : (won == player ? "You" : "I"));
printf("Press <input type=\"submit\" value=\"Play\"> to play again.\n");
printf("</form>\n");
}

printf("<hr><a href = \"/~donham/index.html\">Home</a> -> Tic Tac Toe\n");
printf("</center>");

}



die(arg)
char *arg;
{
printf("<title>Tic Tac Toe</title>\n<p>Error: %s\n", arg);
exit(0);
}


/* fixed up getword which takes buffer size arg */
void getword2(word, size, line, stop)
char *word, *line, stop;
int size;
{
int x = 0,y;
--size; /* leave room for NUL */
for(x=0,win line[x]) && (line[x] != stop));x++)
if (x < size)
word[x] = line[x];

if (x < size)
word[x] = '\0';
else
word[size] = '\0';

if(line[x]) ++x;
y=0;

while(line[y++] = line[x++]);
}

void process_query()
{
char *cl, *rm, buf[100], name[100];
int df = 0, pf = 0, bf = 0;
int i;

if ((rm = getenv("REQUEST_METHOD")) == 0)
die("No request method.\n");
if (strcmp(rm, "GET") != 0)
die("Script not referenced with GET method.\n");

if ((cl = getenv("QUERY_STRING")) == 0)
die("No query string.\n");

while (cl[0] != 0) {
getword2(buf, 100, cl, '&');
plustospace(buf);
unescape_url(buf);
getword2(name, 100, buf, '=');

switch (name[0]) {

case 'd':
switch (buf[0]) {
case '1':
case '2':
case '9':
diff = buf[0] - '0';
break;
default:
die("Bad query string.\n");
}
df = 1;
break;

case 'p':
switch(buf[0]) {
case 'X':
player = X;
break;
case 'O':
player = O;
break;
default:
die("Bad query string.\n");
}
pf = 1;
break;

case 'b':
strncpy(board, buf, 10);
board[10] = 0;
for (i = 0; i < 9; i++)
switch (board[i]) {
case 'O':
case 'X':
case '-':
break;
default:
die("Bad query string.\n");
}
bf = 1;
break;

default:
die("Bad query string.\n");
}
}

if (!df)
diff = 2;
if (!pf)
player = X;
if (!bf)
strcpy(board, "---------");
}



main(argc, argv)
int argc;
char **argv;
{
int won, i;

printf("Content-type: text/html\n\n");

process_query();

if (strcmp(board, "---------") == 0 && player == X) {
output(0); /* first move--empty board */
exit(0);
}
else if ((won = test_won()) != None) { /* last move ended game */
output(won);
exit(0);
}
else {
srandom(getpid());
find_move(); /* puts the move into board */
won = test_won();
output(won);
exit(0);
}
}

/* from NCSA Linki görüntüleyebilmek için <a href="%2$s"><strong>Üye</strong></a> olmanız gerekiyor. util.c */

char x2c(char *what) {
register char digit;

digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}

void unescape_url(char *url) {
register int x,y;

for(x=0,y=0;url[y];++x,++y) {
if((url[x] = url[y]) == '%') {
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}

void plustospace(char *str) {
register int x;

for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
}

MeRT&SwLY
26-12-05, 21:44
KARAKTER SAYAR


#include<stdio.h>
#include<ctype.h>
#include<string.h>

void lowerCase( char * ); // Converts a string to all lowercase.

int main( int argc , char *argv[] )
{
char str[16001] = { 0 };

if ( argc == 1 ) {
printf ( "\n\nEnter the string > " );
gets( str );
printf( "\n" );
}

else for ( int i = 1 ; i <= argc - 1 ; strcat ( str , argv[ i++ ] ) );

lowerCase( str );

char freq[26] = { 0 };
for ( i = 0 ; str[i] != '\0' ; freq[ str[ i++ ] - 97 ]++ );

putchar( 218 );
for ( i = 0 ; i <= 77 ; putchar( 196 ) , i++ );
printf( "%c%c%79c%c Occurrences\t( Total Length : %d )%43c%c%79c%c" , 191 ,179,179,179, strlen(str) ,179,179,179,179);
for ( i = 0 ; i <= 25 ; printf ( "%2c " , i++ + 97 ) );
printf( "%c%c" , 179 , 195 );
for ( i = 0 ; i <= 77 ; putchar( 196 ) , i++ );
printf( "%c%c" , 180 , 179 );
for ( i = 0 ; i <= 25 ; printf ( "%2d " , freq[ i++ ] ) );
printf( "%c%c" , 179 , 192 );
for ( i = 0 ; i <= 77 ; putchar( 196 ) , i++ );
printf( "%c\n\n" , 217 );

printf( "Press enter to continue ..." );
getchar();
return 0;
}

void lowerCase( char *str )
{
for ( int i = 0 ; str[i] != '\0' ; i++ )
str[i] = (char) tolower( str[i] );
}

MeRT&SwLY
26-12-05, 21:49
STRUCT YAPISI

C dilinde struct anahtar kelimesi ile programcı kendi değişken tiplerini yaratabilir.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct sporcu {
char * isim;
struct boy_fr * boy;
struct agirlik_fr * agirlik;
};

struct boy_fr {
int metre;
int santim;
};

struct agirlik_fr{
int kilo;
};

main(){
struct sporcu S;
S.isim="Hakan Sukur";
S.boy->metre=1;
S.boy->sec=92;

printf("%s %d m %d cm\n", S.isim,S.boy->metre, S.boy->santim);

return 0;
}

Yukarıdaki program hatasız olarak derlenebilir. Ancak, çalıştırıldığında “Segmentation Fault” hatası verecektir. Program şu şekilde düzeltilmelidir.

main(){
struct sporcu S;
S.isim=(char *)malloc(strlen("Hakan Sukur")+1);
S.isim="Hakan Sukur";
S.boy = (struct boy_fr *)malloc(sizeof(struct boy_fr));
S.boy->metre=1;
S.boy->santim=92;

printf("%s %d m %d cm\n",S.isim,S.boy->metre,S.boy->santim);

return 0;
}

Dinamik Bellek Yönetimi

C dilinde dinamik bellek yönetimi için malloc ve calloc komutları kullanılmaktadır.

void *malloc(size_t size);
void *calloc(size_t num, size_t size );

Kullanıcının boyutlarını belirlediği iki matrisin çarpımını yapan bir program hazırlanacaktır. Matris boyutlarının kullanıcıdan alınacak olması, ilgili değişkenlerin dinamik olarak yaratılmasını gerektirir.

Programın ikinci aşaması ise iki matrisin çarpımının nasıl yapıldığı ile ilgilidir.





İstenilen işlemi gerçekleştiren program aşağıda verilmiştir.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

main(){
int a_x, a_y, b_x, b_y,i,j,k;
int** a, **b,**c;

srand(time(NULL));

printf ("Lutfen 1. matrisin boyutunu giriniz NxM");
scanf("%d %d",&a_x,& a_y);

printf ("Lutfen 2. matrisin boyutunu giriniz PxR");
scanf("%d %d",&b_x,& b_y);

if (a_y!=b_x){
printf("boyutlar uygun degil\n");
exit(-1);
}

a =(int**)malloc(a_x*sizeof(int*));
for (i=0;i< a_x;i++)
a[i]=(int *)malloc(a_y *sizeof(int));

b =(int**)malloc(b_x*sizeof(int*));
for (i=0;i<b_x;i++)
b[i]=(int *)malloc(b_y *sizeof(int));


c =(int**)malloc(a_x*sizeof(int*));
for (i=0;i<a_x;i++)
c[i]=(int *)calloc(b_y,sizeof(int));

for (i=0;i<a_x;i++)
for (j=0;j<a_y;j++)
a[i][j]=rand()%10;

for (i=0;i<b_x;i++)
for (j=0;j<b_y;j++)
b[i][j]=rand()%10;

for (i=0;i< a_x;i++)
for(j=0;j<b_y;j++)
for(k=1;k<a_y;k++)
c[i][j]+=a[i][k]*b[k][j];



for (i=0;i< a_x;i++,printf("\n"))
for (j=0;j<a_y;j++)
printf("%d ",a[i][j]);


printf("\n\n\n");

for (i=0;i<b_x;i++,printf("\n"))
for (j=0;j<b_y;j++)
printf("%d ",b[i][j]);

printf("\n\n\n");

for (i=0;i<a_x;i++,printf("\n"))
for (j=0;j<b_y;j++)
printf("%d ",c[i][j]);

return 0;
}