Как решить проблему с плавающей точкой в ​​программировании на С? - PullRequest
0 голосов
/ 29 октября 2018

Когда я компилирую свою программу c, float дает мне одно число, а не группу нулей после него для каждой краски. Например: 1.00000000 2.00000000 2,00000000. когда он на самом деле должен дать мне 1,64 2,25 и 2,57, когда я наберу цифры 20 40 9 для размеров и 4 и 8 для количества дверей и окон.

Что мне нужно изменить в третьей функции, чтобы исправить это?

#include <stdio.h>
#include <stdlib.h>
#define AREA_OF_DOOR 21
#define AREA_OF_WINDOW 12
#define SQUARE_FEET_OF_SPLASH_OF_COLOR 550 
#define SQUARE_FEET_OF_CHANGING_THE_MOOD 400
#define SQUARE_FEET_OF_COVER_ALL_SINS 350

void display_room_dimension_user_prompt();
void display_doors_windows_user_prompt();
void display_gallons_needed(int length, int width, int height, int doors, int windows);

int main()
{
    int length; 
    int width;
    int height;
    int doors; 
    int windows;
    
    display_room_dimension_user_prompt();
    scanf("%d %d %d", &length, &width, &height);
    
    display_doors_windows_user_prompt();
    scanf("%d %d", &doors, &windows);
    
    display_gallons_needed(length, width, height, doors, windows);
    
    printf("\nProgram Terminated");
}

void display_room_dimension_user_prompt()
{ 
    int length, width, height;
    printf("Enter the the length, width, and height(in this order).");
    printf("\n(For example: 20 40 9)");
    printf("\n--> ");
    
}

void display_doors_windows_user_prompt()
{
    int doors, windows;
    printf("\nEnter the number of standard doors and windows (in this order).");
    printf("\n(For example: 4 8)");
    printf("\n--> ");        
}

void display_gallons_needed(int length, int width, int height, int doors, int windows)
{
    int SQFT = 2 * height * width + 2 * height * length - doors * AREA_OF_DOOR - windows * AREA_OF_WINDOW;
    float dec_splash_of_color, dec_changing_the_mood, dec_cover_all_sins;
    
    printf("\n%d feet long",length);
    printf("\n%d feet wide and", width);
    printf("\n%d feet high with", height);
    printf("\n%d standard doors and", doors);
    printf("\n%d standard windows", windows);
    
    printf("\nresults in %d sq.ft of wall to be covered.", SQFT);
    
    dec_splash_of_color = SQFT / SQUARE_FEET_OF_SPLASH_OF_COLOR;
    dec_changing_the_mood = SQFT / SQUARE_FEET_OF_CHANGING_THE_MOOD;
    dec_cover_all_sins = SQFT / SQUARE_FEET_OF_COVER_ALL_SINS;
    
    printf("\n%f gallons of 'splash-of-color' type paint or", dec_splash_of_color);
    printf("\n%f gallons of 'change-the-mood' type paint", dec_changing_the_mood);
    printf("\n%f gallons of 'cover- all- sins' type paint", dec_cover_all_sins);
    printf("\nare required to paint the room.");      
}

1 Ответ

0 голосов
/ 29 октября 2018

возможно, это ваши типы в сочетании с тем, как вы выводите ... для 'всплеска цвета', попробуйте:

#define SQUARE_FEET_OF_SPLASH_OF_COLOR 550.0  //note the decimal 

...

float SQFT = 2 * height * width + 2 * height * length - doors * AREA_OF_DOOR - windows * AREA_OF_WINDOW;  //change to float

...

dec_splash_of_color = SQFT / SQUARE_FEET_OF_SPLASH_OF_COLOR;//leave as is

...

printf("\n%.2f gallons of 'splash-of-color' type paint or", dec_splash_of_color);//note the change in "%.2f" (2 decimals)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...