0% found this document useful (0 votes)
4 views2 pages

Micro

The document contains C code for managing a weekly activity calendar and a circular queue. It includes functions for creating, reading, displaying, and freeing memory for the calendar, as well as for inserting, deleting, and displaying elements in the circular queue. The code also handles pattern matching and string replacement operations.

Uploaded by

nishigami16424
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Micro

The document contains C code for managing a weekly activity calendar and a circular queue. It includes functions for creating, reading, displaying, and freeing memory for the calendar, as well as for inserting, deleting, and displaying elements in the circular queue. The code also handles pattern matching and string replacement operations.

Uploaded by

nishigami16424
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_SIZE 100

P1
#include <stdio.h> #include<stdio.h>
#include <stdlib.h> int main() { struct Stack {
#include <stdlib.h> char str[50], pat[20], rep[20], res[50]; char data[MAX_SIZE];
struct Day { int c = 0, m = 0, i = 0, j = 0, k, flag = 0;
#define MAX_SIZE 100 //initialize(); >=
char *dayName; void stringmatch()
int top = -1;
int choice, element; int top; 'A' && infix[i] <= 'Z') || (infix[i] >= '0' &&
int data[MAX_SIZE]; };
int date; {
int isEmpty() { while (1) { infix[i] <=
char *activity; while (str[c] != '\0') void initializeStack(struct Stack *stack) { '9')) {

p4
}; {
return top == -1; printf("\n1. Push\n2. Pop\n3. stack->top = -1; postfix[j++] = infix[i];

P2
}
void create(struct Day *day) { if (str[m] == pat[i]) Display\n4.Check Palindrome\n5. } } else if (infix[i] == '(') {
int isFull() {
day->dayName = (char *)malloc(sizeof(char) * 20); {
return top == MAX_SIZE - 1; Exit\n"); int isEmpty(struct Stack *stack) { push(&stack, '(');
day->activity = (char *)malloc(sizeof(char) * 100); i++; } else if (infix[i] == ')') {
printf("Enter the day name: "); m++;
} printf("Enter your choice: "); return stack->top == -1; while (!isEmpty(&stack) && stack.data[stack.top] !
void push(int element) { }
scanf("%s", day->dayName); if (pat[i] == '\0') scanf("%d", &choice); = '(') {
if (isFull()) { int isFull(struct Stack *stack) {
printf("Enter the date: "); {
printf("Stack overflow!\n"); switch (choice) { postfix[j++] = pop(&stack);
scanf("%d", &day->date); flag = 1; return stack->top == MAX_SIZE - 1; }
printf("Enter the activity for the day: "); for (k = 0; rep[k] != '\0'; k++, j++)
return; case 1: if (!isEmpty(&stack) && stack.data[stack.top] ==
} }
scanf(" %[^\n]s", day->activity); { printf("Enter element to push: "); void push(struct Stack *stack, char ch) { '(') {
top++;
} res[j] = rep[k];
data[top] = element; scanf("%d", &element); if (isFull(stack)) {
pop(&stack);
void read(struct Day *calendar, int size) { } } else {
for (int i = 0; i < size; i++) { i = 0;
} push(element); printf("Stack Overflow\n"); printf("Invalid expression\n");
int pop() {
printf("Enter details for Day %d:\n", i + 1); c = m; break; return; return;
if (isEmpty()) {
create(&calendar[i]); }
printf("Stack underflow!\n"); case 2: } }
} } stack->top++; } else {
} else
return -1; element = pop(); while (!isEmpty(&stack) && precedence(infix[i]) <=
} stack->data[stack->top] = ch;
void display(struct Day *calendar, int size) { { if (element != -1) { precedence(stack.data[stack.top])) {
int poppedElement = data[top]; }
printf("\nWeek's Activity Details:\n"); res[j] = str[c];
top--; printf("Popped element: %d\n", char pop(struct Stack *stack) {
postfix[j++] = pop(&stack);
for (int i = 0; i < size; i++) { j++; }
printf("Day %d:\n", i + 1); c++;
return poppedElement; element); if (isEmpty(stack)) { push(&stack, infix[i]);
}
printf("Day Name: %s\n", calendar[i].dayName); m = c; } printf("Stack Underflow\n"); }
void display() {
printf("Date: %d\n", calendar[i].date); i = 0;
if (isEmpty()) { break; return '\0'; }
printf("Activity: %s\n", calendar[i].activity); } } while (!isEmpty(&stack)) {
printf("Stack is empty.\n"); case 3:
p3
printf("\n"); } postfix[j++] = pop(&stack);
} res[j] = '\0';
return;
display(); char ch = stack->data[stack->top]; }
} stack->top--;
} }
printf("Elements in the stack:\n"); break; postfix[j] = '\0';
void freeMemory(struct Day *calendar, int size) { void main() return ch; }
for (int i = 0; i < size; i++) { {
for (int i = top; i >= 0; i--) { case 5: } int main() {
printf("%d ", data[i]);
free(calendar[i].dayName); printf("Enter the main string:"); exit(0); int precedence(char ch) { char infix[MAX_SIZE], postfix[MAX_SIZE];
}
free(calendar[i].activity); gets(str);
printf("\n"); case 4: if (ch == '^') printf("Enter an infix expression: ");
} printf("\nEnter the pat string:"); scanf("%s", infix);
} gets(pat);
} palindrome(); return 3; infixToPostfix(infix, postfix);
void palindrome() else if (ch == '*' || ch == '/' || ch == '%')
int main() { printf("\nEnter the replace string:"); break; printf("Postfix expression: %s\n", postfix);
{ return 2;
int size; gets(rep);
int i; default: return 0;
printf("Enter the number of days in the week: "); printf("\nThe string before pattern match is:\n %s", str); else if (ch == '+' || ch == '-') }
scanf("%d", &size); stringmatch();
for(i=0;i<=top;i++) printf("Invalid choice!\n");
if(data[i]!=pop()) return 1;
struct Day *calendar = (struct Day *)malloc(sizeof(struct if (flag == 1) } else
{
Day) * size); printf("\nThe string after pattern match and replace is: \n %s ", res);
printf("\nIt is not a palindrome\n"); } return 0;
if (calendar == NULL) { else
printf("Memory allocation failed. Exiting program.\n"); printf("\nPattern string is not found");
return; return 0; }
} void infixToPostfix(char *infix, char *postfix) {
return 1; }
printf("\nIt is a Palindrome\n");
}
}
struct Stack stack;
read(calendar, size); initializeStack(&stack);
display(calendar, size); int i, j = 0;
freeMemory(calendar, size);
free(calendar);
for (i = 0; infix[i] != '\0'; i++) {
return 0; if ((infix[i] >= 'a' && infix[i] <= 'z') || (infix[i]
} >=
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
else switch(choice)
{
char circular_queue[MAX]; {
int front = -1, rear = -1;
printf("Deleted element is
%d",circular_queue[front]); case 1:
int isEmpty()
{ front = (front + 1) % MAX; printf("Enter element to be inserted:
if (front == -1 && rear == -1) } ");
return 1; }
else scanf(" %c", &element);
void display()
return 0;
{ insertElement(element);
}
int isFull()
int i; break;

: p6
{ if (isEmpty())
if ((rear + 1) % MAX == front) { case 2
return 1; printf("Circular Queue is empty\n");
else return; deleteElement();
return 0; } break;
}
void insertElement(char element)
printf("Circular Queue elements: "); case 3:
i = front;
{ display();
if (isFull()) do
{ { break;
printf("%c ", circular_queue[i]);
printf("Circular Queue Overflow\n"); case 4:
return; i = (i + 1) % MAX; printf("Exiting...\n");
} }
else if (isEmpty()) break;
while (i != (rear + 1) % MAX);
{
printf("\n"); default:
front = rear = 0;
} } printf("Invalid choice! Please enter a
else int main() valid
{ {
rear = (rear + 1) % MAX;
option.\n");
int choice;
} char element; }
circular_queue[rear] = element; }
do
}
void deleteElement()
{ while(choice != 4);
printf("\n\n---- Circular Queue Menu ----\n");
{ return 0;
if (isEmpty()) printf("1. Insert an Element\n");
{ printf("2. Delete an Element\n"); }
printf("Circular Queue Underflow\n");
printf("3. Display Circular Queue\n");
return; printf("4. Exit\n");
}
printf("Enter your choice: ");
else if (front == rear)
{
scanf("%d", &choice);
front = rear = -1;
}

You might also like