Four Arithmetic Operation
来源:互联网 发布:floyd算法负权 编辑:程序博客网 时间:2024/05/21 09:13
Tp1: calculation of a formula with only a kind of operator
#! /bin/shn=$#if [ $n -eq 0 ]then echo "Opération manquante" exitelif [ $n -eq 1 ]then echo "Opréation manquante"else res=$2 case $1 in "add") echo -n $2 shift 2 while [ $# -ne 0 ] do echo -n "+$1" res=`expr $res + $1` shift done echo "=${res}";; "supp") echo -n $2 shift 2 while [ $# -ne 0 ] do echo -n "-$1" res=`expr $res - $1` shift done echo "=${res}";; "mult") echo -n $2 shift 2 while [ $# -ne 0 ] do echo -n "x$1" res=`expr $res \* $1` shift done echo "=${res}";; "div") echo -n $2 shift 2 while [ $# -ne 0 ] do echo -n "/$1" if [ $1 -eq 0 ] then echo -e "\nDivision par 0 interdite" exit else res=`expr $res / $1` shift fi done echo "=${res}";; *) echo "Opération non traitée";; esac fi
Tp2: calculation of a formula without considering the priority of each operator
#! /bin/shif [ $# -eq 0 ]then echo " Opérande manquante" exitelse res=$1 echo -n "$1" shift while [ $# -ne 0 ] do case $1 in "+") echo -n " $1 $2" res=`expr $res + $2` shift 2 ;; "-") echo -n " $1 $2" res=`expr $res - $2` shift 2 ;; "x") echo -n " $1 $2" res=`expr $res \* $2` shift 2 ;; "/") echo -n " $1 $2" if [ $2 -eq 0 ] then echo " ->Divivion par 0 interdite" exit else res=`expr $res / $2` shift 2 fi ;; *) echo " $1 -> Opération non traitée" exit esac done echo " = $res"fi
Tp3: calculation of a formula without brackets
#! /bin/shindex=0while [ $# -gt 0 ]do if [ $1 != x -a $1 != / ] then low_priority[$index]=$1 index=`expr $index + 1` shift 1 else case $1 in "x") pre_index=`expr $index - 1` res=`expr ${low_priority[$pre_index]} \* $2` low_priority[$pre_index]=$res shift 2 ;; "/") pre_index=`expr $index - 1` res=`expr ${low_priority[$pre_index]} / $2` low_priority[$pre_index]=$res shift 2 ;; esac fidonen=1length=${#low_priority[@]}end=`expr $length - 2`res=${low_priority[0]}while [ $n -le $end ]do case ${low_priority[$n]} in "+") nex_n=`expr $n + 1` res=`expr $res + ${low_priority[$nex_n]}` n=`expr $n + 2`;; "-") nex_n=`expr $n + 1` res=`expr $res - ${low_priority[$nex_n]}` n=`expr $n + 2`;; *) exit;; esacdoneecho $res
0 0
- Four Arithmetic Operation
- Arithmetic and Logical Operation
- HDU 5938 - Four Operation(贪心)
- four:
- four
- four
- Swift Compiler Error Arithmetic operation '** ' (on type '**') results in an overflow (溢出运算符'&+')
- operation
- 自定义40位大整数类及测试类(Define a huge integer and implement its arithmetic operation)
- four friends
- unit four
- Four ugliness
- Four Sum
- Twenty-four
- four数组
- hibernate four
- Week Four
- Primary Arithmetic
- Python string format
- Leetcode: Basic Calculator
- java break语句详解 break label;语句的使用
- TCP释放连接时为什么time_wait状态必须等待2MSL时间(阅读笔记)?
- XHTML表单
- Four Arithmetic Operation
- VMware Fusion 启动物理磁盘及U盘的方法
- MySQLの执行MySQL备份
- cocos studio 学习笔记
- 为什么常说程序员35岁是道坎?
- MySQLの理解备份选项
- 俞敏洪的作息时间表
- ZigZag Conversion
- Leetcode: Longest Palindromic Substring