Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C Programming Code Examples

C > Beginners Lab Assignments Code Examples

Graphical Calculator Design

/* Graphical Calculator Design */ :#include<graphics.h> #include<conio.h> #include<dos.h> #include<stdio.h> #include<string.h> #include<math.h> class Window; Window * const SCREEN=(Window *)0; enum buttonstatus{unpushed,pushed}; enum boolean{false,true}; class Window { protected: Window * ptr; int left,right,top,bot; int delta,deltacolor,centercolor; public:Window(){} Window(Window *ptro,int l,int t,int r,int b,int dc,int cc); void Display(); }; class Border:public Window { public: Border(Window *ptro,int l,int t,int r,int b,int dc=DARKGRAY,int cc=BLUE) :Window(ptro,l,t,r,b,dc,cc){} }; class Button:public Window { char text[20]; buttonstatus bs; public: Button(Window *ptro,int l,int t,int r,int b,int cc=BLACK,char *tx="") :Window(ptro,l,t,r,b,BLACK,cc) {strcpy(text,tx);bs=unpushed;} void Click(); void Display(); }; class Output:public Window { char text[80]; public: Output(Window *ptro,int l,int t,int r,int b,int dc=BLUE,int cc=WHITE) :Window(ptro,l,t,r,b,dc,cc){} void Text(char *ch); void Number(double d); }; Window::Window(Window* ptro,int l,int t,int r,int b,int dc,int cc) { ptr=ptro;left=l;right=r;top=t;bot=b;deltacolor=dc;centercolor=cc; delta=(-r-l-b-t)/150+3; if(ptr!=SCREEN) { left+=ptr->left; right+=ptr->right; top+=ptr->top; bot+=ptr->bot; } } void Window::Display() { setcolor(WHITE); int p[10]; p[0]=left;p[1]=top;p[2]=right;p[3]=top;p[4]=right;p[5]=bot; p[6]=left;p[7]=bot;p[8]=left;p[9]=top; setfillstyle(SOLID_FILL,deltacolor); fillpoly(5,p); rectangle(left+delta+1,top+delta,right-delta-1,bot-delta); setfillstyle(SOLID_FILL,centercolor); floodfill(left+(right-left)/2,top+delta+1,WHITE); } void Button::Click() { bs=pushed; Button::Display(); sound(500);delay(10);nosound(); delay(250); bs=unpushed; Button::Display(); sound(400);delay(10);nosound(); } void Button::Display() { Window::Display(); moveto(left+(right-left)/2+1,top+(bot-top)/2); settextjustify(CENTER_TEXT,CENTER_TEXT); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,USER_CHAR_SIZE); setusercharsize(5,8,2,3); setcolor(WHITE); outtext(text); moveto(left,top); lineto(left+delta,top+delta); moveto(right,top); lineto(right-delta,top+delta); moveto(left,bot); lineto(left+delta,bot-delta); moveto(right,bot); lineto(right-delta,bot-delta); setfillstyle(SOLID_FILL,LIGHTGRAY); if(bs==unpushed) { floodfill(left+(right-left)/2,top+1,WHITE); floodfill(left+1,top+(bot-top)/2,WHITE); } else { floodfill(left+(right-left)/2,bot-1,WHITE); floodfill(right-1,top+(bot-top)/2,WHITE); } } void Output::Text(char *ch) { Display(); moveto(right-delta,top+(bot-top)/2); settextjustify(RIGHT_TEXT,CENTER_TEXT); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,USER_CHAR_SIZE); setusercharsize(5,8,1,1); setcolor(BLACK); outtext(ch); } void Output::Number(double d) { char buffer[80]; sprintf(buffer,"%16.2lf",d); Output::Text(buffer); } void main() { int gd,gm; gd=EGA; gm=EGAHI; initgraph(&gd,&gm,"\tc\bgi"); Border br1(SCREEN,240,30,480,330); Output o1(&br1,20,20,-20,-240,BLUE,WHITE); Button b0(&br1,30,230,-175,-35,BLACK,"0"); Button b1(&br1,30,180,-175,-85,BLACK,"1"); Button b2(&br1,78,180,-127,-85,BLACK,"2"); Button b3(&br1,127,180,-78,-85,BLACK,"3"); Button b4(&br1,30,130,-175,-135,BLACK,"4"); Button b5(&br1,78,130,-127,-135,BLACK,"5"); Button b6(&br1,127,130,-78,-135,BLACK,"6"); Button b7(&br1,30,80,-175,-185,BLACK,"7"); Button b8(&br1,78,80,-127,-185,BLACK,"8"); Button b9(&br1,127,80,-78,-185,BLACK,"9"); Button bdiv(&br1,175,80,-30,-185,BLUE,"/"); Button bmul(&br1,175,130,-30,-135,BLUE,"*"); Button bsub(&br1,175,180,-30,-85,BLUE,"-"); Button bdot(&br1,78,230,-127,-35,BLACK,"."); Button bclr(&br1,127,230,-78,-35,RED,"clr"); Button badd(&br1,175,230,-30,-35,BLUE,"+="); br1.Display();o1.Display(); b7.Display();b8.Display();b9.Display(); b4.Display();b5.Display();b6.Display(); b1.Display();b2.Display();b3.Display(); b0.Display();bdot.Display();bclr.Display(); bsub.Display();bdiv.Display();badd.Display();bmul.Display(); o1.Number(0.0); const char ESC=27; char ds[80],tb[80],ch,oper; int num=0; boolean isfirst=true,chain=false; double num1,num2,ans; while((ch=getch())!=ESC) { if(ch>='0'&&ch<='9'||ch=='.') { switch(ch) { case '0':b0.Click();break; case '1':b1.Click();break; case '2':b2.Click();break; case '3':b3.Click();break; case '4':b4.Click();break; case '5':b5.Click();break; case '6':b6.Click();break; case '7':b7.Click();break; case '8':b8.Click();break; case '9':b9.Click();break; case '.':bdot.Click();break; } ds[num++]=ch;ds[num]='

setusercharsize gives you finer control over the size of text from stroked fonts used with graphics functions. The values set by setusercharsize are active only if charsize equals 0, as set by a previous call to settextstyle. With setusercharsize, you specify factors by which the width and height are scaled. The default width is scaled by multx: divx, and the default height is scaled by multy: divy.

Copy string. Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.

Write formatted data to string. Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str. The size of the buffer should be large enough to contain the entire resulting string (see snprintf for a safer version). A terminating null character is automatically appended after the content. After the format parameter, the function expects at least as many additional arguments as needed for format.

The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. Current fill pattern and fill color is used to fill the area. setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern.

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.

rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.

Text output after a call to settextjustify is justified around the current position horizontally and vertically, as specified. The default justification settings are LEFT_TEXT (for horizontal) and TOP_TEXT (for vertical). The enumeration text_just in graphics.h provides names for the horiz and vert settings passed to settextjustify.

setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.

initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver.

Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. settextstyle sets the text font, the direction in which text is displayed, and the size of the characters. A call to settextstyle affects all text output by outtext and outtextxy.

fillpoly() draws the outline of a polygon with number points in the current line style and color (just as drawpoly does), then fills the polygon using the current fill pattern and fill color. The header file graphics.h contains fillpoly() function which is implemented to draw and fill a polygon such as triangle, rectangle, pentagon, hexagon etc. So this function require same arguments as drawpoly().

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming. These operators are used to perform logical operations and used with conditional statements like C if-else statements.

Integral division. Returns the integral quotient and remainder of the division of numer by denom ( numer/denom ) as a structure of type div_t, ldiv_t or lldiv_t, which has two members: quot and rem. The div function returns a structure with the resulting quotient and remainder based on the division calculation.

Function lineto() draws a line from the current position (CP) to the point (x, y), you can get current position using getx and gety function. Where, (x, y) are the coordinates upto which the line will be drawn from previous point. It collectively used with the moveto() function in order to draw the line on the screen. lineto() function contains two parameters.

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.

Delay function is used to suspend execution of a program for a particular time. delay() function requires a parameter which should be a number, defining the milliseconds for the delay. To use delay function in your program you should include the "dos.h" header file which is not a part of standard C library. Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds).

floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled,border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle. floodfill fills an enclosed area on bitmap devices. (x,y) is a "seed point" within the enclosed area to be filled. The area bounded by the color border is flooded with the current fill pattern and fill color.

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,

outtext() function is used to display the string at the current position on the screen. This function displays the given string in the graphics mode. It uses the settextstyle() function in order to set the font style of the string and moveto() function in order to change current position. outtext displays a text string in the viewport, using the current font, direction, and size.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block.

Our system can create various sounds on different frequencies. The sound() is very useful as it can create very nice music with the help of programming and our user can enjoy music during working in out the program. Sound function produces the sound of a specified frequency. Used for adding music to a C program, try to use some random values in loop, vary delay and enjoy.

Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language: Simple assignment operator. Assigns values from right side operands to left side operand. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

The header file graphics.h contains moveto() function which changes the current position to (x, y). moveto() moves the current position (CP) to viewport position (x,y). Means if you want to move a point from the current position to a new position then you can use this function.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.

The nosound() function in C language is used to stop the sound played by sound() function. nosound() function is simply silent the system. Sound function produces the sound of a specified frequency and nosound function turn off the PC speaker.

Check whether the FDC is ready. Read status of main status register. Check whether FDC is ready. Input the command parameters. Enter command parameters. Check the status of...