init
This commit is contained in:
commit
3738c83af8
19 files changed
+333
No files matched your search
@@ -0,0 +1,2 @@
|
||||
all: powerCalc.c
|
||||
gcc -o powerCalc -g -ggdb -O0 powerCalc.c
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Author: Simon Schurti
|
||||
*
|
||||
* Licence: MIT
|
||||
*
|
||||
* Informatik 1 exercice:
|
||||
* Takes user input for Voltage and Current in V/A respectively.
|
||||
* calculates and prints Power and Resistance
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
int main() {
|
||||
double u, i, p, r; // Voltage , current, power, resistance
|
||||
while (1) {
|
||||
printf("To Terminate enter anything that is not a valid float.\n\n");
|
||||
|
||||
// fscanf returns bytes scanned, so if ret is 0 no float was detected
|
||||
// fscanf only matches %lf, so "123 asdlkj" would be valid input according
|
||||
// to format specified in fscanf. getc pops next byte from stdin, which is
|
||||
// always \n if user input is valid
|
||||
printf("Voltage[V]: ");
|
||||
int ret = fscanf(stdin, "%lf", &u); // read voltage from user
|
||||
if (ret <= 0 || getc(stdin) != '\n')
|
||||
break; // terminate if user input is invalid
|
||||
printf("Current[A]: ");
|
||||
ret = fscanf(stdin, "%lf", &i); // read current form user
|
||||
if (ret <= 0 || getc(stdin) != '\n')
|
||||
break; // termainte if user input is invalnid
|
||||
|
||||
// power [W] = current [A] * voltage[V]
|
||||
p = i * u;
|
||||
|
||||
// resistance[Ohm] = voltage[V] / current[A]
|
||||
if (i != 0) {
|
||||
r = u / i;
|
||||
printf("Resistance[Ohm]: %.3lf\n", r);
|
||||
printf("Power[W]: %.3lf\n", p);
|
||||
printf("Resistance %.3lf is %s than Power %.3lf\n", r,
|
||||
(r > p) ? "greater" : "smaller", p);
|
||||
} else {
|
||||
printf("No current is flowing. Resistance cannot be "
|
||||
"calculated\nPower: 0 W\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user