在Linux下,用shell编写一个简单的计算器,要实现加减乘除4个功能就行了
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Linux下,用shell编写一个简单的计算器,要实现加减乘除4个功能就行了相关的知识,希望对你有一定的参考价值。
紧急使用,速度快的话可以追加分
不用写吧,本来有个 bc 命令可用,没有下载就成.非要写一个,zsh 的function里有一个,名 zcalc,
贴上来给你
#!/usr/bin/zsh -i
#
# Zsh calculator. Understands most ordinary arithmetic expressions.
# Line editing and history are available. A blank line or `q' quits.
#
# Runs as a script or a function. If used as a function, the history
# is remembered for reuse in a later call (and also currently in the
# shell's own history). There are various problems using this as a
# script, so a function is recommended.
#
# The prompt shows a number for the current line. The corresponding
# result can be referred to with $<line-no>, e.g.
# 1> 32 + 10
# 42
# 2> $1 ** 2
# 1764
# The set of remembered numbers is primed with anything given on the
# command line. For example,
# zcalc '2 * 16'
# 1> 32 # printed by function
# 2> $1 + 2 # typed by user
# 34
# 3>
# Here, 32 is stored as $1. This works in the obvious way for any
# number of arguments.
#
# If the mathfunc library is available, probably understands most system
# mathematical functions. The left parenthesis must be adjacent to the
# end of the function name, to distinguish from shell parameters
# (translation: to prevent the maintainers from having to write proper
# lookahead parsing). For example,
# 1> sqrt(2)
# 1.4142135623730951
# is right, but `sqrt (2)' will give you an error.
#
# You can do things with parameters like
# 1> pi = 4.0 * atan(1)
# too. These go into global parameters, so be careful. You can declare
# local variables, however:
# 1> local pi
# but note this can't appear on the same line as a calculation. Don't
# use the variables listed in the `local' and `integer' lines below
# (translation: I can't be bothered to provide a sandbox).
#
# Some constants are already available: (case sensitive as always):
# PI pi, i.e. 3.1415926545897931
# E e, i.e. 2.7182818284590455
#
# You can also change the output base.
# 1> [#16]
# 1>
# Changes the default output to hexadecimal with numbers preceded by `16#'.
# Note the line isn't remembered.
# 2> [##16]
# 2>
# Change the default output base to hexadecimal with no prefix.
# 3> [#]
# Reset the default output base.
#
# This is based on the builtin feature that you can change the output base
# of a given expression. For example,
# 1> [##16] 32 + 20 / 2
# 2A
# 2>
# prints the result of the calculation in hexadecimal.
#
# You can't change the default input base, but the shell allows any small
# integer as a base:
# 1> 2#1111
# 15
# 2> [##13] 13#6 * 13#9
# 42
# and the standard C-like notation with a leading 0x for hexadecimal is
# also understood. However, leading 0 for octal is not understood --- it's
# too confusing in a calculator. Use 8#777 etc.
#
# Options: -#<base> is the same as a line containing just `[#<base>],
# similarly -##<base>; they set the default output base, with and without
# a base discriminator in front, respectively.
#
#
# To do:
# - separate zcalc history from shell history using arrays --- or allow
# zsh to switch internally to and from array-based history.
emulate -L zsh
setopt extendedglob
local line ans base defbase forms match mbegin mend psvar optlist opt arg
local compcontext="-math-"
integer num outdigits outform=1
# We use our own history file with an automatic pop on exit.
history -ap "$ZDOTDIR:-$HOME/.zcalc_history"
forms=( '%2$g' '%.*g' '%.*f' '%.*E' )
zmodload -i zsh/mathfunc 2>/dev/null
: $ZCALCPROMPT="%1v> "
# Supply some constants.
float PI E
(( PI = 4 * atan(1), E = exp(1) ))
# Process command line
while [[ -n $1 && $1 = -(|[#-]*) ]]; do
optlist=$1[2,-1]
shift
[[ $optlist = (|-) ]] && break
while [[ -n $optlist ]]; do
opt=$optlist[1]
optlist=$optlist[2,-1]
case $opt in
('#') # Default base
if [[ -n $optlist ]]; then
arg=$optlist
optlist=
elif [[ -n $1 ]]; then
arg=$1
shift
else
print "-# requires an argument" >&2
return 1
fi
if [[ $arg != (|\#)[[:digit:]]## ]]; then
print - "-# requires a decimal number as an argument" >&2
return 1
fi
defbase="[#$arg]"
;;
esac
done
done
for (( num = 1; num <= $#; num++ )); do
# Make sure all arguments have been evaluated.
# The `$' before the second argv forces string rather than numeric
# substitution.
(( argv[$num] = $argv[$num] ))
print "$num> $argv[$num]"
done
psvar[1]=$num
while vared -cehp "$(%)ZCALCPROMPT" line; do
[[ -z $line ]] && break
# special cases
# Set default base if `[#16]' or `[##16]' etc. on its own.
# Unset it if `[#]' or `[##]'.
if [[ $line = (#b)[[:blank:]]#('[#'(\#|)(<->|)']')[[:blank:]]#(*) ]]; then
if [[ -z $match[4] ]]; then
if [[ -z $match[3] ]]; then
defbase=
else
defbase=$match[1]
fi
print -s -- $line
line=
continue
else
base=$match[1]
fi
else
base=$defbase
fi
print -s -- $line
case $$line##[[:blank:]]#%%[[:blank:]]# in
q) # Exit if `q' on its own.
return 0
;;
norm) # restore output format to default
outform=1
;;
sci[[:blank:]]#(#b)(<->)(#B))
outdigits=$match[1]
outform=2
;;
fix[[:blank:]]#(#b)(<->)(#B))
outdigits=$match[1]
outform=3
;;
eng[[:blank:]]#(#b)(<->)(#B))
outdigits=$match[1]
outform=4
;;
local([[:blank:]]##*|))
eval $line
line=
continue
;;
*)
# Latest value is stored as a string, because it might be floating
# point or integer --- we don't know till after the evaluation, and
# arrays always store scalars anyway.
#
# Since it's a string, we'd better make sure we know which
# base it's in, so don't change that until we actually print it.
eval "ans=\$(( $line ))"
# on error $ans is not set; let user re-edit line
[[ -n $ans ]] || continue
argv[num++]=$ans
psvar[1]=$num
;;
esac
if [[ -n $base ]]; then
print -- $(( $base $ans ))
elif [[ $ans = *.* ]] || (( outdigits )); then
printf "$forms[outform]\n" $outdigits $ans
else
printf "%d\n" $ans
fi
line=
done
return 0
支援小数点,+ - * / , ok 参考技术A #!/bin/sh
# 例如 1 + 2
# $1: 1
# $2: +
# $3: 2
# $$表示参数个数
# 使用方法: ./jsq 1 + 2
if [ $$ -lt 3 ]
then
echo "Too few args!"
exit 1
fi
case $2
"+")
a=(($1+$3))
;;
"-")
a=(($1-$3))
;;
"*")
a=(($1*$3))
;;
"/")
a=(($1/$3))
;;
*)
;;
esac
echo $a
exit 0本回答被提问者采纳
linux c语言编程
一.编写一个C语言程序,用open函数在当前目录下创建一个文件test.txt,然后将数字1~100按顺序写入文件, 之后分别读出第50(从文件开始处计算)、第100个字节处的数字, 并输出该数字到标准输出。然后关闭并删除文件。
要求:.
i. 在Linux操作系统下调试通过后,在试卷上填写你的程序,并写好完整的注释。
ii. 写下你对程序中所使用的open,、write、close等文件系统调用的功能的理解。
iii. 雷同的程序两者都按零分处理。
二. 首先用shell命令在用户主目录下创建一个文件myfile, 然后用C语言编写一个程序打印出该文件的类型和组权限位, 判断该用户对该文件是否有执行权限。如果没有,请通过chmod函数给它加上执行权限。
要求和问题:
i. 你是如何用shell命令创建一个文件的?什么是用户的主目录?
ii. 在Linux操作系统下调试通过后,在试卷上填写你的程序,并写好完整的注释。
iii. 雷同的程序两者都按零分处理。
作业需要两个C程序,应该不难,哪位大哥帮我看看,调试成功后把程序发给我qq6920391 邮箱6920391@qq.com并留下回答者昵称。成功后直接给分。
PS:不要把程序贴到知道上,以免别人复制。程序要原创的。是linux下的C语言编程不是PC下的。有想做的请在知道后跟贴,方便我给您分数。本人linux小白+_+,还有第一题要用到open、write、close等文件系统调用的功能。
起码是linux 下的C语言编程,搞嵌入式开发的吧。
我也来试试,正想向这个方向研究呢。呵呵
另:公司只能上百度,只能这里给你了.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#define LEN 100
main()
int fd, len;
int i;
char ch[LEN];
fd=open("test.txt",O_CREAT|O_RDWR,10705);
if(fd)
for(i=1;i<101;i++)
sprintf(ch,"%d",i);
write(fd,ch,strlen(ch));
close(fd);
fd = open("test.txt",O_RDWR);
lseek(fd,50,SEEK_SET);
if(fd)
len = read(fd,ch,1);
ch[len] = '\0';
printf("%s\n",ch);
lseek(fd,100,SEEK_SET);
if(fd)
len = read(fd,ch,1);
ch[len] = '\0';
printf("%s\n",ch);
close(fd);
if(!remove("test.txt"))
printf("test.txt have remove\n");
else
printf("can't remove\n");
这是第一题. 参考技术A 我贴个第二题的吧。
#/bin/sh
cd
touch myfile
============================================================
#include<stdio.h>
#include<sys/stat.h>
#include<stdib.h>
main()
struct stat file;
stat("myfile",&file);
print("The file type is :");
if(S_ISREG(file.st_mode))
print("regular file.");
else if(S_ISREK(file.st_mode))
print("directory.");
else if(S_ISDIR(file.st_mode))
print("block special file.");
else if(S_ISLNK(file.st_mode))
print("symbolic link.");
else
print("other type file.");
printf("\nThe group permission is:");
if(file.st_mode & S_IRGRP)
print("read.");
if(file.st_mode & S_IWGRP)
print ("write.");
if(file.st_mode & S_IXGRP)
print ("execute.");
else
chmod("myfile",file.st_mode|S_IXGRP);
print("\nAdded execute permission.");
参考技术B 雷同的程序两者都按零分处理 参考技术C 有一本《Linux C函数大全》的小册子,Linux的API大多数都有,和MSDN有点相似,LZ看看这个。 参考技术D linux的环境你要是认识了 简单的编程是一样的 第5个回答 2009-04-21 退学吧……
以上是关于在Linux下,用shell编写一个简单的计算器,要实现加减乘除4个功能就行了的主要内容,如果未能解决你的问题,请参考以下文章
1.linux系统下shell脚本用case语句编写四则运算 2.linux系统下shell脚本输入数字串。进行反序输出
编写一个弹出式菜单的shell程序,利用函数实现简单的菜单功能,n的值由键盘输入: