scalar/qgl0.c /* * * */ Gleichung zweiten Grades ats 10/86 #include <math.h> #include <stdio.h> int main (void) { printf("Loesungen:\n"); printf("%g\n", (−5.0 + sqrt(5.0 * 5.0 − 4.0 * (−3.0) * 2.0)) / (2.0 * 2.0)); printf("%g\n", (−5.0 − sqrt(5.0 * 5.0 − 4.0 * (−3.0) * 2.0)) / (2.0 * 2.0)); return 0; } scalar/qgl1.c /* * * */ Gleichung zweiten Grades ats 10/90 problematische Typen #include <math.h> #include <stdio.h> int main (void) { printf("Loesungen:\n"); printf("%g\n", (−5 + sqrt(5 * 5 − 4 * (−3) * 2)) / (2 * 2)); printf("%g\n", (−5 − sqrt(5 * 5 − 4 * (−3) * 2)) / (2 * 2)); return 0; } scalar/qgl2.c /* * * */ Gleichung zweiten Grades ats 10/90 benannte Koeffizienten #include <math.h> #include <stdio.h> int main (void) { double a, b, c; double w; double x1, x2; /* Koeffizienten */ /* Wurzel */ /* Loesungen */ a = 2.0; b = 5.0; c = − 3.0; printf("Koeffizienten %g %g %g\n", a, b, c); w = sqrt(b * b − 4 * a * c); x1 = (−b + w) / (2 * a); x2 = (−b − w) / (2 * a); printf("Loesungen %g %g\n", x1, x2); return 0; } scalar/qgl3.cc /* * * */ Gleichung zweiten Grades ats 10/90 Konstanten, C++ Definitionen #include <math.h> #include <stdio.h> int main (void) { const double a = 2.0, b = 5.0, c = − 3.0; printf("Koeffizienten %g %g %g\n", a, b, c); const double w = sqrt(b * b − 4 * a * c); const double x1 = (−b + w) / (2 * a); const double x2 = (−b − w) / (2 * a); printf("Loesungen %g %g\n", x1, x2); return 0; } scalar/qgl4.c /* * * */ Gleichung zweiten Grades ats 10/90 mit Sonderfaellen #include <math.h> #include <stdio.h> int main (void) { double a, b, c; double d; double x1, x2; /* Koeffizienten */ /* Diskriminante */ /* Loesungen */ printf("Koeffizienten ? "); scanf("%lf %lf %lf", & a, & b, & c); d = b * b − 4 * a * c; if (d < 0.0) printf("Es gibt keine reellen Loesungen\n"); else { d = sqrt(d); x1 = (−b + d) / (2 * a); x2 = (−b − d) / (2 * a); printf("Loesungen %g %g\n", x1, x2); } return d < 0.0; /* 0: ok, 1: keine Loesungen */ } scalar/qgl5.c /* * * */ Gleichung zweiten Grades ats 10/90 mit Koeffizienten aus der Kommandozeile #include <math.h> #include <stdio.h> #include <stdlib.h> /* sqrt */ /* atof */ int main (int argc, char * argv[]) { double a, b, c; /* Koeffizienten */ double d; /* Diskriminante */ double x1, x2; /* Loesungen */ if (argc < 4) printf("Aufruf: %s a b c\n", argv[0]); else { a = atof(argv[1]); b = atof(argv[2]); c = atof(argv[3]); printf("Koeffizienten %g %g %g\n", a, b, c); d = b * b − 4 * a * c; if (d < 0.0) printf("Es gibt keine reellen Loesungen\n"); else { d = sqrt(d); x1 = (−b + d) / (2 * a); x2 = (−b − d) / (2 * a); printf("Loesungen %g %g\n", x1, x2); return 0; } } return 1; } scalar/newton.c /* * * */ Quadratwurzel mit Newton Verfahren ats 10/90 #include <stdio.h> int main (void) { double A, t; double w0, w1; /* Argument, Fehler */ printf("Argument der Wurzel ? "); scanf("%lf", & A); printf("Fehlergrenze ? "); scanf("%lf", & t); w1 = A; do { w0 = w1; w1 = (w0 + A / w0) / 2.0; } while (w0 − w1 > t); printf("Wurzel: %g\n", w1); printf("Quadrat: %g\n", w1 * w1); return 0; } scalar/euklid0.c /* * * */ Euklid’s Algorithmus −− groesster gemeinsamer Teiler ats 10/90 #include <stdio.h> int main (void) { int x, y; scanf("%d %d", & x, & y); printf("ggT(%d, %d) == ", x, y); while (x != y) if (x > y) x = x − y; else y = y − x; printf("%d\n", x); return 0; } scalar/euklid1.c /* * * */ Euklid’s Algorithmus −− groesster gemeinsamer Teiler ats 10/90 robustere Eingabe #include <stdio.h> int main (void) { int x, y; while (1) { printf("Bitte zwei Zahlen eingeben: "); if (scanf("%d %d", & x, & y) < 2) break; if (x <= 0 || y <= 0) printf("beide muessen positiv sein\n"); else { printf("ggT(%d, %d) == ", x, y); while (x != y) if (x > y) x = x − y; else y = y − x; printf("%d\n", x); } } return 0; } scalar/euklid2.c /* * * */ Euklid’s Algorithmus −− groesster gemeinsamer Teiler ats 10/90 rekursive Loesung fuer Kommandoargumente #include <stdio.h> #include <stdlib.h> /* atoi */ int ggT (int x, int y) { if (x == y) return x; if (x > y) return ggT(x−y, y); return ggT(x, y−x); } int main (int argc, char * argv[]) { int x, y; if (argc < 3) return printf("Aufruf: %s x y\n", argv[0]); x = atoi(argv[1]); y = atoi(argv[2]); if (x <= 0 || y <= 0) return printf("Zahlen muessen positiv sein\n"); printf("ggT(%d, %d) == %d\n", x, y, ggT(x, y)); return 0; } scalar/int0.c /* * * */ Groesse einer Integer ats 10/90 #include <stdio.h> int main (void) { int i, j = 1; do { i = j; j = i << 1 | 1; } while (j > i); printf("max: %d, ", i); do { i = j; j = i << 1; } while (j < i); printf("min: %d\n", i); return 0; } scalar/int1.c /* * * */ Groesse einer Integer ats 10/90 unsigned #include <stdio.h> int main (void) { unsigned i, j = 1; do i = j, j = i << 1 | 1; while (j > i); printf("max: %u, ", i); if (i == j) i = 0; else do i = j, j = i << 1; while (j < i); printf("min: %d\n", i); return 0; } scalar/int2.c /* * * */ Groesse einer Integer ats 10/90 fuer alle Typen mit C Preprozessor #include <stdio.h> #define TEST(INTEGER) { INTEGER i, j = 1; printf("%−20s", #INTEGER ":"); do i = j, j = i << 1 | 1; while (j > i); printf("max: %lu, ", (unsigned long) i); if (i == j) i = 0; else do i = j, j = i << 1; while (j < i); printf("min: %ld\n", (long) i); } int main (void) { TEST(char); TEST(unsigned char); TEST(signed char); TEST(short); TEST(unsigned short); TEST(int); TEST(unsigned); TEST(long); TEST(unsigned long); return 0; } \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ scalar/int3.c /* * * */ Groesse einer Integer ats 10/90 ohne Schleifen #include <stdio.h> #define max(type, utype) ((type) ˜0L > 0 ? (type) ˜0L : (utype) ˜0L >> 1) \ #define min(type, utype) ((type) ˜0L > 0 ? 0 : (type) ˜ ((utype) ˜0L >> 1)) \ #define TEST(type, utype) \ printf("%−20smax: %lu, min: %ld\n", #type ":", \ (unsigned long) max(type, utype), (long) min(type, utype)) int main (void) { TEST(char, unsigned char); TEST(unsigned char, unsigned char); TEST(signed char, unsigned char); TEST(short, unsigned short); TEST(unsigned short, unsigned short); TEST(int, unsigned); TEST(unsigned, unsigned); TEST(long, unsigned long); TEST(unsigned long, unsigned long); return 0; }