在哪裏應該寫一個程序,它在一個數字,在三個部分把它一個作業: 1.檢查如果數字是正數或負數 2.整數(大小) 3.小數部分如何採取一個數字,它分爲三個部分正或負,整數和分數
要求是應該有一個自己的函數稱爲分離,它具有輸入和輸出參數。
例如:如果您在23.639鍵入,程序應該排序並打印出: 星座:+ 整數大小:23個 小數部分:0.639
問題:對於 1.功能在輸入負數時,確定數字是正數還是負數會導致錯誤答案。它也發佈錯誤的字符。我嘗試過不同的數據類型,如int,char和float,但似乎沒有任何工作。關於如何解決這個任何提示都大爲讚賞,因爲我覺得我被我自己的錯誤矇蔽......
2.功能分離整數小數(分數)將不會從減去整數遠小數點,所以我被整數卡住了。任何人都可以在此發現我的錯誤
* UPDATE *
我設法解決手頭的問題,並做編輯我第一次張貼在這個問題代碼的可怕的n00b錯誤。 我現在已經編輯了一遍代碼來保存原始錯誤,因爲我記得最好。正確的代碼作爲下面的答案發布。
很抱歉的新秀錯誤。
/*
Author: Thorbjørn Elvestad
Student ID: *****
E-mail: [email protected]
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);
}
'(fract,whole)'是雙重的嗎? –
'的scanf( 「%d」,&num);' - >'的scanf( 「%LF」,&num);'和類似'的printf(「DEBUGGING 1(在主):您的數量是%d ...'應該使用類型' %f'。 –
是的,當然......那是做的竅門。謝謝......我有一種感覺,我曾經在某處找到了數據類型的迷宮,並且在我的搜索中搞砸了代碼以修復它。但是,我確實認爲問題出現在函數中的哪個位置,而不是我的scanf,但是這實際上使得很多感覺:) – LotharianBC