简体   繁体   English

如何在C程序中分离这个数字-51.235

[英]How can I separate this number -51.235 in C program

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? 我需要分离文件中的-51.235并将其设置为 - ,51,.235我的问题是当我尝试从文件中读取它并在代码块中打印时我可以把它作为一个int并浮动在同时我可以减去int(51)和浮点数(51.235),我怎样才能将我把它作为一个字符分开? 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?) 并把它交给我:单独的号码为: -510.235000 (?我怎么能消除在最后的3个零)

in the note pad it show: 在记事本中显示:

S -51.235

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 转换为int以获取没有小数的数字
  4. to get decimal : just Substract your original Float by the Int value, result = 0.XXX 得到十进制:只是通过Int值Substract你的原始Float ,结果= 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)));

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;
}

Question : and it give me: the separate number is: -51 , 0.235000 (how can I eliminate the 3 zeros at the end?) 问题:它给了我:单独的数字是:-51,0.235000(我怎么能在最后消除3个零?)

Answer to eliminate the 3 zeros at the end? 答案最后消除3个零?

printf("\n\nthe separate number is: %d , %.3f",y,a); 
#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;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM