eagle_y2j
Youngling
Can any of you check my BCA xcam project to find any bug ? I just completed this stuff using help from internet (answers) not sure are they correct or not !
Help will really be appreciated
Question 1:
Write a C program to create a calculator. The program will accept an arithmetic expression (assumed to be valid expression of only +, -, *, /, 1/x, and % operations) from the user. Evaluate the expression and display the results. Including the above functions, this calculate should provide the temporary buffer (memory function), to store and retrieve the temporary number values.
Note: You may include some features and function in your calculator program.
Question 2: Write a program in 'c' language to merge the contents of two binary search trees into one. Also, calculate the time and space complexities of your program?
Ans:
Question 3: Write a C+program to create functions that calculate the value of Xn.
Assume that X=|=0, n=|=0.
Ans:
Help will really be appreciated
Question 1:
Write a C program to create a calculator. The program will accept an arithmetic expression (assumed to be valid expression of only +, -, *, /, 1/x, and % operations) from the user. Evaluate the expression and display the results. Including the above functions, this calculate should provide the temporary buffer (memory function), to store and retrieve the temporary number values.
Note: You may include some features and function in your calculator program.
Code:
Ans: #include<stdio.h>
#include<process.h>
#include<conio.h>
void main()
{
float a,b,r;
int n1,n2,r1;
char op;
clrscr();
while(1)
{
fflush(stdin);
printf("\n enter operator ");
scanf("%c",&op);
switch(op)
{
case'+':printf("\nenter value of a ");
scanf("%f",&a);
printf("\nenter value of b ");
scanf("%f",&b);
r=a+b;
printf("\nresult=%2f",r);
getch();
break;
case'-':printf("\nenter value of a ");
scanf("%f",&a);
printf("\nenter value of b ");
scanf("%f",&b);
r=a-b;
printf("\nresult=%2f",r);
getch();
break;
case'*':printf("\nenter value of a ");
scanf("%f",&a);
printf("\nenter value of b ");
scanf("%f",&b);
r=a*b;
printf("\nresult=%2f",r);
getch();
break;
case'/':printf("\nenter value of a ");
scanf("%f",&a);
printf("\nenter value of b ");
scanf("%f",&b);
r=a/b;
printf("\nresult=%2f",r);
getch();
break;
case'%':printf("\nenter value of n1 ");
scanf("%d",&n1);
printf("\nenter value of n2 ");
scanf("%d",&n2);
r1=n1%n2;
printf("\nresult=%2d",r1);
getch();
break;
default:printf("\nWRONG ENTRY");
getch();
exit(op);
}}}
Question 2: Write a program in 'c' language to merge the contents of two binary search trees into one. Also, calculate the time and space complexities of your program?
Ans:
Code:
#include<stdio.n>
#include<conio.n>
#include<dlloc.n>
#define.TRUE1
#define FALSE 0
Struct btreenode
{Struct btreenode * leftchild;
int data;
struct btreenode*rightchild;
};
void insert (struct btreenode **, int);
void delete (struct btreenode**,int);
void search (struct btreenode **, int, struct btreenode**, struct btreenode**, int*);
void inorder (struct btreenode*);
void main()
{ Struct btreenode *bt;
int req, i=0, num, a[] = {11, 9, 13, 8, 10, 12, 14, 15, 7};
bt = NULL; /*empty tree*/
clrscr();
while (i<=8)
{insert (&bt. a[i]);
inorder (bt);
delete (& bt. 10);
printf (“\n Binary tree after deletion:\n”);
inorder (bt);
delete (&bt, 14);
Printf (“\nBinary tree after deletion:\n”);
Inorder (bt);
Delete (&bt, 8);
Printf (“\nBinary tree after deletion:\n”);
Inorder (bt);
Delete (&bt, 13);
Printf (“\nBinary ree after deletion:\n”);
Inorder (bt);
}
/* inserts a new node in binary search tree.*\
Void insert (Struct btreenode (**Sr. Int num)
{
if (**sr = = NULL)
{
*Sr = malloc (size of (struct btreenode));
(*sr)-->leftchild = NULL);
(*sr)--> data =num;
(*sr) --> rightchild = NULL;
}
else /* Search the node to which new node will be attached*/
{
/* If new data is less, treaverse to left*/
if(num<(*sr) data)
insert (&((*sr)leftchild),num);
else
/* else treaverse to right*/
insert (&((*sr)rightchild),num);
}
}
/* deletes a node from the binary search tree*/
void delete (struct btreenode **root, int num)
{
int found;
struct breenode * parent, *.*, *SUCC;
/* if tree is empty*/
if (*root == NULL)
{
printf (“\n Tree is empty”);
return;
}
parent = X = NULL
/* call to search function to find the node to be deleted*/
Search (root, num, &parent, &x, &found);
/* if the node to deleted is not found*/
if (found == FALSE)
{
Printf (“\n Data to be deleted not found”);
return;
}
/* if the node to be deleted has two children*/
if (xleftchild! = NULL && x rightchild! = NULL)
{ Parent = x;
XSUCC = x rightchild;
While (xsucc leftchild! = NULL)
{ Parent = xSUCC;
XSUCC = XSUCC leftchild;
}
x--> data = XSUCC data;
x = XSUCC;
}
/* if the node to be deleted has no child*/
if (x leftchild == NULL&&x-> rightchild == NULL)
if (parent -> rightchild == x)
parent rightchild = NULL;
else
parent -> leftchild = NULL;
free (x);
return;
}
/* if the node to be deleted has only rightchild*/
if (x -> leftchild == NULL && x -> rightchild! = NULL)
if (parent --> leftchild ==x)
parent -> leftchild =x->rightchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
/*If the node to be deleted has only left child*/
if (x-> leftchild! = NULL && xrightchild = NULL)
if (parent->leftchild ==x)
parent -> leftchild = x leftchild;
else
parent->rightchild=x ->leftchild;
free (x)
return;
}}
/* returns the address of the node to be deleted, address of its parent and whether the node is found ore not*/
void search (struct btreenode **root int num.struct btreenode ** paire, struct btreendoe, xxx int x found)
{
struct btreenode *q;
q = *root;
* found = FALSE;
*Pare = NULL;
while (q! = NULL)
{ /* if the node to be deleted is found */
if (q data == num)
{ *found == TRUE;
** = q;
return;
} * para = q;
if (q-> data > num)
q = q leftchild;
else
q = q rightchild;
}}
void inorder (struct btreenode.&src)
if (Src ! = NULL) }
inorder (src leftchild);
printf (“%d\t”, src data);
inorder (Src rightchild);
}
}
Question 3: Write a C+program to create functions that calculate the value of Xn.
Assume that X=|=0, n=|=0.
Ans:
Code:
#include<stdio.h>
#include<conio.h>
long int pow(int ba,int a);
void main()
{
int ba,ex,x;
long int res;
clrscr();
x:
printf("\nenter the base ");
scanf("%d",&ba);
printf("\n enter the expo ");
scanf("%d",&ex);
if(ba==0 || ex==0)
{
printf("\n base or expo should not be zero ");
goto x;
}
res=pow(ba,ex);
printf("\nresult=%d",res);
getch();
}
long int pow(int ba,int a)
{
int i;
long int r=1;
for(i=1;i<=a;i++)
r*=ba;
return r;
}
Last edited: