Zamiana cyfr na słowne wartości – dodanie ignorowania spacji i myślników

0

Witam,
Mam program, który zamienia cyfry na słowne wartości.. Potrzebuję by program jeszcze ignorował spacje i myślniki...

int main()
  {
   static const char *tab[100]={"zer","jed", "dwa", "trz", "czt", "pie","sze","sie","osi","dzi"};
   const char *tab2[10];
   unsigned i=0,koncowka,liczba; // aby nie bawić się w ujemne
 
   printf("Podaj liczbe do zamiany na postac  slowna: ");
   scanf("%u",&liczba);
   do
     {
      koncowka=liczba%10;
      liczba/=10;
      tab2[i++]=tab[koncowka];
     }
   while(liczba>0);
   printf("Slownie:");
   while(i) printf(" %s",tab2[--i]);
   printf("\n");
 
   fflush(stdin);
   getchar();
   return 0;    
  }

Proszę o pomoc!

2

czemu w tagach jest c++ skoro to jest napisane w C?

w ktorym jezyku chcesz miec to napisane?

0
// Note: This function returns a pointer to a substring of the original string.
// If the given string was allocated dynamically, the caller must not overwrite
// that pointer with the returned value, since the original pointer must be
// deallocated using the same allocator with which it was allocated.  The return
// value must NOT be deallocated using free() etc.
char *trimwhitespace(char *str)
{
  char *end;

  // Trim leading space
  while(isspace((unsigned char)*str)) str++;

  if(*str == 0)  // All spaces?
    return str;

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace((unsigned char)*end)) end--;

  // Write new null terminator character
  end[1] = '\0';

  return str;
}

uzyj tej funkcji do czyszczenia z bialych znakow. Pamietaj, ze ta funkcja zmienia parametr wejsciowy (nie pracujesz na kopii)

w c++ mialbys cos takiego

#include <iostream>
#include <string>

std::string trim(const std::string& str,
                 const std::string& whitespace = " \t")
{
    const auto strBegin = str.find_first_not_of(whitespace);
    if (strBegin == std::string::npos)
        return ""; // no content

    const auto strEnd = str.find_last_not_of(whitespace);
    const auto strRange = strEnd - strBegin + 1;

    return str.substr(strBegin, strRange);
}

std::string reduce(const std::string& str,
                   const std::string& fill = " ",
                   const std::string& whitespace = " \t")
{
    // trim first
    auto result = trim(str, whitespace);

    // replace sub ranges
    auto beginSpace = result.find_first_of(whitespace);
    while (beginSpace != std::string::npos)
    {
        const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
        const auto range = endSpace - beginSpace;

        result.replace(beginSpace, range, fill);

        const auto newStart = beginSpace + fill.length();
        beginSpace = result.find_first_of(whitespace, newStart);
    }

    return result;
}

int main()
{
    const std::string foo = "    too much\t   \tspace\t\t\t  ";
    const std::string bar = "one\ntwo";

    std::cout << "[" << trim(foo) << "]" << std::endl;
    std::cout << "[" << reduce(foo) << "]" << std::endl;
    std::cout << "[" << reduce(foo, "-") << "]" << std::endl;

    std::cout << "[" << trim(bar) << "]" << std::endl;
}

i teraz powiedz mi czy to sa dwa podobne jezyki...

0

To musisz wczytać liczbę jako ciąg potem odpowiednio przerobić owy ciąg ( usuwając wszystkie znaki prócz cyfr ) i zrobić konwersję z char na int'a.

0

Masz tu mój kod sprzed chyba 20 laty... :)

char *j[] = {"","jeden","dwa","trzy","cztery","pięć","sześć","siedem","osiem","dziewięć"},
     *jn[]= {"dziesięć","jede","dwa","trzy","czter","pięt","szes","siedem","osiem","dziewięt"},
     *Rz[]= {"bilion","miliard","milion","tysi", "złot", "grosz"};

void s3n(char *s, int a, int b, int c)
{
     for(int p = 0; p < 3; p++)
      {
         if( p == 0 )
          {
            if( a > 2 ) { strcat(s, j[a]); strcat(s,(a < 5 ? "sta " : "set ")); }
            else if( a > 0 ) strcat(s, a == 1 ? "sto " : "dwieście ");
          }
         else if( p == 1 )
          {
            if( b == 1 ) { strcat(s, jn[c]); strcat(s, (c ? "naście " : " ")); break; }
            else if( b ) { strcat(s, (b==4?jn:j)[b]); strcat(s, b==2 ? "dzieścia " : b<5?"dzieści ": "dziesiąt "); }
          }
         else if( c ) { strcat(s, j[c]); strcat(s, " "); }
      }
}

void slownie(char *s, double kw)
{
  char str[48];
//  s[0] = 0;

  int i = sprintf(str, "%18.2f", kw);
  if( i > 18 || i < 0 ) return;

  for(i = 0; i < 15 && str[i] == ' '; i++) str[i] = '0';
  if( str[i] == '-' ) str[i] = '0';

  for(int n = 0, k = 0; k < 5+1; k++)
   {
      i = k*3;
      int a = str[i]-'0', b = str[i+1]-'0', c = str[i+2]-'0';
      if( k == 5 )
       {
         a = 0;
         if( n && (b|c) ) strcat(s, " i ");
       }

      if( a|b|c || (n && k == 4) )  // 1'000 -> jeden tysiąc złotych (i a|b|c = 0 !)
       {
          n++;
          s3n(s, a,b,c);

          strcat(s, Rz[k]);
          a = (c == 1 && !(a|b));
          b = (c > 1 && c < 5 && b != 1);
          if( k == 5 )      strcat(s,(b ? "e"    : (a ? ""    : "y"   )));
          else if( k == 4 ) strcat(s,(b ? "e"    : (a ? "y"   : "ych" )));
          else if( k == 3 ) strcat(s,(b ? "ące " : (a ? "ąc " : "ęcy ")));
          else              strcat(s,(b ? "y "   : (a ? " "   : "ów " )));
       }
   }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1