Facebook

adsense

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

Monday, 9 March 2015

SNAKE GAME IN C CODE

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include<time.h>
#include<ctype.h>
#include <time.h>
#include <windows.h>
#include <process.h>

#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77

int length;
int bend_no;
int len;
char key;
void record();
void load();
int life;
void Delay(long double);
void Move();
void Food();
int Score();
void Print();
void gotoxy(int x, int y);
void GotoXY(int x,int y);
void Bend();
void Boarder();
void Down();
void Left();
void Up();
void Right();
void ExitGame();
int Scoreonly();

struct coordinate
{
    int x;
    int y;
    int direction;
};

typedef struct coordinate coordinate;

coordinate head, bend[500],food,body[30];

int main()
{

    char key;

    Print();

    system("cls");

    load();

    length=5;

    head.x=25;

    head.y=20;

    head.direction=RIGHT;

    Boarder();

    Food(); //to generate food coordinates initially

    life=3; //number of extra lives

    bend[0]=head;

    Move();   //initialing initial bend coordinate

    return 0;

}

void Move()
{
    int a,i;

    do
    {

        Food();
        fflush(stdin);

        len=0;

        for(i=0; i<30; i++)

        {

            body[i].x=0;

            body[i].y=0;

            if(i==length)

                break;

        }

        Delay(length);

        Boarder();

        if(head.direction==RIGHT)

            Right();

        else if(head.direction==LEFT)

            Left();

        else if(head.direction==DOWN)

            Down();

        else if(head.direction==UP)

            Up();

        ExitGame();

    }
    while(!kbhit());

    a=getch();

    if(a==27)

    {

        system("cls");

        exit(0);

    }
    key=getch();

    if((key==RIGHT&&head.direction!=LEFT&&head.direction!=RIGHT)||(key==LEFT&&head.direction!=RIGHT&&head.direction!=LEFT)||(key==UP&&head.direction!=DOWN&&head.direction!=UP)||(key==DOWN&&head.direction!=UP&&head.direction!=DOWN))

    {

        bend_no++;

        bend[bend_no]=head;

        head.direction=key;

        if(key==UP)

            head.y--;

        if(key==DOWN)

            head.y++;

        if(key==RIGHT)

            head.x++;

        if(key==LEFT)

            head.x--;

        Move();

    }

    else if(key==27)

    {

        system("cls");

        exit(0);

    }

    else

    {

        printf("\a");

        Move();

    }
}

void gotoxy(int x, int y)
{

    COORD coord;

    coord.X = x;

    coord.Y = y;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}
void GotoXY(int x, int y)
{
    HANDLE a;
    COORD b;
    fflush(stdout);
    b.X = x;
    b.Y = y;
    a = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(a,b);
}
void load()
{
    int row,col,r,c,q;
    gotoxy(36,14);
    printf("loading...");
    gotoxy(30,15);
    for(r=1; r<=20; r++)
    {
        for(q=0; q<=100000000; q++); //to display the character slowly
        printf("%c",177);
    }
    getch();
}
void Down()
{
    int i;
    for(i=0; i<=(head.y-bend[bend_no].y)&&len<length; i++)
    {
        GotoXY(head.x,head.y-i);
        {
            if(len==0)
                printf("v");
            else
                printf("*");
        }
        body[len].x=head.x;
        body[len].y=head.y-i;
        len++;
    }
    Bend();
    if(!kbhit())
        head.y++;
}
void Delay(long double k)
{
    Score();
    long double i;
    for(i=0; i<=(10000000); i++);
}
void ExitGame()
{
    int i,check=0;
    for(i=4; i<length; i++) //starts with 4 because it needs minimum 4 element to touch its own body
    {
        if(body[0].x==body[i].x&&body[0].y==body[i].y)
        {
            check++;    //check's value increases as the coordinates of head is equal to any other body coordinate
        }
        if(i==length||check!=0)
            break;
    }
    if(head.x<=10||head.x>=70||head.y<=10||head.y>=30||check!=0)
    {
        life--;
        if(life>=0)
        {
            head.x=25;
            head.y=20;
            bend_no=0;
            head.direction=RIGHT;
            Move();
        }
        else
        {
            system("cls");
            printf("All lives completed\nBetter Luck Next Time!!!\nPress any key to quit the game\n");
            record();
            exit(0);
        }
    }
}
void Food()
{
    if(head.x==food.x&&head.y==food.y)
    {
        length++;
        time_t a;
        a=time(0);
        srand(a);
        food.x=rand()%70;
        if(food.x<=10)
            food.x+=11;
        food.y=rand()%30;
        if(food.y<=10)

            food.y+=11;
    }
    else if(food.x==0)/*to create food for the first time coz global variable are initialized with 0*/
    {
        food.x=rand()%70;
        if(food.x<=10)
            food.x+=11;
        food.y=rand()%30;
        if(food.y<=10)
            food.y+=11;
    }
}
void Left()
{
    int i;
    for(i=0; i<=(bend[bend_no].x-head.x)&&len<length; i++)
    {
        GotoXY((head.x+i),head.y);
        {
            if(len==0)
                printf("<");
            else
                printf("*");
        }
        body[len].x=head.x+i;
        body[len].y=head.y;
        len++;
    }
    Bend();
    if(!kbhit())
        head.x--;

}
void Right()
{
    int i;
    for(i=0; i<=(head.x-bend[bend_no].x)&&len<length; i++)
    {
        //GotoXY((head.x-i),head.y);
        body[len].x=head.x-i;
        body[len].y=head.y;
        GotoXY(body[len].x,body[len].y);
        {
            if(len==0)
                printf(">");
            else
                printf("*");
        }
        /*body[len].x=head.x-i;
        body[len].y=head.y;*/
        len++;
    }
    Bend();
    if(!kbhit())
        head.x++;
}
void Bend()
{
    int i,j,diff;
    for(i=bend_no; i>=0&&len<length; i--)
    {
        if(bend[i].x==bend[i-1].x)
        {
            diff=bend[i].y-bend[i-1].y;
            if(diff<0)
                for(j=1; j<=(-diff); j++)
                {
                    body[len].x=bend[i].x;
                    body[len].y=bend[i].y+j;
                    GotoXY(body[len].x,body[len].y);
                    printf("*");
                    len++;
                    if(len==length)
                        break;
                }
            else if(diff>0)
                for(j=1; j<=diff; j++)
                {
                    /*GotoXY(bend[i].x,(bend[i].y-j));
                    printf("*");*/
                    body[len].x=bend[i].x;
                    body[len].y=bend[i].y-j;
                    GotoXY(body[len].x,body[len].y);
                    printf("*");
                    len++;
                    if(len==length)
                        break;
                }
        }
        else if(bend[i].y==bend[i-1].y)
        {
            diff=bend[i].x-bend[i-1].x;
            if(diff<0)
                for(j=1; j<=(-diff)&&len<length; j++)
                {
                    /*GotoXY((bend[i].x+j),bend[i].y);
                    printf("*");*/
                    body[len].x=bend[i].x+j;
                    body[len].y=bend[i].y;
                    GotoXY(body[len].x,body[len].y);
                    printf("*");
                    len++;
                    if(len==length)
                        break;
                }
            else if(diff>0)
                for(j=1; j<=diff&&len<length; j++)
                {
                    /*GotoXY((bend[i].x-j),bend[i].y);
                    printf("*");*/
                    body[len].x=bend[i].x-j;
                    body[len].y=bend[i].y;
                    GotoXY(body[len].x,body[len].y);
                    printf("*");
                    len++;
                    if(len==length)
                        break;
                }
        }
    }
}
void Boarder()
{
    system("cls");
    int i;
    GotoXY(food.x,food.y);   /*displaying food*/
    printf("F");
    for(i=10; i<71; i++)
    {
        GotoXY(i,10);
        printf("!");
        GotoXY(i,30);
        printf("!");
    }
    for(i=10; i<31; i++)
    {
        GotoXY(10,i);
        printf("!");
        GotoXY(70,i);
        printf("!");
    }
}
void Print()
{
    //GotoXY(10,12);
    printf("\tWelcome to the mini Snake game.(press any key to continue)\n");
    getch();
    system("cls");
    printf("\tGame instructions:\n");
    printf("\n-> Use arrow keys to move the snake.\n\n-> You will be provided foods at the several coordinates of the screen which you have to eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n-> Here you are provided with three lives. Your life will decrease as you hit the wall or snake's body.\n\n-> YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n-> If you want to exit press esc. \n");
    printf("\n\nPress any key to play game...");
    if(getch()==27)
        exit(0);
}
void record()
{
    char plname[20],nplname[20],cha,c;
    int i,j,px;
    FILE *info;
    info=fopen("record.txt","a+");
    getch();
    system("cls");
    printf("Enter your name\n");
    scanf("%[^\n]",plname);
    //************************
    for(j=0; plname[j]!='\0'; j++) //to convert the first letter after space to capital
    {
        nplname[0]=toupper(plname[0]);
        if(plname[j-1]==' ')
        {
            nplname[j]=toupper(plname[j]);
            nplname[j-1]=plname[j-1];
        }
        else nplname[j]=plname[j];
    }
    nplname[j]='\0';
    //*****************************
    //sdfprintf(info,"\t\t\tPlayers List\n");
    fprintf(info,"Player Name :%s\n",nplname);
    //for date and time

    time_t mytime;
    mytime = time(NULL);
    fprintf(info,"Played Date:%s",ctime(&mytime));
    //**************************
    fprintf(info,"Score:%d\n",px=Scoreonly());//call score to display score
    //fprintf(info,"\nLevel:%d\n",10);//call level to display level
    for(i=0; i<=50; i++)
        fprintf(info,"%c",'_');
    fprintf(info,"\n");
    fclose(info);
    printf("Wanna see past records press 'y'\n");
    cha=getch();
    system("cls");
    if(cha=='y')
    {
        info=fopen("record.txt","r");
        do
        {
            putchar(c=getc(info));
        }
        while(c!=EOF);
    }
    fclose(info);
}
int Score()
{
    int score;
    GotoXY(20,8);
    score=length-5;
    printf("SCORE : %d",(length-5));
    score=length-5;
    GotoXY(50,8);
    printf("Life : %d",life);
    return score;
}
int Scoreonly()
{
    int score=Score();
    system("cls");
    return score;
}
void Up()
{
    int i;
    for(i=0; i<=(bend[bend_no].y-head.y)&&len<length; i++)
    {
        GotoXY(head.x,head.y+i);
        {
            if(len==0)
                printf("^");
            else
                printf("*");
        }
        body[len].x=head.x;
        body[len].y=head.y+i;
        len++;
    }
    Bend();
    if(!kbhit())
        head.y--;
}

Saturday, 7 March 2015

MOBILE COMPANY DATABASE C CODE

PLEASE MAKE THE FILE THAT IS WRITTEN OTHERWISE THE PROGRAM WILL NOT RUN



/*HEADER FILES ARE INCLUDED*/

#include <dos.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include<graphics.h>

/*MACROS ARE DEFINED*/

#define NUM 5
#define PH 12
#define MIN 30
#define MAX 61

/*FUNCTION DECLARATION*/

void add(); // Ask user for input.
void view();            // Diplay all the record.
void search();          // Search for a record.
void c_name();          // Changes the name in a record.
void c_add();           // Changes the address in a record.
void c_ct();            // Changes the connection type in a record.
void c_ph();            // Changes the phone in a record.
void del();             // Deletes a record.
int date(void);         // This fucntion is for displaying System Date.


/*VALIDATION FUNCTIONS*/

void check_id(char *,int); //Checks for the length and validity of SIM ID
void check_name(char *,int);    //Checks for the length and validity of NAME
void check_add(char *,int);     //Checks for the length and validity of ADDRESS
void check_phone(char *,int);   //Checks for the length and validity of PHONE NUMBER
void check_ct(int *);           //Checks for the CONNECTION TYPE
int duplicate(char idno[]); //Checks for the Duplication of the SIM ID

/*MISC FUNCTION*/

void empty();

/*STRUCTURE DECLARATION*/

struct customer
{
char id[NUM]; /* ID of Length 5*/
char name[MIN]; /* Name of Length 30*/
char address[MAX]; /* Address of Length 61*/
char phone[PH]; /* Phone Number of Length 12*/
int  connection; /* Connection Type of length 2*/
int  day;               /* It is used to display the day of length 2*/
int  month;             /* It is used to display the month of length 2*/
int  year;              /* it is used to display the year*/
}rec;

/***************************MAIN FUNCTION STARTS*****************************/
main()
{
int c;

do{
int i;
window(1,1,80,25);
textbackground(LIGHTRED);
textcolor(LIGHTGRAY);

clrscr();

gotoxy(24,2);
printf("<< MOBILINK CUSTOMER DATABASE >>");
gotoxy(24,3);
cprintf("<< Adeel TaLLaT     >>");


for(i=10;i<=70;i++) //This 'FOR' loop will print a combination of
{                       //asteriks '*' and dash '-' horizontally till
gotoxy(i,4);    //the 70th column is reached.
printf("Ü");
gotoxy(71,4);    //the 70th column is reached.
printf("Ü");
gotoxy(i,7);
printf("-");
gotoxy(i,9);
printf("-");
gotoxy(i,11);
printf("-");
gotoxy(i,13);
printf("-");
gotoxy(69,16);
printf("ß");
gotoxy(71,16);
printf("ß");
gotoxy(i,16);
printf("ß");
}

for(i=5;i<=15;i++)      //This 'FOR' loop will print asteriks '*'
{                       //vertically till the 15th row is reached.
gotoxy(10,i);
printf("ÝÞ");
gotoxy(70,i);
printf("ÝÞ");
}

for(i=6;i<=12;i++)      //This 'FOR' loop will print '|' sign
{                       //vertically till the 12th row is reached.
gotoxy(40,i);
printf("|");
}

gotoxy(15,6);
printf("[1] ENTER A RECORD");
gotoxy(15,8);
printf("[2] VIEW RECORDS");
gotoxy(15,10);
printf("[3] SEARCH RECORD");
gotoxy(15,12);
printf("[4] DELETE RECORD");
gotoxy(43,6);
printf("[5] CHANGE NAME");
gotoxy(43,8);
printf("[6] CHANGE ADDRESS");
gotoxy(43,10);
printf("[7] CHANGE CONNECTION TYPE");
gotoxy(43,12);
printf("[8] CHANGE PHONE NUMBER");
gotoxy(37,14);
printf("[9] EXIT");
gotoxy(10,20);
textcolor(LIGHTGRAY);
cprintf("PLEASE SELECT A CHOICE: ");

c = (getche());

switch (c)      //This 'SWITCH' structure will ask the user for input from [1] to [9] and will display error on Invalid Entry.
{
case '1': add();    break;   //It will call the add() funtion to allow user to add and save record.
case '2': view();   break;   //It will call the view() funtion to allow user to view the saved record.
case '3': search(); break;   //It will call the search() funtion to allow user to search records.
case '4': del();    break;   //It will call the delete() funtion to allow user to delete a saved record.
case '5': c_name(); break;   //It will call the c_name() funtion to allow user to modify name of a saved record.
case '6': c_add();  break;   //It will call the c_add() funtion to allow user to modify address of a saved record.
case '7': c_ct();   break;   //It will call the c_ct() funtion to allow user to modify  connection type & date of a saved record.
case '8': c_ph();   break;   //It will call the c_ph() funtion to allow user to modify phone number of a saved record.
case '9': gotoxy(30,24);printf("<<<<--THANK YOU-->>>>");getch();exit(0);  //It will terminate the program.
default:
sound(500);delay(200);nosound(); //sound turns the PC speaker on at the specified frequency, nosound turns the PC speaker off.
gotoxy(26,24);
puts("<<--ENTER FROM 1-9 PLEASE-->>"); //This message will only print on INVALID ENTRY and Will ask again for input.
getch();
}

}while(c != '9'); //This 'WHILE' loop will continue till the Exit option [9] is entered.

return 0;
}
/**************************END OF MAIN FUNCTION******************************/
void add()
{
FILE *fp;
char ch;
char temp[NUM];



if(fp!=NULL)
{
do{
fp=fopen("customer.rec","ab");

clrscr();

gotoxy(25,2);
printf("<<==--ADDING RECORD--==>>");
do{
  gotoxy(1,4);
  printf("Enter [UNIQUE] SIM ID: ");
  check_id(temp, NUM);

}while(duplicate(temp));   //This Condition Will keep on executng till a non duplicate ID is entered.

strcpy(rec.id, temp);     //After verifying, ID in 'temp' will copy to 'rec.id'

gotoxy(1,6);
printf("Enter NAME: ");
check_name(rec.name,MIN);       //It will verify that no neumerical input is given & the name input should not exceed the length limit
gotoxy(1,8);
printf("Enter ADDRESS: ");
check_add(rec.address,MAX);     //It will verify that only alpha neumeric input is entered & should not exceed the length limit
gotoxy(1,10);
printf("Enter PHONE Number: ");
check_phone(rec.phone, PH);     //It will verify phone number of a particular length
gotoxy(1,12);
printf("Enter CONNECTION TYPE: ");
check_ct(&rec.connection);     //It will allow user to select between 'STAR' & 'JAZZ' only

{
  struct date d;

  getdate(&d);
  gotoxy(1,12);
  printf("DATE OF PURCHASE: %d %d %d ",d.da_day,d.da_mon,d.da_year);
  rec.day   = d.da_day;
  rec.month = d.da_mon;
  rec.year  = d.da_year;
}

window(1,1,80,25);
gotoxy(1,12);

fwrite(&rec, sizeof(rec), 1, fp); //Record will be written to file.

fclose(fp);
 textbackground(LIGHTRED);
gotoxy(20,24);

printf("DO YOU WISH TO CONTINUE..[Y]/[N]: ");
ch=toupper(getche());

}while(ch =='Y'); //Repeat loop while user inputs 'Y'
}
else
{
gotoxy(20,24);
printf("Unable To Open File...");
}
}

/**************************************************************************
| Function: view()                                                  |
|                                                                         |
| Purpose : To Display all the records in the file in a                   |
|           formatted output.                                             |
|                                                                         |
| Local Variables:                                                        |
|  |
| >FILE *fp: Decalres file pointer.                                       |
***************************************************************************/

void view()
{
FILE *fp;


fp=fopen("customer.rec","rb");

if(fp!=NULL)
{
clrscr();

while( fread(&rec, sizeof(rec), 1, fp))
if(strcmp(rec.id," ")!=0)
{

clrscr();
gotoxy(28,2);
printf("<<==--VIEWING RECORDS--==>>");
gotoxy(3,5);
printf(" SIM ID: %s",rec.id);
gotoxy(3,7);
printf(" NAME: %s",rec.name);
gotoxy(3,9);
printf(" ADDRESS: %s",rec.address);
gotoxy(3,11);
printf(" PHONE Number: %s",rec.phone);
gotoxy(3,13);
printf(" CONNECTION TYPE: %s",rec.connection);
if(rec.connection==1)
{
gotoxy(21,13);
printf("STAR");
}
else
if(rec.connection==2)
{
gotoxy(21,13);
printf("JAZZ");
}
gotoxy(3,15);
printf(" DATE Of PURCHASE: %d-%d-%d",rec.day,rec.month,rec.year);

getch();
}

if(strcmp(rec.id," ")==NULL)
{
clrscr();
gotoxy(20,13);
sound(500);delay(200);nosound();
printf("<<==--THERE ARE NO RECORDS TO VIEW--==>>");
getch();
}
fclose(fp);
}
else
{
gotoxy(20,24);
printf("Unable To Open File...");
}
}
/*************************END OF VIEW FUNCTION*******************************/


void search()
{
int ch;
FILE *fp;
char sno[NUM];
clrscr();

fp=fopen("customer.rec","rb");

gotoxy(20,13);
printf("PLEASE ENTER SIM ID ==->>>> ");
check_id(sno,NUM);

while ( fread(&rec, sizeof(rec), 1, fp) && strcmp(sno,rec.id) );


ch=strcmp(sno,rec.id);
if(ch!=0)
{
clrscr();
sound(500);delay(200);nosound();
gotoxy(28,13);
printf("<<--RECORD NOT FOUND-->>");
getch();
}
else

{
clrscr();
gotoxy(28,2);
printf("<<==SEARCH SUCCESSFUL==>>");
gotoxy(3,5);
printf(" SIM ID: %s",rec.id);

gotoxy(3,7);
printf(" NAME: %s",rec.name);

gotoxy(3,9);
printf(" ADDRESS: %s",rec.address);

gotoxy(3,11);
printf(" PHONE Number: %s",rec.phone);

gotoxy(3,13);
printf(" CONNECTION TYPE: %s",rec.connection);
if(rec.connection==1)
{
gotoxy(21,13);
printf("STAR");
}
else
if(rec.connection==2)
{
gotoxy(21,13);
printf("JAZZ");
}
gotoxy(3,15);
printf(" DATE Of PURCHASE: %d-%d-%d",rec.day,rec.month,rec.year);

getch();
}
fclose(fp);
}

/*************************END OF SEARCH FUNCTION*****************************/



void c_name()
{
int ch;
FILE *fp;
char sno[NUM];
clrscr();

fp=fopen("customer.rec","rb+");

gotoxy(20,13);
printf("PLEASE ENTER SIM ID ==->>>> ");
check_id(sno,NUM);

while ( fread(&rec, sizeof(rec), 1, fp) && strcmp(sno,rec.id) );

ch=strcmp(sno,rec.id);
if(ch!=0)
{
clrscr();
sound(500);delay(200);nosound();
gotoxy(28,13);
printf("<<--RECORD NOT FOUND-->>");
getch();
}
else
{
clrscr();
gotoxy(28,4);
printf("<<<--==RECORD FOUND==-->>>");

gotoxy(3,8);
printf("OLD NAME: %s",rec.name);   // This will print the old name already stored.
gotoxy(3,12);
printf("ENTER NAME: ");            // This will allow the user to change the old name and input a new name.
check_name(rec.name,MIN);          // The check will validate each character entered turn by turn.

fseek(fp, ftell(fp) - sizeof(rec),0);
 fwrite(&rec, sizeof(rec), 1, fp);  //The new name will be added to the record.

gotoxy(34,24);
printf("RECORD SAVED !!!");

getch();
}
fclose(fp);
}

/*************************END OF CHANGE NAME FUNCTION************************/


void c_add()
{
int ch;
FILE *fp;
char sno[NUM];
clrscr();

fp=fopen("customer.rec","rb+");

gotoxy(20,13);
printf("PLEASE ENTER SIM ID ==->>>> ");
check_id(sno,NUM);

while ( fread(&rec, sizeof(rec), 1, fp) && strcmp(sno,rec.id) );

ch=strcmp(sno,rec.id);  // Here the result after comparing the values of the two strings by using the 'strcmp' function is assigned to 'ch'.
if(ch!=0)       // This loop will execute if the SIM ID's dont compare.
{
clrscr();
sound(500);delay(200);nosound();
gotoxy(28,13);
printf("<<--RECORD NOT FOUND-->>");
getch();
}
else            // If the ID's match, then this block will be executed.
{
clrscr();
gotoxy(28,4);
printf("<<<--==RECORD FOUND==-->>>");

gotoxy(3,8);
printf("OLD ADDRESS: %s",rec.address);
gotoxy(3,12);
printf("ENTER NEW ADDRESS: ");
check_add(rec.address,MAX);

fseek(fp, ftell(fp) - sizeof(rec),0); //fseek sets the file pointer associated with a stream to a new position.
 fwrite(&rec, sizeof(rec), 1, fp); //fwrite appends a specified number of equal-sized data items to an output file.

gotoxy(34,24);
printf("RECORD SAVED !!!");

getch();
}
fclose(fp);
}

/*************************END OF CHANGE ADDRESS FUNCTION*********************/



void c_ct()
{
int ch;
FILE *fp;
char sno[NUM];
clrscr();

fp=fopen("customer.rec","rb+");

gotoxy(20,13);
printf("PLEASE ENTER SIM ID ==->>>> ");
check_id(sno,NUM);

while ( fread(&rec, sizeof(rec), 1, fp) && strcmp(sno,rec.id) );

ch=strcmp(sno,rec.id);
if(ch!=0)
{
clrscr();
sound(500);delay(200);nosound();
gotoxy(28,13);
printf("<<--RECORD NOT FOUND-->>");
getch();
}
else
{
clrscr();
gotoxy(28,2);
printf("<<<--==RECORD FOUND==-->>>");

gotoxy(3,6);
printf("OLD DATE OF PURCHASE: %d %d %d",rec.day,rec.month,rec.year); //Old Date Of purchase is displayed.
{
struct date d;

getdate(&d);
gotoxy(3,8);
printf("NEW DATE OF PURCHASE: %d %d %d ",d.da_day,d.da_mon,d.da_year); //New Date Of Purchase is stored.
rec.day = d.da_day;
rec.month = d.da_mon;
rec.year= d.da_year;
}

gotoxy(3,10);
printf("OLD CONNECTION TYPE: %s",rec.connection); //Old Connection type is displayed
if(rec.connection==1)
{
gotoxy(24,10);
printf("STAR");  //If option stored in record is '1' Then "STAR" will be displayed on screen.
}
else
if(rec.connection==2)
{
gotoxy(24,10);
printf("JAZZ");  //If option stored in record is '2' Then "JAZZ" will be displayed on screen.
}
gotoxy(3,12);
printf("NEW CONNECTION TYPE: "); //New Connection Type is asked for Input.
check_ct(&rec.connection);       //This check provides choices to the user to select b/w "STAR" or "JAZZ".

fseek(fp, ftell(fp) - sizeof(rec),0);
 fwrite(&rec, sizeof(rec), 1, fp);

window(1,1,80,25);
gotoxy(1,12);
gotoxy(35,24);
printf("RECORD SAVED !!!");

getch();
}
fclose(fp);
}

/*******************END OF CHANGE CONNECTION TYPE FUNCTION*******************/



void c_ph()
{
int ch;
FILE *fp;
char sno[NUM];
clrscr();

fp=fopen("customer.rec","rb+");

gotoxy(20,13);
printf("PLEASE ENTER SIM ID ==->>>> ");
check_id(sno,NUM);

while ( fread(&rec, sizeof(rec), 1, fp) && strcmp(sno,rec.id) );

ch=strcmp(sno,rec.id);
if(ch!=0)
{
clrscr();
sound(500);delay(200);nosound();
gotoxy(28,13);
printf("<<--RECORD NOT FOUND-->>");
getch();
}
else
{
clrscr();
gotoxy(28,4);
printf("<<<--==RECORD FOUND==-->>>");

gotoxy(3,8);
printf("OLD PHONE NUMBER: %s",rec.phone); //Old phone number is displayed.
gotoxy(3,12);
printf("ENTER NEW PHONE NUMBER: ");       //New phone number is asked by the user.
check_phone(rec.phone,PH);

fseek(fp, ftell(fp) - sizeof(rec),0);
 fwrite(&rec, sizeof(rec), 1, fp);

gotoxy(34,24);
printf("RECORD SAVED !!!");

getch();
}
fclose(fp);
}

/*********************END OF CHANGE PHONE NUMBER FUNCTION********************/


void del()
{
int ch;
FILE *fp;
char sno[NUM];
clrscr();

fp=fopen("customer.rec","rb+");

gotoxy(20,13);
printf("PLEASE ENTER SIM ID ==->>>> ");
check_id(sno,NUM);

while ( fread(&rec, sizeof(rec), 1, fp) && strcmp(sno,rec.id) );

ch=strcmp(sno,rec.id);
if(ch!=0)
{
clrscr();
sound(500);delay(200);nosound();
gotoxy(28,13);
printf("<<--RECORD NOT FOUND-->>");
getch();
}
else     //Here the saved record is displayed.
{
clrscr();
gotoxy(28,2);
printf("<<==SEARCH SUCCESSFUL==>>");
gotoxy(3,5);
printf(" SIM ID: %s",rec.id);

gotoxy(3,7);
printf(" NAME: %s",rec.name);

gotoxy(3,9);
printf(" ADDRESS: %s",rec.address);

gotoxy(3,11);
printf(" PHONE Number: %s",rec.phone);

gotoxy(3,13);
printf(" CONNECTION TYPE: %s",rec.connection);
if(rec.connection==1)
{
gotoxy(21,13);
printf("STAR");
}
else
if(rec.connection==2)
{
gotoxy(21,13);
printf("JAZZ");
}
gotoxy(3,15);
printf(" DATE Of PURCHASE: %d-%d-%d",rec.day,rec.month,rec.year);

gotoxy(26,24);
printf("PRESS ANY KEY TO DELETE RECORD.....");
getch();
empty(); //this is a Function which will erase the record in memory & NOT physically.

fseek(fp, ftell(fp) - sizeof(rec), 0);
 fwrite(&rec, sizeof(rec), 1, fp);

clrscr();
sound(500);delay(200);nosound();
gotoxy(33,13);
printf("RECORD DELETED !!!");
getch();
}

fclose(fp);
}

/***************************END OF DELETE FUNCTION***************************/


void check_id(char *p, int size)
{
int i=0;
char ch;

do
{
ch=getch();

if( (ch>='0' && ch<='9') && (i<size-1) )
{
*p=ch;        //The value is assigned to the pointer.
p++;          //Pointer is incremented.
i++;          //Length counter is incremented.
printf("%c",ch); //Validated character is printed.
}
else      //This block of commands controls backspace.
if(ch==8 && i>0)
{
printf("%c%c%c",8,32,8);
i--;              //Length counter is decremented.
p--;              //Pointer is decremented.
}

}while(ch!=13 || i<size-1);       //this loop will continue till the total given length is reached or 'ENTER' is pressed.

*p='\0';

}


void check_phone(char *ph, int size)
{
int p=0;
char s[20],ch;

do
{
ch=getch();

if( ((ch>='0' && ch<='9')||(ch=='-')) && (p<size-1) )
{
*ph=ch;
ph++;
p++;
printf("%c",ch);
}

else

if( (ch==8)&&(p>0) )
{
printf("%c%c%c",8,32,8);
p--;
ph--;
}

}while(ch!=13 || p<size-1);

s[p]='\0';
*ph='\0';

}

void check_name(char *p, int size)
{
int j=0;
char x[40],cj;

do
{

cj=toupper(getch());

if(((cj>='a' && cj<='z')||(cj>='A' && cj<='Z')||(cj==' ')) && (j<size-1))
{
*p++=cj;
j++;
printf("%c",cj);
}

else

if(cj==8 && j>0)
{
printf("%c%c%c",8,32,8);
j--; p--;
}

}while(cj!=13);

x[j]='\0';
*p='\0';
}

void check_add(char *p, int size)
{
int l=0;
char a[50],ad;

do
{

ad=toupper(getch());

if(((ad>='a' && ad<='z')||(ad>='A' && ad<='Z')||(ad==' ')||(ad==',')||(ad=='.')||(ad>='0' && ad<='9')) && (l<size-1))
{
*p++=ad;
l++;
printf("%c",ad);
}

else

if( (ad==8)&&(l>0) )
{
printf("%c%c%c",8,32,8);
l--; p--;
}

}while(ad!=13);

a[l]='\0';   *p='\0';
}
void check_ct(int *p)
{
int ab=1;
char ch;
int aa;

window(24,18,51,24);
textbackground(BLACK);
clrscr();

window(25,19,50,23);
textbackground(WHITE);
textcolor(BLACK);
clrscr();

gotoxy(11,2);
printf("STAR");
gotoxy(11,4);
printf("JAZZ");

    do{
ch = getch();

if(ch==0)
{
ch = getch();

if(ch==72)
{
ab=ab-1;
}
else
if(ch==80)
{
ab=ab+1;
}
}

if(ab<1)
{
ab=1;

}
else

if(ab==1)
{
window(1,1,80,25);
window(35,22,40,22);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
printf("JAZZ");
window(1,1,80,25);
window(35,20,41,20);
textcolor(RED);
clrscr();
printf("STAR");
aa=1;
window(1,1,80,25);
}

else

if(ab==2)
{
window(1,1,80,25);
window(35,20,41,20);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
printf("STAR");
window(1,1,80,25);
window(35,22,40,22);
textcolor(RED);
clrscr();
aa=2;
printf("JAZZ");
window(1,1,80,25);
}

if(ab>2)
{
ab=2;
}
    }while(ch != 13);

if (aa==1)
{
gotoxy(24,12);
printf("STAR");
*p=1;
}
else

if (aa==2)
{
gotoxy(24,12);
printf("JAZZ");
*p=2;
}


getch();
window(24,18,51,24);
textbackground(LIGHTRED);
textcolor(LIGHTGRAY);
clrscr();

}

void empty()
{
     strcpy(rec.id," ");
     strcpy(rec.name," ");
     strcpy(rec.address," ");
     strcpy(rec.phone," ");
     rec.connection=0;
     rec.day=0;
     rec.month=0;
     rec.year=0;
}


int duplicate(char idno[])
{
    FILE *fp;

    fp=fopen("customer.rec","rb");

    while(fread(&rec, sizeof(rec), 1, fp) && strcmp(rec.id, idno));
    fclose(fp);

    if(strcmp(rec.id, idno)==0)
       return 1;
    else
       return 0;
}


/*²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
²****************************|        |*****************************²
²****************************| END OF PROJECT |*****************************²
²****************************|        |*****************************²
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²*/

 

Wednesday, 4 March 2015

MANIPULATE THE SIZE OF SQUARE IN C CODE

#include<stdio.h>
int  square(int side);
int main()
{       int a;
        printf("ENTER THE SIDE OF SQUARE      :      "); scanf("%d",&a);
        square(a);
return 0;
}
int  square(int side)
{   int i,j;
                for(i=1;i<=side;i++)
                      {for(j=1;j<=side;j++)
                             printf("*");
                        printf("\n");}
}

Tuesday, 3 March 2015

C PROGRAM FOR ELIGIBILITY AND SALARY OF EMPLOYEE

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

int main(int argc, char *argv[])
{
   
    char  ch,qual;
    int a;

    printf("enter the gender\n[M] for male\n[FM] for female\n qualification \n[G] for graduate \n[P] for post graduate\n year of service ");
    scanf("%c%c%d",&ch,&qual,&a);
   
    getchar();
    if(ch=='M' || ch=='m'){
                     if(qual=='g'  ||  qual=='G'){
                                            if(a>=10)
                                                      printf("salary is 10000");
                                             else
                                                       printf(" salary is 7000");
                                            }
                     else if(qual=='p' || qual=='P'){
                                            if(a>=10)
                                                      printf("salary is 15000");
                                             else
                                                       printf(" salary is 10000");
                                            }
                     }
     else if(ch=='FM' || ch=='fm') {
                     if(qual=='g'  ||  qual=='G'){
                                            if(a>=10)
                                                      printf("salary is 9000");
                                             else
                                                       printf(" salary is 6000");
                                            }
                     else if(qual=='p' || qual=='P'){
                                            if(a>=10)
                                                      printf("salary is 12000");
                                             else
                                                       printf(" salary is 10000");
                                            }
                               }
     else
           printf("invalid entry");

 
  system("PAUSE");
  return 0;
}