Function Based Menu Program
Project: by Satyam Singh#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void i_o_str(); //UDF to take a string
int strcomp(char[],char[],int); //UDF to compare two strings
void strcopy(); //UDF to copy a string
void rstrrev(); //UDF to reverse character by character eg: i have a ball, output: i evah a llab
void strCapt(); //UDF to capitalize first alphabet eg: i have a Ball output: I Have A Ball
void convrad(); //UDF to convert angle in degree to radian
void projectile(); //UDF to show the trajectory of a projectile motion
void clock(); //UDF to show time in analogue clock
void main()
{ int opt;
do{ clrscr();
cout<<"\n\t\t WELCOME TO THE PROJECT \n\t\t\t BY- SATYAM SINGH";
getch();
cout<<"\nWhat do you want to do \n1.Comparision of two strings.\
\n2.Copy a string.\n3.Reverse the words of a string.\n4.Capitalize the \
first alphabet of every word.\n5.Convert angle (degree to radians).\
\n6.Projectile Motion. \n7.Clock \n8.EXIT\n Choose your option: ";
cin>>opt;
switch(opt) //Calling functions according to users desire
{ case 1: i_o_str();break;
case 2: strcopy();break;
case 3: rstrrev();break;
case 4: strCapt();break;
case 5: convrad();break;
case 6: projectile();break;
case 7: clock();break;
case 8: cout<<"Thank You ";
getch();
exit(0);
}
}while(opt!=8);
}
//FUNCTION DEFINITIONS STARTS FROM HERE
void i_o_str()
{ clrscr();
char a[20],b[20];
int i=0,compval;
cout<<"\n Enter the two string: ";
gets(a);
gets(b);
compval=strcomp(a,b,i);
cout<<"\n "<<compval;
getch();
}
int strcomp(char a[],char b[],int c)
{ for(int i=0;a[i]!='\0';++i);
for(int j=0;b[j]!='\0';++j);
if(i!=j)
{c=1;
return c;
}
else
{ for(i=0;i<=j;++i)
{if(a[i]>='A'&&a[i]<='Z')
if(a[i]!=b[i]&&a[i]!=(b[i]-32))
{c=1;
break;
}
else if(a[i]>='a'&&a[i]<='z')
if(a[i]!=b[i]&&a[i]!=(b[i]+32))
{c=1;
break;
}
else
if(a[i]!=b[i])
{c=1;
break;
}
}
}
return c;
}
void strcopy()
{ clrscr();
int i,j;
char cstr[50],str[50];
cout<<"\n Enter the string: ";
gets(str);
for(j=0;str[j]!='\0';++j);
for(i=0;i<=j;++i)
cstr[i]=str[i];
puts(cstr);
getch();
}
void rstrrev()
{ clrscr();
int i,j,k,s,l=0;
char b[50],str[50];
cout<<"Enter the string: ";
gets(str);
for(i=0;str[i]!='\0';++i);
for(j=0;j<=i;++j)
{ if(str[j]==' '||j==i||str[j]=='.')
{ k=j-1;
for(s=0;s<l;++s,--k)
{str[k]=b[s];
}
l=0;
}
else
{b[l]=str[j];
++l;}
}
puts(str);
getch();
}
void strCapt()
{ clrscr();
int i,cap=0;
char str[50];
cout<<"\n Enter the string: ";
gets(str);
for(i=0;str[i]!='\0';++i)
{if(str[i]>=97&&str[i]<=122)
if(cap==1||i==0)
{str[i]-=32;
cap=0;
}
if(str[i]>=97&&str[i]<=122)
if(str[i]==' ')
cap=1;
}
puts(str);
getch();
}
void convrad()
{ clrscr();
float pi=3.14,theta,radpi;
cout<<"\n Enter the angle(in degree): ";
cin>>theta;
radpi=theta/180;
cout<<"\n The angle in radians is: "<<radpi*pi<<" or "<<radpi<<"ã";
getch();
}
void projectile()
{ clrscr();
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, ""); //To start graphic mode graphic functions wont work until the
const float pi=3.14,g=9.8; //graph mode is called
float Tf,v0,theta,rangle,T1,T,x,y,x1=0,y1=0;
cout<<"\n Enter the velocity at which the object is thrown: ";
cin>>v0;
cout<<"\n Enter the angle of the velocity vector: ";
cin>>theta;
rangle=(theta*pi)/180; //as trigonometric functions have been used angle is required in radians
Tf=2*v0*sin(rangle)/g;
T1=T=Tf;
float R=2*(v0*cos(rangle)*Tf);
line(200,460,200+R,460); //graphic function to draw line from initial to final position of projectile
cout<<"\n\n\n ";
for(float i=0;i<=Tf;++i)
{
x=2*(v0*cos(rangle)*i);
y=2*((v0*sin(rangle)*i)-(0.5*g*(i*i)));
setcolor(GREEN); //graphic function to draw anything from this point in green color
line(200+x1,460-y1,200+x,460-y); //to draw line from time t to t+1
x1=x,y1=y;
cout<<" TIME LEFT: "<<T--; //to show time left till the object reaches its final position
sound(400); //dos.h function to give a sound of frequency 400 its value can be changed
delay(100); //dos.h function to delay for a particular amount of time
nosound(); //dos.h function to stop the playing sound
delay(900);
cout<<"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
}
line(200+x1,460-y1,200+R,460);
cout<<" TIME LEFT: 0 ";getch();
cout<<"\n\n \
";
getch();
closegraph();
restorecrtmode();
}
void clock()
{clrscr();
int gdriver = DETECT, gmode,hr,min,sec=0,hrs_x,hrs_y,sec_x,sec_y,min_x,min_y;
initgraph(&gdriver, &gmode, "");
struct time t; //making an object t of system defined structure time
int x=getmaxx()/2,y=getmaxy()/2;
setcolor(CYAN);
circle(x,y,150); //graphic function to draw a circle
circle(x,y,170);
setfillstyle(1,BLUE); //graphic function to get the design and colour that has to be use in floodfill
floodfill(319+112,239+101,CYAN); //coordinates of where the colouring should start and end
float PI=(3.14); //(cont.)after reaching a certain colour
char *k[]={"4","5","6","7","8","9","10","11","12","1","2","3"}; //numbers for clock
int a,b;
for(int i=1;i<13;i++)
{ a=140*cos(PI/6*i); //to get the position where the hour number should be placed
b=140*sin(PI/6*i);
outtextxy(a+314,b+235,k[i-1]); //to place the number
}
while(!kbhit()) //to check keystrokes of user
{ gettime(&t); //time.h function to get system time
if(sec!=t.ti_sec)
{
sound(400); // to give a sound every second
delay(200);
nosound();
}
hr=t.ti_hour, min=t.ti_min, sec=t.ti_sec; //to save current hour minute & second in a variable
if(hr>12)
hr-=12;
sec_x=130*cos(((PI/30)*sec)-(PI/2));
sec_y=130*sin(((PI/30)*sec)-(PI/2)); //to get the coordinates of second hand
min_x=100*cos(((PI/30)*min)-(PI/2));
min_y=100*sin(((PI/30)*min)-(PI/2)); //to get the coordinates of minute hand
hrs_x= 80*cos(((PI/6)*hr)-(PI/2))+((PI/360)*min);
hrs_y= 80*sin(((PI/6)*hr)-(PI/2))+((PI/360)*min); //to get the coordinates of hour hand
setcolor(GREEN); //to set colour for second hand
line(x,y,sec_x+x,sec_y+y); //to draw second hand
setcolor(BLUE); //to set colour for minute hand
line(x,y,min_x+x,min_y+y); //to draw minute hand
setcolor(WHITE); //to set colour for hour hand
line(x,y,hrs_x+x,hrs_y+y); //to draw hour hand
delay(50);
setcolor(BLACK);
line(x,y,sec_x+x,sec_y+y);
line(x,y,min_x+x,min_y+y);
line(x,y,hrs_x+x,hrs_y+y); //to remove the last positions of second minute and hour hand
}
getch();
closegraph(); //to close the graph that was opened at the start of the function
restorecrtmode();
}
// END OF PROGRAM --SATYAM SINGH
0 comments :