move exercises and lectures into subfolders
This commit is contained in:
18
Exercises/C19/DefineExamples/define_example.c
Normal file
18
Exercises/C19/DefineExamples/define_example.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#define UP 1
|
||||
#define DOWN 2
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 1;
|
||||
|
||||
if (i == UP )
|
||||
{
|
||||
// Do something
|
||||
}
|
||||
|
||||
if ( i == DOWN)
|
||||
{
|
||||
// Doe something else
|
||||
}
|
||||
return 0;
|
||||
}
|
22
Exercises/C19/enum_example.c
Normal file
22
Exercises/C19/enum_example.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum DOW { sun, mon, tue, wed, thu, fri, sat } ;
|
||||
|
||||
// Main () - execution starts here
|
||||
int main (void)
|
||||
{
|
||||
enum DOW day;
|
||||
|
||||
/* Code that get a value for 'day' */
|
||||
day = tue;
|
||||
|
||||
switch (day)
|
||||
{
|
||||
case sun : printf ("Sunday\n") ; break ;
|
||||
case mon : printf ("Monday\n") ; break ;
|
||||
case tue : printf ("Tuesday\n") ; break ;
|
||||
/* etc. */
|
||||
}
|
||||
return (0); // Exit indicating success
|
||||
}
|
20
Exercises/C19/ex1.c
Normal file
20
Exercises/C19/ex1.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
struct Person {
|
||||
int age;
|
||||
char forename[30];
|
||||
char surname[50];
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
struct Person p;
|
||||
|
||||
printf("Enter age: "); scanf("%d", &p.age);
|
||||
printf("Enter forename: "); scanf("%29s", p.forename);
|
||||
printf("Enter surname: "); scanf("%49s", p.surname);
|
||||
|
||||
printf("p.age=%d p.forename=%s p.surname=%s\n", p.age, p.forename, p.surname);
|
||||
return 0;
|
||||
}
|
25
Exercises/C19/ex2.c
Normal file
25
Exercises/C19/ex2.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
struct Person {
|
||||
int age;
|
||||
char forename[30];
|
||||
char surname[50];
|
||||
};
|
||||
|
||||
|
||||
void print_person(struct Person p) {
|
||||
printf("p.age=%d p.forename=%s p.surname=%s\n", p.age, p.forename, p.surname);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
struct Person p;
|
||||
|
||||
printf("Enter age: "); scanf("%d", &p.age);
|
||||
printf("Enter forename: "); scanf("%29s", p.forename);
|
||||
printf("Enter surname: "); scanf("%49s", p.surname);
|
||||
|
||||
print_person(p);
|
||||
return 0;
|
||||
}
|
27
Exercises/C19/ex3.c
Normal file
27
Exercises/C19/ex3.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
struct Person {
|
||||
int age;
|
||||
int year_of_birth;
|
||||
char forename[30];
|
||||
char surname[50];
|
||||
};
|
||||
|
||||
|
||||
void print_person(struct Person p) {
|
||||
printf("p.age=%d p.year_of_birth=%d p.forename=%s p.surname=%s\n", p.age, p.year_of_birth, p.forename, p.surname);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
struct Person p;
|
||||
|
||||
printf("Enter age: "); scanf("%d", &p.age);
|
||||
printf("Enter birth year: "); scanf("%d", &p.year_of_birth);
|
||||
printf("Enter forename: "); scanf("%29s", p.forename);
|
||||
printf("Enter surname: "); scanf("%49s", p.surname);
|
||||
|
||||
print_person(p);
|
||||
return 0;
|
||||
}
|
50
Exercises/C19/ex4.c
Normal file
50
Exercises/C19/ex4.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
struct Person {
|
||||
int age;
|
||||
int year_of_birth;
|
||||
char forename[30];
|
||||
char surname[50];
|
||||
};
|
||||
|
||||
|
||||
void print_person(struct Person p) {
|
||||
printf("p.age=%d p.year_of_birth=%d p.forename=%s p.surname=%s\n", p.age, p.year_of_birth, p.forename, p.surname);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
struct Person p;
|
||||
FILE *fp;
|
||||
char choice;
|
||||
char filename[4097];
|
||||
|
||||
printf("Read or write a file? (r/w) "); scanf("%c", &choice);
|
||||
|
||||
if (choice == 'w') {
|
||||
printf("Enter age: "); scanf("%d", &p.age);
|
||||
printf("Enter birth year: "); scanf("%d", &p.year_of_birth);
|
||||
printf("Enter forename: "); scanf("%29s", p.forename);
|
||||
printf("Enter surname: "); scanf("%49s", p.surname);
|
||||
|
||||
printf("Enter filename to save to: "); scanf("%4096s", filename);
|
||||
fp = fopen(filename, "wb");
|
||||
if (fp == NULL) {
|
||||
printf("Failed to open file for writing\n");
|
||||
return 1;
|
||||
}
|
||||
fwrite(&p, 1, sizeof(p), fp);
|
||||
} else {
|
||||
printf("Enter file to read from: "); scanf("%4096s", filename);
|
||||
fp = fopen(filename, "rb");
|
||||
if (fp == NULL) {
|
||||
printf("Failed to open file for reading\n");
|
||||
return 1;
|
||||
}
|
||||
fread(&p, 1, sizeof(p), fp);
|
||||
}
|
||||
|
||||
print_person(p);
|
||||
return 0;
|
||||
}
|
26
Exercises/C19/static_variable_example.c
Normal file
26
Exercises/C19/static_variable_example.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void DisplayHelloWorld (void)
|
||||
{
|
||||
static int k = 0; // Counter for how many times the function is called
|
||||
|
||||
printf ("Hello World\n");
|
||||
|
||||
// Increment counter and display value
|
||||
k = k + 1;
|
||||
printf ("I have now said this %d times\n",k);
|
||||
}
|
||||
// Main () - execution starts here
|
||||
int main (void)
|
||||
{
|
||||
int i;
|
||||
|
||||
// Loop calling out function 10 times
|
||||
for ( i =0 ; i < 10 ; i++ )
|
||||
{
|
||||
DisplayHelloWorld();
|
||||
}
|
||||
|
||||
return (0); // Exit indicating success
|
||||
}
|
Reference in New Issue
Block a user