简体   繁体   中英

How to recreate this logical argument(syllogism) using logical operators and any statements and data-types in C#

  1. This cake is either vanilla or chocolate.

This cake is not chocolate. Therefore, this cake is vanilla.

  1. All men are mortal

Socrates is a man. Therefore, Socrates is mortal.

This is the logical argument - how to recreate these using operators in C#?

I'll give it a try of what I think is asked in this assignment:

First example

1. This cake is either vanilla or chocolate.
2. This cake is not chocolate. 
=> Therefore, this cake is vanilla.
class Cake{

    public Cake(bool isChocolate){
        this.IsChocolate = isChocolate;
    }

    public bool IsChocolate {get; private set;} = false;
    public bool IsVanilla => !IsChocolate; // "Not IsChocolate" == opposite of IsChocolate
}

// ...
public static void Main()
{
    var cake = new Cake(false);
    if( cake.IsChocolate ) Console.WriteLine("This cake is Chocolate!");
    else Console.WriteLine("This cake is Vanilla!"); // No further check: If it's not choc, 
                                                     // it must be vanilla.
}

Or maybe through Interface:

public interface ICake{
   // Cake Properties
}

public class Chocolate : ICake{} // Chocolate is-a Cake

public class Vanilla : ICake{} // Vanilla is-a Cake

// ...

public static void Main(){
    ICake cake = new Vanilla();
    if( cake is Chocolate chocoCake )
    {
         Console.WriteLine("It's chocolate!");
    }
    else // Not chocolate => must be vanilla
    {
         Console.WriteLine("It's vanilla!");
    }
}


/* Attention:
 * This will of course break your logic, if you added 
 * class Strawberry: ICake{}
 */

or

public enum CakeType{
   Chocolate,
   Vanilla
}

class Cake{
    public CakeType TypeACake {get; private set;} = CakeType.Vanilla;
}

// ...

public static void Main(){
   var thisCake = new Cake();
   Console.WriteLine($"This cake is {(cake.TypeACake == CakeType.Chocolate ? "": "not ")}Chocolate.");
   Console.WriteLine($"Therefore it is {(cake.TypeACake == CakeType.Vanilla ? "": "not ")}Vanilla.");
}

There are tons of variations of this, actually. Depends on how it is supposed to be used. So, imho not really great of an assignment - although this could be exactly the goal of the lesson rather than having you find a "correct implementation".

Second example

1. All men are mortal
2. Socrates is a man. 
=> Therefore, Socrates is mortal.
class Man {
    public bool IsMortal => true;
}

// ...

public static void Main(){
  Man Socrates = new Man();
  
  Console.WriteLine($"{nameof(Socrates)} is a '{nameof(Man)}', therefore he is {(Socrates.IsMortal?"mortal":"immortal")}.");
}

See https://dotnetfiddle.net/J3xzwx

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
           bool VanillaCakeIS = true;
            bool ChocolateCakeIS =false ;
            if ( VanillaCakeIS || !ChocolateCakeIS)
            {
                Console.WriteLine("This Cake is vanilla");
            }
            else {
                Console.WriteLine("This Cake is chocolate");}
            
            bool MenAreMortal = true;
            bool SocratesIsMan = true;
            if (MenAreMortal && SocratesIsMan)
            {
              Console.WriteLine("socrates is mortal");  
            }
            else {
                Console.WriteLine("socrates is not mortal"); }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM