Advertisement
Infernale

NCTU LAB 24/9 NUM 4 [Lambda's]

Sep 26th, 2019
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <iostream>
  2. #include<cmath>
  3.  
  4. using namespace std;
  5.  
  6. long long Factorial(int a){
  7.     if(a == 1) return 1;
  8.     else return a * Factorial(a-1);
  9. }
  10.  
  11. long long Prime(int a){
  12.     if(a == 1)return 0;
  13.     for(int i = 2;i<=sqrt(a);i++){
  14.         if(a%i == 0) return 0;
  15.     }
  16.     return 1;
  17. }
  18.  
  19. long long OddEven(int a){
  20.     return (a%2 == 0)?1:0;
  21. }
  22.  
  23. long long Operate(int a, long long (*fp)(int)){
  24.     return (*fp)(a);
  25. }
  26.  
  27. int main()
  28. {
  29.     int op,in;
  30.     bool flag=true;
  31.     cout << "Function Pointer" <<endl;
  32.     do{
  33.         cin >> op;
  34.         if(op == 4){
  35.             break;
  36.         }else{
  37.             cin >> in;
  38.             switch(op){
  39.                 case 1:
  40.                     cout << Operate(in,Factorial) << endl;
  41.                     break;
  42.                 case 2:
  43.                     if(Operate(in,Prime) == 0) cout << "Not Prime" <<endl;
  44.                     else cout << "Prime" << endl;
  45.                     break;
  46.                 case 3:
  47.                     if(Operate(in,OddEven) == 0) cout << "Odd" << endl;
  48.                     else cout << "Even" << endl;
  49.                     break;
  50.             }
  51.         }
  52.     }while(flag);
  53.     flag = true;
  54.     cout << "Lambda Expression" << endl;
  55.     do{
  56.         cin >> op;
  57.         if(op == 4){
  58.             return 0;
  59.         }else{
  60.             cin >> in;
  61.             switch(op){
  62.                 case 1:{
  63.                     auto fact = [in](){
  64.                         int res = 1;
  65.                         for(int i=2; i<=in; i++){
  66.                             res*=i;
  67.                         }
  68.                         return res;
  69.                     };
  70.                     cout << "Result : " << fact() << endl;
  71.                     break;
  72.                 }
  73.                 case 2:{
  74.                     auto prime = [in](){
  75.                         if(in == 1)
  76.                             return 0;
  77.                         for(int i = 2;i<=sqrt(in);i++){
  78.                             if(in%i == 0)
  79.                                 return 0;
  80.                         }
  81.                         return 1;
  82.                     };
  83.                     cout << (prime() == 0 ? "Not Prime" : "Prime") << endl;
  84.                     break;
  85.                 }
  86.                 case 3:{
  87.                     auto even = [in](){
  88.                         return in % 2 == 0 ? 1 : 0;
  89.                     };
  90.                     cout << (even() == 1 ? "Even" : "Odd") << endl;
  91.                     break;
  92.                 }
  93.             }
  94.         }
  95.     }while(flag);
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement