2016-10-16 32 views
-2

在哪裏應該寫一個程序,它在一個數字,在三個部分把它一個作業: 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); 
} 
+0

'(fract,whole)'是雙重的嗎? –

+0

'的scanf( 「%d」,&num);' - >'的scanf( 「%LF」,&num);'和類似'的printf(「DEBUGGING 1(在主):您的數量是%d ...'應該使用類型' %f'。 –

+0

是的,當然......那是做的竅門。謝謝......我有一種感覺,我曾經在某處找到了數據類型的迷宮,並且在我的搜索中搞砸了代碼以修復它。但是,我確實認爲問題出現在函數中的哪個位置,而不是我的scanf,但是這實際上使得很多感覺:) – LotharianBC

回答

1

你宣佈你sorting_sign函數返回一個double,當你返回一個int集到char的價值...你那種類型的。

+0

事實上,對你的類型進行排序 - 對於'scanf'和'printf'錯誤的格式類型 –

+0

謝謝......我搞砸了數據t在我的搜索類型排序全部出來,它真的讓我陷入了一個混亂......感謝提示,你們兩個:)... printf(「輸入你的號碼:」); 012fscanf(「%lf」,&num);並將該函數更改爲int函數。 – LotharianBC

1

已解決! 爲了將來的參考,我在此發佈完整工作程序的代碼:

/* 
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 */ 
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); 
} 
相關問題