如何用C ++编写Scientific计算器 [英] How to code a Scientific calculator in C++

查看:115
本文介绍了如何用C ++编写Scientific计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



作为个人项目的一部分,我需要制作一个基于控制台的科学计算器。



它需要能够为每个命令添加,减去,乘以和除以大量数字(例如:2 + 2 + 2 + 2 + 2 + 2 * 9/7 * sin 45-88.277)并且能够使用BIDMAS正确操作数字。



它需要能够计算三角函数,对数,整数的mod,计算阶乘和计算指数和根到第n学位。



我怎样才能这样做以及需要多少行代码?



我现在不希望它有GUI。



任何源代码都将被发布,整个程序将是免费的。





我已经看了好几年了,我能找到的只是计算器,只能添加两个数字



非常感谢提前

解决方案

这主要是一个解析任务,不适合C ++初学者。

解析专家会在几个小时内解决这个问题 - 一个完整的新手可能需要几个月才能完成它。

我会选择一个不同的主题,这取决于你的个人兴趣或爱好。

还要考虑今天C ++的用处并选择相应的主题。



如果你坚持使用C ++进行解析器任务,你可以使用flex / bison来学习

- tokeinzing(定义标记)

- 解析(定义制作)

- 符号表(类型系统,变量/运算符/函数查找,存储和检索数据,支持的操作,执行操作)

- 动作(为解析的作品定义动作以及如何传递值,例如堆栈)



你可能需要买一本关于编译器构建的书。



祝你好运。



Andi


这并不容易,并且需要比你想象的更多的代码行。 />


您将需要解析输入字符串以搜索符号和数字,区分三角函数,检查拼写(可能给出错误或自动更正),添加()在需要的地方(意思是:找到一个*或/或一个数字后跟字母2sin 45,然后搜索到后面和前面以设置操作的优先级)等等......并且,只读取输入。



然后按照优先顺序调用相应的操作并处理异常(如跳过0,虚构根等等) on)。



结论......太宽泛的主题只是立即解释。





开始编码和c当你遇到更具体的问题时,你会得到更好的答案,如果你将问题划分到一个特定的问题,你会得到更多有用的帮助。



祝你好运并享受有趣的编码:)


如果你想用C ++(或者用C ++ / CLI在混合模式下)你也可以使用boost。



http://www.boost.org/doc/libs /1_49_0/libs/spirit/doc/html/index.html [ ^ ]



对于代码行数,这将取决于很多你是怎么做的......


Hello

As part of a personal project, I need to make a console based Scientific calculator.

It needs to be able to add, subtract, multiply and divide a large amount of numbers per command (eg: 2+2+2+2+2+2*9/7*sin 45- 88.277) and be able to manipulate numbers correctly using BIDMAS.

It needs to be able to compute trigonometry functions, logarithms, mod of an integer, calculate factorials and calculate exponents and roots to the nth degree.

How can I go about doing this and how many lines of code will it take?

I do not want it to have a GUI at the moment.

Any source code will be released and the entire program will be free.


I have looked around for ages and all I can find are calculators which can only add two numbers

Thanks very much in advance

解决方案

This is mainly a parsing task, not really suited for a C++ beginner.
An expert in parsing would solve this within a few hours - a complete novice might take months to get it somehow done.
I would choose a different topic, depending on your personal interests or hobby.
Think also of where C++ is useful today and choose a respective topic.

If you insist on a parser task in C++, you might take flex/bison and learn
- tokeinzing (define tokens)
- parsing (define productions)
- symbol table (type system, variable/operator/function lookup, storing and retrieving data, supported operations, executing operations)
- actions (define actions for parsed productions and how to pass values around, e.g. by a stack)

You might need to buy a book on compiler construction.

Good luck.

Andi


It is not going to be easy, and going to need more lines of code than you think.

You will need to parse the input string to search for the symbols and the numbers, differenciate the trigonometry functions, check spelling (possibly giving errors or autocorrection), add the () where needed (what means: find a "*" or a "/" or a number followed by letters "2sin 45", and then search to the back and to the front to set the priority of the operations) and more... and that, only to read the input.

Then call the respective operations following the priority order and dealing with the exceptions (like diving by 0, imaginary roots, and so on).

In conclusion... too wide thema just to explain at once.


Start coding and come back when you get more concrete problems, you will get better answers and more usefull help if you delimitate the questions to a particular issue.

Good luck and have fun coding :)


You might also uses boost if you want to do it in C++ (or in mixed mode with C++/CLI).

http://www.boost.org/doc/libs/1_49_0/libs/spirit/doc/html/index.html[^]

For the number of lines of code, it would depends a lot on how you do it...


这篇关于如何用C ++编写Scientific计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆