0

I need to separate -51.235 which is in a file and put it as - , 51 , .235 my problem is that when I try to read it form the file and print it in code block i can put it as an int and float at the same time so i can subtract the int (51) and the float (51.235) and how can I separate the sign I put it as a character? this what i have so far

if (comand == 'S')
{
  fscanf(entrada, "%d %f",&y, &a);
  printf("\n\nthe separate number is: %d , %f",y,a); 
}

and it give me: the separate number is: -51 , 0.235000 (how can I eliminate the 3 zeros at the end?)

in the note pad it show:

S -51.235
Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • Do you need the parts of the string to be typed? Would just reading the line in as a char[] and looping looking for the negation symbol and the decimal point? – Colin D Jul 13 '12 at 15:22
  • http://stackoverflow.com/questions/277772/avoid-trailing-zeroes-in-printf – Colin D Jul 13 '12 at 15:23
  • Use a precision in the `printf` format, `%.3f` prints three places after the decimal point. – Daniel Fischer Jul 13 '12 at 15:23
  • Being that this is homework I cannot give you the answer but I will push you in the right direction. You want to Look at the String data type which will provide methods for what you are looking to accomplish. – Sorceri Jul 13 '12 at 15:23
  • the only thing is giving me a hard time is that I need to separate the number that the user will input in the file (notepad) because if I just need to write it and setting without reading the file it will be much easier because I will put the number as an int and float and then subtract it and it will give me the decimal part and the rest of the number like – Jennifer Ramirez Jul 13 '12 at 16:06
  • int y; float x; float z; y=x; z=(x-y) – Jennifer Ramirez Jul 13 '12 at 16:07

4 Answers4

1

Only few step :

  1. Check if Positive, if YES : put your -
  2. Your number = Absolute of your number (remove the - if positive, nothing if not).
  3. Convert To int to get your number without decimal
  4. to get decimal : just Substract your original Float by the Int value, result = 0.XXX

All of this in one line :

float num = -51.235555;
printf("%c %d %.3f", ((num > 0.0 ) ? ' ' : '-'), (int)num, (num - (float)((int)num)));
Damien Locque
  • 1,810
  • 2
  • 20
  • 42
1

you can do it as follows:

#include<stdio.h>
#include<math.h>

int main(void)
{
  float num = -51.235;
  char sign;
  int intpart;
  float floatpart;

  sign=(num>=0.00f)?'+':'-';
  intpart=floor(fabs(num));
  floatpart=fabs(num)-intpart;
  printf("%c,%d,%g",sign,intpart,floatpart);
  return 0;
}
phoxis
  • 60,131
  • 14
  • 81
  • 117
fuzzy
  • 229
  • 1
  • 4
  • 15
0

Question : and it give me: the separate number is: -51 , 0.235000 (how can I eliminate the 3 zeros at the end?)

Answer to eliminate the 3 zeros at the end?

printf("\n\nthe separate number is: %d , %.3f",y,a); 
KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
  • I can declare how many decimal it will be because if I change in the file the number for 30.35355 it have to separate everything so it have to be something for a random number that the user will input in the notepad and the program will separate. – Jennifer Ramirez Jul 13 '12 at 15:36
  • you can do `printf (".*f\n", 3, a);` to print only 3 bytes to the right of the decimal point, changing the `3` changes this value - you can find the length of the entire number, subtract the length of the whole number part, plus the sign, plus the decimal point, this gives you the length of the decimal part of the number. Insert that value (yes, this is hard, but it's fun!) – KevinDTimm Jul 13 '12 at 15:45
0
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char getChar(char **p){
    return *(*p)++;
}

void skipSpace(char **p){
    while(isspace(getChar(p)));//skip space char
    --(*p);//unget
}

char getSign(char **p){
    if(**p=='+' || **p=='-') return *(*p)++;
    return ' ';
}

void getNatural(char **p, char *buff){
    while(isdigit(**p))
        *buff++=*(*p)++;
    *buff='\0';
}

void getFloatPart(char **p, char *buff){
    char point;
    point = getChar(p);
    if(point != '.'){
        *buff = '\0';
    } else {
        *buff++ = point;
        getNatural(p, buff);
    }
}

void separate_float(char **p, char *sign, char *int_part, char *float_part){
    skipSpace(p);
    *sign = getSign(p);
    getNatural(p, int_part);
    getFloatPart(p, float_part);
}

int main(){
    char line[128]="S -51.235";
    char *p = line;
    char command;
    char sign, int_part[32], float_part[32];

    command = getChar(&p);
    if(command == 'S'){
        separate_float(&p, &sign, int_part, float_part);
        printf("%c,%s,%s\n", sign, int_part, float_part);//-,51,.235
        printf("%d %g\n", atoi(int_part), atof(float_part));//51 0.235
    }
    return 0;
} 
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70