Code đọc số sang chữ trong lập trình C#

Using System; using System.Collections.Generic; using System.Text; namespace VR { class Class1 { private string[] strSo ={ "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" }; private string[] strDonViNho ={ "linh", "lăm", "mười", "mươi", "mốt", "trăm" }; private string[] strDonViLon ={ "", "ngàn", "triệu", "tỷ" }; private string[] strMainGroup; private string[] strSubGroup; private string Len1(string strA) { return strSo[int.Parse(strA)]; } private string Len2(string strA) { if (strA.Substring(0, 1) == "0") { return strDonViNho[0] + " " + Len1(strA.Substring(1, 1)); } else if (strA.Substring(0, 1) == "1") { if (strA.Substring(1, 1) == "5") { return strDonViNho[2] + " " + strDonViNho[1]; } else if (strA.Substring(1, 1) == "0") { return strDonViNho[2]; } else { return strDonViNho[2] + " " + Len1(strA.Substring(1, 1)); } } else { if (strA.Substring(1, 1) == "5") { return Len1(strA.Substring(0, 1)) + " " + strDonViNho[3] + " " + strDonViNho[1]; }

doc6 trang | Chia sẻ: tlsuongmuoi | Lượt xem: 2076 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Code đọc số sang chữ trong lập trình C#, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Code đọc số sang chữ trong lập trình C#? Chào mọi người! em đang học C#, em có tìm được đoạn code đọc số sang chữ, nhưng đọc vô không hiểu gì hết trơn. Mọi người giúp em giải thích ý nghĩa của từng đoạn code nha, cảm ơn nhiều! PHP Code: using System;  using System.Collections.Generic;  using System.Text;  namespace VR  {      class Class1      {          private string[] strSo ={ "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };          private string[] strDonViNho ={ "linh", "lăm", "mười", "mươi", "mốt", "trăm" };          private string[] strDonViLon ={ "", "ngàn", "triệu", "tỷ" };          private string[] strMainGroup;          private string[] strSubGroup;          private string Len1(string strA)          {              return strSo[int.Parse(strA)];          }          private string Len2(string strA)          {              if (strA.Substring(0, 1) == "0")              {                  return strDonViNho[0] + " " + Len1(strA.Substring(1, 1));              }              else if (strA.Substring(0, 1) == "1")              {                  if (strA.Substring(1, 1) == "5")                  {                      return strDonViNho[2] + " " + strDonViNho[1];                  }                  else if (strA.Substring(1, 1) == "0")                  {                      return strDonViNho[2];                  }                  else                  {                      return strDonViNho[2] + " " + Len1(strA.Substring(1, 1));                  }              }              else              {                  if (strA.Substring(1, 1) == "5")                  {                      return Len1(strA.Substring(0, 1)) + " " + strDonViNho[3] + " " + strDonViNho[1];                  }                  else if (strA.Substring(1, 1) == "0")                  {                      return Len1(strA.Substring(0, 1)) + " " + strDonViNho[3];                  }                  else if (strA.Substring(1, 1) == "1")                  {                      return Len1(strA.Substring(0, 1)) + " " + strDonViNho[3] + " " + strDonViNho[4];                  }                  else                  {                      return Len1(strA.Substring(0, 1)) + " " + strDonViNho[3] + " " + Len1(strA.Substring(1, 1));                  }              }          }          private string Len3(string strA)          {              if ((strA.Substring(0, 3) == "000"))              {                  return null;              }              else if ((strA.Substring(1, 2) == "00"))              {                  return Len1(strA.Substring(0, 1)) + " " + strDonViNho[5];              }              else              {                  return Len1(strA.Substring(0, 1)) + " " + strDonViNho[5] + " " + Len2(strA.Substring(1, strA.Length - 1));              }          }          /////////////////////          private string FullLen(string strSend)          {              bool boKTNull = false;              string strKQ ="";              string strA = strSend.Trim();              int iIndex = strA.Length - 9;              int iPreIndex = 0;              if (strSend.Trim() == "")              {                  return Len1("0");              }              //tra ve khong neu la khong              for (int i = 0; i = 0; i--)              {                  if (iIndex >= 0)                  {                      iPreIndex = iIndex;                      strMainGroup[i] = strA.Substring(iIndex, 9);                      iIndex -= 9;                  }                  else                  {                      strMainGroup[i] = strA.Substring(0, iPreIndex);                  }              }              /////////////////////////////////              //tach moi maingroup thanh nhieu subgroup              //xuoi              for (int j = 0; j = 0; i--)                  {                      if (iIndex >= 0)                      {                          iPreIndex = iIndex;                          strSubGroup[i] = strMainGroup[j].Substring(iIndex, 3);                          iIndex -= 3;                      }                      else                      {                          strSubGroup[i] = strMainGroup[j].Substring(0, iPreIndex);                      }                  }                  //duyet subgroup de lay string                  for (int i = 0; i = 0; i--)                  {                       if(strTmpRight.Substring(i,1)=="0")                      {                          strTmpRight = strTmpRight.Remove(i,1);                      }                      else                      {                          break;                      }                  }                  if (strTmpRight != "")                  {                      string strRight = "";                      for (int i = 0; i < strTmpRight.Length; i++)                      {                          strRight += Len1(strTmpRight.Substring(i, 1)) + " ";                      }                      return FullLen(strTmp[0]) + " " + strOutSeparator + " " + strRight.TrimEnd();                  }                  else                  {                      return FullLen(strTmp[0]);                  }              }              catch              {                  return FullLen(strTmp[0]);              }          }      }  }  

Các file đính kèm theo tài liệu này:

  • docCode đọc số sang chữ trong lập trình C#.doc
Tài liệu liên quan