r/Coding_for_Teens • u/TrueWar515 • 4h ago
Hmm check out my C program
I'm not new to C programming but it's been a while since ive done a calculator so I fixed up this random ahh calculator program. feel free to add on to it.
clang calculator.c -o calc -lm is the cmd i used since clang did not want to link to my math.h
```#include <stdio.h>
include <math.h>
include <string.h>
void help(){ printf("\nHelp\nProgram init\n"); printf("--help: Enter this help screen:\n\n"); printf("addition: a + b, adding the sum of both numbers.\n"); printf("subtraction: a - b, taking the total of the second number and deducting it from the first number\n"); printf("multiplication: a * b, taking the value of a and adding itself depending on the times b says\n"); printf("division: a / b, how many of a can fit into b\n"); printf("power's: a ^ b, a times itself based on the amount of times b says\n"); printf("square roots: a ! \"DUMMY\", weird choice but its what times itself = a \n"); printf("Dont divide by 0 thats dumb, but i give you full access to do it?\n"); }
int main(int count, char* input[]){ long double a; long double b;
char op[2];
// printf("==========Calculator==========="); if(count == 2){ if(strcmp(input[1], "--help") == 0) { help(); return 0; } else{ printf("unknown input: %s", input[1]); return 1; } }
if(count < 0) printf("HOW IS THAT POSSIBLE!!!");
printf("Calculator program, type help if you need any instructions\n\n");
while(1){
printf("~> ");
scanf(" %Lf %s %Lf", &a, op, &b); //welp ill deal with operator precendence later.
if(strcmp(op, "+") == 0){ printf(" = %Lf\n", a + b);}
if(strcmp(op, "-") == 0){ printf(" = %Lf\n", a - b);}
if(strcmp(op, "*") == 0 || strcmp(op, "x") == 0){ printf(" = %Lf\n", a * b);}
if(strcmp(op, "/") == 0){ printf(" = %Lf\n", a / b);}
if(strcmp(op, "^") == 0){ printf(" = %Lf\n", powl(a, b));}
if(strcmp(op, "!") == 0){ printf(" = %Lf\n", sqrtl(a));}
}
}
// 50 ish lines of code with this comment <3 ```