Sunteți pe pagina 1din 43

ASSIGNMENT-8

1.Write a program in c to store information (name, roll, marks) of a student


using structure
Code:
#include<stdio.h>

struct student

char name[20];

int roll;

float marks;

}s1;

int main()

printf("**Enter student details**");

printf("\nEnter Name => ");

scanf("%s",&s1.name);

printf("\nEnter Roll => ");

scanf("%d",&s1.roll);

printf("\nEnter Marks => ");

scanf("%f",&s1.marks);

printf("\n**Entered student details are**");

printf("\nName=> %s",s1.name);

printf("\nRoll=> %d",s1.roll);

printf("\nMarks=> %f",s1.marks);

return 0;

Output:
2.Write a program in C to add two distances (in inch-feet) using structure.
Code:
#include<stdio.h>

struct add

float d1,d2,sum;

}s1;

int main()

printf("\nEnter Distance one => ");

scanf("%f",&s1.d1);

printf("\nEnter Distance two => ");

scanf("%f",&s1.d2);

s1.sum = s1.d1+s1.d2;

printf("\n Sum => %f",s1.sum);

return 0;

Output:
3. Write a program in C to calculate the difference between two time periods.
Code:
#include<stdio.h>

struct tp

int h,m,s,d;

}t1,t2,d1,d2,d3;

int main()

printf("\nEnter first time period");

printf("\nHour => ");

scanf("%d",&t1.h);

printf("\nMinute => ");

scanf("%d",&t1.m);

printf("\nSecond => ");

scanf("%d",&t1.s);

printf("\nEnter Second time period");

printf("\nHour => ");


scanf("%d",&t2.h);

printf("\nMinute => ");

scanf("%d",&t2.m);

printf("\nSecond => ");

scanf("%d",&t2.s);

d1.d=t1.h-t2.h;

d2.d=t1.m-t2.m;

d3.d=t1.s-t2.s;

printf("\n Difference => %d:%d:%d",d1.d,d2.d,d3.d);

return 0;

Output:

4.Write a program in c to store information (name, roll, marks) of 10 students


using structure.
Code:
#include<stdio.h>

struct student

char name[20];

int roll;
float marks;

}s[10];

int main()

int i;

printf("**Enter student details**");

for(i=0;i<10;i++)

printf("\nEnter Name => ");

scanf("%s",&s[i].name);

printf("\nEnter Roll => ");

scanf("%d",&s[i].roll);

printf("\nEnter Marks => ");

scanf("%f",&s[i].marks);

for(i=0;i<10;i++)

printf("\n**Entered student details are**");

printf("\nName=> %s",s[i].name);

printf("\nRoll=> %d",s[i].roll);

printf("\nMarks=> %f",s[i].marks);

return 0;

output:
5.Write a program in c to store information using structures for n elements
dynamically.
Code:
#include<stdio.h>

#include<stdlib.h>

struct student

int num;

};

int main()

{
int i,n;

struct student *p;

printf("The size of the array =>");

scanf("%d",&n);

p=(struct student*)malloc(n*sizeof(struct student));

printf("\nThe enter values :\n");

for(i=0;i<n;i++)

scanf("%d",&p[i]);

printf("\nThe entered values are :");

for(i=0;i<n;i++)

printf("\n%d",p[i]);

return 0;

Output:
6.Write a program in c to access array members using structure.
Code:
#include<stdio.h>

struct student

char name[20];

int roll;

float marks;

};

int main()

struct student s[3];

int i;

printf("**Enter student details**");

for(i=0;i<3;i++)

printf("\nEnter Name => ");

scanf("%s",&s[i].name);

printf("\nEnter Roll => ");

scanf("%d",&s[i].roll);

printf("\nEnter Marks => ");

scanf("%f",&s[i].marks);

printf("\n**Entered student details are**");

for(i=0;i<3;i++)

printf("\n\nName=> %s",s[i].name);

printf("\n\nRoll=> %d",s[i].roll);

printf("\n\nMarks=> %f",s[i].marks);

return 0;
}

Output:

7.Write a program in c to access structure members using structure pointers.


Code:
#include<stdio.h>

struct student

char name[20];

int roll;

float marks;

};

int main()

struct student s[3];

struct student *t;

int i;

printf("**Enter student details**");


for(i=0;i<3;i++)

printf("\nEnter Name => ");

scanf("%s",&s[i].name);

printf("\nEnter Roll => ");

scanf("%d",&s[i].roll);

printf("\nEnter Marks => ");

scanf("%f",&s[i].marks);

t=s;

printf("\n**Entered student details are**");

for(i=0;i<3;i++)

printf("\n\nName=> %s",t[i].name);

printf("\n\nRoll=> %d",t[i].roll);

printf("\n\nMarks=> %f",t[i].marks);

return 0;

Output:
8.Write a program in c to store employee details in a structure named
employee.Store address of employee in another structure named address
which is nested .Then print the total information of the employee uing the
concept of nested structure.
Code:
#include<stdio.h>

#include<stdlib.h>

struct address

char adrs[30];

};

struct employee

int empid;
char name[20];

struct address ads;

char mobno[11];

};

int main()

int n,i;

struct employee *p;

printf("The size of the array =>");

scanf("%d",&n);

p=(struct employee*)malloc(n*sizeof(struct employee));

printf("\nThe Employee details :\n");

for(i=0;i<n;i++)

printf("\n\nEnter Employee id\n\n");

scanf("%d",&p[i].empid);

printf("\nName\n");

scanf("%s",p[i].name);

printf("\nAddress\n");

scanf("%s",p[i].ads.adrs);

printf("\nMobile number\n");

scanf("%s",p[i].mobno);

printf("\nThe Employee details are :");

for(i=0;i<n;i++)

printf("\n\nEmployee id\n\n");

printf("%d",p[i].empid);

printf("\n\nName\n");

printf("%s",p[i].name);

printf("\n\nAddress\n");
printf("%s",p[i].ads.adrs);

printf("\nMobile number\n");

printf("%s",p[i].mobno);

free(p);

return 0;

Output:

10. Write a C program to convert infix expression into


postfix expression.
Code:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>

#define MAX 50

typedef struct stack


{
int data[MAX];
int top;
}stack;

int precedence(char);
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int top(stack *);
void infix_to_postfix(char infix[],char postfix[]);

int main()
{
char infix[30],postfix[30];
printf("Enter an infix expression(eg: 5+2*4): ");
gets(infix);
infix_to_postfix(infix,postfix);
printf("\nPostfix expression: %s",postfix);
}

void infix_to_postfix(char infix[],char postfix[])


{
stack s;
char x,token;
int i,j;
init(&s);
j=0;
for(i=0;infix[i]!='\0';i++)
{
token=infix[i];
if(isalnum(token))
postfix[j++]=token;
else
if(token=='(')
push(&s,'(');
else
if(token==')')
while((x=pop(&s))!='(')
postfix[j++]=x;
else
{
while(precedence(token)<=precedence(top(&s))&&!
empty(&s))
{
x=pop(&s);
postfix[j++]=x;
}
push(&s,token);
}
}

while(!empty(&s))
{
x=pop(&s);
postfix[j++]=x;
}
postfix[j]='\0';
}

int precedence(char x)
{
if(x=='(')
return(0);
if(x=='+'||x=='-')
return(1);
if(x=='*'||x=='/'||x=='%')
return(2);

return(3);
}

void init(stack *s)


{
s->top=-1;
}

int empty(stack *s)


{
if(s->top==-1)
return(1);

return(0);
}

int full(stack *s)


{
if(s->top==MAX-1)
return(1);

return(0);
}

void push(stack *s,int x)


{
s->top=s->top+1;
s->data[s->top]=x;
}

int pop(stack *s)


{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}

int top(stack *p)


{
return (p->data[p->top]);
}

Output:
11. Write a C program to convert infix expression into
prefix expression.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

int pop();
int precedence(char symbol);
int isEmpty();
void infix_to_prefix();
int checker(char symbol);
void push(long int symbol);

char prefix_string[20], infix_string[20], postfix_string[20];


int top;
long int stack[20];

int main()
{
int count, length;
char temp;
top = -1;
printf("\nEnter an Expression in Infix format:\t");
scanf("%[^\n]s", infix_string);
infix_to_prefix();
printf("\nExpression in Postfix Format: \t%s\n", postfix_string);
length = strlen(postfix_string) - 1;
strncpy(prefix_string, postfix_string, 20);
for(count = 0; count < length; count++, length--)
{
temp = prefix_string[count];
prefix_string[count] = prefix_string[length];
prefix_string[length] = temp;
}
printf("\nExpression in Prefix Format: \t%s\n", prefix_string);
return 0;
}

void infix_to_prefix()
{
unsigned int count, temp = 0;
char next;
char symbol;
for(count = 0; count < strlen(infix_string); count++)
{
symbol = infix_string[count];
if(!checker(symbol))
{
switch(symbol)
{
case '(': push(symbol);
break;
case ')':
while((next = pop()) != '(')
{
postfix_string[temp++] = next;
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
while(!isEmpty() && precedence(stack[top]) >=
precedence(symbol))
postfix_string[temp++] = pop();
push(symbol);
break;
default:
postfix_string[temp++] = symbol;
}
}
}
while(!isEmpty())
{
postfix_string[temp++] = pop();
}
postfix_string[temp] = '\0';
}

int precedence(char symbol)


{
switch(symbol)
{
case '(': return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
default:
return 0;
}
}

int checker(char symbol)


{
if(symbol == '\t' || symbol == ' ')
{
return 1;
}
else
{
return 0;
}
}

void push(long int symbol)


{
if(top > 20)
{
printf("Stack Overflow\n");
exit(1);
}
top = top + 1;
stack[top] = symbol;
}

int isEmpty()
{
if(top == -1)
{
return 1;
}
else
{
return 0;
}
}

int pop()
{
if(isEmpty())
{
printf("Stack is Empty\n");
exit(1);
}
return(stack[top--]);
}

Output:
12. Write a C program to convert prefix expression into
infix expression
Code:
#include<stdio.h>
#include <string.h>
#include <ctype.h>

char operand_stack[50][80], operator_stack[50];


int top_operator = -1, top_operand = -1;

int push_operator(char operator)


{
operator_stack[++top_operator] = operator;
}

int push_operand(char *opnd)


{
strcpy(operand_stack[++top_operand], opnd);
}

char pop_operator()
{
return(operator_stack[top_operator--]);
}

char *pop_operand()
{
return(operand_stack[top_operand--]);
}

int empty(int t)
{
if(t == 0)
{
return 1;
}
else
{
return 0;
}
}

int main()
{
char prefix_expression[50], ch, temporary_string[50], operand_a[50],
operand_b[50], operator[2];
int count = 0, k = 0, operand_count = 0;
printf("\nEnter a Prefix Expression:\t");
scanf("%s", prefix_expression);
while((ch = prefix_expression[count++]) != '\0')
{
if(isalnum(ch))
{
temporary_string[0] = ch;
temporary_string[1]='\0';
push_operand(temporary_string);
operand_count++;
if(operand_count >= 2)
{
strcpy(operand_b, pop_operand());
strcpy(operand_a, pop_operand());
strcpy(temporary_string, "(");
strcat(temporary_string, operand_a);
ch = pop_operator();
operator[0] = ch;
operator[1] = '\0';
strcat(temporary_string, operator);
strcat(temporary_string, operand_b);
strcat(temporary_string, ")");
push_operand(temporary_string);
operand_count = operand_count - 1;
}
}
else
{
push_operator(ch);
if(operand_count == 1)
{
operand_count = 0;
}
}
}
if(!empty(top_operand))
{
strcpy(operand_b, pop_operand());
strcpy(operand_a, pop_operand());
strcpy(temporary_string, "(");
strcat(temporary_string, operand_a);
ch = pop_operator();
operator[0] = ch;
operator[1] = '\0';
strcat(temporary_string, operator);
strcat(temporary_string, operand_b);
strcat(temporary_string, ")");
push_operand(temporary_string);
}
printf("\nInfix Expression:\t %s\n", operand_stack[top_operand]);
return 0;
}

Output:

13. Write a C program to convert postfix expression into


infix expression
Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

# define MAX 20
char str[MAX], stack[MAX];
int top = -1;

char pop()
{
return stack[top--];
}

void push(char ch)


{
stack[++top] = ch;
}

void postfix_to_infix(char expression[])


{
int count, length;
char element, operator;
length = strlen(expression);
for(count = 0; count < MAX; count++)
{
stack[count] = 0;
}
printf("\nInfix Expression:\t");
printf("%c", expression[0]);
for(count = 1; count < length; count++)
{
if(expression[count] == '-' || expression[count] == '/' || expression[count]
== '*'|| expression[count] == '+')
{
element = pop();
operator = expression[count];
printf(" %c %c", operator, element);
}
else
{
push(expression[count]);
}
}
printf("%c", expression[top--]);
}

int main()
{
char postfix_expression[50];
printf("\nEnter Postfix Expression:\t");
scanf("%s", postfix_expression);
postfix_to_infix(postfix_expression);
printf("\n");
return 0;
}

Output:

14. Write a ‘C’ program to evaluate postfix expression (using stack).


Code:
#include <stdio.h>

#include <ctype.h>
#define MAXSTACK 100

#define POSTFIXSIZE 100

int stack[MAXSTACK];

int top = -1;

void push(int item)

if (top >= MAXSTACK - 1) {

printf("stack over flow");

return;

else {

top = top + 1;

stack[top] = item;

int pop()

int item;

if (top < 0) {

printf("stack under flow");

else {

item = stack[top];

top = top - 1;

return item;

void EvalPostfix(char postfix[])

{
int i;

char ch;

int val;

int A, B;

for (i = 0; postfix[i] != ')'; i++) {

ch = postfix[i];

if (isdigit(ch)) {

push(ch - '0');

else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {

A = pop();

B = pop();

switch (ch)

case '*':

val = B * A;

break;

case '/':

val = B / A;

break;

case '+':

val = B + A;

break;

case '-':
val = B - A;

break;

push(val);

printf(" \n Result of expression evaluation : %d \n", pop());

int main()

int i;

char postfix[POSTFIXSIZE];

printf("ASSUMPTION: There are only four operators(*, /, +, -) in an expression and operand is


single digit only.\n");

printf(" \nEnter postfix expression,\npress right parenthesis ')' for end expression : ");

for (i = 0; i <= POSTFIXSIZE - 1; i++) {

scanf("%c", &postfix[i]);

if (postfix[i] == ')')

break;

EvalPostfix(postfix);
return 0;

Output:

15. C program to push and pop elements from multiple


stacks.
Code:
#include <stdio.h>

#define SIZE 10
int ar[SIZE];

int top1 = -1;

int top2 = SIZE;

void push_stack1 (int data)

if (top1 < top2 - 1)


{

ar[++top1] = data;

else

printf ("Stack Full! Cannot Push\n");

void push_stack2 (int data)

if (top1 < top2 - 1)

ar[--top2] = data;

else

{
printf ("Stack Full! Cannot Push\n");

}
void pop_stack1 ()

if (top1 >= 0)

int popped_value = ar[top1--];

printf ("%d is being popped from Stack 1\n", popped_value);

else

printf ("Stack Empty! Cannot Pop\n");

void pop_stack2 ()

{
if (top2 < SIZE)

int popped_value = ar[top2++];

printf ("%d is being popped from Stack 2\n", popped_value);

else

printf ("Stack Empty! Cannot Pop\n");

void print_stack1 ()

int i;

for (i = top1; i >= 0; --i)

printf ("%d ", ar[i]);


}

printf ("\n");

void print_stack2 ()

int i;

for (i = top2; i < SIZE; ++i)

printf ("%d ", ar[i]);

printf ("\n");

int main()

int ar[SIZE];
int i;

int num_of_ele;

printf ("We can push a total of 10 values\n");

for (i = 1; i <= 6; ++i)

push_stack1 (i);

printf ("Value Pushed in Stack 1 is %d\n", i);

for (i = 1; i <= 4; ++i)

push_stack2 (i);

printf ("Value Pushed in Stack 2 is %d\n", i);

print_stack1 ();
print_stack2 ();

printf ("Pushing Value in Stack 1 is %d\n", 11);

push_stack1 (11);

num_of_ele = top1 + 1;

while (num_of_ele)

pop_stack1 ();
--num_of_ele;
}
pop_stack1 ();
return 0;
}

Output:
16. C program to check whether expression is correctly
parenthesized using stack.
Code:
#include <stdio.h>

#include <stdlib.h>

#include <string.h>
int top = -1;

char stack[100];

void push(char);

void pop();

void find_top();

intmain()
{

int i;

char a[100];

printf("enter expression\n");

scanf("%s", &a);

for (i = 0; a[i] != '\0';i++)

if (a[i] == '(')

push(a[i]);

else if (a[i] == ')')

pop();

}
find_top();

void push(char a)

stack[top] = a;

top++;

void pop()

if (top == -1)

printf("expression is invalid\n");

exit(0);

else

{
top--;

void find_top()

if (top == -1)
printf("\nexpression is valid\n");
else
printf("\nexpression is invalid\n");
}

Output:

S-ar putea să vă placă și