如何接受一个数字并将其分为正面或负面,整数和分数三部分

时间:2016-10-16 13:57:14

标签: c sorting modular

在学校作业中,我们应该编写一个程序,该程序接受一个数字并将其分为三部分: 1.检查数字是正数还是负数 2.整数(幅度) 3.分数部分

要求是应该有一个名为separate的自己的函数,它具有输入和输出参数。

例如:如果您输入23.639,程序应排序并打印出来: 签名:+ 整数:23 分数部分:0.639

问题: 1.在输入负数时,排序数字是正数还是负数的功能会给出错误的答案。它也会发布错误的字符。我尝试过不同的数据类型,比如int,char和float,但似乎都没有。关于如何解决这个问题的任何提示都非常感谢,因为我认为我被自己的错误蒙蔽了......

2.将小数与整数(分数)分开的函数不会从小数中减去整数,所以我被整数所困。谁能在这里发现我的错误?

*更新*

我设法解决了手头的问题,并且在编辑我在这个问题中首次发布的代码时遇到了可怕的n00b错误。 我现在再次编辑代码以尽可能地记住原始错误。正确的代码将在下面发布。

抱歉新手错误。

/*
Author: Thorbjørn Elvestad
Student ID: *****
E-mail: drommevandrer@gmail.com


This program take in number typed in by the user, and then divide it into three parts.

SIGN: '+' or '-'
Whole number: Show number as a whole number
Fraction: Show fractions 

The program uses function to sort out the number, and print out the result*/

/* Declaring libraries */
#include <stdio.h>
#include <stdlib.h>


/* Declaring functions */
double sorting_sign(char x);
double sorting_whole(double x);
double sorting_fract(double x, int y);



/* Calling main function */
int main()
{
    double num, fractures; /* declaring variables */
    int sign_sorted, part;
    double whole_sorted;

    printf("LET ME TELL YOU SOME INTERESTING STUF ABOUT YOUR NUMBER!\n\n");
    printf("Enter your number: ");
    scanf("%d", &num);

    sign_sorted = sorting_sign(num); /* Calling the function that sorts out if this number is '+' or '-' */
    whole_sorted = sorting_whole(num); /* Calling the function separating whole number from decimals */
    fractures = sorting_fract(num, num); /* Calling the function removing the whole number from the fractures */

    printf("Sign: %c\nWhole: %0.lf\nFraction: %f", sign_sorted, whole_sorted, fractures);

    return 0;
}


/* Function for sorting of if number is '+' or '-' */
double sorting_sign(char x)
{
    int sign;

    /* true if number is less than 0 */
    if(x < 0.0){sign = '-';}

    /* true if number is greater than 0 */
    else if(x > 0.0){sign = '+';}

    return (sign);
}


/* Function for sorting out the whole number */
double sorting_whole (double x)
{
    int whole;

    whole = x;

    return (whole);
}

/* Function for sorting out the fractions */
double sorting_fract(double x)
{
    int whole;
    double fract;
    whole = y;
    fract = x - whole;


    return (fract, whole);
}

2 个答案:

答案 0 :(得分:1)

当您将sorting_sign设置为double的值时,您已宣布自己的int函数返回char。 ..排序你的类型。

答案 1 :(得分:1)

解决了! 为了将来参考,我特此发布完整工作程序的代码:

/*
Author: Thorbjørn Elvestad
Student ID: *****
E-mail: drommevandrer@gmail.com


This program take in number typed in by the user, and then divide it into three parts.

SIGN: '+' or '-'
Whole number: Show number as a whole number
Fraction: Show fractions 

The program uses function to sort out the number, and print out the result*/

/* Declaring libraries */
#include <stdio.h>
#include <stdlib.h>


/* Declaring functions */
int sorting_sign(int x);
double sorting_whole(double x);
double sorting_fract(double x);



/* Calling main function */
int main()
{
double num, fractures; /* declaring variables */
int sign_sorted, part;
double whole_sorted;

printf("LET ME TELL YOU SOME INTERESTING STUF ABOUT YOUR NUMBER!\n\n");
printf("Enter your number: ");
scanf("%lf", &num);

sign_sorted = sorting_sign(num); /* Calling the function that sorts out if this number is '+' or '-' */
whole_sorted = sorting_whole(num); /* Calling the function separating whole number from decimals */
fractures = sorting_fract(num); /* Calling the function removing the whole number from the fractures */

printf("Sign: %c\nWhole: %0.lf\nFraction: %f", sign_sorted, whole_sorted, fractures);

return 0;
}


/* Function for sorting of if number is '+' or '-' */
int sorting_sign(int x)
{
int sign;

/* true if number is less than 0 */
if(x < 0.0){sign = '-';}

/* true if number is greater than 0 */
else if(x > 0.0){sign = '+';}

return (sign);
}


/* Function for sorting out the whole number */
double sorting_whole (double x)
{
int whole;

whole = x;

return (whole);
}

/* Function for sorting out the fractions */
double sorting_fract(double x)
{
int whole;
double fract;
whole = (int)x;
fract = x - whole;


return (fract);
}