int main() { float voltage; int i; char conn; char component; int count; float *componentsresistance; float totalresistance; float current; float e12[3]; int e12count; printf( "Ange spänningskälla[V]: " ); scanf( "%f", &voltage ); getchar(); printf( "Ange komponenttyp: " ); scanf( "%c", &component ); getchar(); printf( "Ange koppling (s för serie- och p för parallellkoppling): " ); scanf( "%c", &conn ); printf( "Ange antalet komponenter: " ); scanf( "%d", &count ); componentsresistance = (float*) malloc(count*sizeof(float)); for (i = 0; i < count; i++) { printf( "Komponent %d[ohm]: ", i ); scanf("%f", &componentsresistance[i]); } totalresistance = calc_resistans(count, conn, componentsresistance); printf("\nErsättningsresistans: %g ohm", totalresistance); /* LibPower */ if(component == 'R' || component == 'r') { current = voltage/totalresistance; if(calc_power_r(voltage, totalresistance) == calc_power_i(voltage, current)) printf("\nEffekt: %5.2f W", calc_power_i(voltage, current)); } /* LibComponent */ e12count = e_resistance(totalresistance, e12); printf("\nErsättningsresistanser i E12-serien kopplade i serie: "); for (i = 0; i < e12count; i++) { printf("%g, ", e12[i]); } printf("\n"); return 1; }
void libcomponent_tests(void) { CU_ASSERT_EQUAL(e_resistance(0, NULL), 0); CU_ASSERT_EQUAL(e_resistance(1398, NULL), 3); float E12_values_test1[MAX_CALCULATED_E12_RESISTORS] = {1200.0, 180.0, 18.0}; libcomponent_test(1398.0, E12_values_test1, 3); float E12_values_test2[MAX_CALCULATED_E12_RESISTORS] = {1200.0, 0, 0}; libcomponent_test(1200.0, E12_values_test2, 1); float E12_values_test3[MAX_CALCULATED_E12_RESISTORS] = {1000.0, 0, 0}; libcomponent_test(1000.0, E12_values_test3, 1); float E12_values_test4[MAX_CALCULATED_E12_RESISTORS] = {120.0, 18.0, 1.8}; libcomponent_test(139.80, E12_values_test4, 3); float E12_values_test5[MAX_CALCULATED_E12_RESISTORS] = {12.0, 1.80, 0}; libcomponent_test(13.980, E12_values_test5, 2); float E12_values_test6[MAX_CALCULATED_E12_RESISTORS] = {1200.0, 270.0, 27.0}; libcomponent_test(1498.0, E12_values_test6, 3); }
float *res_array int main() { printf("Ersättningsresistansen [Ohm] : "); if (scanf("%f", &totRes)!=1) { printf ("Error - Inmatat värde är inte en integer\n"); return -1; } printf("Ersättningsresistanser i E12-serien kopplade i serie: "); for (antal = e_resistance(totRes, &resistors[0]); antal>0; antal--){ /* avrundar till närmaste E12 och skriver ut utan decimal*/ printf("%1.0f", *val_pekare++); /* behövs fler ersättningsresistanser? */ if (antal>1) printf(", "); } printf("\n"); }
/* * Simple test program for component library */ int main(int argc, char**argv){ float *p_array; p_array = (float *)malloc(sizeof(float) *3); if(argc > 2){ /* simple error test */ printf("Usage: test x, where x is a number from the E12 resistor series\n"); } else { int x = e_resistance(atof(argv[1]), p_array); /* calculate resistors */ printf("no of resitors: %d values = %f %f %f \n", x, p_array[0], p_array[1], p_array[2]); free(p_array); } return 0; }
/** * @brief Event method that is fired when it's time to calculate the output of the input * * Event method that fires when the button is clicked. This calculates the result of the electrolib functionality. * @return void **/ static void calculateAndPresentOutput(){ int RESISTANCEOUTPUTLABELROW = 6; int VOLTAGEOUTPUTLABELROW = 7; int E12OUTPUTLABELROW = 8; float* replaceResistanceValues = calloc(3,sizeof(float)); float totalresistance = calc_resistance(getNumberOfComponents(), getConnectionType(), getResistanceItems()); float totalpower = calc_power_r(getVoltage(), totalresistance); int numberOfResistors = e_resistance(totalresistance, replaceResistanceValues); char formatedMessage[100]; sprintf(formatedMessage, "Ersättningsresistance: %.1f ohm", totalresistance); addOutputLabel(_mainGrid, formatedMessage, 1, RESISTANCEOUTPUTLABELROW, 2); sprintf(formatedMessage, "Effekt: %.2f W", totalpower); addOutputLabel(_mainGrid, formatedMessage, 1, VOLTAGEOUTPUTLABELROW, 2); sprintf(formatedMessage, "Ersättningsresistans i E12-serien koppliade i serie: %.0f, %.0f, %.0f", *replaceResistanceValues, *(replaceResistanceValues+1), *(replaceResistanceValues+2)); addOutputLabel(_mainGrid, formatedMessage, 1, E12OUTPUTLABELROW, 2); }
int main() { int count, i; float totRes; float volt=2.0; float power; float *array; char conn; float resistors[3]; float *fp=&resistors[0]; int antal; printf("Ange koppling[S | P]: "); if (scanf("%c", &conn)!=1) { printf ("Error - Inmatat värde är inte en char\n"); return -1; } printf("Antal komponenter: "); if (scanf("%i", &count)!=1) { printf ("Error - Inmatat värde är inte en integer\n"); return -1; } array = (float*) malloc(sizeof(float) * count); for (i=0; i < count; i++) { printf("Komponent %d i ohm: ",i + 1); if (scanf("%f", &array[i])!=1) { printf ("Error - Inmatat värde är inte en float\n"); return -1; } } // Calculate values totRes = calc_resistance(count,conn,array); if (totRes == -1) {printf ("Error - Misslyckades med att räkna ut ersättningsresistans\n"); return -1;} printf("Ersättningsresistans: %.2f ohm\n",totRes); printf("Ange spänningskälla i V: "); if (scanf("%f", &volt)!=1){ printf("Error - Inmatat värde är inte en float\n"); return -1;} power = calc_power_r(volt, totRes); printf("Effekt: %.2f W\n", power); printf("Ersättningsresistanser i E12-serien kopplade i serie: "); for (antal = e_resistance(totRes, &resistors[0]); antal>0; antal--){ printf("%1.0f", *fp++); if (antal>1) printf(", "); } printf("\n"); free(array); return 0; }
void libcomponent_test(float resistance, float *expected_E12_values, int expected_count) { float actual_E12_values[MAX_CALCULATED_E12_RESISTORS]; float actual_value, expected_value; printf("\nlibcomponent_test::resistance: %.3f\n", resistance); int actual_count = e_resistance(resistance, actual_E12_values); printf("\tactual_count: %d, expected_count: %d\n", actual_count, expected_count); CU_ASSERT_EQUAL(actual_count, expected_count); for (int i = 0; i < expected_count; i++) { expected_value = expected_E12_values[i]; actual_value = actual_E12_values[i]; printf("\tindex: %d, actual value: %.3f, expected value: %.3f\n", i, actual_value, expected_value); CU_ASSERT_DOUBLE_EQUAL(actual_value, expected_value, GRANULARITY); } }