Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Aufgabe 5-1------------------------------------------
- #include <iostream>
- using namespace std;
- void arrPrint(bool in[],int length){
- int i=0;
- while(i<length){
- cout << i+1 << ": "<< in[i] << endl;
- i++;
- }
- }
- int main(){
- int n=15;
- cout << "Gebe Testzahl ein, natürliche Zahl größer 0:";cin >> n;
- int i=0;
- int j=0;
- bool boolprime[n];
- while(i<n){
- boolprime[i]=true;
- i++;
- }
- boolprime[0]=false;
- i=0;
- while((i+1)*(i+1)<=n){//falls eine Zahl größer als die Wurzel von n n teilt, teilt bereits eine kleinere ebenfalls n. Weitere Rechnungen sind unnötig, auch alle nicht Primzahlen vor n wurden bereits getroffen
- if(boolprime[i]){
- j=2;
- while(j*(i+1)<=n){
- boolprime[j*(i+1)-1]=false;
- j++;
- }
- }
- i++;
- }
- arrPrint(boolprime,n);
- }
- Aufgabe 5-2-----------------------------------------------------------
- #include <iostream>
- using namespace std;
- struct Person{
- string name;
- string surname;
- int birthday;
- int birthmonth;
- int birthyear;
- };
- Person erstellePerson(string cname,string sname,int d, int m, int y){//Man darf ja keine Konstruktoren verwenden ~sigh
- Person out;
- out.name=cname;
- out.surname=sname;
- out.birthday=d;
- out.birthmonth=m;
- out.birthyear=y;
- return out;
- }
- void printPersonArray(int length, Person arr[]){
- cout << "\nName\t\t: ";//if you destroy the formatting with ridiculous names it's not my problem
- for(int i=0; i< length; i++){
- cout << arr[i].name << "\t";
- }
- cout << "\nSurname\t\t: ";
- for(int i=0; i< length; i++){
- cout << arr[i].surname << "\t";
- }
- cout << "\nBirthday\t: ";
- for(int i=0; i< length; i++){
- cout << arr[i].birthday << "\t";
- }
- cout << "\nBirthmonth\t: ";
- for(int i=0; i< length; i++){
- cout << arr[i].birthmonth << "\t";
- }
- cout << "\nBirthyear\t: ";
- for(int i=0; i< length; i++){
- cout << arr[i].birthyear << "\t";
- }
- }
- bool interval(int x, int lowerbound, int upperbound){
- if(x< lowerbound) return false;
- if(x > upperbound) return false;
- return true;
- }
- bool inSet(int x, int set[], int setSize){
- for(int i=0;i<setSize;i++){
- if(x==set[i]) return true;
- }
- return false;
- }
- bool check(int d, int m, int y){
- if(!interval(d,1,31)) return false;
- if(!interval(m,1,12)) return false;
- if(m==2){//Ein Herz für Sonderfaelle
- if(interval(d,1,28)) return true;
- if(y%4 != 0) return false; //d=29, kein Schaltjahr -> false
- if(y%400 == 0) return true; // 400 overrules all other rules, year ist Schaltjahr
- if(y%100 == 0) return false; // 100 overrules Schaltjahr
- return true; // Schaltjahr
- }
- int monthsw31days[]={1,3,5,7,8,10,12};
- if(inSet(m,monthsw31days,7)){
- if(interval(d,1,31)) return true;
- return false;
- }
- if(interval(d,1,30)) return true;
- return false;//Hier sollte nie angekommen werden
- }
- int main(){
- int k=3;
- Person group[k];
- int i=0;
- int d;
- int m;
- int y;
- string n,s;
- while(i<k){
- cout << "Name:\t\t";
- cin >> n;
- cout << "Surname:\t";
- cin >> s;
- cout << "Day of birth:\t";
- cin >> d;
- cout << "Month of birth\t";
- cin >> m;
- cout << "Year of birth\t";
- cin >> y;
- if(!check(d,m,y)) cout << "Non-Valid date set, try again:"<<endl;
- else{
- group[i]=erstellePerson(n,s,d,m,y);
- i++;
- }
- }
- printPersonArray(k,group);
- return 42;
- }
- Aufgabe 5-3---------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement