Friday, November 28, 2014

Implement simpson C pro



XOR list Program

#include
#include
struct xnode
{
int data;
unsigned long direction;
};
struct xnode *add_data(int data, struct xnode* list);
void walk_list(struct xnode *list);
int main(void)
{
struct xnode *l2 = add_data(2, NULL);
struct xnode *l1 = add_data(1, l2);
struct xnode *l3 = add_data(3, l2);
struct xnode *l4 = add_data(4, l3);
printf("front -> back....\n");
walk_list(l1);
printf("back -> front....\n");
walk_list(l4);
return 0;
}
struct xnode *add_data(int data, struct xnode *list)
{
struct xnode *newxnode = malloc(sizeof(struct xnode));
assert(newxnode);
newxnode->direction = (unsigned long)list;
newxnode->data = data;
if(list != NULL)
list->direction ^= (unsigned long)newxnode;
return newxnode;
}
void walk_list(struct xnode *list)
{
unsigned long prev = 0;
while(list != NULL)
{
unsigned long next = prev ^ list->direction;
printf("%d ", list->data);
prev = (unsigned long)list;
list = (struct xnode *)next;
}
printf("\n");
}

Write c program to print a triangle

#include
#include
void main()
{
int n;
clrscr();
printf("enter the lines :");
scanf("%d",&n);
for(int i=1;i<=n/2;i++)
{
printf("*\n");
for(int j=1;j<=2*i;j++)
printf("%d",j);
printf("\n");
}
if(n%2!=0)
printf("\n*");
getch();
}

Write c program to implement Simpson method

#include
#include
#include
char postfix[80];
float stack[80];
char stack1[80];
int top=-1,top1=-1;
float eval(char postfix[], float x1);

void infix_postfix(char infix[]);
void main()
{
float x0, xn, h, s,e1,e2, e3;
char exp[80], arr[80];
int i,n,l=0;
clrscr();
printf(“\nEnter an expression: “);
gets(exp);
puts(“Enter x0, xn and number of sub-intervals: “);
scanf(“%f%f%d”, &x0, &xn, &n);
h=(xn-x0)/n;
if(exp[0]==’l'&& exp[1]==’o'&& exp[2]==’g')
{
l=strlen(exp);
for(i=0;i arr[0]=exp[i+3];
arr[i]=”;
infix_postfix(arr);
e1=eval(postfix,x0);
e2=eval(postfix,xn);
e3=4*eval(postfix, x0+h);
s=log(e1)+log(e2)+log(e3);
for (i=3;i<=n-1;i+=2)
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);
}
else
{
infix_postfix(exp);
s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h);
for (i=3;i<=n-1;i+=2)
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);
}
printf(“The value of integral is %6.3f\n”,(h/3)*s);
return(0);
}
void push(float item)
{
if(top==99)
{
printf(“\n\tThe stack is full”);
getch();
exit(0);
}
else
{
top++;
stack[top]=item;
}
return;
}
float pop()
{
float item;
if(top==-1)
{
printf(“\n\tThe stack is empty\n\t”);
getch();
}
item=stack[top];
top–;
return (item);
}
void push1(char item)
{
if(top1==79)
{
printf(“\n\tThe stack is full”);
getch();
exit(0);
}
else
{
top1++;
tack1[top1]=item;

}
return;
}
char pop1()
{
char item;
if(top1==-1)
{
printf(“\n\tThe stack1 is empty\n\t”);
getch();
}
item=stack1[top1];
top1–;
return (item);
}
void infix_postfix(char infix[])
{
int i=0,j=0,k;
char ch;
char token;
for(i=0;i<79 br="" i=""> postfix[i]=’ ‘;
push1(‘?’);
i=0;
token=infix[i];
while(token!=”)
{
if(isalnum(token))
{
postfix[j]=token;
j++;
}
else if(token==’(‘)
{
push1(‘(‘);
}
else if(token==’)')
{
while(stack1[top1]!=’(‘)
{
ch=pop1();
postfix[j]=ch;
j++;
}
ch=pop1();
}
else
{
while(ISPriority(stack1[top1])>=ICP(token))
{
ch=pop1();
postfix[j]=ch;
j++;
}
push1(token);
}
i++;
token=infix[i];
}
while(top1!=0)
{
ch=pop1();
postfix[j]=ch;
j++;
}
postfix[j]=”;
}
int ISPriority(char token)
{
switch(token)
{
case ‘(‘:return (0);
case ‘)’:return (9);
case ‘+’:return (7);
case ‘-’:return (7);
case ‘*’:return (8);
case ‘/’:return (8);
case ‘?’:return (0);
default: printf(“Invalid expression”);
}
return 0;
}
int ICP(char token)
{
switch(token)
{
case ‘(‘:return (10);
case ‘)’:return (9);
case ‘+’:return (7);
case ‘-’:return (7);
case ‘*’:return (8);
case ‘/’:return (8);
case ”:return (0);
default: printf(“Invalid expression”);
}
return 0;
}
float eval(char p[], float x1)
{
float t1,t2,k,r;
int i=0,l;
l=strlen(p);
while(i {
if(p[i]==’x')
push(x1);
else
if(isdigit(p[i]))
{
k=p[i]-’0′;
push(k);
}
else
{
t1=pop();
t2=pop();
switch(p[i])
{
case ‘+’:k=t2+t1;
break;
case ‘-’:k=t2-t1;
break;
case ‘*’:k=t2*t1;
break;
case ‘/’:k=t2/t1;
break;
default: printf(“\n\tInvalid expression”);
}
push(k);
}
i++;
}
if(top>0)
{
printf(“You have entered the operands more than the operators”);
exit(0);
}
else
{
r=pop();
return (r);
}
return 0;
}

No comments:

Post a Comment