(C) David Vajda 2024-12-22 Grammar Expressions for Integration Excersize expr ::= term term ::= fact | fact + term fact ::= smpl | (expr) | (expr) * fact expr ::= term | expr + simpleterm term ::= factor fact ::= simpleterm | (expr) | (expr) * fact simpleterm ::= simple expr ::= term | simpleexpr simpleexpr ::= simpleexpr + simpleterm term ::= factor fact ::= simpleterm | (expr) | (expr) * fact simpleterm ::= simple expr ::= term | simpleexpr simpleexpr ::= simpleexpr + simple term ::= fact fact ::= simple | (expr) | (expr) * fact expr ::= term | simpleexpr simpleexpr ::= simple + simpleexpr term ::= fact fact ::= simple | (expr) | (expr) * fact expr ::= term | simpleexpr simpleexpr ::= simple + simpleexpr term ::= fact fact ::= simple | (expr) | (expr) * notsimpleexpr notsimpleexpr ::= (expr) | (expr) * notsimpleexpr expr ::= term | simpleexpr simpleexpr ::= simple + simpleexpr term ::= fact fact ::= simple | (expr) | (expr) * notsimplefact notsimpleexpr ::= (expr) | (expr) * notsimplefact fact ::= (expr) * simple | simple * (expr) | (expr) * (expr) fact ::= simple | (expr) * simple | simple * (expr) | (expr) * (expr) expr ::= term | term + expr term ::= fact fact ::= simple | (expr) * simple | simple * (expr) | (expr) * (expr) expr ::= term | term + expr expr2 ::= term + expr term ::= fact fact ::= simple | (expr2) * simple | simple * (expr2) | (expr2) * (expr2) |
Das ist die richtige
expr ::= term | term + expr expr2 ::= term + expr term ::= fact fact ::= simple | (expr2) * simple | simple * (expr2) | (expr2) * (expr2) |
C-Code
#include <stdio.h> #include <time.h> #include <stdlib.h> /* expr ::= term | term + expr expr2 ::= term + expr term ::= fact fact ::= simple | (expr2) * simple | simple * (expr2) | (expr2) * (expr2) */ void expr (void); void expr2 (void); void term (void); void fact (void); void simple (void); int main (void) { time_t t; struct tm tm = *localtime(&(time_t){time(NULL)}); printf ("(C) David Vajda\n"); printf ("%s", asctime(&tm)); printf ("Uebungen fuer Integralrechnung und Extrema\n\n"); srand ((unsigned)time (&t)); expr (); printf ("\n"); return 0; } void expr (void) { term (); if ((rand () % 2) == 1) { printf (" + "); expr (); } return; } void expr2 (void) { term (); printf (" + "); expr (); return; } void term (void) { fact (); return; } int count = 0; void fact (void) { int r = rand () % 4; count++; if ((r == 0) | (count > 3)) simple (); else if (r == 1) { printf ("("); expr2 (); printf (") * "); simple (); } else if (r == 2) { simple (); printf (" * ("); expr2 (); printf (")"); } else if (r == 3) { printf ("("); expr2 (); printf (") * ("); expr2 (); printf (")"); } return; } void simple (void) { printf ("%i*x^%i", 1+rand() % 8, rand () % 8); } |
Erzeugt:
(C) David Vajda Sun Dec 22 19:38:48 2024 Uebungen fuer Integralrechnung und Extrema ((3*x^7 + 7*x^4) * 7*x^3 + 5*x^7) * (5*x^5 + 1*x^3) (C) David Vajda Sun Dec 22 19:46:00 2024 Uebungen fuer Integralrechnung und Extrema 3*x^0* (8*x^2* (3*x^0 + 8*x^0 + 7*x^1 + 1*x^1) + 1*x^1 + 2*x^6 + 1*x^2 + 8*x^2 + 7*x^0 + 7*x^5 + 6*x^4) + 2*x^6 (C) David Vajda Sun Dec 22 19:50:18 2024 Uebungen fuer Integralrechnung und Extrema 3*x^2 * (2*x^3 * (4*x^0 + 7*x^2) + 5*x^7) |