{"id":601,"date":"2014-04-01T00:01:34","date_gmt":"2014-04-01T08:01:34","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=601"},"modified":"2014-03-15T08:54:10","modified_gmt":"2014-03-15T15:54:10","slug":"a-simple-calculator","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=601","title":{"rendered":"A Simple Calculator"},"content":{"rendered":"<p>Of course computers can do math! Of course, programming has some math in it! Yet one reason that programmers are not necessarily geniuses at math is that it&#8217;s the computer that does the math, not the programmer. To prove that truth, why not code your very own calculator program?<br \/>\n<!--more--><br \/>\nForget about being all fancy with a display, scrolling input list, or other eloquent doodads. The most basic calculator program uses simple equation input: Type the first operand, the symbol, and then the second operand. It looks something like this:<\/p>\n<pre><code>Enter math equation:\r\n value [+-*\/] value : <span style=\"color:green\">2 * 16<\/span><\/code><\/pre>\n<p>Then the program then makes the calculation and display the results, something like this:<\/p>\n<p><code>2.00 * 16.00 = 32.00<\/code><\/p>\n<p>The input can be handled by a single <em>scanf()<\/em> function.<\/p>\n<p>Here&#8217;s the code snippet:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    float first,second;\r\n    char symbol;\r\n\r\n    puts(\"Enter math equation:\");\r\n    printf(\" value [+-*\/] value : \");\r\n    scanf(\"%f %c %f\",&first,&symbol,&second);\r\n\r\n\/* Perform Calculations Here *\/\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Your job is to write the meat of the program, the part that takes variables first and second and performs some type of arithmetic wizardry on both. How you do that exactly is this month&#8217;s exercise.<\/p>\n<p>Keep in mind you need to perform only the four basic operations: addition, subtraction, multiplication, and division.<\/p>\n<p>The <em>scanf()<\/em> function helps you by limiting input to only the three types of variables listd: <em>float<\/em>, <em>char<\/em>, and <em>float<\/em>. Even so, your code also needs to handle what happens when an illegal operator (<em>char<\/em>) is input. That shouldn&#8217;t be too terrible.<\/p>\n<p>As usual, please avoid peeking at my solution until you&#8217;ve given this one a stab. And keep in mind that you have multiple ways of solving this puzzle, some of which are better than others but nothing in C is considered the absolute perfect answer.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/04\/04exercise.c\">Exercise Solution<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most basic programs you can build is the calculator. If you haven&#8217;t yet coded your first text-mode calculator, here&#8217;s your chance. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=601\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-601","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=601"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/601\/revisions"}],"predecessor-version":[{"id":625,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/601\/revisions\/625"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}