Saturday 1 February 2014

Mini project Employee record system using C

The employee record system is very simple and for very beginner mini project. It is based one the menu-driven program for elementary database management. It employ all the basic technique of file handling in C. It consists of following features
  • Writing the data in binary file
  • Reading the data from binary file
  • Modify the record
  • Delete the record
This project is a learning milestone for beginner who want to step into the database management project in C.
Source Code
/**
*  A menu-driven program for elementary database management
*  @author: Bibek Subedi
*  @language: C
*  This program uses file handling in Binary mode
*/

/// List of library functions
#include <stdio.h> ///for input output functions like printf, scanf
#include <stdlib.h>
#include <conio.h>
#include <windows.h> ///for windows related functions (not important)
#include <string.h>  ///string operations

/** List of Global Variable */
COORD coord = {0,0}; /// top-left corner of window

/**
    function : gotoxy
    @param input: x and y coordinates
    @param output: moves the cursor in specified position of console
*/
void gotoxy(int x,int y){
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

/** Main function started */

int main(){
    FILE *fp, *ft; /// file pointers
    char another, choice;

    /** structure that represent a employee */
    struct emp{
        char name[40]; ///name of employee
        int age; /// age of employee
        float bs; /// basic salary of employee
    };

    struct emp e; /// structure variable creation

    char empname[40]; /// string to store name of the employee

    long int recsize; /// size of each record of employee

    /** open the file in binary read and write mode
    * if the file EMP.DAT already exists then it open that file in read write mode
    * if the file doesn't exit it simply create a new copy
    */
    fp = fopen("EMP.DAT","rb+");
    if(fp == NULL){
        fp = fopen("EMP.DAT","wb+");
        if(fp == NULL){
            printf("Connot open file");
            exit(1);
        }
    }

    /// sizeo of each record i.e. size of structure variable e
    recsize = sizeof(e);

    /// infinite loop continues untile the break statement encounter
    while(1){
        system("cls"); ///clear the console window
        gotoxy(30,10); /// move the cursor to postion 30, 10 from top-left corner
        printf("1. Add Record"); /// option for add record
        gotoxy(30,12);
        printf("2. List Records"); /// option for showing existing record
        gotoxy(30,14);
        printf("3. Modify Records"); /// option for editing record
        gotoxy(30,16);
        printf("4. Delete Records"); /// option for deleting record
        gotoxy(30,18);
        printf("5. Exit"); /// exit from the program
        gotoxy(30,20);
        printf("Your Choice: "); /// enter the choice 1, 2, 3, 4, 5
        fflush(stdin); /// flush the input buffer
        choice  = getche(); /// get the input from keyboard
        switch(choice){
            case '1':  /// if user press 1
                system("cls");
                fseek(fp,0,SEEK_END); /// search the file and move cursor to end of the file
                                        /// here 0 indicates moving 0 distance from the end of the file

                another = 'y';
                while(another == 'y'){ /// if user want to add another record
                    printf("\nEnter name: ");
                    scanf("%s",e.name);
                    printf("\nEnter age: ");
                    scanf("%d", &e.age);
                    printf("\nEnter basic salary: ");
                    scanf("%f", &e.bs);

                    fwrite(&e,recsize,1,fp); /// write the record in the file

                    printf("\nAdd another record(y/n) ");
                    fflush(stdin);
                    another = getche();
                }
                break;
            case '2':
                system("cls");
                rewind(fp); ///this moves file cursor to start of the file
                while(fread(&e,recsize,1,fp)==1){ /// read the file and fetch the record one record per fetch
                    printf("\n%s %d %.2f",e.name,e.age,e.bs); /// print the name, age and basic salary
                }
                getch();
                break;

            case '3':  /// if user press 3 then do editing existing record
                system("cls");
                another = 'y';
                while(another == 'y'){
                    printf("Enter the employee name to modify: ");
                    scanf("%s", empname);
                    rewind(fp);
                    while(fread(&e,recsize,1,fp)==1){ /// fetch all record from file
                        if(strcmp(e.name,empname) == 0){ ///if entered name matches with that in file
                            printf("\nEnter new name,age and bs: ");
                            scanf("%s%d%f",e.name,&e.age,&e.bs);
                            fseek(fp,-recsize,SEEK_CUR); /// move the cursor 1 step back from current position
                            fwrite(&e,recsize,1,fp); /// override the record
                            break;
                        }
                    }
                    printf("\nModify another record(y/n)");
                    fflush(stdin);
                    another = getche();
                }
                break;
            case '4':
                system("cls");
                another = 'y';
                while(another == 'y'){
                    printf("\nEnter name of employee to delete: ");
                    scanf("%s",empname);
                    ft = fopen("Temp.dat","wb");  /// create a intermediate file for temporary storage
                    rewind(fp); /// move record to starting of file
                    while(fread(&e,recsize,1,fp) == 1){ /// read all records from file
                        if(strcmp(e.name,empname) != 0){ /// if the entered record match
                            fwrite(&e,recsize,1,ft); /// move all records except the one that is to be deleted to temp file
                        }
                    }
                    fclose(fp);
                    fclose(ft);
                    remove("EMP.DAT"); /// remove the orginal file
                    rename("Temp.dat","EMP.DAT"); /// rename the temp file to original file name
                    fp = fopen("EMP.DAT", "rb+");
                    printf("Delete another record(y/n)");
                    fflush(stdin);
                    another = getche();
                }
                break;
            case '5':
                fclose(fp);  /// close the file
                exit(0); /// exit from the program
        }
    }
    return 0;
}

Output

employee

Mini project “Calendar Application” Using C/C++ – Free download.

Mini project “Calendar Application” is also a simple project made using C. It uses many windows properties to make it colorful, for example, to indicate the vacation, it uses the red foreground color.The calendar can be used for two purposes. First to  see the date and month as usual calendars and second to find out the day corresponding to given date. Some of the silent features of the project are
  • It uses various windows properties to make the program colorful although it has lack of graphics
  • It entirely uses C codes, code is written in simple manner with lots of comments
  • Important notes can be added. The date with such notes appears different than others with red background color.
  • The months can be navigated using arrows key
Some of the screen shots of the program are:
Enter date (DD MM YYYY) : 10 03 1991 Day is : Sunday Press any key to continue......

Calendar1
Calendar2

DOWNLOAD CALENDAR APPLICATION 

Friday 31 January 2014

Minor project: Cyber Management System using C

cyber_thumb[2] C.yber is a program which interconnects different computers which allows users to communicate over the computer network and provide security from unauthorized users by login system in client server.The project aims at managing the cyber cafe with multiple clients and give the clients access of services that the cyber is providing when it log in. The clients can log in as members or guests and use the services that the cyber is providing. The client can request services of cafe like tea, coffee or any others.
The above program uses file handling as database. The various features of C like Multithreading, Socket (Networking) are used. It is very useful for the student or beginner in the programming field. It also helps in building the other type of management software or projects like library management, student record, and many more.
Block Diagram:
basic operation_thumb[6]

DOWNLOAD CYBER MANAGEMENT PROJECT

Mini Project Student Record System in C

Mini project student record system is another project based on programming language C. It also uses files as database. This project is similar to another mini projects of this blog but every project in Programming Techniques has unique style of coding and presentation so that reader get clear about all aspects of programming. In this project, a console window is virtually splited into two parts, one is static which doesn’t change and another is dynamic which changes time to time. The text are written using various colors to make them static.
The program is not complete, reader can download the code and make improvement and also make it complete. There are many places for further enhancement. I think this project becomes milestone for your study of C programming and making project using C.
Student Record System

C Mini Project Ideas with a Sample Calculator Project

Do you want to build a simple application in C but you don’t know how and where to start? Or you know how to build a C application but don’t know any project ideas? then do not worry you are at the right place. If you finished learning  C and became familiar with its programming paradigm then I encourage you to build some applications (weather it is application software or system software) to actually sharpen your skills in C. If you do projects, then you will know how to apply those programming constructs accurately in building a projects. So here I will explain how to start a new C projects for completely beginner and give some projects ideas about what type of application you can build using C language
I have some suggestions for people who are about to write their first C application.

  1. Please do not try to build a big application. Start with a very very simple application, try to learn how it works and only then think about how to extend it to build relatively large application.
  2. Always start with console application, without including attractive graphics. To include graphics, you must learn about different graphics libraries. Just thing you are the beginner of C just trying to build some application, and if you include graphics then you must learn the graphics libraries more than C. This creates you confusion and you mind will be messed up. So please try to build some console application first.
  3. Try to include more programming constructs. Since building first application implies making you strong and confident in C, always try to include more and more programming constructs. This includes basics arithmetic operations, conditional statements (if, if else), loops ( while, do while, for), user defined functions, arrays, pointers, structure, dynamic memory allocation, file handling and if possible some low level operations. You just combine these techniques and try to build an application that includes most of them.
For the very beginner who have not done any project on C but know the programming, I have attached a compressed file please download and read this. It consists of two files one pdf file which is the simple documentation of the project and other a C file which contains some coding with lot of comments that help you to step through the project. The project is about building a simple Calculator that solve some mathematical operations like Matrix, Complex number, polynomial equations etc. Please read the documentation carefully. And also the C file consists of some pre written code. Run the code and see the sample output of some operations and I am sure you will get some idea about creating a simple project. If you faced any difficulties then free feel to ask as a comment.
You can download the file for here.
Now for those who want some project ideas. There are lot of projects that you can do in C. For example, consider a project that keeps record of your daily expenses. And at the end of the month shows you the record of total expenses you did over a month along with on which items you made the expense. Similarly think of a application that keeps record of the employees in an organization, you can register new employee, search the employee, edit the record of employee and perform other various operation. or you can make simple non-graphics game like hangman, tic-tac-toe etc. Other various projects ideas are presented below
  1. Library management system
  2. Calendar application
  3. Employee record system
  4. Quiz application
  5. Student record system
  6. Electricity billing system
  7. Supermarket billing system
  8. Contact manager
  9. Calculator
  10. Simple encryption algorithm
  11. Physics problem solver
  12. Sudoku solver (difficult)
  13. Generate different start patterns
  14. Hospital management system
  15. Random number generator
  16. Electric circuit solver etc.

Thursday 30 January 2014

Drawing a Circle with Mid – Point Circle Algorithm in C/C++

As in the previous line drawing algorithm, we sample at unit intervals and determine the closest pixel position to the specified circle path at each step. For a given radius r and screen center position (xc, yc), we can first set up our algorithm to calculate pixel positions around a circle path centered at the coordinate origin (0, 0). Then each calculated  position (x, y) is moved to its proper screen position by adding xc to x and yc to y. Along the circle section from x = 0 to x = y in the first quadrant, the slope of the curve varies from 0 to –1.  Therefore, we can take unit steps in the positive x direction over this octant and use a decision parameter to determine which of the two possible y positions is closer to the circle path at each step. Position in the other seven octants are then obtained by symmetry.

The following section implements Mid – Point Cicle  Algorithm in C/C++. The source code is complied using gcc Compiler and Code::Blocks IDE. To print a pixel, SetPixel() function of windows.h is used.
Note: to run this code in your machine with Code::blocks IDE, add a link library libgdi32.a (it is usually inside MinGW\lib )  in linker setting.
Souce Code
#include <windows.h>
#include <cmath>
#define ROUND(a) ((int) (a + 0.5))
/* set window handle */
static HWND sHwnd;
static COLORREF redColor=RGB(255,0,0);
static COLORREF blueColor=RGB(0,0,255);
static COLORREF greenColor=RGB(0,255,0);
void SetWindowHandle(HWND hwnd){
    sHwnd=hwnd;
}
/* SetPixel */
void setPixel(int x,int y,COLORREF& color=redColor){
    if(sHwnd==NULL){
        MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR);
        exit(0);
    }
    HDC hdc=GetDC(sHwnd);
    SetPixel(hdc,x,y,color);
    ReleaseDC(sHwnd,hdc);
    return;
}
void circlePoints(int xCenter, int yCenter, int x, int y){
    setPixel(xCenter + x, yCenter + y);
    setPixel(xCenter - x, yCenter + y);
    setPixel(xCenter + x, yCenter - y);
    setPixel(xCenter - x, yCenter - y);
    setPixel(xCenter + y, yCenter + x);
    setPixel(xCenter - y, yCenter + x);
    setPixel(xCenter + y, yCenter - x);
    setPixel(xCenter - y, yCenter - x);
}
void drawCircle(int xCenter, int yCenter, int radius){
    int x = 0;
    int y = radius;
    int p = 1 - radius;
    circlePoints(xCenter, yCenter, x, y);
    while(x < y){
        x++;
        if (p < 0){
            p += 2 * x + 1;
        }else{
            y--;
            p += 2 * (x - y) + 1;
        }
        circlePoints(xCenter, yCenter, x, y);
    }
}
/* Window Procedure WndProc */
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
    switch(message){
        case WM_PAINT:
            SetWindowHandle(hwnd);
            drawCircle(200, 200, 100);
            break;
        case WM_CLOSE: // FAIL THROUGH to call DefWindowProc
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default:
        break; // FAIL to call DefWindowProc //
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
    static TCHAR szAppName[] = TEXT("Circle");
    WNDCLASS wndclass;
    wndclass.style         = CS_HREDRAW|CS_VREDRAW ;
    wndclass.lpfnWndProc   = WndProc ;
    wndclass.cbClsExtra    = 0 ;
    wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = hInstance ;
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName  = NULL ;
    wndclass.lpszClassName = szAppName ;
    // Register the window //
    if(!RegisterClass(&wndclass)){
        MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR);
        exit(0);
    }
    // CreateWindow //
    HWND hwnd=CreateWindow(szAppName,"Mid Point Circle Drawing - Programming Techniques",
                WS_OVERLAPPEDWINDOW,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 NULL,
                 NULL,
                 hInstance,
                 NULL);
    if(!hwnd){
        MessageBox(NULL,"Window Creation Failed!","Error",MB_OK);
        exit(0);
    }
    // ShowWindow and UpdateWindow //
    ShowWindow(hwnd,iCmdShow);
    UpdateWindow(hwnd);
    // Message Loop //
    MSG msg;
    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    /* return no error to the operating system */
    return 0;
}
Output

MidPointCicleAlgorithm

Implementing Bresenham’s Line Drawing Algorithm in C/C++

Bresenham’s Line Drawing Algorithm is an accurate and efficient raster line-generating algorithm developed by Bresenham. In this algorithm, we first consider the scan – conversion process for lines with positive slope less than 1. Pixel positions along a line path are then determined by sampling at unit x intervals. Starting from the left end point (x0, y0) of a given line, we step to each successive column (x position) and plot the pixel whose scan – line y values is closet to the line path. Assuming we have determined that the pixel at (x(k), y(k)) is to be displayed, we next need to decide which pixel to plot in column x(k+1). Our choices are the pixels at positions (x(k) + 1, y(k)) and (x(k) + 1, y(k) + 1).

The following section implements Bresenham’s  Algorithm in C/C++. The source code is complied using gcc Compiler and Code::Blocks IDE. To print a pixel, SetPixel() function of windows.h is used.
#include <windows.h>
#include <cmath>
#define ROUND(a) ((int) (a + 0.5))
/* set window handle */
static HWND sHwnd;
static COLORREF redColor=RGB(255,0,0);
static COLORREF blueColor=RGB(0,0,255);
static COLORREF greenColor=RGB(0,255,0);
void SetWindowHandle(HWND hwnd){
    sHwnd=hwnd;
}
/* SetPixel */
void setPixel(int x,int y,COLORREF& color=redColor){
    if(sHwnd==NULL){
        MessageBox(NULL,"sHwnd was not initialized !","Error",MB_OK|MB_ICONERROR);
        exit(0);
    }
    HDC hdc=GetDC(sHwnd);
    SetPixel(hdc,x,y,color);
    ReleaseDC(sHwnd,hdc);
    return;
// NEVERREACH //
}
void drawLineBresenham(int xa, int ya, int xb, int yb){
    int dx = abs(xa - xb), dy = abs(ya - yb);
    int p = 2 * dy - dx;
    int twoDy = 2 * dy, twoDyDx = 2 * (dy - dx);
    int x, y, xEnd;
    if (xa > xb){
        x = xb;
        y = yb;
        xEnd = xa;
    }else{
        x = xa;
        y = ya;
        xEnd = xb;
    }
    setPixel(x, y);
    while(x < xEnd){
        x++;
        if(p < 0)
            p += twoDy;
        else{
            y++;
            p += twoDyDx;
        }
        setPixel(x, y);
    }
}
/* Window Procedure WndProc */
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
    switch(message){
        case WM_PAINT:
            SetWindowHandle(hwnd);
            drawLineBresenham(10, 20, 250, 300);
            break;
        case WM_CLOSE: // FAIL THROUGH to call DefWindowProc
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default:
        break; // FAIL to call DefWindowProc //
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){
    static TCHAR szAppName[] = TEXT("Straight Line");
    WNDCLASS wndclass;
    wndclass.style         = CS_HREDRAW|CS_VREDRAW ;
    wndclass.lpfnWndProc   = WndProc ;
    wndclass.cbClsExtra    = 0 ;
    wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = hInstance ;
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    wndclass.lpszMenuName  = NULL ;
    wndclass.lpszClassName = szAppName ;
    // Register the window //
    if(!RegisterClass(&wndclass)){
        MessageBox(NULL,"Registering the class failled","Error",MB_OK|MB_ICONERROR);
        exit(0);
    }
    // CreateWindow //
    HWND hwnd=CreateWindow(szAppName,"Bresenham's Algorithm - Programming Techniques",
                WS_OVERLAPPEDWINDOW,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 CW_USEDEFAULT,
                 NULL,
                 NULL,
                 hInstance,
                 NULL);
    if(!hwnd){
        MessageBox(NULL,"Window Creation Failed!","Error",MB_OK);
        exit(0);
    }
    // ShowWindow and UpdateWindow //
    ShowWindow(hwnd,iCmdShow);
    UpdateWindow(hwnd);
    // Message Loop //
    MSG msg;
    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    /* return no error to the operating system */
    return 0;
}

The output generated by above program is 

Bresenham

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews