Advertisement
Randomsurpriseguy

Untitled

Jan 14th, 2021
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. Aufgabe 5-1------------------------------------------
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. void arrPrint(bool in[],int length){
  7.   int i=0;
  8.   while(i<length){
  9.     cout << i+1 << ": "<< in[i] << endl;
  10.     i++;
  11.  
  12.   }
  13. }
  14.  
  15. int main(){
  16.   int n=15;
  17.   cout << "Gebe Testzahl ein, natürliche Zahl größer 0:";cin >> n;
  18.  
  19.   int i=0;
  20.   int j=0;
  21.   bool boolprime[n];
  22.   while(i<n){
  23.     boolprime[i]=true;
  24.     i++;
  25.   }
  26.   boolprime[0]=false;
  27.   i=0;
  28.   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
  29.     if(boolprime[i]){
  30.       j=2;
  31.       while(j*(i+1)<=n){
  32.         boolprime[j*(i+1)-1]=false;
  33.         j++;
  34.       }
  35.  
  36.     }
  37.     i++;
  38.   }
  39.  
  40.   arrPrint(boolprime,n);
  41.  
  42.  
  43. }
  44.  
  45. Aufgabe 5-2-----------------------------------------------------------
  46.  
  47. #include <iostream>
  48. using namespace std;
  49.  
  50. struct Person{
  51.   string name;
  52.   string surname;
  53.   int birthday;
  54.   int birthmonth;
  55.   int birthyear;
  56. };
  57.  
  58. Person erstellePerson(string cname,string sname,int d, int m, int y){//Man darf ja keine Konstruktoren verwenden ~sigh
  59.   Person out;
  60.   out.name=cname;
  61.   out.surname=sname;
  62.   out.birthday=d;
  63.   out.birthmonth=m;
  64.   out.birthyear=y;
  65.   return out;
  66. }
  67.  
  68. void printPersonArray(int length, Person arr[]){
  69.   cout << "\nName\t\t: ";//if you destroy the formatting with ridiculous names it's not my problem
  70.   for(int i=0; i< length; i++){
  71.     cout << arr[i].name << "\t";
  72.   }
  73.   cout << "\nSurname\t\t: ";
  74.   for(int i=0; i< length; i++){
  75.     cout << arr[i].surname << "\t";
  76.   }
  77.   cout << "\nBirthday\t: ";
  78.   for(int i=0; i< length; i++){
  79.     cout << arr[i].birthday << "\t";
  80.   }
  81.   cout << "\nBirthmonth\t: ";
  82.   for(int i=0; i< length; i++){
  83.     cout << arr[i].birthmonth << "\t";
  84.   }
  85.   cout << "\nBirthyear\t: ";
  86.   for(int i=0; i< length; i++){
  87.     cout << arr[i].birthyear << "\t";
  88.   }
  89.  
  90. }
  91.  
  92. bool interval(int x, int lowerbound, int upperbound){
  93.   if(x< lowerbound) return false;
  94.   if(x > upperbound) return false;
  95.   return true;
  96. }
  97.  
  98. bool inSet(int x, int set[], int setSize){
  99.   for(int i=0;i<setSize;i++){
  100.     if(x==set[i]) return true;
  101.   }
  102.   return false;
  103.  
  104. }
  105.  
  106. bool check(int d, int m, int y){
  107.   if(!interval(d,1,31)) return false;
  108.   if(!interval(m,1,12)) return false;
  109.   if(m==2){//Ein Herz für Sonderfaelle
  110.     if(interval(d,1,28)) return true;
  111.     if(y%4 != 0) return false; //d=29, kein Schaltjahr -> false
  112.     if(y%400 == 0) return true; // 400 overrules all other rules, year ist Schaltjahr
  113.     if(y%100 == 0) return false; // 100 overrules Schaltjahr
  114.     return true; // Schaltjahr
  115.   }
  116.   int monthsw31days[]={1,3,5,7,8,10,12};
  117.   if(inSet(m,monthsw31days,7)){
  118.     if(interval(d,1,31)) return true;
  119.     return false;
  120.   }
  121.   if(interval(d,1,30)) return true;
  122.   return false;//Hier sollte nie angekommen werden
  123. }
  124.  
  125.  
  126. int main(){
  127.   int k=3;
  128.   Person group[k];
  129.   int i=0;
  130.   int d;
  131.   int m;
  132.   int y;
  133.   string n,s;
  134.   while(i<k){
  135.     cout << "Name:\t\t";
  136.     cin >> n;
  137.     cout << "Surname:\t";
  138.     cin >> s;
  139.     cout << "Day of birth:\t";
  140.     cin >> d;
  141.     cout << "Month of birth\t";
  142.     cin >> m;
  143.     cout << "Year of birth\t";
  144.     cin >> y;
  145.     if(!check(d,m,y)) cout << "Non-Valid date set, try again:"<<endl;
  146.     else{
  147.       group[i]=erstellePerson(n,s,d,m,y);
  148.       i++;
  149.     }
  150.   }
  151.   printPersonArray(k,group);
  152.   return 42;
  153. }
  154.  
  155. Aufgabe 5-3---------------------------------------------------------------------------------
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement