Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
1.5 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define a structure for a quadruple
typedef struct {
    char op[10];      // Operator
    char arg1[10];    // First argument
    char arg2[10];    // Second argument
    char result[10];  // Result
} Quadruple;

// Function to print the quadruples
void printQuadruples(Quadruple *quad, int n) {
    printf("Quadruples:\n");
    printf("%-10s %-10s %-10s %-10s\n", "Op", "Arg1", "Arg2", "Result");
    for (int i = 0; i < n; i++) {
        printf("%-10s %-10s %-10s %-10s\n", quad[i].op, quad[i].arg1, quad[i].arg2, quad[i].result);
    }
}

int main() {
    int n;

    // Ask user for number of quadruples
    printf("Enter the number of quadruples: ");
    scanf("%d", &n);

    // Allocate memory for quadruples
    Quadruple *quad = (Quadruple *)malloc(n * sizeof(Quadruple));
    
    // Input quadruples from user
    for (int i = 0; i < n; i++) {
        printf("Enter operator for quadruple %d: ", i + 1);
        scanf("%s", quad[i].op);
        printf("Enter arg1 for quadruple %d: ", i + 1);
        scanf("%s", quad[i].arg1);
        printf("Enter arg2 for quadruple %d: ", i + 1);
        scanf("%s", quad[i].arg2);
        printf("Enter result for quadruple %d: ", i + 1);
        scanf("%s", quad[i].result);
    }

    // Print the quadruples
    printQuadruples(quad, n);

    // Free allocated memory
    free(quad);

    return 0;
}
Leave a Comment