stack program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define Max_st 5
void Menu();
int stack[Max_st];
int *sp,a,z=1,c=0,x;
int CreateEmptyStack(int
stack[]);
void ShowStack();
void Push(int x);
void Pop();
void main()
{
CreateEmptyStack(stack);
printf("Create empty stack is done \n\n");
ShowStack();
while(z!=0)
{
Menu();
}
getch();
}
void Menu()
{
printf("-------------------------------------------------\n");
printf("*****Selec Mode To Push or Pop******\n");
printf("Push Enter 1\n");
printf("Pop Enter 2\n");
printf("Exit Enter 3\n");
printf("-------------------------------------------------\n");
printf("Enter your selec:");
scanf("%d",&a);
switch(a)
{
case 1: printf("Enter
data to Push:");
scanf("%d",&x);
Push(x);
break;
case 2: system("cls");
Pop();
break;
case 3: z=0;
break;
}
}
int CreateEmptyStack(int
stack[])
{
sp=&stack[-1];
return *sp;
}
void ShowStack()
{
printf("Stack[");
for(int
c=0;c<Max_st;c++)
{
printf(" %d",stack[c]);
}
printf("]\n");
}
void Push(int x)
{
system("cls");
printf("\n****Push %d****\n",x);
if(sp>&stack[Max_st-2])
{
printf("Starck is full!!!!\n\n");
}
else
{
sp++;
*sp=x;
}
ShowStack();
}
void Pop()
{
if(sp<&stack[0])
{
printf("\nStack is empty!!!\n\n");
}
else
{
printf("\nPop %d from stack\n",*sp);
*sp=0;
sp--;
}
ShowStack();
}
Circular Queue
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define Max_q 5
int queue[Max_q];
int *f,*r,a,z=1;
void CreateEmptyQueue(int
queue[])
{
f=&queue[0];
r=&queue[0];
}
void ShowQueue()
{
printf("Queue [");
for(int
c=0;c<=Max_q-1;c++)
{
printf("%d ",queue[c]);
}
printf("]\n\n");
printf("Ba=%d\n",&queue[0]);
printf("F=%d\n",f);
printf("R=%d\n",r);
printf("*F=%d\n",*f);
printf("*R=%d\n",*r);
}
void InsertQueue(int x)
{
printf("*****Insert Data %d*****\n",x);
if(f==r&&*r!=0)
{
printf("Error!!!!\nQueue is Overflow\n");
}
else
{
if(r==&queue[Max_q-1]&&*f!=0)
{
*r=x;
r=&queue[0];
}
else
{
*r=x;
r++;
}
}
ShowQueue();
}
void DeleteQueue()
{
printf("*****Delete Data %d*****\n",*f);
if(f==r&&*r==0)
{
printf("Error!!! \nQueue is Underflow!!!\n");
}
else
{
if(f==&queue[Max_q-1])
{
*f=NULL;
f=&queue[0];
}
else
{
*f=NULL;
f++;
if(f==r)
{
f=&queue[0];
r=&queue[0];
}
}
}
ShowQueue();
}
void Menu()
{
int x;
printf("-------------------------------------------------\n");
printf("*****Selec Mode To
Push or Pop******\n");
printf("InsertQueue
Enter 1\n");
printf("DeleteQueue
Enter 2\n");
printf("Exit
Enter 3\n");
printf("-------------------------------------------------\n");
printf("Enter your selec:");
scanf("%d",&a);
switch(a)
{
case 1: printf("Insert
Data To Queue:");
scanf("%d",&x);
system("cls");
InsertQueue(x);
break;
case 2: system("cls");
DeleteQueue();
break;
case 3: z=0;
printf("--Exit Program--");
break;
}
}
void main()
{
CreateEmptyQueue(queue);
ShowQueue();
while(z!=0)
{
Menu();
}
getch();
}
taylor series program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
float sump,pow,x;
int sumf,fac;
float Pow(float m,int n);
int Fac(int n);
void menu();
void sin();
void cos();
void main()
{
menu();
getch();
}
void menu()
{
char get;
int n=1;
do{
printf("-----------------Menu-------------------\n");
printf("\tFunction Sin(x) enter 1\n");
printf("\tFunction Cos(x) enter 2\n");
printf("\tExit Program
enter Q\n");
printf("----------------------------------------\n");
printf("Enter Mode:\n");
get=getch();
switch(get)
{
case '1':system("cls");
sin();
break;
case '2':system("cls");
cos();
break;
case 'q':system("cls");
printf("Thank you");
n=0;
break;
default:system("cls");
}
}while(n!=0);
}
void sin()
{
int m=1,n=1;
float sum=0;
printf("Sin(x) Program\n");
printf("enter value x:");
scanf("%f",&x);
x=(x*22)/(7*180);
for(int
i=0;i<10;i++)
{
fac=Fac(n);
pow=Pow(x,n);
n=n+2;
if(m==1)
{
sum=sum+(pow/fac);
m=0;
}
else
{
sum=sum-(pow/fac);
m=1;
}
}
system("cls");
printf("\n\t\tSin(%.0f)=%.4f\n\n",x*180*7/22,sum);
}
void cos()
{
int m=1,n=0;
float sum=0;
printf("Cos(x) Program\n");
printf("enter value x:");
scanf("%f",&x);
x=(x*22)/(7*180);
for(int
i=0;i<10;i++)
{
fac=Fac(n);
pow=Pow(x,n);
n=n+2;
if(m==1)
{
sum=sum+(pow/fac);
m=0;
}
else
{
sum=sum-(pow/fac);
m=1;
}
}
system("cls");
printf("\n\t\tCos(%.0f)=%.4f\n\n",x*180*7/22,sum);
}
float Pow(float m,int n)
{
float sum=1.0;
if(n>=1)
sum=sum*m*Pow(m,n-1);
else
printf(" ");
return sum;
}
int Fac(int n)
{
int sum=1;
if(n>1)
sum=sum*n*Fac(n-1);
else
printf("");
return (sum);
}
intersection set program
#include <stdio.h>
#include<conio.h>
int *getSet(int *num);
int intersect(int *setA,int nA,int *setB,int nB,int **setC);
int main()
{
int num1, num2, num3, i;
int *set1, *set2, *set3;
set1=getSet(
&num1 );
set2=getSet(
&num2 );
num3=intersect(set1,num1,set2,num2,&set3);
printf(
"{ " );
for( i=0; i<num3; i++)
{
if( i>0 )
printf(
", " );
printf(
"%d ",set3[i] );
}
printf( " }"
);
delete [] set1;
delete [] set2;
delete [] set3;
getch();
return 0;
}
int *getSet( int *num )
{
int *set, i;
scanf( "%d",num);
set = new int[*num];
for( i=0; i<*num; i++)
scanf( "%d", &set[i] );
return set;
}
int intersect(int *setA,int nA,int *setB,int nB,int **setC)
{
int nC=0;
*setC=new int [nA+nB];
for(int
i=0;i<nA;i++)
{
for(int
j=0;j<nB;j++)
{
if(setB[j]==setA[i])
{
(*setC)[nC]=setA[i];
nC++;
break;
}
}
}
return nC;
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น