2.2 Struct - Repräsentation von Daten
Contents
2.2 Struct - Repräsentation von Daten#
Eine Studierenden-Datenbank#
In diesem Beispiel wird ein Struct Student
erstellt, das drei Felder enthält:
name
für den Namen des Studierenden,age
für das Alter des Studierenden undstudentID
für dieID
des Studierenden.
Es werden zwei Studierenden-Instanzen erstellt und ihre Felder initialisiert. Dann werden die Informationen der Studierenden mit Hilfe von std::cout
auf der Konsole ausgegeben.
Als Hilfe: Structs.
Click to show
#include <iostream>
#include <string>
// Definition des Studenten-Structs
struct Student
{
std::string name; // Name des Studenten
int age; // Alter des Studenten
int studentID; // ID des Studenten
};
int main()
{
// Erstellung von Studenten-Instanzen
Student student1; // Erstellung einer Studenten-Instanz mit Namen student1
Student student2; // Erstellung einer weiteren Studenten-Instanz mit Namen student2
// Initialisierung der Felder der Studenten-Instanzen
student1.name = "Alice";
student1.age = 21;
student1.studentID = 12345;
student2.name = "Bob";
student2.age = 20;
student2.studentID = 67890;
// Ausgabe der Informationen der Studenten
std::cout << "Name: " << student1.name << std::endl;
std::cout << "Alter: " << student1.age << " Jahre" << std::endl;
std::cout << "ID: " << student1.studentID << std::endl;
std::cout << "Name: " << student2.name << std::endl;
std::cout << "Alter: " << student2.age << " Jahre" << std::endl;
std::cout << "ID: " << student2.studentID << std::endl;
return 0;
}
main();
Aufgabe A: Erweitern um Feld für Geburtstag#
Click to show
#include <iostream>
#include <string>
// Define a struct to represent a date
struct Date
{
int day;
int month;
int year;
};
// Define a struct to represent a student
struct Student
{
std::string name;
int age;
int studentID;
Date birthday; // Added birthday field of type Date
};
int main()
{
// Create an instance of the Student struct
Student student1;
// Initialize the fields of the student struct
student1.name = "Alice";
student1.age = 20;
student1.studentID = 12345;
student1.birthday.day = 15; // Set day
student1.birthday.month = 6; // Set month
student1.birthday.year = 2000; // Set year
// Access and print the fields of the student struct
std::cout << "Name: " << student1.name << std::endl;
std::cout << "Age: " << student1.age << std::endl;
std::cout << "Student ID: " << student1.studentID << std::endl;
std::cout << "Birthday: " << student1.birthday.day << "/" << student1.birthday.month << "/" << student1.birthday.year << std::endl;
// Modify the fields of the student struct
student1.name = "Alicia";
student1.age = 21;
student1.birthday.year = 2001; // Modify year
// Access and print the modified fields of the student struct
std::cout << "Modified Name: " << student1.name << std::endl;
std::cout << "Modified Age: " << student1.age << std::endl;
std::cout << "Modified Birthday: " << student1.birthday.day << "/" << student1.birthday.month << "/" << student1.birthday.year << std::endl;
return 0;
}
main();
Aufgabe B: Funktion zum errechnen des Alters#
Anstatt eines expliziten Feldes für das Alter, wollen wir dies nun ausrechnen.
Entfernen sie das Feld
age
undführen sie eine Funktion ein, die das Alter des Studierenden errechnet.
Hierzu benötigen sie Funktionen, die auf das aktuelle Datum zugreifen - aus dem std::chrono
:
std::chrono::system_clock::now(): Diese Funktion ruft das aktuelle Datum und die aktuelle Uhrzeit von der Systemuhr ab.
std::chrono::duration: Diese Klasse repräsentiert eine Dauer, wie zum Beispiel die Differenz zwischen zwei Datumsangaben.
std::chrono::time_point: Diese Klasse repräsentiert einen Zeitpunkt, wie zum Beispiel das aktuelle Datum und die aktuelle Uhrzeit, die mit std::chrono::system_clock::now() erhalten werden.
std::chrono::year_month_day: Diese Klasse repräsentiert ein Datum mit den Komponenten Jahr, Monat und Tag.
std::chrono::year: Diese Klasse repräsentiert ein Jahr.
Zur Erinnerung – über ?std::chrono
erhalten sie den entsprechenden Referenzeintrag.
Click to show
#include <iostream>
#include <string>
#include <chrono>
// Define a struct to represent a date
struct Date
{
int day;
int month;
int year;
};
// Define a struct to represent a student
struct Student
{
std::string name;
Date birthday; // Changed age field to birthday field of type Date
int studentID;
};
Click to show
// Function to calculate age (in years) from birthday and current date
int calculateAge(const Date& birthday)
{
// Get the current date and time from the system clock
auto currentTime = std::chrono::system_clock::now();
// Convert the current time to time_t to extract year, month, and day
std::time_t currentTime_t = std::chrono::system_clock::to_time_t(currentTime);
std::tm* currentTM = std::localtime(¤tTime_t);
// Extract the current year, month, and day
int currentYear = currentTM->tm_year + 1900; // Add 1900 to get actual year
int currentMonth = currentTM->tm_mon + 1; // Add 1 to get actual month (0-based index)
int currentDay = currentTM->tm_mday;
// Calculate the age (in years) by subtracting the birth year from the current year
int age = currentYear - birthday.year;
// Subtract 1 from the age if the birth month and day are later than the current month and day
if (currentMonth < birthday.month ||
(currentMonth == birthday.month && currentDay < birthday.day))
{
age--;
}
return age;
}
int main()
{
// Create an instance of the Student struct
Student student1;
// Initialize the fields of the student struct
student1.name = "Alice";
student1.birthday.day = 15;
student1.birthday.month = 6;
student1.birthday.year = 2000;
student1.studentID = 12345;
// Calculate and print the age of the student
int age = calculateAge(student1.birthday);
std::cout << "Name: " << student1.name << std::endl;
std::cout << "Birthday: " << student1.birthday.day << "/" << student1.birthday.month << "/" << student1.birthday.year << std::endl;
std::cout << "Age: " << age << " years" << std::endl;
return 0;
}
main()