Pages

Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

Saturday, March 14, 2009

Game Designing

This is my first game designed and developed through C. Rapid progress! Ha ha... Check out guys!
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "time.h"
#define limit 10
int main()
{
int k,num,guess,i=0,c=0;
double y;
char ch,a;
printf("THIS IS A SIMPLE GUESS GAME DEVEOPED BY CHAMMA\n\n");
printf("In this game, you get 3 chances to guess a number predefined by the computer\n");
printf("You have to enter a number between 0 and 9, inclusive\n");
printf("If you were able to guess it correctly, inside those 3 chances, YOU WIN!\n\n");
for(;;)
{
srand(time(NULL));
num=rand()%limit;
i++;
printf("GAME # %d:\n\n",i);
for(k=3;k>0;k--)
{
printf("You have %d guess%s left\n",k,k==1 ? "":"es");
printf("Enter your guess:");
scanf("%d",&guess);
getchar();
if(guess==num)
{
c++;
printf("BRAVO, YOU WIN!!!\n");
goto P;
}
else if(guess!=num && (k==3 || k==2))
printf("\nWrong guess. Try again\n");
else
{
printf("Damn, you lose the game %d\n",i);
printf("Number that chosen was %d!\n",num);
goto P;
}
}
P:
printf("\nIf you wish to continue to another game, press Enter\n");
printf("If you wish to quit and go to the summary, press X\n");
ch=getchar();
fflush(stdin);
a=toupper(ch);
if(a=='X')
break;
}
printf("Results summary:\n");
printf("\tNumber of games played :%d\n",i);
printf("\tNumber of wins :%d\n",c);
y=(double)(c/i)*100;
printf("\tWinning percentage :%f%%\n",y);
getchar();
return 0;
}

Friday, March 13, 2009

Dealing With Complex Numbers

Complex numbers was my favourite lesson of GCE A/L Mathematics, although I horribly screwed up the question under it, during the A/L examinaion. I was startled by finding out that calculations of complex numbers can be done with C programming. Here is my first complex number program.
#include "stdio.h"
#include "complex.h"
#include "stdlib.h"
int main()
{
double x1,x2,y1,y2;
char x[3],w[3],y[3],k[3];
printf("This is a complex number program\n");
printf("Enter your numbers in x+iy format\n\n");
printf("Enter real part of the #1, RZ1 :");
gets(x);
x1=atof(x);
printf("Enter imaginary part of the #1, IZ1 :");
gets(w);
y1=atof(w);
printf("\nZ1= %.2f+%.2fi\n\n",x1,y1);
double complex z1=x1 + y1*I;
printf("Enter real part of the #2, RZ2 :");
gets(y);
x2=atof(y);
printf("Enter imaginary part of the #2, IZ2 :");
gets(k);
y2=atof(k);
printf("\nZ2= %.2f+%.2fi\n\n",x2,y2);
double complex z2=x2 + y2*I;
double complex sum=z1+z2;
printf("Sum of the numbers(Z1+Z2) :%7.2f%+5.2fi\n",creal(sum),cimag(sum));
double complex dif=z1-z2;
printf("Difference of the numbers(Z1-Z2) :%7.2f%+5.2fi\n",creal(dif),cimag(dif));
double complex pro=z1*z2;
printf("Product of the numbers(Z1XZ2) :%7.2f%+5.2fi\n",creal(pro),cimag(pro));
double complex quo=z1/z2;
printf("Quotient of the numbers(Z1/Z2) :%7.2f%+5.2fi\n",creal(quo),cimag(quo));
double complex con1=conj(z1);
double complex con2=conj(z2);
printf("Conjugate of Z1:%7.2f%+5.2fi\nConjugate of Z2:%7.2f%+5.2fi\n\n",creal(con1)
,cimag(con1),creal(con2),cimag(con2));
printf("(Z1+Z2)(Z1-Z2) =%.2f%+.2fi\n",creal(sum*dif),cimag(sum*dif));
printf("(x1+iy1)(x1-iy1)=x1%c+y1%c= %.2f%+.2fi\n",253,253,creal(z1*con1),cimag(z1*con1));
printf("(x2+iy2)(x2-iy2)=x2%c+y2%c= %.2f%+.2fi\n",253,253,creal(z2*con2),cimag(z2*con2));
getchar();
return 0;
}

Monday, March 2, 2009

My First Serious Program

I've been programming in C for about a month. It's only by now I have started serious programming. This is my first serious program in C. It's a simple calculator. I'm waiting to upgrade it in the days to come as my C knowledge improves. Till then, enjoy it!:D
#include "stdio.h"
#include "stdlib.h"
#define Y 1
int main()
{
char num[5],name[10],ch;
int dst,i;
double one,two,add,sub,multi,div;
printf("***THIS IS THE BASIC CALCULATOR PROGRAM CREATED BY CHAMMA***\n");
printf("All Rights Reserved ® Chamitha Rathnayake\n");
printf("Enter the user's name:");
gets(name);
for(i=0;i=Y;i++)
{
printf("Specify your intended #1:");
gets(num);
one=atof(num);
printf("Specify your intended #2:");
gets(num);
two=atof(num);
printf("If you want to perform addition only, enter #1\n");
printf("If you want to perform substraction only, enter #2\n");
printf("If you want to perform multiplication only, enter #3\n");
printf("If you want to perform division only, enter #4\n");
printf("If you want to perform all-in-one, enter #5\n");
gets(num);
dst=atoi(num);
printf("Press Enter to continue to the result...\n");
getchar();
printf("%s's calculator derives the following result:\n",name);
if(dst==1)
{
add=one+two;
printf("Addition of the numbers you entered is:%lf\n",add);
}
if(dst==2)
{
sub=one-two;
printf("Substraction of the numbers you entered is:%lf\n",sub);
}
if(dst==3)
{
multi=one*two;
printf("Multiplication of the numbers you entered is:%lf\n",multi);
}
if(dst==4)
{
div=one/two;
printf("Division of the numbers you entered is:%lf\n",div);
}
if(dst==5)
{
add=one+two;
printf("Addition of the numbers you entered is:%lf\n",add);
sub=one-two;
printf("Substraction of the numbers you entered is:%lf\n",sub);
multi=one*two;
printf("Multiplication of the numbers you entered is:%lf\n",multi);
div=one/two;
printf("Division of the numbers you entered is:%lf\n",div);
}
printf("That is the end of a silly program.\n");
printf("One more thing! Press Enter to continue...\n");
getchar();
printf("I love you %s!\n",name);
getchar();
printf("Press Enter to go to the begining\n");
printf("Press 'X' and then 'Enter' to exit\n");
ch=getchar();
fflush(stdin);
if(ch=='X' or ch=='x')
{
break;
}
}
printf("Thanks for using this calculator\n");
printf("Promote FOSS,the Free and Open Source Software\n");
printf("Bye!\n");
getchar();
return(0);
}