Problem: Calc

tl;dr

Implement a program that implements a command line calculator.

$ ./calc 50 x 28
1400.000000

Academic Honesty

This course’s philosophy on academic honesty is best stated as "be reasonable." The course recognizes that interactions with classmates and others can facilitate mastery of the course’s material. However, there remains a line between enlisting the help of another and submitting the work of another. This policy characterizes both sides of that line.

The essence of all work that you submit to this course must be your own. Collaboration on problems is not permitted (unless explicitly stated otherwise) except to the extent that you may ask classmates and others for help so long as that help does not reduce to another doing your work for you. Generally speaking, when asking for help, you may show your code or writing to others, but you may not view theirs, so long as you and they respect this policy’s other constraints. Collaboration on quizzes and tests is not permitted at all. Collaboration on the final project is permitted to the extent prescribed by its specification.

Below are rules of thumb that (inexhaustively) characterize acts that the course considers reasonable and not reasonable. If in doubt as to whether some act is reasonable, do not commit it until you solicit and receive approval in writing from your instructor. If a violation of this policy is suspected and confirmed, your instructor reserves the right to impose local sanctions on top of any disciplinary outcome that may include an unsatisfactory or failing grade for work submitted or for the course itself.

Reasonable

  • Communicating with classmates about problems in English (or some other spoken language).

  • Discussing the course’s material with others in order to understand it better.

  • Helping a classmate identify a bug in his or her code, such as by viewing, compiling, or running his or her code, even on your own computer.

  • Incorporating snippets of code that you find online or elsewhere into your own code, provided that those snippets are not themselves solutions to assigned problems and that you cite the snippets' origins.

  • Reviewing past years' quizzes, tests, and solutions thereto.

  • Sending or showing code that you’ve written to someone, possibly a classmate, so that he or she might help you identify and fix a bug.

  • Sharing snippets of your own solutions to problems online so that others might help you identify and fix a bug or other issue.

  • Turning to the web or elsewhere for instruction beyond the course’s own, for references, and for solutions to technical difficulties, but not for outright solutions to problems or your own final project.

  • Whiteboarding solutions to problems with others using diagrams or pseudocode but not actual code.

  • Working with (and even paying) a tutor to help you with the course, provided the tutor does not do your work for you.

Not Reasonable

  • Accessing a solution to some problem prior to (re-)submitting your own.

  • Asking a classmate to see his or her solution to a problem before (re-)submitting your own.

  • Decompiling, deobfuscating, or disassembling the staff’s solutions to problems.

  • Failing to cite (as with comments) the origins of code, writing, or techniques that you discover outside of the course’s own lessons and integrate into your own work, even while respecting this policy’s other constraints.

  • Giving or showing to a classmate a solution to a problem when it is he or she, and not you, who is struggling to solve it.

  • Looking at another individual’s work during a quiz or test.

  • Paying or offering to pay an individual for work that you may submit as (part of) your own.

  • Providing or making available solutions to problems to individuals who might take this course in the future.

  • Searching for, soliciting, or viewing a quiz’s questions or answers prior to taking the quiz.

  • Searching for or soliciting outright solutions to problems online or elsewhere.

  • Splitting a problem’s workload with another individual and combining your work (unless explicitly authorized by the problem itself).

  • Submitting (after possibly modifying) the work of another individual beyond allowed snippets.

  • Submitting the same or similar work to this course that you have submitted or will submit to another.

  • Using resources during a quiz beyond those explicitly allowed in the quiz’s instructions.

  • Viewing another’s solution to a problem and basing your own solution on it.

Getting Started

Start off by opening up CS50 IDE and then type

cd ~/chapter2

within a terminal window to navigate to your chapter2 directory. If you somehow closed your terminal window (and can’t find it!), make sure that Console is checked under the View menu, then click the green, circled plus (+) in CS50 IDE’s bottom half, then select New Terminal. If you need a hand, do just ask via the channels noted at the top of this specification.

Next, create a new calc directory inside chapter2 (remember how?). Next create a new file entitled calc.c in your calc directory.

Though we’ll remind you of the existence of this video many times throughout the problems in this chapter, be sure to have a look at Doug’s short video on command-line arguments.

If you happen to see (and are confused by!) char * in this and other shorts, know for now that char * simply means string. But more on that soon!

The shorts on arrays wouldn’t hurt either, if you want a refresher.

Divide and Conquer

In this problem, you will be tasked with implementing a very simple command-line based calculator program. Your program will accept inputs like this (wherein underlined text represents user input):

~/chapter2/ $ ./calc 4 + 5
9.000000

or, indeed like this (allowing the user to perform some basic floating-point arithmetic)

~/chapter2/ $ ./calc 8.38 - 5.12
3.260000

such that the user can perform all five of the basic math operations that C permits — addition, subtraction, multiplication, division, and modulo.

Notice that unlike many other programs you’ve likely written up to this point, and just like Old Friends, users are not entering any information after the program has started running. Rather, they are providing all of their input to the program at the command line, before the program has begun.

Recall from Doug’s short that if we collect information from the user at the command line, we can use two special parameters passed into main (conventionally called argc and argv) which represent the number of arguments the user provided and the actual data the user provided, respectively. Given the example use case above, how many command-line arguments is the user expected to provide?

If they fail to provide the correct number, your program should exit (possibly printing out an error message that tells them how they should have run the program) and return 1; so that we can automate testing of your code.

Assuming we have the right number of command-line arguments, we’re well on our way. There’s a catch, though.

Just because the user types a real number at the command line, that doesn’t mean their input will be automatically stored in a float. Actually, it will be stored as a string that just so happens to look like an float; after all, remember the data type of argv? It’s an array where each element is a string! And so you’ll need to convert that string to an actual float. As luck would have it, a function, atof, exists for exactly that purpose! Here’s how you might use it:

float a = atof(argv[1]);

There are two values that need to be converted from a string to a float (argv[1] and argv[3], specifically). So that just leaves dealing with the operator. Recall from the shorts on arrays and strings that a string in C is really just an array of characters. And we can access individual elements of an array by using square bracket notation to index into that array.

string s = "Calculator";
printf("%c\n", s[0]); // prints 'C'

Similarly, if we have another string which just so happens to be called argv[2] can we index into its first element, which will be a single character (char).

printf("%c\n", argv[2][0]); // prints the first character of argv[2]

And that also means we can compare argv[2][0] against a variety of possible values (such as +, -, x, / or %, for example) and make certain decisions in our program based on what that character is, perhaps by making use of some Boolean expressions and conditional statements. (Of course, since there are only a small number of characters that we care about in argv[2][0], you might also find this a good opportunity to use a switch statement for perhaps the first time.)

Note above that we suggest using a lowercase x instead of the typical asterisk used to represent multiplication. The reason for that is that the asterisk means something special at the command line, and so ordinarily it will not be processed correctly. So just be sure when you encounter an x at the command line that you actually perform a multiplication!

Once you’ve performed the arithmetic, just print out the result to the user on its own line, so we can automate testing of your code. By default, C will print out floating point values to six decimal places. Might as well leave it that way, there’s plenty to do otherwise in this problem!

The Mod Squad

If you’re reading this section after you’ve already tried to implement modulo (%) in your calculator, you’ve likely noticed an error when compiling that looks something like the following:

error: invalid operands to binary expression ('float' and 'float')

Why are you seeing this? Well as it turns out, the modulo operator is not well-defined for floating point numbers. That is to say, there’s no defined value for an expression like:

10.7 % 3.28

Rather, it turns out that modulo is only defined (in C, anyway) for integers. How, then, can we implement the operator while still allowing the user to input floating point values at the command line? Seems like we’re going to have to do some extra work. After all, if modulo is really just the remainder after dividing the number on the left of the operator by the number on the right, a quick long division[1] will tell us that 10.7 % 3.28 should equal 0.86, the remainder after calculating 10.7 / 3.28.

That leads to a discussion of today’s Arithmetic Fun Fact™[2]. If

x % y = z

then that means that

x / y = q rem z

or put differently

q = (int) (x / y);
z = x - (y * q);

Perhaps best to illustrate this with an example, as the formulas are perhaps a bit on the intimidating side. Let’s return to our prior example of calculating 10.7 % 3.28.

q = (int) (10.7 / 3.28);
q = (int) 3.262195;
q = 3
mod = 10.7 - (3.28 * 3);
mod = 10.7 - 9.84
mod = 0.86

So that is one way to implement the modulo operator by using other operators that C has defined for floats. To be sure, there are others, but this one should do the trick!

Subtract the Confusion

To be clear, you may make the following assumptions with respect to your calculator.

  • argv[1], should it be present, is guaranteed to consist only of digit characters, possibly a decimal point, and possibly a negative sign. You do not need to check otherwise.

  • argv[2], should it be present, is not guaranteed to be +, -, x, / or %. Your program should respond somehow if the user provides an invalid operator and return 1;.

  • argv[3], should it be present, is guaranteed to consist only of digit characters, possibly a decimal point, and possibly a negative sign. You do not need to check otherwise.

  • You needn’t worry about floating-point imprecision with your calculator, and you needn’t print out the correct answer to more than six decimal places.

Testing

Correctness

If you’d like to check the correctness of your program with check50, you may execute the below.

check50 cs50/problems/2019/ap/calc

Style

style50 calc.c

Staff Solution

If you’d like to play with the staff’s own implementation of calc, you may execute the below.

~cs50/2019/ap/chapter2/calc

How to Submit

Step 1 of 2

  • Be sure that calc.c is in ~/chapter2/calc/, as with:

    cd ~/chapter2/calc/
    ls

Step 2 of 2

  • To submit calc, execute

    cd ~/chapter2/calc/
    submit50 cs50/problems/2019/ap/calc

    inputting your GitHub username and GitHub password as prompted.

If you run into any trouble, email sysadmins@cs50.harvard.edu!

You may resubmit any problem as many times as you’d like.

Your submission should be graded for correctness within 2 minutes, at which point your score will appear at submit.cs50.io!

This was Calc.


1. Bet you didn’t think long division would come in handy again! And frankly, it probably didn’t. Likely, you just used Google. That’s thinking with portals!
2. We don’t actually hold a trademark on this term.