Week 8
Week 8
Week 8
pg 132 ***
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char infix[MAX_SIZE];
char postfix[MAX_SIZE];
fgets(infix, MAX_SIZE, stdin);
infix_to_postfix(infix, postfix);
printf("%s\n", postfix);
return 0;
}
-----------------------------------------------------------------------------
pg 133 **
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
while (n--) {
int stack[100], top = -1, a, b;
char c;
while ((c = getchar()) != '?') {
if (c == ' ') continue;
if (c >= '0' && c <= '9') {
int n = 0;
while (c >= '0' && c <= '9') {
n = n * 10 + (c - '0');
c = getchar();
}
stack[++top] = n;
continue;
}
b = stack[top--];
a = stack[top--];
switch (c) {
case '+': stack[++top] = a + b; break;
case '-': stack[++top] = a - b; break;
case '*': stack[++top] = a * b; break;
case '/': stack[++top] = a / b; break;
case '$': stack[++top] = a % b; break;
}
}
printf("%d\n", stack[top--]);
}
return 0;
}
-------------------------------------------------------------------------
pg 134 **
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
scanf("%d", &n);
while (n--) {
char postfix[1000];
scanf("%[^\n]%*c", postfix);
printf("%d\n", evaluate_postfix(postfix));
}
return 0;
}
-------------------------------------------------------------------------
pg 135**
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int is_operator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}
int get_priority(char c) {
if (c == '^') {
return 3;
} else if (c == '*' || c == '/') {
return 2;
} else if (c == '+' || c == '-') {
return 1;
} else {
return 0;
}
}
int main() {
char infix[MAX_LEN], postfix[MAX_LEN];
fgets(infix, MAX_LEN, stdin);
infix_to_postfix(infix, postfix);
printf("%s\n", postfix);
return 0;
}
------------------------------------------------------------------
pg 136 ****
#include <stdio.h>
#include <string.h>
// example usage
int main() {
char expression[100];
scanf("%s", expression);
if (is_balanced_expression) {
printf("1"); // balanced
} else {
printf("0"); // not balanced
}
return 0;
}
---------------------------------------------------------------
pg 137***
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
return stack[0];
}
// example usage
int main() {
char postfix[100];
scanf("%s", postfix);
return 0;
}
-----------------------------------------------------------------
pg 138 **
#include <stdio.h>
#include <math.h>
int main() {
char infix[] = "I-J/K+L^M^N";
float I=2, J=4, K=1, L=3, M=-2, N=5;
float stack[10];
int top = -1;
char c;
for (int i = 0; infix[i] != '\0'; i++) {
c = infix[i];
if (c >= 'a' && c <= 'z') {
stack[++top] = *(float*)(&I + (c - 'a'));
} else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
float operand2 = stack[top--];
float operand1 = stack[top--];
switch (c) {
case '+': stack[++top] = operand1 + operand2; break;
case '-': stack[++top] = operand1 - operand2; break;
case '*': stack[++top] = operand1 * operand2; break;
case '/': stack[++top] = operand1 / operand2; break;
case '^': stack[++top] = pow(operand1, operand2); break;
}
}
}
printf("Result: %.2f\n", stack[top]);
return 0;
}
------------------------------------------------------------
pg 139**
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
return postfix;
}
int main() {
int t;
char expr[MAX_SIZE];
scanf("%d", &t);
for (int i = 0; i < t; i++) {
scanf("%s", expr);
char* postfix = infix_to_postfix(expr);
printf("%s\n", postfix);
free(postfix);
}
return 0;
}
---------------------------------------------------------
pg 140**
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main() {
char expr[] = "((a+b)*(c-d))/(e+f)";
if (check_balanced_parentheses(expr)) {
printf("The expression has balanced parentheses.\n");
} else {
printf("The expression does not have balanced parentheses.\n");
}
return 0;
}
-------------------------------------------------------------
pg 141**
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
int main() {
char expr[] = "((3+4)*5)";
int result = evaluate_expression(expr);
printf("The result is: %d\n", result);
return 0;
}
-------------------------------------------------------------------
pg 142 **
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &stack[i]);
}
int sum = 0;
// Pop two elements from the stack and push them to the queue
for (int i = 0; i < 2 && n > 0; i++) {
n--;
queue[++rear] = stack[n];
}
// Convert the remaining elements of the stack to a queue
while (n > 0) {
n--;
while (front <= rear && stack[n] > queue[rear]) {
stack[++n] = queue[rear--];
}
queue[++rear] = stack[n];
}
// Remove k elements from the queue and add their values to sum
for (int i = 0; i < k && front <= rear; i++) {
sum += queue[front++];
}
printf("%d\n", sum);
return 0;
}
---------------------------------------------------------------------------
pg 143*****
void construct_tower(int N) {
int tower[N], top = -1;
for (int i = 1; i <= N; i++) {
tower[++top] = i;
for (int j = top; j > 0; j--) {
if (tower[j] < tower[j-1]) {
int temp = tower[j];
tower[j] = tower[j-1];
tower[j-1] = temp;
} else {
break;
}
}
for (int j = 0; j <= top; j++) {
printf("%d ", tower[j]);
}
printf("\n");
}
}
-----------------------------------------------------------------------------------
--
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main() {
char infix[MAX_SIZE];
char postfix[MAX_SIZE];
infixToPostfix(infix, postfix);
return 0;
}
-----------------------------------COMPLETED
🙃-----------------------------------------------
COCAINE