## calc
This is a simple calculator written in C. It uses `readline` to get user input.
Initially written on January 25, 2021, current version: `1.5`
## Available commands
Currently, `calc` supports the following commands:
| Command | Description |
|--------------|-----------------------------------------|
| `calc` | Enter just-calculator mode (see [1]) |
| `clear` | Clear the screen |
| `color` | Enable colored output |
| `examples` | Enable examples in help section |
| `exit` | Close this program |
| `flags` | Enable flags in help section |
| `help` | Print help/usage |
| `nocalc` | Disable just-calculator mode (see [1]) |
| `nocolor` | Disable colored output |
| `noexamples` | Disable examples in help section |
| `noflags` | Disable flags in help section |
| `operands` | Print list of operands |
| `ops` | Print list of operands |
| `quit` | Close this program |
| `specvals` | Print [Special Values](#Special-Values) |
[1]: the `calc` command is only available outside of just-calculator mode, and
the `nocalc` command is only available inside just-calculator mode.
## Flags
Currently, `calc` supports the following command-line flags:
| Flag | Description |
|------|----------------------------------|
| `-c` | Enter just-calculator mode |
| `-e` | Disable examples in help section |
| `-f` | Disable flags in help section |
| `-h` | Print help and exit |
| `-m` | Disable program compilation info |
| `-n` | Disable colored output |
NOTE: Flag order matters! ([cefmnh])
## Example operations
| Command | Can be written as | Description | Result |
|------------|-------------------|----------------------|---------------|
| `1 + 1` | `1 p 1` | Addition | Returns 2 |
| `1 - 1` | `1 s 1` | Subtraction | Returns 0 |
| `2 * 2` | `2 t 2` | Multiplication | Returns 4 |
| `4 / 2` | `4 d 2` | Division | Returns 2 |
| `4 % 2` | `4 m 2` | Modulus | Returns 0 |
| `1 < 16` | `1 l 16` | Bit-shifting (left) | Returns 65536 |
| `4096 > 1` | `4096 r 4` | Bit-shifting (right) | Returns 2048 |
*(See bit-shifting info [here](#Bit-Shifting))*
## Special Values
You can (optionally) use Special Values, thanks to the `<math.h>` library.
Here's a table containing the currently implemented values.
| Special Value | Description |
|---------------|-----------------------------|
| E | The value of Euler's number |
| Pi | The value of Pi |
| Pi2 | Pi / 2 |
| Pi4 | Pi / 4 |
| 1Pi | 1 / Pi |
| 2Pi | 2 / Pi |
| Pisq | Pi² (Pi * Pi) |
NOTE: these are case-insensitive, so don't worry about writing them all lowercase, uppercase, etc.
For example:
```
calc> Pi + 2
5.1415926536
calc> piSq / pi
3.1415926536
calc> e + Pi2
4.2890781553
calc> pi4 * 1pI
0.2500000000
```
It's also possible to just print a value/number, for example:
```
calc> pi
3.1415926536
calc> 7
7
calc> e
2.7182818285
calc> pisq
9.8696044011
calc> 3
3
```
## Bit Shifting
In C, there are two kinds of bit-shifting:
- Bit-shifting to the left (`<<`)
- Bit-shifting to the right (`>>`)
___What is bit shifting?___
Bit shifting is, well, used to shift bits.
In the computer world, integers are represented using 0s and 1s;
For example, if we take the number `7`, it would be represented as follows:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|-----|----|----|----|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
(That is `00000111` in binary; for simplicity's sake we'll just do a byte)
Let's say we want to convert `30` to binary. In this case, we'll have:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|-----|----|----|----|---|---|---|---|
| 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 |
(`00011110` in binary)
Now, let's talk about bit shifting.
When we "shift a bit", it means to move the first (or last) bit one place
to the right (`>>`) or to the left (`<<`).
For example, I want to bit shift 7 one place to the right:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|-----|----|----|----|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
Now, the only values left are `2` and `1`, which make up `3`.
Instead, let's try to do `30`:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|-----|----|----|----|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
As you can see, the only digits left now are `8`, `4`, `2`, and `1`,
which make up `15`.
This time, let's do a bigger value, like `124`:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|-----|----|----|----|---|---|---|---|
| 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
Will become `62`:
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|-----|----|----|----|---|---|---|---|
| 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 |
The same applies to bit-shifting to the right.
Bit shifting is useful to calculate powers of 2, for example:
2^16: `1 << 16`: 65536
2^30: `1 << 30`: 2147483648 (32 bit limit)
2^31: `1 << 31`: 4294967296 (64 bit limit)
## Statically linking & building
If you want to statically link `calc` to share it, make sure you have `musl`, `musl-gcc`, and a copy of the source code of `libreadline` and `libncurses` (needed by `libreadline`).
After you've met all of the requirements, run the following:
```bash
cd path/to/calc
mkdir -p libs/{readline-src,readline,ncurses-src,ncurses}
cd libs
mv /path/to/libreadline readline-src
mv /path/to/libncurses ncurses-src
cd readline-src
./configure --prefix=../readline CC=musl-gcc && make -kj$(nproc) && make install-static
cd ../ncurses-src
./configure --prefix=../ncurses CC=musl-gcc && make -kj$(nproc) && make install
cd ../..
make static-deb
```
It should compile succesfully; When compiling `static-deb` with `make`, the output should look like this:
```bash
[STATIC DEBUG]
gcc: warning: ./libs/readline/lib/libreadline.a: linker input file unused because linking not done
gcc: warning: ./libs/ncurses/lib/libncurses.a: linker input file unused because linking not done
CC calc.c
CC calc-debstatic.o
```
## Why was this written?
This was written because I was tired of using the slow Python as a command-line calculator.
I wanted something fast, that I could modify whenever I wanted to.
So, I quickly started writing the barebones of the calculator. It's nothing fancy, really, but a nice tool to have.
More features might be added, if necessary.
For any help, either:
- Create an issue;
- Open a pull request;
- Send me an email [(saloniamatteo@pm.me)](mailto:saloniamatteo@pm.me).
没有合适的资源?快使用搜索试试~ 我知道了~
calc:一个用C语言编写的简单计算器
共9个文件
h:4个
makefile:1个
gitignore:1个
需积分: 50 5 下载量 52 浏览量
2021-03-16
00:25:19
上传
评论 2
收藏 24KB ZIP 举报
温馨提示
计算 这是一个用C语言编写的简单计算器。它使用readline获取用户输入。 最初写于2021年1月25日,当前版本: 1.5 可用命令 当前, calc支持以下命令: 命令 描述 calc 进入仅计算器模式(请参阅[1]) clear 清除萤幕 color 启用彩色输出 examples 在帮助部分启用示例 exit 关闭这个程序 flags 在帮助部分启用标志 help 打印帮助/用法 nocalc 禁用仅计算器模式(请参阅[1]) nocolor 禁用彩色输出 noexamples 在帮助部分禁用示例 noflags 禁用帮助部分中的标志 operands 打印操作数列表 ops 打印操作数列表 quit 关闭这个程序 specvals 打印 [1]: calc命令仅在just-calculator模式之外可用,而nocalc命令仅在just-c
资源详情
资源评论
资源推荐
收起资源包目录
calc-master.zip (9个子文件)
calc-master
color.h 4KB
calc.c 14KB
platform.h 3KB
compiler.h 523B
LICENSE 34KB
README.md 7KB
Makefile 2KB
optimizations.h 851B
.gitignore 14B
共 9 条
- 1
八普
- 粉丝: 34
- 资源: 4551
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0