2016-05-20 23 views
0

非常に紛失して混乱しています。C - getcharを使用してfloat値を読み取り、printfを使用してfloatを出力する

getcharを使用して3.432のような浮動小数点数で読み込む必要があります。次に、printfを使用して小数点以下4桁の精度で浮動小数点数として再度印刷する必要があります。したがって3.432 - > 3.4320および.450 - > 4500、および453 - > 453.0000。

私はgetchar()を使用していましたが、理解していますが、浮動小数点として値を再変換しようとすると、私はちょうど非常に紛失してしまいました。

float num = 0.0; 
char ch; 
while((ch = getchar()) != '\n'){ 
     num = ch - '0'; 
     printf("%.4f", num); 
} 

それは間違っていると、それは出力何なぜ私は知っているが、それは私がこれまで

EDIT持っているものだ。(もう時間)を試験しない

+2

'atof'許可されていませんか? –

+0

'ch'に小数点が含まれている場合、' num'にはどんな値が出現しますか?すなわち 'num = ch - '0';'? –

+0

はいgetchar、atofは使用できません。私は浮動小数点にキャストしようとしていましたが、それぞれの文字は浮動小数点です – Feath

答えて

3

値私はfloatを読み取るためにgetchar関数を使用することができます。それが役に立てば幸い。

#include <stdio.h> 

int main(void) 
{ 
    float num = 0.0; 
    float i = 1.0; 
    char ch; 

    printf("Enter a float number: "); 

    while((ch = getchar()) != '\n') 
    { 
     if (ch == '.') 
     { 
      i = 0.1; 
     } 
     else if ((ch>= '0') && (ch <='9')) 
     { 
      if (i==1) 
      { 
       num *= 10; 
       num += ch - '0'; 
      } 
      else 
      { 
       num += (ch - '0') * i; 
       i /= 10; 
      } 
     } 
    } 

    printf("%.4f\n", num); 

    return 0; 
} 
0

整数を読み取る関数を最初に書き込むと簡単になります。

次に、小数部分を読み取って結果を結合する関数を書くことについて考えることができます。

また、読み込まれた情報を蓄積する必要があります。現時点では、以前に読み取った数字を新しい数字で上書きしています。

3

[OK]を、あなたは最初にあなたが欲しいものを指定する必要があります - あなたは正確にあなたが構築したいものを知っているまで、いつものようにkeybordから遠ざける:

  • 読み取りファイルまたは最初の新しい行の終わりまで
  • 最初の空白文字
  • (高価なオプションではありませんが)末尾の空白文字(オプションですが、高価ではない)を無視する
  • が最初の任意のcharactを拒否
  • 空白を末尾の後に任意の非空白を拒否スキップブランク、数字と(最初のドットまで)ドット
  • プロセス整数部が、以外のER 10によって電流値を乗算し、文字コードマイナス文字「0」
  • 確保するせいぜい1つのドット
  • プロセス小数部を添加私はあなたがどのように示したボーナスとして

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <ctype.h> 
    
    void error(int pos, char c) { 
        fprintf(stderr, "Unwanted character %c at %d\n", c, pos); 
        exit(1); 
    } 
    
    int main() { 
        double f = 0.; 
        int c; 
        bool initial = 1, final=0; 
        int pos = 0; 
        double decimal = 0; 
    
        while (((c = getchar()) != EOF) && (c != '\n')) { 
         pos += 1; 
         if (isspace(c)) { // accept spaces before and after the number 
          if (initial || final) continue; 
          else { 
           final = 1; 
           continue; 
          } 
         } 
         else if (final) { // do not accept anything after a space after the number 
          error(pos, c); 
         } 
         initial = 0;  // at least one non blank char 
         if (c == '.') { 
          if (decimal) { // accept only one decimal dot 
           error(pos, c); 
          } 
          else decimal = 1; 
         } 
         else if (! isdigit(c)) { // only digits 
          error(pos, c); 
         } 
         else if (decimal == 0) { 
          f = f * 10 + c - '0'; // integer part 
         } 
         else { 
          decimal *= .1;  // fractional part 
          f += (c - '0') * decimal; 
         } 
        } 
        printf("%.4f\n", f); 
    
        return 0; 
    } 
    

    :「0」それは、コーディングが簡単で、可能性が明記されていたら、0.1のパワー小数点の位置

を掛けた - 文字を追加することにより、プロセスのエラー条件

0

stdioタスクを解決するを使用して別の可能性は、単純な2段階のプロセスのようになります。

  • 宣言した文字列への入力を読んで、いくつかの多かれ少なかれ洗練されたフールプルーフを使用して
  • 小数点の左辺と右辺の配列メンバーを「解析」し、( '0'のオフセット減算値)に10の累乗を掛けます。

_

#include <stdio.h> 

int main(void) 
{ 
    float power_of_ten, num = 0.; 
    char c, ch[32]; 
    int j, i = 0; 
    int point_pos = -1; //initialize decimal point position 'offscale' 

    while(((c = getchar()) != EOF) && (c != '\n')) //simple fool-proof check 
    if(((c >= '0')&&(c <= '9')) || ((c == '.')&&(point_pos == -1))){ 
     ch[i] = c; 
     if(ch[i] == '.') 
      point_pos = i; 
     i++; 
     } 
    ch[++i] = '\0'; //length of the array 
    //parsing the array 
    if(point_pos >= 0){ //to the right of decimal point 
     power_of_ten = .1; 
     for(j = point_pos + 1; j < i-1; j++){  
      num += (float)(ch[j] - '0')*power_of_ten; 
      power_of_ten *= .1; 
      } 
     } 

    power_of_ten = 1.; //to the left of decimal point 
    if(point_pos == -1)point_pos = i-1; 
    for(j = point_pos - 1; j >= 0 ; j --){ 
     num += (float)(ch[j] - '0')*power_of_ten; 
     power_of_ten *= 10; 
     } 

    printf("%.4f\n", num); 

    return 0; 
} 
0

この情報がお役に立てば幸いです!

#include<stdio.h> 
#include<string.h> 
#include<math.h> 
int findNumOfDigits(int num); 

int main(void) 
{ 
    char c; 
    float f, mod, fractional; 
    char *buff = malloc(10), *fptr; 
    char *str = buff; 
    int digits; 
    printf("Enter any number\n"); 

    c = getchar(); 
    while(c!='\n') 
    { 
     *buff = c; 
     buff = buff+1; 
     c = getchar(); 
    } 
    *buff = '\0'; 

    mod = atoi(str); 
    fptr = strstr(str, "."); 
    if(fptr!=NULL) 
     fptr++; 
    fractional = atoi(fptr); 
    digits = findNumOfDigits(fractional); 
    f = (mod + (fractional/pow(10,digits))); 
    printf("Number converted to float = %f", f); 

    return 0; 
} 

int findNumOfDigits(int num) 
{ 
    int i; 
    for(i = 1; num >= 10; i++) 
    { 
     num = num/10; 
    } 
    return i; 
} 
関連する問題