added ws202425 courses
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,6 @@
|
||||
- [X] Übung 1
|
||||
- [X] Übung 2
|
||||
- [X] Übung 3
|
||||
- [ ] Übung 4
|
||||
- [ ] Übung 5
|
||||
- [ ] Übung 6
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void){
|
||||
for(int i = 12; i >= 0; i-=2){
|
||||
printf("%i, ",i);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for(int i = -1; i >= -13; i-=2){
|
||||
printf("%i, ", i);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for(int i = 0; i <= 9; i++){
|
||||
printf("%i, ", i*i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/* formatelemente.c
|
||||
*
|
||||
* ACHTUNG: Enthaelt Fehler, die Sie bereinigen sollen!
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(){
|
||||
int i = 66;
|
||||
double x = .000007;
|
||||
double y = 123.45;
|
||||
|
||||
/* Printing the integer 'i' in various formats
|
||||
%d prints the integer in decimal format
|
||||
%o prints the integer in octal format
|
||||
%x prints the integer in hexadecimal (lowercase)
|
||||
%X prints the integer in hexadecimal (uppercase)*/
|
||||
printf("\ni:\n%d \n%o \n%x \n %X", i, i, i, i);
|
||||
|
||||
// Printing the double 'x' in various formats
|
||||
// %f prints the double in fixed-point notation
|
||||
// %e prints the double in scientific notation (lowercase 'e')
|
||||
// %E prints the double in scientific notation (uppercase 'E')
|
||||
// %g prints the double in either %f or %e, whichever is shorter, removing trailing zeros
|
||||
// %G prints the double in either %f or %E, whichever is shorter, removing trailing zeros
|
||||
printf("\nx:\n%f \n%e \n%E \n%g \n%G", x, x, x, x, x);
|
||||
|
||||
// Printing the double 'y' in a couple of formats
|
||||
// %g prints the double in either %f or %e, depending on which is shorter, removing trailing zeros
|
||||
// %G prints the double in either %f or %E, depending on which is shorter, removing trailing zeros
|
||||
printf("\ny:\n%g \n%G\n", y, y);
|
||||
|
||||
// Print sentence
|
||||
printf("Ich kenne die Formatelemente zu 100 %%.\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
/* 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;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main (){
|
||||
|
||||
printf("\n Ich kenn die Formatelemente zu 100%% \n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/* 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;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void){
|
||||
int c_round;
|
||||
float c_exact;
|
||||
int f=0;
|
||||
int max_f = 300;
|
||||
int steps = 20;
|
||||
|
||||
printf("Fahrenheit \t Celsius(gerundet) \t Celsius(exakt) \n");
|
||||
|
||||
while(f <= max_f){
|
||||
c_exact = (5.0/9.0) * (f - 32.0);
|
||||
printf("%i \t\t %i \t\t\t %f \n", f, (int)c_exact, c_exact);
|
||||
f+=steps;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/* typengroesse.c
|
||||
*
|
||||
* Bestimmung der Groesse der einfachen typen mit sizeof
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
char c = 127;
|
||||
unsigned char uc = 255;
|
||||
|
||||
printf("\n");
|
||||
printf("char: \t\t%2d\n", (int) sizeof c);
|
||||
printf("short: \t\t%2d\n", (int) sizeof (short));
|
||||
printf("int: \t\t%2d\n", (int) sizeof (int));
|
||||
printf("long: \t\t%2d\n", (int) sizeof (long));
|
||||
printf("long long: \t%2d\n", (int) sizeof (long long));
|
||||
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("_Bool: \t\t%2d\n", (int) sizeof (_Bool));
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void){
|
||||
|
||||
int i = 12;
|
||||
while(i>=0){
|
||||
printf("%i, ",i);
|
||||
i-=2;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
i = -1;
|
||||
while(i>=-13){
|
||||
printf("%i, ", i);
|
||||
i-=2;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
i = 0;
|
||||
do{
|
||||
printf("%i, ", i*i);
|
||||
i++;
|
||||
} while(i<=9);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/** Bindung.java
|
||||
*
|
||||
* Eine Klasse, die am Beispiel des figure-Pakets
|
||||
* statische und dynamische Typen ausprobiert
|
||||
*/
|
||||
|
||||
import figure.*;
|
||||
|
||||
class Bindung {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Figure f = new Square();
|
||||
f.moveTo(2,3);
|
||||
f.area();
|
||||
f.show();
|
||||
|
||||
if (f instanceof Square) {
|
||||
((Square)f).show(); // analog: (double)17
|
||||
}
|
||||
|
||||
System.out.println("Flaecheninhalt: " + f.area());
|
||||
f = new Circle(10);
|
||||
System.out.println("Flaecheninhalt: " + f.area());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Bindung
|
||||
Binary file not shown.
@@ -0,0 +1,26 @@
|
||||
|
||||
|
||||
|
||||
public class Verdeckung{
|
||||
|
||||
void display(int n) {
|
||||
int m = 11;
|
||||
System.out.println(String.format("A1: m = %d, n = %d", m, n));
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Verdeckung v = new Verdeckung();
|
||||
int a = 12;
|
||||
int m = 1;
|
||||
int n = 2;
|
||||
v.display(a);
|
||||
|
||||
if (m == 1) {
|
||||
int x = 22;
|
||||
System.out.println(String.format("A2: m = %d, n = %d", m, x));
|
||||
}
|
||||
System.out.println(String.format("A3: m = %d, n = %d", m, n));
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,22 @@
|
||||
|
||||
import java.util.*; //hashset, linked liste auch hier
|
||||
|
||||
public class Hash{
|
||||
|
||||
|
||||
public static void main(String args[]){
|
||||
Set<String> a = new HashSet<String>();
|
||||
a.add("Aal");
|
||||
a.add("Tanne");
|
||||
a.add("Birke");
|
||||
a.add("Baum");
|
||||
|
||||
Iterator<String> iter = a.iterator();
|
||||
while (iter.hasNext()) {
|
||||
String s = iter.next();
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,23 @@
|
||||
|
||||
|
||||
public class Pair<T1, T2>{
|
||||
T1 first;
|
||||
T2 second;
|
||||
|
||||
|
||||
Pair(T1 first, T2 second){
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
T1 getFirst(){
|
||||
return this.first;
|
||||
}
|
||||
|
||||
|
||||
T2 getSecond(){
|
||||
return this.second;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,36 @@
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class PairComp<T1 extends Number, T2 extends Number> implements Comparable<PairComp<T1,T2>> {
|
||||
T1 first;
|
||||
T2 second;
|
||||
|
||||
|
||||
PairComp(T1 first, T2 second){
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
T1 getFirst(){
|
||||
return this.first;
|
||||
}
|
||||
|
||||
|
||||
T2 getSecond(){
|
||||
return this.second;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(PairComp<T1,T2> other){
|
||||
if (this.getFirst().doubleValue() < other.getFirst().doubleValue()){
|
||||
return -1;
|
||||
}
|
||||
if (this.getFirst().doubleValue() > other.getFirst().doubleValue()){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,16 @@
|
||||
|
||||
|
||||
public class TestPair{
|
||||
|
||||
|
||||
public static void main (String args[]){
|
||||
Pair<Integer, Integer> p = new Pair<Integer, Integer>(6,2);
|
||||
System.out.println(p.getFirst());
|
||||
System.out.println(p.getSecond());
|
||||
|
||||
Pair<Double, Integer> p2 = new Pair<Double, Integer>(6.0,2);
|
||||
System.out.println(p2.getFirst());
|
||||
System.out.println(p2.getSecond());
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
public class TestPairComp{
|
||||
|
||||
|
||||
public static void main (String args[]){
|
||||
PairComp<Integer, Integer> p1 = new PairComp<Integer, Integer>(6,2);
|
||||
System.out.println(p1.getFirst());
|
||||
System.out.println(p1.getSecond());
|
||||
|
||||
PairComp<Integer, Integer> p2 = new PairComp<Integer, Integer>(2,3);
|
||||
System.out.println(p2.getFirst());
|
||||
System.out.println(p2.getSecond());
|
||||
|
||||
System.out.println(p1.compareTo(p2));
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,22 @@
|
||||
|
||||
import java.util.*; //hashset, linked liste auch hier
|
||||
|
||||
public class Tree{
|
||||
|
||||
|
||||
public static void main(String args[]){
|
||||
Set<String> a = new TreeSet<String>();
|
||||
a.add("Aal");
|
||||
a.add("Tanne");
|
||||
a.add("Birke");
|
||||
a.add("Baum");
|
||||
|
||||
Iterator<String> iter = a.iterator();
|
||||
while (iter.hasNext()) {
|
||||
String s = iter.next();
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,18 @@
|
||||
public void insert(ListElement<T> newElem, int i) {
|
||||
if (i == 0)
|
||||
insert(newElem);
|
||||
else {
|
||||
ListElement<T> tmp = head;
|
||||
ListElement<T> prev = null;
|
||||
int l = 0;
|
||||
// Aufsuchen des i-ten Elements
|
||||
while (l < i) {
|
||||
prev = tmp;
|
||||
tmp = prev.next;
|
||||
l++;
|
||||
} // l == i ist erreicht
|
||||
prev.next = newElem;
|
||||
newElem.next = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,88 @@
|
||||
|
||||
|
||||
public class List<T>{
|
||||
private ListElement<T> head;
|
||||
|
||||
public List(ListElement<T> initLe){
|
||||
head = initLe;
|
||||
}
|
||||
|
||||
|
||||
public void insert(ListElement<T> newLe){
|
||||
newLe.setNext(head);
|
||||
this.head = newLe;
|
||||
}
|
||||
|
||||
|
||||
public boolean delete(){
|
||||
if (this.isEmpty()){
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
this.head = head.getNext();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isEmpty(){
|
||||
if (head == null){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int length(){
|
||||
int count = 0;
|
||||
|
||||
ListElement<T> curr = head;
|
||||
while (curr != null){
|
||||
count += 1;
|
||||
curr = curr.getNext();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
public void show(){
|
||||
ListElement<T> curr = head;
|
||||
while (curr != null){
|
||||
if (curr.getNext() == null){
|
||||
System.out.println(""+ curr.getValue());
|
||||
}
|
||||
else {
|
||||
System.out.print(curr.getValue() + ", ");
|
||||
}
|
||||
curr = curr.getNext();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean insertIndex(ListElement<T> newElem, int i) {
|
||||
if (i == 0){
|
||||
insert(newElem);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
ListElement<T> tmp = head;
|
||||
ListElement<T> prev = null;
|
||||
int l = 0;
|
||||
// Aufsuchen des i-ten Elements
|
||||
while (l < i) {
|
||||
prev = tmp;
|
||||
tmp = prev.getNext();
|
||||
l++;
|
||||
if (l > length()){
|
||||
return false;
|
||||
}
|
||||
} // l == i ist erreicht
|
||||
prev.setNext(newElem);
|
||||
newElem.setNext(tmp);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,38 @@
|
||||
|
||||
/*
|
||||
* class ListElement
|
||||
*
|
||||
*/
|
||||
|
||||
public class ListElement<T>{
|
||||
private T value;
|
||||
private ListElement<T> next;
|
||||
|
||||
ListElement(T initValue){
|
||||
this.value = initValue;
|
||||
this.next = null;
|
||||
}
|
||||
|
||||
ListElement(T initValue, ListElement<T> initNext){
|
||||
this.value = initValue;
|
||||
this.next = initNext;
|
||||
}
|
||||
|
||||
public void setNext(ListElement<T> nextLe){
|
||||
next = nextLe;
|
||||
}
|
||||
|
||||
public ListElement<T> getNext(){
|
||||
return this.next;
|
||||
}
|
||||
|
||||
public void setValue(T newValue){
|
||||
value = newValue;
|
||||
|
||||
}
|
||||
|
||||
public T getValue(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
public class ListIndexOutOfBoundsException extends IndexOutOfBoundsException{
|
||||
|
||||
int faulty_index;
|
||||
|
||||
ListIndexOutOfBoundsException(){
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,54 @@
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class UseList {
|
||||
|
||||
public static void main(String [] args){
|
||||
ListElement<Integer> le1 = new ListElement<Integer>(10, null);
|
||||
List<Integer> list = new List<Integer>(le1);
|
||||
ListElement<Integer> le2 = new ListElement<Integer>(20, null);
|
||||
ListElement<Integer> le3 = new ListElement<Integer>(30, null);
|
||||
ListElement<Integer> le4 = new ListElement<Integer>(40, null);
|
||||
|
||||
list.insert(le2);
|
||||
list.insert(le3);
|
||||
|
||||
list.show();
|
||||
|
||||
//System.out.println("Laenge: " + list.length());
|
||||
//list.delete();
|
||||
//list.show();
|
||||
|
||||
//list.insertIndex(le4, 1);
|
||||
//list.show();
|
||||
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
while(true){
|
||||
try{
|
||||
System.out.print("Gebe eine positive Zahl ein: ");
|
||||
int index = Integer.parseInt(scan.next());
|
||||
list.insertIndex(le4, index);
|
||||
list.show();
|
||||
break;
|
||||
}
|
||||
|
||||
catch(NumberFormatException e){
|
||||
System.err.println("Keine ganze Zahl eingegeben!");
|
||||
continue;
|
||||
}
|
||||
|
||||
catch(InputMismatchException e){
|
||||
//bei parser nutzen
|
||||
System.err.println("Keine ganze Zahl eingegeben!");
|
||||
continue;
|
||||
}
|
||||
|
||||
catch(NullPointerException e){
|
||||
System.err.println("index blöd sein");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
public void insert(ListElement<T> newElem, int i) {
|
||||
if (i == 0)
|
||||
insert(newElem);
|
||||
else {
|
||||
ListElement<T> tmp = head;
|
||||
ListElement<T> prev = null;
|
||||
int l = 0;
|
||||
// Aufsuchen des i-ten Elements
|
||||
while (l < i) {
|
||||
prev = tmp;
|
||||
tmp = prev.next;
|
||||
l++;
|
||||
} // l == i ist erreicht
|
||||
prev.next = newElem;
|
||||
newElem.next = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,12 @@
|
||||
#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.
@@ -0,0 +1,30 @@
|
||||
#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.
@@ -0,0 +1,15 @@
|
||||
#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.
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void){
|
||||
int i;
|
||||
int *ptr = NULL;
|
||||
ptr = &i;
|
||||
i = 1;
|
||||
printf("Pointer Wert: \t\t\t %i\n", *ptr);
|
||||
printf("Pointer Adresse: \t\t %p\n", ptr);
|
||||
printf("Variablen Wert: \t\t %i\n", i);
|
||||
*ptr = 2;
|
||||
printf("Veraenderten Variablen Wert: \t %i\n", i);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int y;
|
||||
|
||||
int func(int a, int b){
|
||||
if (b == 0){
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
return (2 * (a * a)) / b;
|
||||
}
|
||||
}
|
||||
|
||||
int main (void){
|
||||
int x = 4;
|
||||
printf("Geben Sie eine ganze Zahl ein: ");
|
||||
scanf("%i", &y);
|
||||
|
||||
int result = func(x,y);
|
||||
if(result == -1){
|
||||
printf("Ein Fehler ist aufgetreten.\n");
|
||||
} else {
|
||||
printf("Berechneter Wert: %i\n", result);
|
||||
}
|
||||
|
||||
for(int i = 0; i <= 30; i++){
|
||||
y = i;
|
||||
result = func(x,y);
|
||||
if(result == -1){
|
||||
printf("Ein Fehler ist aufgetreten.\n");
|
||||
} else {
|
||||
printf("x: %i, y: %i, z: %i\n", x,y, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void swap(int *a, int *b){
|
||||
int temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
int x = 1;
|
||||
int y = 2;
|
||||
printf("x: %i, y: %i\n",x,y);
|
||||
swap(&x, &y);
|
||||
printf("x: %i, y: %i\n",x,y);
|
||||
}
|
||||
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,47 @@
|
||||
/* array_1.c
|
||||
*
|
||||
* Adressierung von Array-Elementen
|
||||
* Indizes vs. Pointer
|
||||
*/
|
||||
|
||||
// ACHTUNG: Datei enthaelt zu beseitigende Fehler!!!
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int i;
|
||||
int ar[4];
|
||||
ar[0] = 10;
|
||||
ar[1] = 20;
|
||||
ar[2] = 30;
|
||||
ar[3] = 40;
|
||||
|
||||
printf("\nar: %p \t &ar[0]: %p\n", ar, &ar[0]);
|
||||
|
||||
// i) Array-Syntax
|
||||
printf("\n\nElemente von ar, indiziert:\n");
|
||||
for (int i = 0; i < sizeof(ar)/sizeof(int); i++){
|
||||
printf("%d\t\n", ar[i]);
|
||||
}
|
||||
|
||||
printf("\n\nAdressen:\n");
|
||||
for (int i = 0; i < sizeof(ar)/sizeof(int); i++){
|
||||
printf("%p\t\n", &ar[i]);
|
||||
}
|
||||
|
||||
// ii) Pointer-Syntax
|
||||
printf("\n\nElemente von ar, referenziert:\n");
|
||||
for (int i = 0; i < sizeof(ar)/sizeof(int); i++){
|
||||
printf("%d\t\n", *(ar + i));
|
||||
}
|
||||
|
||||
printf("\n\nAdressen:\n");
|
||||
for (int i = 0; i < sizeof(ar)/sizeof(int); i++){
|
||||
printf("%p\t", ar+i);
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/* array_1.c
|
||||
*
|
||||
* Adressierung von Array-Elementen
|
||||
* Indizes vs. Pointer
|
||||
*/
|
||||
|
||||
// ACHTUNG: Datei enthaelt zu beseitigende Fehler!!!
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
int i;
|
||||
int ar[4];
|
||||
ar[0] = 10;
|
||||
ar[1] = 20;
|
||||
ar[2] = 30;
|
||||
|
||||
printf("\nar: %p \t &ar[0]: %p\n", ar, &ar[0]);
|
||||
|
||||
// i) Array-Syntax
|
||||
printf("\n\nElemente von ar, indiziert:\n");
|
||||
for (int i = 0; i < (sizeof(ar)/sizeof(int)) + 2; i++){
|
||||
printf("%d\t\n", ar[i]);
|
||||
}
|
||||
|
||||
printf("\n\nAdressen:\n");
|
||||
for (int i = 0; i < (sizeof(ar)/sizeof(int)) + 2; i++){
|
||||
printf("%p\t\n", &ar[i]);
|
||||
}
|
||||
|
||||
// ii) Pointer-Syntax
|
||||
printf("\n\nElemente von ar, referenziert:\n");
|
||||
for (int i = 0; i < (sizeof(ar)/sizeof(int)) + 2; i++){
|
||||
printf("%d\t\n", *(ar + i));
|
||||
}
|
||||
|
||||
printf("\n\nAdressen:\n");
|
||||
for (int i = 0; i < (sizeof(ar)/sizeof(int)) + 2; i++){
|
||||
printf("%p\t", ar+i);
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,49 @@
|
||||
/* 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.
@@ -0,0 +1,49 @@
|
||||
/* 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.
@@ -0,0 +1,12 @@
|
||||
#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.
@@ -0,0 +1,20 @@
|
||||
#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.
@@ -0,0 +1,30 @@
|
||||
#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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#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.
@@ -0,0 +1,240 @@
|
||||
SCANF(3) Library Functions Manual SCANF(3)
|
||||
|
||||
NAME
|
||||
fscanf, scanf, sscanf, vfscanf, vscanf, vsscanf – input format conversion
|
||||
|
||||
LIBRARY
|
||||
Standard C Library (libc, -lc)
|
||||
|
||||
SYNOPSIS
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
fscanf(FILE *restrict stream, const char *restrict format, ...);
|
||||
|
||||
int
|
||||
scanf(const char *restrict format, ...);
|
||||
|
||||
int
|
||||
sscanf(const char *restrict s, const char *restrict format, ...);
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
vfscanf(FILE *restrict stream, const char *restrict format, va_list arg);
|
||||
|
||||
int
|
||||
vscanf(const char *restrict format, va_list arg);
|
||||
|
||||
int
|
||||
vsscanf(const char *restrict s, const char *restrict format,
|
||||
va_list arg);
|
||||
|
||||
DESCRIPTION
|
||||
The scanf() family of functions scans input according to a format, as
|
||||
described below. This format may contain conversion specifiers; the
|
||||
results from such conversions, if any, are stored through the pointer
|
||||
arguments. The scanf() function reads input from the standard input
|
||||
stream stdin, fscanf() reads input from the stream pointer stream, and
|
||||
sscanf() reads its input from the character string pointed to by s.
|
||||
|
||||
The vfscanf() function is analogous to vfprintf(3) and reads input from
|
||||
the stream pointer stream using a variable argument list of pointers (see
|
||||
stdarg(3)). The vscanf() function scans a variable argument list from
|
||||
the standard input and the vsscanf() function scans it from a string;
|
||||
these are analogous to the vprintf() and vsprintf() functions,
|
||||
respectively.
|
||||
|
||||
Each successive pointer argument must correspond properly with each
|
||||
successive conversion specifier (but see the * conversion below). All
|
||||
conversions are introduced by the % (percent sign) character. The format
|
||||
string may also contain other characters. White space (such as blanks,
|
||||
tabs, or newlines) in the format string match any amount of white space,
|
||||
including none, in the input. Everything else matches only itself.
|
||||
Scanning stops when an input character does not match such a format
|
||||
character. Scanning also stops when an input conversion cannot be made
|
||||
(see below).
|
||||
|
||||
Extended locale versions of these functions are documented in scanf_l(3).
|
||||
See xlocale(3) for more information.
|
||||
|
||||
CONVERSIONS
|
||||
Following the % character introducing a conversion, there may be a number
|
||||
of flag characters, as follows:
|
||||
|
||||
* Suppresses assignment. The conversion that follows
|
||||
occurs as usual, but no pointer is used; the result of
|
||||
the conversion is simply discarded.
|
||||
|
||||
hh Indicates that the conversion will be one of dioux or n
|
||||
and the next pointer is a pointer to a char (rather than
|
||||
int).
|
||||
|
||||
h Indicates that the conversion will be one of dioux or n
|
||||
and the next pointer is a pointer to a short int (rather
|
||||
than int).
|
||||
|
||||
l (ell) Indicates that the conversion will be one of dioux or n
|
||||
and the next pointer is a pointer to a long int (rather
|
||||
than int), that the conversion will be one of a, e, f, or
|
||||
g and the next pointer is a pointer to double (rather
|
||||
than float), or that the conversion will be one of c, s
|
||||
or [ and the next pointer is a pointer to an array of
|
||||
wchar_t (rather than char).
|
||||
|
||||
ll (ell ell) Indicates that the conversion will be one of dioux or n
|
||||
and the next pointer is a pointer to a long long int
|
||||
(rather than int).
|
||||
|
||||
L Indicates that the conversion will be one of a, e, f, or
|
||||
g and the next pointer is a pointer to long double.
|
||||
|
||||
j Indicates that the conversion will be one of dioux or n
|
||||
and the next pointer is a pointer to a intmax_t (rather
|
||||
than int).
|
||||
|
||||
t Indicates that the conversion will be one of dioux or n
|
||||
and the next pointer is a pointer to a ptrdiff_t (rather
|
||||
than int).
|
||||
|
||||
z Indicates that the conversion will be one of dioux or n
|
||||
and the next pointer is a pointer to a size_t (rather
|
||||
than int).
|
||||
|
||||
q (deprecated.) Indicates that the conversion will be one
|
||||
of dioux or n and the next pointer is a pointer to a long
|
||||
long int (rather than int).
|
||||
|
||||
In addition to these flags, there may be an optional maximum field width,
|
||||
expressed as a decimal integer, between the % and the conversion. If no
|
||||
width is given, a default of “infinity” is used (with one exception,
|
||||
below); otherwise at most this many bytes are scanned in processing the
|
||||
conversion. In the case of the lc, ls and l[ conversions, the field
|
||||
width specifies the maximum number of multibyte characters that will be
|
||||
scanned. Before conversion begins, most conversions skip white space;
|
||||
this white space is not counted against the field width.
|
||||
|
||||
The following conversions are available:
|
||||
|
||||
% Matches a literal ‘%’. That is, “%%” in the format string matches
|
||||
a single input ‘%’ character. No conversion is done, and
|
||||
assignment does not occur.
|
||||
|
||||
d Matches an optionally signed decimal integer; the next pointer must
|
||||
be a pointer to int.
|
||||
|
||||
i Matches an optionally signed integer; the next pointer must be a
|
||||
pointer to int. The integer is read in base 16 if it begins with
|
||||
‘0x’ or ‘0X’, in base 8 if it begins with ‘0’, and in base 10
|
||||
otherwise. Only characters that correspond to the base are used.
|
||||
|
||||
o Matches an octal integer; the next pointer must be a pointer to
|
||||
unsigned int.
|
||||
|
||||
u Matches an optionally signed decimal integer; the next pointer must
|
||||
be a pointer to unsigned int.
|
||||
|
||||
x, X Matches an optionally signed hexadecimal integer; the next pointer
|
||||
must be a pointer to unsigned int.
|
||||
|
||||
a, A, e, E, f, F, g, G
|
||||
Matches a floating-point number in the style of strtod(3). The
|
||||
next pointer must be a pointer to float (unless l or L is
|
||||
specified.)
|
||||
|
||||
s Matches a sequence of non-white-space characters; the next pointer
|
||||
must be a pointer to char, and the array must be large enough to
|
||||
accept all the sequence and the terminating NUL character. The
|
||||
input string stops at white space or at the maximum field width,
|
||||
whichever occurs first.
|
||||
|
||||
If an l qualifier is present, the next pointer must be a pointer to
|
||||
wchar_t, into which the input will be placed after conversion by
|
||||
mbrtowc(3).
|
||||
|
||||
S The same as ls.
|
||||
|
||||
c Matches a sequence of width count characters (default 1); the next
|
||||
pointer must be a pointer to char, and there must be enough room
|
||||
for all the characters (no terminating NUL is added). The usual
|
||||
skip of leading white space is suppressed. To skip white space
|
||||
first, use an explicit space in the format.
|
||||
|
||||
If an l qualifier is present, the next pointer must be a pointer to
|
||||
wchar_t, into which the input will be placed after conversion by
|
||||
mbrtowc(3).
|
||||
|
||||
C The same as lc.
|
||||
|
||||
[ Matches a nonempty sequence of characters from the specified set of
|
||||
accepted characters; the next pointer must be a pointer to char,
|
||||
and there must be enough room for all the characters in the string,
|
||||
plus a terminating NUL character. The usual skip of leading white
|
||||
space is suppressed. The string is to be made up of characters in
|
||||
(or not in) a particular set; the set is defined by the characters
|
||||
between the open bracket [ character and a close bracket ]
|
||||
character. The set excludes those characters if the first
|
||||
character after the open bracket is a circumflex ^. To include a
|
||||
close bracket in the set, make it the first character after the
|
||||
open bracket or the circumflex; any other position will end the
|
||||
set. The hyphen character - is also special; when placed between
|
||||
two other characters, it adds all intervening characters to the
|
||||
set. To include a hyphen, make it the last character before the
|
||||
final close bracket. For instance, ‘[^]0-9-]’ means the set
|
||||
“everything except close bracket, zero through nine, and hyphen”.
|
||||
The string ends with the appearance of a character not in the (or,
|
||||
with a circumflex, in) set or when the field width runs out.
|
||||
|
||||
If an l qualifier is present, the next pointer must be a pointer to
|
||||
wchar_t, into which the input will be placed after conversion by
|
||||
mbrtowc(3).
|
||||
|
||||
p Matches a pointer value (as printed by ‘%p’ in printf(3)); the next
|
||||
pointer must be a pointer to void * (or other pointer type).
|
||||
|
||||
n Nothing is expected; instead, the number of characters consumed
|
||||
thus far from the input is stored through the next pointer, which
|
||||
must be a pointer to int. This is not a conversion, although it
|
||||
can be suppressed with the * flag.
|
||||
|
||||
The decimal point character is defined in the program's locale (category
|
||||
LC_NUMERIC).
|
||||
|
||||
For backwards compatibility, a “conversion” of ‘%\0’ causes an immediate
|
||||
return of EOF.
|
||||
|
||||
RETURN VALUES
|
||||
These functions return the number of input items assigned. This can be
|
||||
fewer than provided for, or even zero, in the event of a matching
|
||||
failure. Zero indicates that, although there was input available, no
|
||||
conversions were assigned; typically this is due to an invalid input
|
||||
character, such as an alphabetic character for a ‘%d’ conversion. The
|
||||
value EOF is returned if an input failure occurs before any conversion
|
||||
such as an end-of-file occurs. If an error or end-of-file occurs after
|
||||
conversion has begun, the number of conversions which were successfully
|
||||
completed is returned.
|
||||
|
||||
SEE ALSO
|
||||
getc(3), mbrtowc(3), printf(3), scanf_l(3), strtod(3), strtol(3),
|
||||
strtoul(3), wscanf(3)
|
||||
|
||||
STANDARDS
|
||||
The functions fscanf(), scanf(), sscanf(), vfscanf(), vscanf(), and
|
||||
vsscanf() conform to ISO/IEC 9899:1999 (“ISO C99”).
|
||||
|
||||
BUGS
|
||||
Earlier implementations of fscanf treated %D, %E, %F, %O and %X as their
|
||||
lowercase equivalents with an l modifier. In addition, fscanf treated an
|
||||
unknown conversion character as %d or %D, depending on its case. This
|
||||
functionality has been removed.
|
||||
|
||||
Numerical strings are truncated to 512 characters; for example, %f and %d
|
||||
are implicitly %512f and %512d.
|
||||
|
||||
The %n$ modifiers for positional arguments are not implemented.
|
||||
|
||||
The fscanf family of functions do not correctly handle multibyte
|
||||
characters in the format argument.
|
||||
|
||||
macOS 15.0 January 4, 2003 macOS 15.0
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define NL printf("\n");
|
||||
#define LEN 10
|
||||
|
||||
int main(void){
|
||||
int ar[LEN];
|
||||
|
||||
for(int i = 0; i < sizeof(ar)/sizeof(int); i++){
|
||||
ar[i] = (i+1) * (i+1);
|
||||
printf("%i ", ar[i]);
|
||||
}
|
||||
NL
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define NL printf("\n");
|
||||
#define SIZE 10
|
||||
|
||||
void rev_print(int *ar, int size){
|
||||
for(int i = 0; i < size; i++){
|
||||
printf("%i ",ar[size - 1 - i]);
|
||||
}
|
||||
NL
|
||||
}
|
||||
|
||||
int main(void){
|
||||
int ar[SIZE];
|
||||
|
||||
for(int i = 0; i < SIZE; i++){
|
||||
ar[i] = (i+1) * (i+1);
|
||||
printf("%i ", ar[i]);
|
||||
}
|
||||
NL
|
||||
rev_print(ar, SIZE);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define NL printf("\n");
|
||||
#define LEN 1000
|
||||
|
||||
int main(void){
|
||||
int ar[LEN];
|
||||
for(int i = 0; i < LEN; i++){
|
||||
ar[i] = (i + 1) * (i + 1);
|
||||
}
|
||||
|
||||
while(1){
|
||||
printf("Geben Sie eine ganze Zahl zwischen 1 und %i ein: ", LEN);
|
||||
int input;
|
||||
scanf("%i", &input);
|
||||
|
||||
|
||||
if(input == 0){
|
||||
break;
|
||||
}
|
||||
else if(input < 1 || input > LEN) {
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("Quadrat von %i: %i", input, ar[input - 1]);
|
||||
|
||||
NL
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define NL printf("\n");
|
||||
|
||||
int main(void){
|
||||
|
||||
int ar[3][4] = {
|
||||
{0, 1, 2, 3},
|
||||
{10, 11, 12}
|
||||
};
|
||||
|
||||
for(int i = 0; i < sizeof(ar)/sizeof(ar[0]);i++){
|
||||
for(int j = 0; j < sizeof(ar[0])/sizeof(int);j++){
|
||||
printf("%i\t", ar[i][j]);
|
||||
}
|
||||
NL
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
/* 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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/* fehler.c
|
||||
*
|
||||
* Ein Programm, dass Compilerfehler,
|
||||
* Laufzeitfehler und logische Fehler demonstriert.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
|
||||
int num1, num2;
|
||||
num1 = 0;
|
||||
nun2 = 1;
|
||||
|
||||
printf("Der Quotient der Variablen ist: ")
|
||||
printf("%d\n", num1/num2);
|
||||
printf(\n); // Leerzeile
|
||||
|
||||
printf("Jetzt werden die Variablenwerte vertauscht.\n");
|
||||
|
||||
// Dies muss korrigiert werden. (logischer Fehler!!!)
|
||||
num1 = num2;
|
||||
num2 = num1;
|
||||
|
||||
printf("Der Quotient der Vaiablen ist nun: ");
|
||||
printf("%d\n", num1/num2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
#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;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/* 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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/* 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.
@@ -0,0 +1,31 @@
|
||||
/* 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;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#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.
Binary file not shown.
@@ -0,0 +1,21 @@
|
||||
|
||||
#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.
@@ -0,0 +1,37 @@
|
||||
/* 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.
@@ -0,0 +1,43 @@
|
||||
/* 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.
@@ -0,0 +1,47 @@
|
||||
/* 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;
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
#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.
@@ -0,0 +1,31 @@
|
||||
// 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;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user