/* * Author : Amit Dutta * Date : 08 Feb 2026 * Repo : https://github.com/notamitgamer/bsc * License : MIT License (See the LICENSE file for details) */ /* Write a calculator utility using command line arguments.\nUsage: calc \nwhere switch is arithmetic operator or comparison operator. */ /* Let Us C, Chap- 20 (More Issues In Input/Output), Qn No.: A(d) */ /* This file is auto-generated by a bot. */ /* This code is not compiled; it is for reference only. */ #include #include #include #include int main(int argc, char *argv[]) { float n, m, res; char operator; if (argc != 4) { printf("Usage: %s \n", argv[0]); printf("Example: %s + 10 20\n", argv[0]); printf("Note: For multiplication (*), use '*' or x to avoid shell expansion.\n"); exit(1); } operator = argv[1][0]; // First character of the switch argument n = atof(argv[2]); m = atof(argv[3]); switch (operator) { // Arithmetic case '+': printf("%.2f\n", n + m); break; case '-': printf("%.2f\n", n - m); break; case 'x': case '*': printf("%.2f\n", n * m); break; case '/': if (m == 0) printf("Error: Division by zero\n"); else printf("%.2f\n", n / m); break; case '%': printf("%d\n", (int)n % (int)m); break; // Comparison case '<': printf("%s\n", (n < m) ? "True" : "False"); break; case '>': printf("%s\n", (n > m) ? "True" : "False"); break; // Handling symbols that might be multi-char (e.g. <=, >=, ==) is tricky // with argv[1][0], but basic logic for typical single char switches: case '=': printf("%s\n", (n == m) ? "True" : "False"); break; default: printf("Unknown operator: %c\n", operator); break; } return 0; }