initial commit

This commit is contained in:
Steffen Vogel 2010-11-09 03:16:35 +01:00
commit 8fd792eb14
30 changed files with 314 additions and 0 deletions

BIN
1_n_a_factorial Executable file

Binary file not shown.

14
1_n_a_factorial.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#define N 10
int main() {
int i, solution = 1;
for (i = 1; i <= N; i++) {
solution *= i;
printf("The factorial of %d is:\t%d\n", i, solution);
}
return 0;
}

BIN
1_n_b_ascii Executable file

Binary file not shown.

11
1_n_b_ascii.c Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
int main() {
char letter = 'A';
printf("Letter\tDec in ASCII-Table\n");
do printf("%c\t\t\t%d\n", letter, letter);
while (letter++ <= 'Z');
return 0;
}

BIN
1_p_a_kanten Executable file

Binary file not shown.

16
1_p_a_kanten.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#define DELTA 1
#define MAX 10
int main() {
int umfang, flaeche, a = 0;
do {
umfang = 4*a;
flaeche = a*a;
printf("Seitenlaenge = %d cm \t=> Fläche = %d cm^2 \tund Umfang = %d cm\n", a, flaeche, umfang);
a = a + DELTA;
} while (a <= MAX);
return 0;
}

BIN
1_v_a_square Executable file

Binary file not shown.

16
1_v_a_square.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <math.h>
#define MAX 10
int main() {
int n = 1;
printf("Liste der ersten %d Quadratzahlen:\n", MAX);
while (n <= MAX) {
printf("%d\n", (int) pow(n, 2));
n += 1;
}
return 0;
}

BIN
2_p_b_raten Executable file

Binary file not shown.

27
2_p_b_raten.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#define MAX 3
#define FIND 'z'
int main() {
int anzahl;
char versuch;
for(anzahl=1; anzahl <= 3; anzahl++) {
printf("Enter letter:\t");
scanf(" %c", &versuch);
if (versuch == FIND) {
printf("Super!\n");
return 0;
}
else {
printf("Wrong! Next try!\n");
}
}
printf("You missed all your chances!\n");
return -1;
}

BIN
2_p_c_tree Executable file

Binary file not shown.

14
2_p_c_tree.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
int main() {
int level;
for (level = 1; level < 7; level++) {
int column;
for (column = 1; column <= level; column++) {
printf("*");
}
printf("\n");
}
return 0;
}

BIN
2_p_d_einXeins Executable file

Binary file not shown.

14
2_p_d_einXeins.c Normal file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
int main() {
int a, b;
for (a = 0; a <= 10; a++) {
for (b = 0; b <= 10; b++) {
printf("%d\t", a*b);
}
printf("\n");
}
return 0;
}

BIN
2_v_b_logic Executable file

Binary file not shown.

16
2_v_b_logic.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
int main() {
/* table header */
printf("a\tb\t!a\ta || b\ta && b\ta^b\n");
printf("--------------------------------------------\n");
int a, b;
for (a= 0; a < 2; a++) {
for (b = 0; b < 2; b++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", a, b, !a, a || b, a && b, a^b);
}
}
return 0;
}

BIN
3_n_a_xor_encryption Executable file

Binary file not shown.

1
3_n_a_xor_encryption.c Normal file
View File

@ -0,0 +1 @@
#include <stdio.h> #include <string.h> #define MAX_LENGTH 31 // 30 characters + '\0' termination #define SALT 15 int main() { char input[MAX_LENGTH]; int offset = 0; char character; // prompt for input printf("Please enter message: "); while (offset + 1 < MAX_LENGTH && (character = getchar()) != '\n') { input[offset++] = character; } input[offset] = '\0'; // encrypt message for (offset = 0; offset < strlen(input); offset++) { input[offset] ^= SALT; } printf("Encrypted message: %s\n", input); // decrypt message for (offset = 0; offset < strlen(input); offset++) { input[offset] ^= SALT; } printf("Decrypted message: %s\n", input); return 0; }

BIN
3_n_b_scalarm Executable file

Binary file not shown.

36
3_n_b_scalarm.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
#include <math.h>
#define DIMENSION 2
int main() {
float matrix[DIMENSION][DIMENSION] = {
{2.1, 3.0},
{4.2, 3.5}
};
float scalar;
int i, j;
printf("Matrix before operation:\n");
for (i = 0; i < DIMENSION; i++) {
for (j = 0; j < DIMENSION; j++) {
printf("%.3f\t", matrix[i][j]);
}
printf("\n");
}
printf("Please enter a scalar to multiply with: ");
scanf("%f", &scalar);
printf("Matrix after operation:\n");
for (i = 0; i < DIMENSION; i++) {
for (j = 0; j < DIMENSION; j++) {
matrix[i][j] *= scalar;
printf("%.3f\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}

BIN
3_n_c_userdb Executable file

Binary file not shown.

50
3_n_c_userdb.c Normal file
View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ACCOUNTS 10
#define BUFFER_SIZE 256
typedef struct {
char active;
char * name;
char * password;
} user;
int main() {
user users[MAX_ACCOUNTS];
char input_buffer[BUFFER_SIZE];
int count, offset;
for (offset = 0, count = 0; offset < MAX_ACCOUNTS; offset++, count++) {
printf("Should this user be activated? (0/1/-1): "); // -1 for break input
fgets(input_buffer, BUFFER_SIZE, stdin);
users[offset].active = atoi(input_buffer);
if (users[offset].active == -1) {
break;
}
printf("Please enter a username: ");
fgets(input_buffer, BUFFER_SIZE, stdin);
users[offset].name = malloc(strlen(input_buffer));
strncpy(users[offset].name, input_buffer, strlen(input_buffer)-1);
printf("Please enter a password: ");
fgets(input_buffer, BUFFER_SIZE, stdin);
users[offset].password = malloc(strlen(input_buffer));
strncpy(users[offset].password, input_buffer, strlen(input_buffer)-1);
}
if (count > 0) {
/* table header */
printf("No.\tName\t\t\tPassword\t\tActive?\n");
for (offset = 0; offset < count; offset++) {
printf("%d\t%-24s%-24s%s\n", offset+1, users[offset].name, users[offset].password, users[offset].active ? "yes" : "no");
}
return 0;
}
else {
return -1;
}
}

BIN
3_p_a_zaehlen Executable file

Binary file not shown.

17
3_p_a_zaehlen.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#define NEEDLE 'e'
int main() {
int counter = 0;
char character;
printf("Please enter a message: ");
while ((character = getchar()) != '\n') {
if (character == NEEDLE) counter++;
}
printf("Found '%c' %d times\n", NEEDLE, counter);
return 0;
}

BIN
3_p_b_transponieren Executable file

Binary file not shown.

31
3_p_b_transponieren.c Normal file
View File

@ -0,0 +1,31 @@
#include <stdio.h>
#define DIMENSION 4
int main() {
int matrix[DIMENSION][DIMENSION] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int temp, x, y;
for (x = 0; x < DIMENSION; x++) {
for (y = 0; y < x; y++) {
temp = matrix[x][y];
matrix[x][y] = matrix[y][x];
matrix[y][x] = temp;
}
}
for (x = 0; x < DIMENSION; x++) {
for (y = 0; y < DIMENSION; y++) {
printf("%d\t", matrix[x][y]);
}
printf("\n");
}
return 0;
}

BIN
3_p_c_parteien Executable file

Binary file not shown.

36
3_p_c_parteien.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
#define MAX_SEATS 200
#define THRESHOLD 5
typedef struct {
char * name;
float percentage;
int seats;
} party;
int main() {
party parties[] = {
{"MFG", 40.3},
{"Mitte", 55.0},
{"EBS", 4.7}
};
int i;
const int party_count = sizeof parties / sizeof (party);
float significant_percentage = 0;
for (i = 0; i < party_count; i++) {
if (parties[i].percentage >= THRESHOLD) {
significant_percentage += parties[i].percentage;
}
}
for (i = 0; i < party_count; i++) {
parties[i].seats = parties[i].percentage >= THRESHOLD ? MAX_SEATS * (parties[i].percentage / significant_percentage) : 0;
printf("%s\t%.1f%%\t(%d Sitze)\n", parties[i].name, parties[i].percentage, parties[i].seats);
}
return 0;
}

12
Makefile Normal file
View File

@ -0,0 +1,12 @@
SRCS = $(wildcard *.c)
PROGS = $(patsubst %.c,%,$(SRCS))
CFLAGS += -g -Wall -O0
all: $(PROGS)
%: %.c
$(CC) $(CFLAGS) -o $@ $<
clean:
rm -f *.o *.save $(PROGS)

3
README Normal file
View File

@ -0,0 +1,3 @@
Lösungen zu den Aufgaben der KGÜ Information 1 im WS 2010/11 an der RWTH Aachen
Benutze "make all" um alle Programme auf einmal zu kompilieren