南邮离散实验一(JavaScript)

内容:编写程序实现利用真值表法求主析取和主合取范式。
要求:能够列出任意合式公式的真值表并给出响应主析取和主合取范式。
HTML部分

<!doctype html>
<html lang="en">
<!-- !否定 |主析取 &主合取 >条件 ~双条件
-->
<head>
  <meta charset="utf-8">
  <title>Page Not Found</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    * {
      line-height: 1.2;
      margin: 0;
    }

    html {
      color: #888;
      display: table;
      font-family: sans-serif;
      height: 100%;
      text-align: center;
      width: 100%;
    }

    body {
      display: table-cell;
      vertical-align: middle;
      margin: 2em auto;
    }

    .root {
      width: 100%;
      height: 100%;
    }

    .resout {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }

    .ip {
      width: 50%;
      height: 50px;
      margin-top: 50px;
    }

    .OkBtn {
      margin-top: 40px;
    }

  </style>
</head>

<body>
  <div class="root">
    <div class="resout">
      <input class="ip" id="input1" placeholder="请输入公式">
      <button class="OkBtn" id="OkBtn">确定</button>
    </div>

  </div>
</body>

</html>

JavaScript部分

<script>
  let expression
  // let hasStr = []
  // let M = Map(String,Number)
  let map = new Map()
  let alpha = [] //记录所有字母
  let Atrue = [] //记录真值为T
  let Afalse = [] //记录真值为F
  let Hou = [] //后缀表达式
  var inputs = document.getElementById("input1")
  inputs.oninput = function (e) {
    expression = inputs.value
  }
  let OkBtn = document.getElementById("OkBtn")
  OkBtn.onclick = function () {
    // for (let i = 0; i < expression.length; i++) {
    //   if (expression[i] === "!" || expression[i] === "&" || expression[i] === "|" || expression[i] === ">" || expression[i] === "~") {
    //     continue
    //   }
    //   hasStr.push(expression[i])
    // }
    //中缀转后缀表达式
    alpha = []
    Atrue = []
    Afalse = []
    Hou = []
    map.clear()
    suffixExpression(expression)
  }

  //使用数组dataStore保存站内元素,构造函数将其初始化为一个空数组
  //变量top定义栈顶的位置,构造是初始化为0,表示栈的起始位置为0
  function Stack() {
    this.dataStore = []; //保存栈内元素
    this.top = 0;
    this.push = push;
    this.pop = pop;
    this.peek = peek;
    this.clear = clear;
    this.length = length;
    this.printElement = printStack
    //注意++操作符的位置,它放在this.top的后面,
    // 这样新入栈的元素就被放在top的当前位置对应的位置,
    // 同时top自加1,指向下一个位置
    function push(element) {
      this.dataStore[this.top++] = element
    }
    //返回栈顶元素,同时top位置减1
    function pop() {
      return this.dataStore[--this.top]
    }
    //peek()方法返回数组的第top-1个位置的元素,即栈顶元素
    function peek() {
      return this.dataStore[this.top-1]
    }
    //将top的值设置0,即清空一个栈
    function clear() {
      this.top = 0
    }
    //返回变量top的值即为栈内元素的个数
    function length() {
      return this.top
    }

    //输入栈内元素
    function printStack() {
      while (this.top > 0) {
        document.writeln(this.pop()+"&nbsp;&nbsp;")
      }
    }
  }

  //中缀表达式转后缀表达式
  function suffixExpression(s) {
    let str = s;
    let stack = new Stack(); //符号栈
    let outStack = new Array();
    // <!-- !否定 |主析取 &主合取 >条件 ~双条件-->
    for (let i = 0; i < str.length; i++) {
      //遇到')'弹出符号栈中元素直到栈空或遇到'(','('只弹出不输出
      if (')' === str[i]) {
        while (true) {
          let top = stack.peek()
          stack.pop()
          if ('(' != top) {
            outStack[outStack.length] = top
          } else {
            break
          }
        }
      } else if(['(','!','|','&','~','>'].indexOf(str[i]) > -1){
        if (['~'].indexOf(str[i]) > -1) {
          if (['!','|','&','>'].indexOf(stack.peek()) > -1) {
            while (['!','|','&','>'].indexOf(stack.peek()) > -1) {
              outStack[outStack.length] = stack.peek()
              stack.pop()
            }
          }
        } else if (['>'].indexOf(str[i]) > -1) {
          if (['!','|','&'].indexOf(stack.peek()) > -1) {
            while (['!','|','&'].indexOf(stack.peek()) > -1){
              outStack[outStack.length] = stack.peek()
              stack.pop()
            }
          }
        } else if (['|'].indexOf(str[i]) > -1) {
          if (['!','&'].indexOf(stack.peek()) > -1) {
            while (['!','&'].indexOf(stack.peek()) > -1){
              outStack[outStack.length] = stack.peek()
              stack.pop()
            }
          }
        } else if (['&'].indexOf(str[i]) > -1) {
          if (['!'].indexOf(stack.peek()) > -1) {
            while (['!'].indexOf(stack.peek()) > -1){
              outStack[outStack.length] = stack.peek()
              stack.pop()
            }
          }
        }
        stack.push(str[i])
      }else {
        outStack[outStack.length] = str[i]
        if (alpha.indexOf(str[i]) === -1) {
          alpha.push(str[i])
        }
      }
    }
    let length = stack.length()
    while (length) {
      outStack[outStack.length] = stack.peek()
      stack.pop()
      length --
    }
    Hou = outStack
    dfs(0)
    Perhaps()
    Also()
  }

  //对后缀表达式进行计算
  function cal() {
    let stack = new Stack()
    let ch
    // let j = 0
    let t1
    let t2
    for (let i = 0; i < Hou.length; i++) {
      ch = Hou[i]
      if ((ch>='A'&&ch<='Z') || (ch>='a'&&ch<='z')) {
        stack.push(map.get(ch))
      } else {
        if (ch === '!') {
          t1 = stack.peek()
          stack.pop()
          t1 = ((t1 === 0) ? 1 : 0)
          stack.push(t1)
        } else if (ch === '&') {
          t1 = stack.peek()
          stack.pop()
          t2 = stack.peek()
          stack.pop()
          if (t1 === 1 && t2 === 1) {
            stack.push(1)
          } else {
            stack.push(0)
          }
        } else if (ch === '|') {
          t1 = stack.peek()
          stack.pop()
          t2 = stack.peek()
          stack.pop()
          if (t1 === 0 && t2 === 0) {
            stack.push(0)
          } else {
            stack.push(1)
          }
        } else if (ch === '>') {
          t1 = stack.peek()
          stack.pop()
          t2 = stack.peek()
          stack.pop()
          if (t1 === 0 && t2 === 1) {
            stack.push(0)
          } else {
            stack.push(1)
          }
        } else if (ch === '~') {
          t1 = stack.peek()
          stack.pop()
          t2 = stack.peek()
          stack.pop()
          if ((t1===1&&t2===1)||(t1===0&&t2===0)) {
            stack.push(1)
          }else {
            stack.push(0)
          }
        }
      }
    }
    let ans = stack.peek()
    return ans
  }

  //递归枚举每一种字符变量的取值情况
  function dfs(cur) {
    if (cur == alpha.length) {
      let ans = cal()
      // console.log(ans)
      for (let i = 0; i < alpha.length; i++) {
        if (map.get(alpha[i])) {
          console.log(alpha[i] + ': T')
        }else {
          console.log(alpha[i] + ': F')
        }
      }
      if (ans === 1) {
        //表达式真值为T,计入到Atrue数组,以待后面的主析取范式使用
        console.log('表达式:' + 'T')
        let t = []
        for (let i = 0; i < alpha.length; i++) {
          // t = map.get(alpha[i])
          t.push(map.get(alpha[i]))
          // Atrue.push(t)
        }
        Atrue.push(t)
      }else {
        console.log('表达式:' + 'F')
        let t = []
        for (let i = 0; i < alpha.length; i++) {
          // t = map.get(alpha[i])
          t.push(map.get(alpha[i]))
          // Afalse.push(t)
        }
        Afalse.push(t)
      }
      return
    }
    // M[alpha[cur]] = 1
    // console.log(M)
    map.set(alpha[cur],1)
    dfs(cur+1)
    // M[alpha[cur]] = 0
    map.set(alpha[cur],0)
    dfs(cur+1)
  }

  //获取主析取范式
  function Perhaps() {
    let length = Atrue.length
    let str = ""
    for (let i = 0; i < length; i++) {
      if (i != 0) {
        str = str + '∨'
        // str.push('∨')
      }
      let p = Atrue[i]
      str = str + '('
      // str.push('(')
      for (let j = 0; j < alpha.length; j++) {
        if (j != 0){str = str + '∧'}
        if (p[j] == 1) {
          str = str + alpha[j]
        }else {
          let c = '¬' + alpha[j]
          str = str + c
        }
      }
      str = str + ')'
    }
    console.log('主析取范式' + str)
  }

  //获取主合取范式
  function Also() {
    let str = ''
    for (let i = 0; i < Afalse.length; i++) {
      if (i != 0){str += '∧'}
      let p = Afalse[i]
      str += '('
      for (let j = 0; j < alpha.length; j++) {
        if (j != 0){str += '∨'}
        if (p[j] == 0) {
          str += alpha[j]
        }else {
          let c = "¬" + alpha[j]
          str += c
        }
      }
      str = str + ')'
    }
    console.log('主合取范式' + str)
  }
</script>

测试结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值