Upload files to "DS/C/Lab/Week6"

This commit is contained in:
Aadit Agrawal 2024-09-03 11:27:33 +05:30
parent 5b8ed261ff
commit 8de2a08e83
3 changed files with 132 additions and 75 deletions

View File

@ -6,85 +6,110 @@
char stack[MAX_SIZE]; char stack[MAX_SIZE];
int top = -1; int top = -1;
void push(char item) { void push(char item)
if (top >= MAX_SIZE - 1) { {
if (top >= MAX_SIZE - 1)
{
printf("Stack Overflow\n"); printf("Stack Overflow\n");
return; return;
} }
stack[++top] = item; stack[++top] = item;
} }
char pop() { char pop()
if (top < 0) { {
if (top < 0)
{
printf("Stack Underflow\n"); printf("Stack Underflow\n");
return -1; return -1;
} }
return stack[top--]; return stack[top--];
} }
int precedence(char symbol) { int precedence(char symbol)
switch (symbol) { {
case '^': return 3; switch (symbol)
case '*': {
case '/': return 2; case '^':
case '+': return 3;
case '-': return 1; case '*':
default: return 0; case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
} }
} }
void reverse(char *str) { void reverse(char *str)
{
int len = strlen(str); int len = strlen(str);
int i, j; int i, j;
char temp; char temp;
for (i = 0, j = len - 1; i < j; i++, j--) { for (i = 0, j = len - 1; i < j; i++, j--)
{
temp = str[i]; temp = str[i];
str[i] = str[j]; str[i] = str[j];
str[j] = temp; str[j] = temp;
} }
} }
int isalnum(char c) { int isalnum(char c)
{
return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
} }
void infixToPrefix(char infix[], char prefix[]) { void infixToPrefix(char infix[], char prefix[])
{
char symbol, temp[2]; char symbol, temp[2];
int i, j = 0; int i, j = 0;
reverse(infix); reverse(infix);
for (i = 0; infix[i] != '\0'; i++) { for (i = 0; infix[i] != '\0'; i++)
{
symbol = infix[i]; symbol = infix[i];
if (isalnum(symbol)) { if (isalnum(symbol))
{
prefix[j++] = symbol; prefix[j++] = symbol;
} else if (symbol == ')') { }
else if (symbol == ')')
{
push(symbol); push(symbol);
} else if (symbol == '(') { }
while ((temp[0] = pop()) != ')') { else if (symbol == '(')
{
while ((temp[0] = pop()) != ')')
{
prefix[j++] = temp[0]; prefix[j++] = temp[0];
} }
} else { }
while (top > -1 && precedence(stack[top]) > precedence(symbol)) { else
{
while (top > -1 && precedence(stack[top]) > precedence(symbol))
{
prefix[j++] = pop(); prefix[j++] = pop();
} }
push(symbol); push(symbol);
} }
} }
while (top > -1) { while (top > -1)
{
prefix[j++] = pop(); prefix[j++] = pop();
} }
prefix[j] = '\0'; prefix[j] = '\0';
reverse(prefix); reverse(prefix);
} }
int main() { int main()
{
char infix[MAX_SIZE], prefix[MAX_SIZE]; char infix[MAX_SIZE], prefix[MAX_SIZE];
printf("Enter infix expression: "); printf("Enter infix expression: ");
fgets(infix, MAX_SIZE, stdin); gets(infix);
infix[strcspn(infix, "\n")] = 0; // Remove newline if present
infixToPrefix(infix, prefix); infixToPrefix(infix, prefix);
printf("Prefix expression: %s\n", prefix); printf("Prefix expression: %s\n", prefix);
return 0; return 0;

View File

@ -6,65 +6,86 @@
int stack[MAX_SIZE]; int stack[MAX_SIZE];
int top = -1; int top = -1;
void push(int item) { void push(int item)
if (top >= MAX_SIZE - 1) { {
if (top >= MAX_SIZE - 1)
{
printf("Stack Overflow\n"); printf("Stack Overflow\n");
return; return;
} }
stack[++top] = item; stack[++top] = item;
} }
int pop() { int pop()
if (top < 0) { {
if (top < 0)
{
printf("Stack Underflow\n"); printf("Stack Underflow\n");
return -1; return -1;
} }
return stack[top--]; return stack[top--];
} }
int isOperator(char c) { int isOperator(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/'); return (c == '+' || c == '-' || c == '*' || c == '/');
} }
int isDigit(char c) { int isDigit(char c)
{
return (c >= '0' && c <= '9'); return (c >= '0' && c <= '9');
} }
int evaluatePrefix(char* expr) { int evaluatePrefix(char *expr)
{
int len = strlen(expr); int len = strlen(expr);
for (int i = len - 1; i >= 0; i--) { for (int i = len - 1; i >= 0; i--)
if (isDigit(expr[i])) { {
if (isDigit(expr[i]))
{
push(expr[i] - '0'); push(expr[i] - '0');
} else if (isOperator(expr[i])) { }
else if (isOperator(expr[i]))
{
int operand1 = pop(); int operand1 = pop();
int operand2 = pop(); int operand2 = pop();
switch (expr[i]) { switch (expr[i])
case '+': push(operand1 + operand2); break; {
case '-': push(operand1 - operand2); break; case '+':
case '*': push(operand1 * operand2); break; push(operand1 + operand2);
case '/': break;
if (operand2 != 0) push(operand1 / operand2); case '-':
else { push(operand1 - operand2);
printf("Error: Division by zero\n"); break;
return -1; case '*':
} push(operand1 * operand2);
break; break;
case '/':
if (operand2 != 0)
push(operand1 / operand2);
else
{
printf("Error: Division by zero\n");
return -1;
}
break;
} }
} }
} }
return pop(); return pop();
} }
int main() { int main()
{
char expr[MAX_SIZE]; char expr[MAX_SIZE];
printf("Enter prefix expression: "); printf("Enter prefix expression: ");
fgets(expr, MAX_SIZE, stdin); gets(expr);
expr[strcspn(expr, "\n")] = 0; // Remove newline if present
int result = evaluatePrefix(expr); int result = evaluatePrefix(expr);
if (result != -1) { if (result != -1)
{
printf("Result: %d\n", result); printf("Result: %d\n", result);
} }
return 0; return 0;
} }

View File

@ -6,40 +6,51 @@
char stack[MAX_SIZE][MAX_SIZE]; char stack[MAX_SIZE][MAX_SIZE];
int top = -1; int top = -1;
void push(const char* item) { void push(const char *item)
if (top >= MAX_SIZE - 1) { {
if (top >= MAX_SIZE - 1)
{
printf("Stack Overflow\n"); printf("Stack Overflow\n");
return; return;
} }
strcpy(stack[++top], item); strcpy(stack[++top], item);
} }
char* pop() { char *pop()
if (top < 0) { {
if (top < 0)
{
printf("Stack Underflow\n"); printf("Stack Underflow\n");
return NULL; return NULL;
} }
return stack[top--]; return stack[top--];
} }
int isOperator(char c) { int isOperator(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/'); return (c == '+' || c == '-' || c == '*' || c == '/');
} }
int isAlphanumeric(char c) { int isAlphanumeric(char c)
{
return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
} }
void prefixToInfix(char* prefix) { void prefixToInfix(char *prefix)
{
int len = strlen(prefix); int len = strlen(prefix);
char temp[MAX_SIZE]; char temp[MAX_SIZE];
for (int i = len - 1; i >= 0; i--) { for (int i = len - 1; i >= 0; i--)
if (isAlphanumeric(prefix[i])) { {
if (isAlphanumeric(prefix[i]))
{
temp[0] = prefix[i]; temp[0] = prefix[i];
temp[1] = '\0'; temp[1] = '\0';
push(temp); push(temp);
} else if (isOperator(prefix[i])) { }
else if (isOperator(prefix[i]))
{
char op1[MAX_SIZE], op2[MAX_SIZE], result[MAX_SIZE]; char op1[MAX_SIZE], op2[MAX_SIZE], result[MAX_SIZE];
strcpy(op1, pop()); strcpy(op1, pop());
strcpy(op2, pop()); strcpy(op2, pop());
@ -47,17 +58,17 @@ void prefixToInfix(char* prefix) {
push(result); push(result);
} }
} }
printf("Infix expression: %s\n", stack[top]); printf("Infix expression: %s\n", stack[top]);
} }
int main() { int main()
{
char prefix[MAX_SIZE]; char prefix[MAX_SIZE];
printf("Enter prefix expression: "); printf("Enter prefix expression: ");
fgets(prefix, MAX_SIZE, stdin); gets(prefix);
prefix[strcspn(prefix, "\n")] = 0; // Remove newline if present
prefixToInfix(prefix); prefixToInfix(prefix);
return 0; return 0;
} }