update
This commit is contained in:
Binary file not shown.
@@ -1,47 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main() {
|
||||
int i;
|
||||
printf("\n");
|
||||
for (i=12; i>=0; i=i-2) {
|
||||
if (i==0){
|
||||
|
||||
printf("%d \n", i);
|
||||
}
|
||||
else {
|
||||
printf("%d, ", i);
|
||||
}
|
||||
}
|
||||
|
||||
for (i=-1; i>=-13; i=i-2){
|
||||
if (i==-13){
|
||||
printf("%d \n", i);
|
||||
}
|
||||
else {
|
||||
printf("%d, ", i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (i=0; i<=9; i++){
|
||||
if (i == 9){
|
||||
printf("%d \n", (int)pow(i,(double)2));
|
||||
}
|
||||
else{
|
||||
printf("%d, ", (int)pow(i,(double)2));
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0; i<=9; i++){
|
||||
if (i==9){
|
||||
printf("%d \n", i*i);
|
||||
}
|
||||
else{
|
||||
printf("%d, ", i*i);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
/* formatelemente.c
|
||||
*
|
||||
* ACHTUNG: Enthaelt Fehler, die Sie bereinigen sollen!
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
main()i {
|
||||
int i = 66;
|
||||
double x = .000007;
|
||||
double y = 123.45;
|
||||
|
||||
printf("\ni:\n%d \n%o \n%x \n%X", i, i, i, i);
|
||||
|
||||
printf("\nx:\n%f \n%e \n%E \n%g \n%G", x, x, x, x, x);
|
||||
|
||||
printf("\ny:\n%g \n%G \n", y,y);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main() {
|
||||
int fahrenheit = 0;
|
||||
int stepsize = 20;
|
||||
int maxF = 300;
|
||||
|
||||
double celsius;
|
||||
|
||||
printf("\n%s %s %s \n", "Fahrenheit","Celsius (gerundet)","Celsius (exakt)");
|
||||
|
||||
while (fahrenheit <= maxF) {
|
||||
celsius = (5.0/9.0) * (fahrenheit - 32);
|
||||
printf("%d\t\t%d\t\t%f\n", fahrenheit, (int)celsius, celsius);
|
||||
fahrenheit += stepsize;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main (){
|
||||
|
||||
printf("\n Ich kenn die Formatelemente zu 100%% \n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/* typengroesse.c
|
||||
*
|
||||
* Bestimmung der Groesse der einfachen typen mit sizeof
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
char c = 127;
|
||||
unsigned char uc = 255;
|
||||
|
||||
printf("\n");
|
||||
|
||||
//Ganzzahltypen
|
||||
printf("char: \t\t%2d\n", (int) sizeof c);
|
||||
printf("uns. char: \t%2d\n", (int) sizeof uc);
|
||||
|
||||
printf("short: \t\t%2d\n", (int) sizeof (short));
|
||||
printf("uns. short: \t%2d\n", (int) sizeof (unsigned short));
|
||||
|
||||
printf("int: \t\t%2d\n", (int) sizeof (int));
|
||||
printf("uns. int: \t%2d\n", (int) sizeof (unsigned int));
|
||||
|
||||
printf("long: \t\t%2d \n", (int) sizeof (long));
|
||||
printf("uns. long: \t%2d \n", (int) sizeof (unsigned long));
|
||||
|
||||
printf("long long: \t%2d \n", (int) sizeof (long long));
|
||||
printf("uns. long long: %2d \n", (int) sizeof (unsigned long long));
|
||||
|
||||
// Gleitpunkttypen
|
||||
printf("float: \t\t%2d\n", (int) sizeof (float));
|
||||
printf("double: \t%2d\n", (int) sizeof (double));
|
||||
printf("long double: \t%2d\n", (int) sizeof (long double));
|
||||
|
||||
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main(){
|
||||
int i;
|
||||
|
||||
printf("\n");
|
||||
|
||||
i=12;
|
||||
while(i>=0){
|
||||
if (i==0){
|
||||
|
||||
printf("%d \n", i);
|
||||
}
|
||||
else {
|
||||
printf("%d, ", i);
|
||||
}
|
||||
i=i-2;
|
||||
}
|
||||
|
||||
i=-1;
|
||||
do{
|
||||
if (i==-13){
|
||||
printf("%d \n", i);
|
||||
}
|
||||
else {
|
||||
printf("%d, ", i);
|
||||
}
|
||||
i=i-2;
|
||||
}
|
||||
while(i>=-13);
|
||||
|
||||
i=0;
|
||||
while(i<=9){
|
||||
if (i==9){
|
||||
printf("%d \n", i*i);
|
||||
}
|
||||
else{
|
||||
printf("%d, ", i*i);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -1,12 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(){
|
||||
int i;
|
||||
int *ptr = &i;
|
||||
i = 1;
|
||||
printf("ptr = %p \n", ptr); // ptr = 0x7fffc953d9f4
|
||||
printf("*ptr = %d \n", *ptr); // *ptr = 1
|
||||
printf("i = %d \n", i);
|
||||
*ptr = 2;
|
||||
printf("i = %d \n", i);
|
||||
}
|
||||
Binary file not shown.
@@ -1,30 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int y;
|
||||
|
||||
int f(int param1, int param2){
|
||||
if (param2 == 0) {
|
||||
printf("Fehler: Division durch %d nicht möglich \n", param2);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else {
|
||||
int res = (2*(param1*param1)) / param2;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
int x = 4;
|
||||
while (1) {
|
||||
scanf("%d", &y); // scanf erwartet Speicheradresse
|
||||
if (y != 0){
|
||||
break;
|
||||
}
|
||||
else {
|
||||
printf("Fehler: Division durch 0 nicht möglich \n");
|
||||
}
|
||||
}
|
||||
printf("%d \n",f(x,y));
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -1,15 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void swap(int *a1, int *a2){
|
||||
int s = *a1;
|
||||
*a1 = *a2;
|
||||
*a2 = s;
|
||||
}
|
||||
-
|
||||
int main() {
|
||||
int var1 = 4;
|
||||
int var2 = 5;
|
||||
printf("var1: %d, var2: %d \n", var1, var2);
|
||||
swap(&var1, &var2);
|
||||
printf("swapped var1: %d, swapped var2: %d \n", var1, var2);
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,49 +0,0 @@
|
||||
/* array_1.c
|
||||
*
|
||||
* Adressierung von Array-Elementen
|
||||
* Indizes vs. Pointer
|
||||
*/
|
||||
|
||||
// ACHTUNG: Datei enthaelt zu beseitigende Fehler!!!
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int i = 0;
|
||||
int ar[4];
|
||||
ar[0] = 10;
|
||||
ar[1] = 20;
|
||||
ar[2] = 30;
|
||||
ar[3] = 40;
|
||||
int len = (int) sizeof(ar)/sizeof(int);
|
||||
printf("%d \n", len);
|
||||
|
||||
printf("\nar: %p\t &ar[0]: %p", ar, &ar[0]);
|
||||
|
||||
// i) Array-Syntax
|
||||
printf("\n\nElemente von ar, indiziert:\n");
|
||||
for (i=0; i<=(len-1) ; i++){ // hier ergaenzen!!!
|
||||
printf("%d\t",ar[i] ); // hier ergaenzen!!!
|
||||
}
|
||||
|
||||
printf("\n\nAdressen:\n");
|
||||
for (i=0; i<=(len-1); i++){ // hier ergaenzen!!!
|
||||
printf("%p\t",&ar[i] ); // hier ergaenzen!!!
|
||||
}
|
||||
|
||||
// ii) Pointer-Syntax
|
||||
printf("\n\nElemente von ar, referenziert:\n");
|
||||
for (i=0; i<=(len-1); i++){ // hier ergaenzen!!!
|
||||
printf("%d\t", *(ar+i) ); // hier ergaenzen!!!
|
||||
}
|
||||
|
||||
printf("\n\nAdressen:\n");
|
||||
for (i=0; i<=(len-1); i++){ // hier ergaenzen!!!
|
||||
printf("%p\t", ar+i ); // hier ergaenzen!!!
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -1,49 +0,0 @@
|
||||
/* array_1.c
|
||||
*
|
||||
* Adressierung von Array-Elementen
|
||||
* Indizes vs. Pointer
|
||||
*/
|
||||
|
||||
// ACHTUNG: Datei enthaelt zu beseitigende Fehler!!!
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int i = 0;
|
||||
int ar[3];
|
||||
ar[0] = 10;
|
||||
ar[1] = 20;
|
||||
ar[2] = 30;
|
||||
ar[3] = 40;
|
||||
int len = (int) sizeof(ar)/sizeof(int);
|
||||
printf("%d \n", len);
|
||||
|
||||
printf("\nar: %p\t &ar[0]: %p", ar, &ar[0]);
|
||||
|
||||
// i) Array-Syntax
|
||||
printf("\n\nElemente von ar, indiziert:\n");
|
||||
for (i=0; i<=(len+1) ; i++){ // hier ergaenzen!!!
|
||||
printf("%d\t",ar[i] ); // hier ergaenzen!!!
|
||||
}
|
||||
|
||||
printf("\n\nAdressen:\n");
|
||||
for (i=0; i<=(len+1); i++){ // hier ergaenzen!!!
|
||||
printf("%p\t",&ar[i] ); // hier ergaenzen!!!
|
||||
}
|
||||
|
||||
// ii) Pointer-Syntax
|
||||
printf("\n\nElemente von ar, referenziert:\n");
|
||||
for (i=0; i<=(len+1); i++){ // hier ergaenzen!!!
|
||||
printf("%d\t", *(ar+i) ); // hier ergaenzen!!!
|
||||
}
|
||||
|
||||
printf("\n\nAdressen:\n");
|
||||
for (i=0; i<=(len+1); i++){ // hier ergaenzen!!!
|
||||
printf("%p\t", ar+i ); // hier ergaenzen!!!
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -1,12 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#define LEN 10
|
||||
|
||||
int main (){
|
||||
int ar[LEN];
|
||||
for (int i = 1; i<=LEN; i++){
|
||||
ar[i-1] = i*i;
|
||||
printf("%d ",ar[i-1]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -1,20 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#define LEN 10
|
||||
|
||||
|
||||
void rev_print(int * p){
|
||||
for (int i = LEN-1; i>= 0; i--){
|
||||
printf("%d ", *(p+i));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
int main (){
|
||||
int ar[LEN];
|
||||
for (int i = 1; i<=LEN; i++){
|
||||
ar[i-1] = i*i;
|
||||
}
|
||||
rev_print(ar);
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,30 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#define LEN 1000
|
||||
|
||||
int main (){
|
||||
int ar[LEN];
|
||||
|
||||
for (int i = 0; i<=LEN; ++i){
|
||||
ar[i] = i*i;
|
||||
}
|
||||
|
||||
int input;
|
||||
printf("Geben Sie eine ganze Zahl zwischen 1 und %d an: ", LEN);
|
||||
|
||||
while (1){
|
||||
scanf("%d", &input);
|
||||
printf("%d \n",input);
|
||||
|
||||
if (input == 0){
|
||||
break;
|
||||
}
|
||||
if (input < 1 || input > LEN){
|
||||
continue;
|
||||
}
|
||||
|
||||
else{
|
||||
printf("%d \n", ar[input]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#define zv printf("\n")
|
||||
|
||||
int main(){
|
||||
int ar[3][4] = {{0, 1, 2, 3}, {10, 11, 12}};
|
||||
|
||||
int len_ar = (int) sizeof(ar)/sizeof(int);
|
||||
int col = (int) sizeof(ar[0])/sizeof(int);
|
||||
int row = len_ar/col;
|
||||
|
||||
for (int i = 0; i<row; i++){
|
||||
for (int j = 0; j<col; j++){
|
||||
printf("%d \t", ar[i][j]);
|
||||
}
|
||||
zv;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,25 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
|
||||
char *statS = "Hallo PdP";
|
||||
char dynS[10] = "Hallo PdP";
|
||||
|
||||
printf("stat Buchstabe an 2. Stelle: %c \n", dynS[1]);
|
||||
printf("dyn. Buchstabe an 2. Stelle: %c \n", statS[1]);
|
||||
|
||||
//dynS[1] = 'e';
|
||||
//printf("dyn Buchstabe an 2. Stelle verändert: %c \n", dynS[1]);
|
||||
|
||||
//statS[1] = 'e';
|
||||
//printf("stat Buchstabe an 2. Stelle verändert: %c \n", statS[1]);
|
||||
|
||||
statS = "neuer String";
|
||||
printf("stat Buchstabe an 2. Stelle verändert: %c \n", statS[1]);
|
||||
|
||||
dynS = "neuer String";
|
||||
printf("dyn. Buchstabe an 2. Stelle verändert: %c \n", dynS[1]);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/* char_array.c
|
||||
*
|
||||
* Entscheidung, ob eine Zeichenkette ein 'a' enthaelt
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#define MAX 40
|
||||
|
||||
int main() {
|
||||
int index;
|
||||
char eingabe [MAX];
|
||||
|
||||
printf("Bitte String eingeben (max. %d Zeichen): ", MAX-1);
|
||||
fgets(eingabe, MAX, stdin);
|
||||
|
||||
printf("\nEingabe: %s", eingabe);
|
||||
|
||||
for (index = 0; eingabe[index] != '\0'; index++)
|
||||
if (eingabe[index] == 'a')
|
||||
break;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
/* char_array.c
|
||||
*
|
||||
* Entscheidung, ob eine Zeichenkette ein 'a' enthaelt
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#define MAX 40
|
||||
|
||||
int main() {
|
||||
int index;
|
||||
char eingabe [MAX];
|
||||
|
||||
printf("Bitte String eingeben (max. %d Zeichen): ", MAX-1);
|
||||
fgets(eingabe, MAX, stdin);
|
||||
|
||||
printf("\nEingabe: %s", eingabe);
|
||||
|
||||
for (index = 0; eingabe[index] != '\0'; index++){
|
||||
if (eingabe[index] == 'a'){
|
||||
printf("Der String enthält an Position %d ein 'a' \n", index );
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if (eingabe[++index] == '\0'){
|
||||
printf("Der String enthält kein 'a'");
|
||||
}
|
||||
else {continue;}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,31 +0,0 @@
|
||||
/* fehler.c
|
||||
*
|
||||
* Ein Programm, dass Compilerfehler,
|
||||
* Laufzeitfehler und logische Fehler demonstriert.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
|
||||
int num1, num2;
|
||||
num1 = 0;
|
||||
num2 = 1;
|
||||
int temp;
|
||||
|
||||
printf("Der Quotient der Variablen ist: "); //Sytnaxfehler ; einfügen
|
||||
printf("%d\n", num1/num2);
|
||||
printf("\n"); // Syntaxfehler: Anführungzeichen fehlten
|
||||
|
||||
printf("Jetzt werden die Variablenwerte vertauscht.\n");
|
||||
|
||||
// Dies muss korrigiert werden. (logischer Fehler!!!)
|
||||
temp = num1;
|
||||
num1 = num2;
|
||||
num2 = temp;
|
||||
|
||||
printf("Der Quotient der Vaiablen ist nun: ");
|
||||
printf("%d\n", num1/num2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define MAX 100
|
||||
|
||||
int main(){
|
||||
|
||||
char s1[MAX];
|
||||
char s2[MAX];
|
||||
char name[2*MAX];
|
||||
int index;
|
||||
|
||||
// AUffordeurng Eingabe Name
|
||||
printf("Gebe deinen Vornamen an: \n");
|
||||
fgets(s1, MAX, stdin);
|
||||
printf("Gebe deinen Nachnamen an: \n");
|
||||
fgets (s2, MAX, stdin);
|
||||
|
||||
// ob Vorund Nachname verschieden oder gleich sind
|
||||
int rvalue = strcmp(s1, s2);
|
||||
if (rvalue == 0){
|
||||
printf("Namen sind gleich \n");
|
||||
}
|
||||
else {
|
||||
printf("Strings sind ungleich \n");
|
||||
}
|
||||
|
||||
// Großschreibung Nachname
|
||||
for (index = 0; ((s2[index]) != '\0'); ++index){
|
||||
if ((int)s2[index] >= 97 && (int)s2[index] <= 122){
|
||||
s2[index] = (int) s2[index] - 32;
|
||||
}
|
||||
else{continue;}
|
||||
}
|
||||
// printf("großgeschriebener Nachname: %s \n", s2);
|
||||
|
||||
// Vorname NACHNAME String
|
||||
int len_s1 = strlen(s1);
|
||||
if (len_s1 > 0 && s1[len_s1 - 1] == '\n') {
|
||||
s1[len_s1 - 1] = ' ';
|
||||
}
|
||||
else {}
|
||||
strcpy(name, s1);
|
||||
strncat(name, s2, 2*MAX);
|
||||
printf("%s", name);
|
||||
|
||||
// Länge von Namen
|
||||
int len_letters = 0;
|
||||
for (index = 0; name[index] != '\0'; ++index){
|
||||
if ((name[index] >= 'A' && name[index] <= 'Z') || (name[index] >= 'a' && name[index] <= 'z')){
|
||||
len_letters ++;
|
||||
}
|
||||
else{continue;}
|
||||
}
|
||||
printf("Länge des Names: %d \n", len_letters);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -1,21 +0,0 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char * argv[]){
|
||||
if (argc == 2) {
|
||||
printf("bitte argument übergeben"); //argv ist programmname,
|
||||
//argv[1] ist zhl daher 2 stk argc=2
|
||||
}
|
||||
else {
|
||||
double zahl = atof(argv[1]); //covert to float
|
||||
printf("%s³ = %f\n", argv[1], zahl*zahl*zahl);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
@@ -1,37 +0,0 @@
|
||||
/* point_1.c
|
||||
*
|
||||
* Datentyp eines zweidimensionalen Punktes
|
||||
* verschiedene Methoden der Komponentenselektion
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct point {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
int main() {
|
||||
struct point p1, p2;
|
||||
struct point * ptr;
|
||||
|
||||
ptr = &p1;
|
||||
|
||||
// Pfeil- VS. Punktschreibweise
|
||||
p1.x = 3; // Hier .x da p1 ein struct point ist
|
||||
ptr->y = 4; // Hier ->y da ptr ein POINTER auf struct point ist
|
||||
|
||||
printf("\nx-Koordinate von p1: %f", ptr->x);
|
||||
printf("\ny-Koordinate von p1: %f", (*ptr).y);
|
||||
|
||||
p2 = p1;
|
||||
|
||||
printf("\nx-Koordinate von p2: %f", p2.x);
|
||||
printf("\ny-Koordinate von p2: %f", (&p2)->y);
|
||||
|
||||
printf("\n\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
@@ -1,43 +0,0 @@
|
||||
/* point_1.c
|
||||
*
|
||||
* Datentyp eines zweidimensionalen Punktes
|
||||
* verschiedene Methoden der Komponentenselektion
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct point {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
void move(struct point * p, float newx, float newy){
|
||||
p->x = newx;
|
||||
p->y = newy;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct point p1, p2;
|
||||
struct point * ptr;
|
||||
|
||||
ptr = &p1;
|
||||
|
||||
// Pfeil- VS. Punktschreibweise
|
||||
p1.x = 3; // Hier .x da p1 ein struct point ist
|
||||
ptr->y = 4; // Hier ->y da ptr ein POINTER auf struct point ist
|
||||
|
||||
printf("\nx-Koordinate von p1: %f", ptr->x);
|
||||
printf("\ny-Koordinate von p1: %f", (*ptr).y);
|
||||
|
||||
p2 = p1;
|
||||
|
||||
move(ptr, -5, -3);
|
||||
printf("\n Nach dem verschieben von p1:");
|
||||
printf("\n x-Koordinate von p1 %f", p1.x);
|
||||
printf("\ny-Koordinate von p1: %f", (&p1)->y);
|
||||
printf("\nx-Koordinate von p2: %f", p2.x);
|
||||
printf("\ny-Koordinate von p2: %f", (&p2)->y);
|
||||
|
||||
printf("\n\n");
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -1,47 +0,0 @@
|
||||
/* point_3.c
|
||||
*
|
||||
* Datentyp eines zweidimensionalen Punktes
|
||||
* mit Funktion zum Verschieben
|
||||
* und dynamischer Speicherverwaltung
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct point {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
|
||||
void move(struct point * punkt, float dx, float dy) {
|
||||
punkt->x += dx;
|
||||
punkt->y += dy;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
struct point * ptr;
|
||||
|
||||
ptr = malloc(sizeof(struct point)); // impl. Typcast des Pointers
|
||||
|
||||
if (ptr != NULL) {
|
||||
ptr->x = 10;
|
||||
ptr->y = 20;
|
||||
|
||||
printf("\nx-Koordinate von p: %f", ptr->x);
|
||||
printf("\ny-Koordinate von p: %f", ptr->y);
|
||||
|
||||
free(ptr);
|
||||
|
||||
printf("\n");
|
||||
printf("\nnach free():");
|
||||
|
||||
// Dangling Pointer!
|
||||
printf("\nx-Koordinate von p: %f", ptr->x);
|
||||
printf("\ny-Koordinate von p: %f", ptr->y);
|
||||
|
||||
printf("\n\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,166 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Definition eines Punktes
|
||||
typedef struct Point {
|
||||
int x;
|
||||
int y;
|
||||
struct Point* next;
|
||||
} Point;
|
||||
|
||||
// Funktion zum Erstellen eines neuen Punktes
|
||||
Point* create_point(int x, int y) {
|
||||
Point* new_point = (Point*)malloc(sizeof(Point));
|
||||
if (new_point == NULL) {
|
||||
printf("Memory allocation failed\n");
|
||||
return NULL;
|
||||
}
|
||||
new_point->x = x;
|
||||
new_point->y = y;
|
||||
new_point->next = NULL;
|
||||
return new_point;
|
||||
}
|
||||
|
||||
// Funktion zum Anhängen eines Punktes an das Ende der Liste
|
||||
void append(Point** head, int x, int y) {
|
||||
Point* new_point = create_point(x, y);
|
||||
if (*head == NULL) {
|
||||
*head = new_point;
|
||||
return;
|
||||
}
|
||||
Point* temp = *head;
|
||||
while (temp->next != NULL) {
|
||||
temp = temp->next;
|
||||
}
|
||||
temp->next = new_point;
|
||||
}
|
||||
|
||||
// Funktion zum Entfernen des letzten Punktes aus der Liste
|
||||
int shorten(Point** head) {
|
||||
if (*head == NULL) {
|
||||
return -1; // Liste ist leer
|
||||
}
|
||||
if ((*head)->next == NULL) {
|
||||
free(*head);
|
||||
*head = NULL;
|
||||
return 0;
|
||||
}
|
||||
Point* temp = *head;
|
||||
while (temp->next->next != NULL) {
|
||||
temp = temp->next;
|
||||
}
|
||||
free(temp->next);
|
||||
temp->next = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Funktion zum Ausgeben der Punkte in der Liste
|
||||
void pretty_print(Point* head) {
|
||||
Point* temp = head;
|
||||
while (temp != NULL) {
|
||||
printf("(%d, %d) -> ", temp->x, temp->y);
|
||||
temp = temp->next;
|
||||
}
|
||||
printf("NULL\n");
|
||||
}
|
||||
|
||||
// Funktion zum Einfügen eines Punktes an einer bestimmten Position
|
||||
int insert(Point** head, int pos, int x, int y) {
|
||||
if (pos < 0) {
|
||||
return -1; // Ungültige Position
|
||||
}
|
||||
Point* new_point = create_point(x, y);
|
||||
if (pos == 0) {
|
||||
new_point->next = *head;
|
||||
*head = new_point;
|
||||
return 0;
|
||||
}
|
||||
Point* temp = *head;
|
||||
for (int i = 0; i < pos - 1; i++) {
|
||||
if (temp == NULL) {
|
||||
free(new_point);
|
||||
return -1; // Position außerhalb der Liste
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
new_point->next = temp->next;
|
||||
temp->next = new_point;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Funktion zum Spiegeln der Liste
|
||||
int mirror(Point** head) {
|
||||
if (*head == NULL || (*head)->next == NULL) {
|
||||
return -1; // Leere Liste oder nur ein Element
|
||||
}
|
||||
Point* prev = NULL;
|
||||
Point* current = *head;
|
||||
Point* next = NULL;
|
||||
while (current != NULL) {
|
||||
next = current->next;
|
||||
current->next = prev;
|
||||
prev = current;
|
||||
current = next;
|
||||
}
|
||||
*head = prev;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Hauptfunktion zum Testen der Implementierung
|
||||
int main() {
|
||||
Point* head = NULL;
|
||||
|
||||
// Testen der append-Funktion
|
||||
append(&head, 1, 2);
|
||||
append(&head, 3, 4);
|
||||
pretty_print(head);
|
||||
|
||||
// Testen der shorten-Funktion
|
||||
shorten(&head);
|
||||
pretty_print(head);
|
||||
shorten(&head);
|
||||
pretty_print(head);
|
||||
shorten(&head); // Sollte -1 zurückgeben, da die Liste leer ist
|
||||
|
||||
// Testen der insert-Funktion
|
||||
insert(&head, 0, 5, 6);
|
||||
insert(&head, 1, 7, 8);
|
||||
pretty_print(head);
|
||||
|
||||
// Testen der mirror-Funktion
|
||||
mirror(&head);
|
||||
pretty_print(head);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
punkt (x,y)
|
||||
|
||||
shorten(input: head, output: int (-1 nihctvgekalppt (bei leerer Liste, 0 hat geklappt)
|
||||
free(speicherdresse von letztes element), vorletzten next auf null setzen,
|
||||
|
||||
next von next (übernächstes)
|
||||
wenn liste nur noch ein element: head von nächstes auf null
|
||||
|
||||
|
||||
insert: (input: head, pos, le), output: 0,-1(
|
||||
-liste leer
|
||||
-position außerhalb liste pos> len(head)
|
||||
- erstes,
|
||||
- insert: nur elemente an position einfügen, die zurzeit schin besetzsind
|
||||
- nihct letzte Element+1 als position möglich keine verlängerung sondern nur zwischen vorhanden elementen! möglich muss zwischen elemente gepackt werden
|
||||
-
|
||||
|
||||
mirror: input:head, ouput: int (-1(leere liste),0)
|
||||
randfalle (ranfälle als komentar: wenn nru ein element, oer leer kein mirror möglich)
|
||||
1. leere liste -> -1
|
||||
2. 1 element -> -1
|
||||
|
||||
nxt pointer auf vorherigr le setzen
|
||||
|
||||
pretty print
|
||||
|
||||
*/
|
||||
|
||||
Binary file not shown.
@@ -1,31 +0,0 @@
|
||||
// typfehler.c
|
||||
//
|
||||
// Benutzereingaben mit Typfehler abfangen
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
|
||||
float zahl;
|
||||
int status;
|
||||
char c;
|
||||
|
||||
|
||||
while (1) {
|
||||
printf("Geben Sie eine Zahl ein: ");
|
||||
status = scanf("%f", &zahl);
|
||||
if (status != 0){
|
||||
break;
|
||||
}
|
||||
do {
|
||||
//
|
||||
c = getchar();
|
||||
}
|
||||
while (c != '\n');
|
||||
printf(" Typfehler bei Benutzeingabe");
|
||||
}
|
||||
|
||||
printf("Die Zahl ist %f\n", zahl);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -1,18 +0,0 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
unsigned long long explode(unsigned int m, unsigned short n);
|
||||
|
||||
int main(){
|
||||
printf("2* 2^n: %llu", explode(2,3));
|
||||
}
|
||||
|
||||
unsigned long long explode(unsigned int m, unsigned short n){
|
||||
return m << n; // n mal nach links shiften m*2^n
|
||||
}
|
||||
|
||||
struct Node {
|
||||
int data;
|
||||
struct Node *next;
|
||||
};
|
||||
Binary file not shown.
@@ -1,17 +0,0 @@
|
||||
//date.c
|
||||
|
||||
#include <stdio.h>
|
||||
#include "date.h"
|
||||
|
||||
struct date setInitDate(int year){
|
||||
struct date d;
|
||||
d.year = year;
|
||||
d.month = 1;
|
||||
d.day = 1;
|
||||
return d;
|
||||
}
|
||||
|
||||
void printDate(struct date * datum){
|
||||
printf("date: %d.%d.%d \n", datum->day, datum->month, datum->year);
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
// date.h
|
||||
|
||||
#ifndef DATE_H
|
||||
#define DATE_H
|
||||
|
||||
struct date{
|
||||
int day, month, year;
|
||||
};
|
||||
|
||||
struct date setInitDate(int year);
|
||||
void printDate(struct date *d);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
@@ -1,28 +0,0 @@
|
||||
//highscore.c
|
||||
|
||||
#include "highscore.h"
|
||||
#include <stdio.h>
|
||||
|
||||
//define TEST
|
||||
#if defined TEST
|
||||
int main(){
|
||||
struct highscore hs;
|
||||
struct date datum = setInitDate(2077);
|
||||
int score = 9001;
|
||||
setHighscore(&hs, score, datum);
|
||||
printHighscore(&hs);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void setHighscore(struct highscore *hs, struct date datum, int newscore){
|
||||
hs->date = datum;
|
||||
hs->score = newscore;
|
||||
}
|
||||
|
||||
void printHighscore(struct highscore * hs){
|
||||
printf(" Scores ab %d \n", firstYear);
|
||||
printf("your highscore: %d at date %d.%d.%d \n", hs->score, hs->date.day, hs->date.month, hs->date.year);
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
// highscore.h
|
||||
|
||||
#include "date.h"
|
||||
|
||||
#ifndef HIGHSCORE_H
|
||||
#define HIGHSCORE_H
|
||||
|
||||
int firstYear;
|
||||
|
||||
struct highscore{
|
||||
struct date date;
|
||||
int score;
|
||||
};
|
||||
|
||||
void setHighscore(struct highscore * hs, struct date datum, int newscore);
|
||||
void printHighscore(struct highscore * hs);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
@@ -1,28 +0,0 @@
|
||||
// rec_static.c
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void rec_out(int n);
|
||||
void decr(int n);
|
||||
|
||||
|
||||
int main(){
|
||||
rec_out(6);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void decr(int n){
|
||||
rec_out(--n);
|
||||
}
|
||||
|
||||
|
||||
void rec_out(int n){
|
||||
static int count = 1;
|
||||
printf("Die %d. Ausgabe.\n", count);
|
||||
count++;
|
||||
if (n > 1){
|
||||
decr(n);
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,13 +0,0 @@
|
||||
#include "highscore.h"
|
||||
|
||||
int firstYear = 2000;
|
||||
|
||||
int main(){
|
||||
struct highscore hs;
|
||||
struct date datum = setInitDate(2077);
|
||||
int score = 9001;
|
||||
setHighscore(&hs, datum, score);
|
||||
printHighscore(&hs);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user