Reverse Polish Notation assignment - RPN 1.0============================================In this assignment you will make a Reverse Polish Notation (RPN) calculator.You can read about RPN here: https://en.wikipedia.org/wiki/Reverse_Polish_notationThe assignment will be to make an RPN calculator that runs from the commandline as in $ ./rpn.exe 1 2 3 + + 6Packaging=========Your submission should be a zip file with the following structure: rpn-1.0.zip rpn-1.0/ README.txt Makefile rpnmain.c tests/If I unpack this zip file and cd into the folder I can compile the code withmake $ cd rpn-1.0 $ makewhich will produce an executable called rpn.exe.The README.txt describes author, date, changelog etc. It should show examplesof usage and should explain what has or has not been implemented. Your C codewill all be in the file rpnmain.c. The tests directory contains the test filesfrom Blackboard.OSX and Linux users may want to use the following Makefile rule to create thezip file: zip: cd .. && zip -r rpn-1.0.zip rpn-1.0 -x rpn-1.0/.DS_Store rpn-1.0/rpn.exeThen you can run make zip to make the zip file.NOTE: Check your zip file before submitting it. Create the zip file then moveit to another folder and extract it. Then cd into the folder and run ls -Ato see if any other files are in there.Command line options====================Your program should tell the user its version and usage: $ ./rpn.exe --version 1.0 $ ./rpn.exe --usage ./rpn.exe --usage ./rpn.exe --version ./rpn.exe TOKENS...These messages as well as normal successful operation should be printed tostdout and the return code should be set to 0 for success. If the user runsthe program with no arguments then the same usage message should be printed tostderr and the return code should be 1.RPN calculator==============Your program should be able to execute RPN instructions provided on thecommand line including four binary operators: + Addition - Substraction x Multiplication / DivisionOperands will always be integers. Valid operand strings have possibly beginwith an ASCII minus sign - but otherwise consist only of ASCII digits 0-9.Evaluating RPN is done with a stack based algorithm: https://en.wikipedia.org/wiki/Stack_(abstract_data_type)The natural way to implement a stack in C is to have an array to store theelements of the stack and an int variable that keeps track of the currentstack size (how many items are in the stack).Your RPN calculator should have a maximum stack size of 10. If the user blowsthe stack by providing too many operands it should print an error message: $ ./rpn.exe 1 2 3 4 5 6 7 8 9 10 11 + + + + + + + + + + Stack overflow at 11Note that there can be more than 10 operands provided they are interleavedwith operators. Only if pushing an operands blows the stack sould stackoverflow be printed. You will also need to detect stack underflow: $ ./rpn.exe 1 x Stack underflow at xYour program should also report an error if there are multiple tokens left onthe stack at exit: $ ./rpn.exe 1 2 3 - Tokens left on stack: stack[0] = 1 stack[1] = -1Otherwise it should print the result of the RPN calculations to stdout.Division========The operations addition, multiplication and subtraction are always welldefined for integers (although see the overflow section below). Howeverdivision is more complicated. Your program will need to check for division byzero and also for inexact division: $ ./rpn.exe 10 0 / Zero division in 10 / 0 $ ./rpn.exe 10 2 2 - / Zero division in 10 / 0 $ ./rpn.exe 10 2 / 5 $ ./rpn.exe 10 3 / 10 is not divisible by 3Integer overflow================Your calculator will exclusively use 4-byte (32-bit) signed integerarithmetic. The natural way to do this in C is using the int type. It isntnecessary to use the int type provided you can reproduce the behaviour of32-bit signed arithmetic. This means that the minimum and maximum possiblenumbers are -2147483648 and 2147483647 (-2**31 and 2**31-1). You will need todetect when an operand is outside of this range and print an error. Yourcalculator must work in all situations that do not overflow this arithmetic: $ ./rpn.exe 2147483648 Integer overflow at token 2147483648 $ ./rpn.exe -2147483648 -2147483648The function that I have used to parse strings for this is /* * Parse decimal string str into result. * * Returns 0 in the case of overflow and 1 for success. * Doesnt check that the string is valid decimal literal. */ int string_to_int(char *str, int *result) { long long_val = strtol(str, NULL, 10); if ( (long_val == LONG_MIN && errno == ERANGE) || (long_val == LONG_MAX && errno == ERANGE) || long_val || long_val > INT_MAX) { return 0; } *result = (int)long_val; return 1; }With this you can do (using pointers): int operand; if(!string_to_int(str, &operand)) { ... handle overflow } else { ... use operand }You will also need to detect overflow in the various operations: $ ./rpn.exe 2147483647 1 + Integer overflow in 2147483647 + 1One way to do this is to make functions that handle the different operationssafely checking for overflow and returning 0 or 1 to indicate success/failure.To help with overflow checking the limits header (#include )defines helpful constants: INT_MIN and INT_MAX which give the minimum andmaximum possible values for the int type.You have to be careful though that you dont overflow while checking foroverflow. For example if x and y are ints then x+y overflows if x + y > INT_MAXHowever if you try to check that in C with if(x + y > INT_MAX)Then the expression x + y will overflow and the test wont work properly.However if you know that y > 0 you can rearrange the expression to if(x > INT_MAX - y)where INT_MAX - y is guaranteed not to overflow if y > 0. On the other hand ify should check for overflow by going under INT_MIN. You can use this idea toprevent overflow in addition, subtraction and multiplication. For division theonly possible way that overflow can occur is with: -2147483648/-1Test script===========There is a test script on Blackboard in the folder tests.zip. You shoul useextract this in the folder with your code. Then from within the root (rpn-1.0)directory of your code you can run the tests with $ python tests/runtests.py -q .................................................... Passed 52/52 tests 0 failsThe test script can be run with or without the -q flag (for quiet) or the -kflag (for keep going) as needed. You can see this if you run it with --help $ python tests/runtests.py --help Usage: runtests.py [options] Options: -h, --help show this help message and exit -q, --quiet Show more information -k, --keep-going Continue running all testsIf you add the following rule to your Makefile then you can run the tests withmake test: test: rpn.exe python tests/runtests.pyThis rule depends on rpn.exe so it will ensure that your program is compiledbefore running the tests.If you look in the file tests/tests.txt you can see what is being tested.NOTE: Dont edit the test code. In the past some students have changed thetests to get them to pass. When marking we will test with different testfiles.NOTE: Dont hard code the examples tested for. They are just examples - whenmarking we will test with different examples.Marks breakdown===============60% of the marks for this assignment are for having correct output (i.e.passing the tests). The remaining 40% are for the quality of your code. Goodquality code features several things:Consistent indentation. Poorly indented code is very hard to read. Dont tryto fix the indentation at the end - keep the indentation good the wholetime.Good comments to explain whats going on in the code. A big comment near thetop of your code should explain the general layout - which functions do whatand why.Throughout things that are *not obvious* should be explained with comments.That could mean having a comment that summarises what a number of linesbelow do. It could also mean a more meaningful explanation of what somecryptic code does. Stating the obvious in a comment does not help someone toread the code. Heres a bad comment: /* Loop i from 1 to 10 */ for(int i=0; iWhats wrong with this comment? Firstly the loop is from 0 to 9 so the commentis inaccurate. Secondly if we fix it so that it says from 0 to 9 it is stillstating the obvious. I can see the line below and if I understand C then Iknow that it means a loop with i from 0 to 9. A better comment might be /* Loop through the input tokens */ for(int token_number = 0; token_number Your program should also have good variable names. While it is often okay tohave throwaway variables like int i for a small loop, in any larger block ofcode it will become hard to decifer what i, j, k etc are supposed to be.Good use of functions to eliminate repetition and break the program downinto small understandable pieces. The functions should have reasonablesignatures so that they are reusable (you could use the same function in adifferent program without needing to change it).Functions should also have good, meaningful names. The style I want to see isnames that are words separated by underscores e.g. get_input. I do now want tosee camel-case getInput or GetInput. Those are the conventions in Java, not inC. Think carefully about the names of your functions: should it be a noun or averb or even a question? The names should communicate the intent or effect ofthe function so that I can understand what it does just by looking at wherethe functions is *called*.If you have a number of similar functions give them similar names in asystematic way e.g. handle_add and handle_sub. Note here Im shorteningaddition to add which is okay in some contexts where the shortened name iscommon. Generally though longer names such as handle_addition are preferred.The idea of code quality is generally driven by two important propertiesthat we want from code: it should be readable and it should be maintainable.Since code is generally read more than it is written (even while it is beingwritten) producing readable code straight away saves a lot of time.Maintainable code is readable but is also written in such a way that thedifferent parts of the code can be easily modified in isolation. It should bepossible to fix one part without breaking another. See https://en.wikipedia.org/wiki/Spaghetti_codeGlobal variables are bad - they lead to spaghetti code. Note that global*constants* are fine. It is only a global variable that changes while yourprogram runs that is bad. You will be penalised for having any globalvariables in your code. The different functions should interact usingarguments and return values not using global variables. Note that there aresome situations where global variables are necessary/useful but they will notoccur during this assignment.Writing good code requires some element of design / planning. Before you beginwriting think how you would structure it: what functions will you have? Whatwill be their signatures etc? Dont be afraid to clean up messy code byrewriting everything after youve finished - it doesnt take as long as youthink. Professional code is often written first as a proof of concept and thenrewritten perhaps multiple times until it becomes sensibly organised.This assignment is not so big that there is any excuse for messy code. Myversion is 200 lines long (including comments and blank lines) and has about10 functions.本团队核心人员组成主要包括硅谷工程师、BAT一线工程师,精通德英语!我们主要业务范围是代做编程大作业、课程设计等等。我们的方向领域:window编程 数值算法 AI人工智能 金融统计 计量分析 大数据 网络编程 WEB编程 通讯编程 游戏编程多媒体linux 外挂编程 程序API图像处理 嵌入式/单片机 数据库编程 控制台 进程与线程 网络安全 汇编语言 硬件编程 软件设计 工程标准规等。其中代写编程、代写程序、代写留学生程序作业语言或工具包括但不限于以下范围:C/C++/C#代写Java代写IT代写Python代写辅导编程作业Matlab代写Haskell代写Processing代写Linux环境搭建Rust代写Data Structure Assginment 数据结构代写MIPS代写Machine Learning 作业 代写Oracle/SQL/PostgreSQL/Pig 数据库代写/代做/辅导Web开发、网站开发、网站作业ASP.NET网站开发Finance Insurace Statistics统计、回归、迭代Prolog代写Computer Computational method代做因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com 微信:codehelp
网友评论