如何取一个数并将其分为正数或负数、整数和分数三部分
How to take in a number and divide it into three parts positive or negative, whole number and fractions
在一项学校作业中,我们应该编写一个程序来接收一个数字并将其分为三部分:
1.检查数字是正数还是负数
2.整数(量级)
3.小数部分
要求是应该有一个名为 separate 的函数,它具有输入和输出参数。
例如:如果您输入 23.639,程序应该排序并打印出来:
符号:+
整数震级:23
小数部分:0.639
问题:
1.The 判断数字是正数还是负数的函数在输入负数时给出错误答案。它还发布了错误的字符。我尝试过不同的数据类型,如 int、char 和 float,但 none 似乎有效。非常感谢有关如何解决此问题的任何提示,因为我认为我被自己的错误蒙蔽了双眼...
2.The 将小数与整数(分数)分开的函数不会从小数中减去整数,所以我坚持使用整数。谁能在这里发现我的错误?
* 更新 *
我设法解决了手头的问题,并在编辑我第一次发布在这个问题中的代码时犯了可怕的 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);
}
您已将 sorting_sign
函数声明为 return 和 double
,当您 return 将 int
的值设为char
... 整理您的类型。
已解决!
为了将来参考,我特此 post 完整工作程序的代码:
/*
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);
}
在一项学校作业中,我们应该编写一个程序来接收一个数字并将其分为三部分: 1.检查数字是正数还是负数 2.整数(量级) 3.小数部分
要求是应该有一个名为 separate 的函数,它具有输入和输出参数。
例如:如果您输入 23.639,程序应该排序并打印出来: 符号:+ 整数震级:23 小数部分:0.639
问题: 1.The 判断数字是正数还是负数的函数在输入负数时给出错误答案。它还发布了错误的字符。我尝试过不同的数据类型,如 int、char 和 float,但 none 似乎有效。非常感谢有关如何解决此问题的任何提示,因为我认为我被自己的错误蒙蔽了双眼...
2.The 将小数与整数(分数)分开的函数不会从小数中减去整数,所以我坚持使用整数。谁能在这里发现我的错误?
* 更新 *
我设法解决了手头的问题,并在编辑我第一次发布在这个问题中的代码时犯了可怕的 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);
}
您已将 sorting_sign
函数声明为 return 和 double
,当您 return 将 int
的值设为char
... 整理您的类型。
已解决! 为了将来参考,我特此 post 完整工作程序的代码:
/*
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);
}