2012-11-07 20 views
1

我需要一個C程序,它將使用switch語句打印輸入數字的數字。前導零 - 打印數字的單詞數字

例如: - 如果我輸入'001'作爲值,則應打印zero zero one作爲輸出。

我知道如何將數字打印到其他數字的單詞中,即首先反轉數字,然後使用模數運算符提取數字,然後使用切換條件打印單詞。

默認C自動取001作爲1。我怎麼能阻止呢?我也想打印前導零。

+1

這是C還是C#? – kennytm

+0

@KennyTM它的C .. – Nigel

回答

1

要有前導零,你可以接受一個字符串作爲輸入。如果你想,之後,你可以從你的字符串中提取數字atoistrto*

例子:

#include <stdio.h> 
#include <string.h> 

char buf[SIZE]; 
const char *text_number[] = { 
    "zero", "one", "two", "three", "four", "five", "six", 
    "seven", "eight", "nine" 
}; 

if (fgets(buf, sizeof buf, stdin) != NULL) { 
    char *peol = strchr(buf, '\n'); 

    if (peol != NULL) { 
     size_t size = peol - buf; /* assume `peol` is a valid pointer */ 

     for (i = 0; i < size; ++i) { 
      switch (buf[i]) { 
      case '0': 
      case '1': 
      case '2': 
      case '3': 
      case '4': 
      case '5': 
      case '6': 
      case '7': 
      case '8': 
      case '9': 
       putchar(text_number[buf[i]]); 
       break; 
      default: 
       /* treat "not a digit" error */ 
      } 
     } 
     putchar('\n'); 
    } else { 
     /* treat strchr error */ 
    } 
} else { 
    /* treat fgets error */ 
} 
+0

像保存數字到一個字符串?我知道。當輸入數字具有前導零時,是否可以打印零點,如我在示例中所述? – Nigel

+0

然後,你可以穿過你的字符串。如果它是一個數字,您可以打印它。我會舉一個例子。 – md5

+0

請參閱我的編輯。 ;-) – md5

0

掃描輸入像一個字符串,並在打印的同時做到這一點與使用ASCII表示,像這樣(只的想法)字母:

int main (void) 
{ 
    char number[ 30 ]; 
    int i = 0; 

    printf("Enter the number: "); 

    /*You should use fgets here for safe scan*/ 
    scanf("%s", number); 


    while(*(number + i) != '\0'){ 
    switch(*(number + i)){ 
     i++; 

     /* Cause numbers are stored in their ASCII code*/ 
     case 48: /* 0 */ 
     printf('zero'); 
     break; 
     case 49: /* 1 */ 
     printf('one'); 
     break; 
     case 50: /* 2 */ 
     printf('two'); 
     break; 
     case 51: /* 3 */ 
     printf('three'); 
     break; 
     case 52: /* 4 */ 
     printf('four'); 
     break; 
     case 53: /* 5 */ 
     printf('five'); 
     break; 
     case 54: /* 6 */ 
     printf('six'); 
     break; 
     case 55: /* 7 */ 
     printf('seven'); 
     break; 
     case 56: /* 8 */ 
     printf('eight'); 
     break; 
     case 57: /* 9 */ 
      printf('nine'); 
      break; 
     default: 
      /*In case you enter a symbol, or a letter enter something here*/ 
    } 
    return 0;   
} 
+0

0)使用''0'而不是'48'1)添加一些'break;'s 2)也許增加'i'。 3)'number [i]'與你的'*(number + i)'相同' – wildplasser

+0

我只想說一些與你不同的東西,我用十進制Ascii表示,回答與你沒有的東西相同的東西值 –

+0

關鍵是'0'符號也適用於非ascii系統。另外它對人類更具可讀性;甚至超過了'0x30'。注意:在*使用第一個十進制字符之前,您會增加i *。 – wildplasser

0

我注意到,字是需要的。以字符串的形式將字符讀入數字[]並循環,如下面的片段所示。如果需要,請添加錯誤檢測。你真的需要開關嗎?以下使用更少的代碼行。

 
#include "ctype.h"  // isdigit() 

int const *digits[] = { "zero", "one", "two", "three", "four...., ...nine" }; 

char number[10]; 

int n; 
for (n = 0; n < sizeof(number) && isdigit(number[n]); n++) 
{ 
    printf("%s%s", n ? " " : "", digits[number[n] - '0']); 
} 

if (n) 
    printf("\n"); 

免責聲明:未編譯或運行。

+0

也許''(不含分號)和'char const * digits []'。而且,OP似乎用'switch'語句提出了一個解決方案。 – md5

+0

你是對的......昨天寫了太多的shell腳本和perl。 – Gilbert