bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc098.c (2002B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 08 Feb 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a calculator utility using command line arguments.\nUsage: calc <switch> <n> <m>\nwhere switch is arithmetic operator or comparison operator.
      9 */
     10 /* Let Us C, Chap- 20 (More Issues In Input/Output), Qn No.: A(d) */
     11 
     12 /* This file is auto-generated by a bot. */
     13 /* This code is not compiled; it is for reference only. */
     14 
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <ctype.h>
     20 
     21 int main(int argc, char *argv[])
     22 {
     23     float n, m, res;
     24     char operator;
     25 
     26     if (argc != 4)
     27     {
     28         printf("Usage: %s <switch> <n> <m>\n", argv[0]);
     29         printf("Example: %s + 10 20\n", argv[0]);
     30         printf("Note: For multiplication (*), use '*' or x to avoid shell expansion.\n");
     31         exit(1);
     32     }
     33 
     34     operator = argv[1][0]; // First character of the switch argument
     35     n = atof(argv[2]);
     36     m = atof(argv[3]);
     37 
     38     switch (operator)
     39     {
     40     // Arithmetic
     41     case '+':
     42         printf("%.2f\n", n + m);
     43         break;
     44     case '-':
     45         printf("%.2f\n", n - m);
     46         break;
     47     case 'x': 
     48     case '*':
     49         printf("%.2f\n", n * m);
     50         break;
     51     case '/':
     52         if (m == 0) printf("Error: Division by zero\n");
     53         else printf("%.2f\n", n / m);
     54         break;
     55     case '%':
     56         printf("%d\n", (int)n % (int)m);
     57         break;
     58 
     59     // Comparison
     60     case '<':
     61         printf("%s\n", (n < m) ? "True" : "False");
     62         break;
     63     case '>':
     64         printf("%s\n", (n > m) ? "True" : "False");
     65         break;
     66     
     67     // Handling symbols that might be multi-char (e.g. <=, >=, ==) is tricky 
     68     // with argv[1][0], but basic logic for typical single char switches:
     69     case '=':
     70         printf("%s\n", (n == m) ? "True" : "False");
     71         break;
     72 
     73     default:
     74         printf("Unknown operator: %c\n", operator);
     75         break;
     76     }
     77 
     78     return 0;
     79 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror