Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43438
  • 博文数量: 9
  • 博客积分: 1617
  • 博客等级: 上尉
  • 技术积分: 85
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-21 00:56
文章分类
文章存档

2010年(9)

分类: C/C++

2010-04-21 01:02:27

这些天做的都是字符串的处理,把一系列字符串处理放在一起,就形成了一个字符串处理函数库了~自己写的,在这里总结给自己,以备以后要用。


/***************************************************
* file name: pubfun_string.c creat on 21-4-2010    *
 * ----------------------------------------------- *
 * function: a collection of the string handle fu- *
 * nctions.                                        *
 * ------------------------------------------------*
 * author: Wayne Lee company: Congine. Suzhou.     *
 * *************************************************/



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

#define STR_MAX 200


/* statement of the string handle functions */

int string_revs(char*); //reverse the order of the string.


int string_rl(char*); //remove the left blank bits if any in string.


int string_rr(char*); //remove the right blank bits if any in string.


int string_rb(char*); //remove both the left and right blank bits if any in string.


int string_ntotc(char*); //translate the numeric characters into traditional chinese.


int string_sbf(char*); //split the numeric string with float point every 3 bits with a "," right to left before ".", ended with two bits fraction part.


int string_tcmon(char*); //translate the numeric money to traditional chinese format output.


int string_fc(char*, int b);



// the start of string_fc function.



/* handle the carry of the float */

int string_fc(char* s, int b) {
  char *ptr;
  char sbuf[STR_MAX];

  int len_s;
  int len_f;
  int n;

  double f;

  strcpy(sbuf, s);
  s[0] = 0x00;
  len_s = strlen(sbuf);
  ptr = strstr(sbuf, ".");
  len_f = strlen(ptr) - 1;
  f = atof(sbuf);

  if (f >= 0.0) {
    n = (len_s-1) - (len_f - b);
  }
  else {
    n = (len_s-2) - (len_f - b);
  }
  gcvt(f, n, s);
  ptr = NULL;
  ptr = strstr(s, ".");
  if (ptr == NULL) {
    strcat(s, ".");
    for (; b > 0; b--) {
      strcat(s, "0");
    }
  }

  ptr = strstr(s, ".");
  len_f = strlen(ptr) - 1;
  if (len_f < b) {
    n = b - len_f;
    for (; n > 0; n--) {
      strcat(s, "0");
    }
  }
  return 0;
}

// the end of string_fc function.




// the start of string_revs function.



/* reverse the order of the string */

int string_revs(char* s) {
  char buf[STR_MAX];
  int i;
  int len;

  strcpy(buf, s);
  memset(s, 0x00, sizeof(s));
  len = strlen(buf);
  for (i=0; len-i>0; i++) {
    s[i] = buf[len - 1 - i];
  }
  s[i] = '\0';
}

//the end of string_revs function.


//


//the start of string_rl function.



/* stings contain left blank space *
 * we find it out and remove them */


int string_rl(char* s) {
    char buf_l[STR_MAX];
    int j;
    int i = 0;
    int len;
    memset(buf_l,0x00,sizeof(buf_l));
    strcpy(buf_l, s);
    memset(s,0,sizeof(s));
    len = strlen(buf_l);
    while(buf_l[i] == ' ') {
      i++;
    }
    for (j=0; i<len; i++,j++) {
      s[j] = buf_l[i];
    }
    s[j] = '\0';
    return 0;
}

// the end of string_rl function.


//


// the start of string_rr function.



/* stings contain right blank space *
 * we find it out and remove them */


int string_rr(char* s) {

  char buf_r[STR_MAX];
  int i;
  int j;
  int len;

  memset(buf_r, 0, sizeof(buf_r));
  len = strlen(s);
  for (i=len-1, j=0; i >= 0; i--, j++) {
   buf_r[j] = s[i];
  }
  memset(s, 0, sizeof(s));
  string_rl(buf_r);
  len = strlen(buf_r);
  for (i=len-1, j=0; i >= 0; i--, j++) {
    s[j] = buf_r[i];
  }
  s[j] = '\0';
  return 0;
}

// the end of string_rr function.


//


// the start of string_rb function.



/* stings contain left and right blank space *
 * we find it out and remove them */

  int string_rb(char* s) {
  string_rl(s);
  string_rr(s);
  return 0;
}

// the end of string_rb function.


//


// the start of string_ntotc function.



/* translate the numeric characters to the*
 * traditional chinese! */


int string_ntotc(char* os) {
  char buf[STR_MAX];
  char s[STR_MAX];
  int len = 0;
  int i;

  strcpy(buf, os);
  memset(s, 0x00, sizeof(s));
  len = strlen(buf);
  for (i=0; i<len; i++) {
      switch (buf[i]) {
        case '0' : strcat(s, "零");
        break;
        case '1' : strcat(s, "壹");
        break;
        case '2' : strcat(s, "貳");
        break;
        case '3' : strcat(s, "叁");
        break;
        case '4' : strcat(s, "肆");
        break;
        case '5' : strcat(s, "伍");
        break;
        case '6' : strcat(s, "陆");
        break;
        case '7' : strcat(s, "柒");
        break;
        case '8' : strcat(s, "捌");
        break;
        case '9' : strcat(s, "玖");
        break;
        case '.' : strcat(s, ".");
        break;
      }
  }
  strcpy(os, s);
  return 0;
}

// the end of string_ntotc function.


//


// the start of string_sbf function.



/* count the bits of the numeric string *
 * and split it every 3 bits with a "'" *
 * right to left before ".", ended with 2 *
 * bits fraction part. */

int string_sbf(char* s) {
  char buf[STR_MAX];
  int len = 0;
  int i;
  int j;

  strcpy(buf, s);
  memset(s, 0x00, sizeof(s));
  len = strlen(buf);
  if (buf[len - 3] == '.') {
    s[2] = buf[len - 3];
    s[1] = buf[len - 2];
    s[0] = buf[len - 1];
    for (i=len-4, j=3; i>=0; i--, j++) {
      if (j%4 == 2) {
        s[j] = ',';
        i++;
      }
      else {
        s[j] = buf[i];
      }
    }
  }
  else {
    for (i=len-1, j=0; i>=0; i--,j++) {
      if (j%4 == 3) {
        s[j] = ',';
        i++;
      }
      else {
        s[j] = buf[i];
      }
    }
  }
  s[j] = '\0';
  string_revs(s);
  return 0;
}

// the end of string_sbf function.


//


//the start of string_tcmon function.



/* translate the numeric money to traditional *
 * chinese format output. */

int string_tcmon(char *s) {
  char buf[STR_MAX];
  char temp[STR_MAX];
  char *unit[] = {"分","角","元","拾","佰","仟"};
  char frac[STR_MAX];
  int i;
  int len;
  int sn;
  int mod;

  memset(buf, 0x00, sizeof(buf));
  memset(temp, 0x00, sizeof(temp));
  memset(frac, 0x00, sizeof(frac));

  strcpy(buf, s);
  memset(s, 0x00, sizeof(s));

  len = strlen(buf);
 
/* whether the string represent the value 0.00 */
  if (atof(buf) == 0.0) {
    strcpy(s, "零元");
    return 0;
  }

/* translate the fraction part first */

  temp[0] = buf[len - 2];
  string_ntotc(temp);
  strcpy(frac, temp);
  strcat(frac, unit[1]);
  memset(temp, 0x00, sizeof(temp));
  temp[0] = buf[len - 1];
  string_ntotc(temp);
  strcat(frac, temp);
  strcat(frac, unit[0]);

/* handle the case : integer part is 0 */

  strcpy(s, buf);
  s[len - 1] = '0';
  s[len - 2] = '0';
  if (atof(s) == 0.0) {
    memset(s, 0x00, sizeof(s));
    strcpy(s, frac);
    return 0;
  }

/* Now translate the integer part */
  memset(s, 0x00, sizeof(s));
 
  sn = (len - 4)/4 + 1;
  mod = (len - 4)%4 + 1;
 
  if (sn == 3) {
    for (i=0; i<mod; i++) {
      if (buf[i] != '0') {
        temp[0] = buf[i];
        string_ntotc(temp);
        strcat(s, temp);
        if (i != mod-1)
          strcat(s, unit[len - 10 - i]);
      }
      else {
        while (buf[++i] == '0') {
          if (i == mod-1) {
            ++i;
            break;
          }
        }
        --i;
        if (i != mod-1) {
          strcat(s, "零");
        }
      }
    }
    strcat(s, "亿");
    if (buf[mod-1] == '0') {
      strcat(s, "零");
    }
    for (i=mod; i<mod+4; i++) {
      if (buf[i] != '0') {
        temp[0] = buf[i];
        string_ntotc(temp);
        strcat(s, temp);
        if (i != mod+3)
          strcat(s, unit[len - 6 - i]);
      }
      else {
        while (buf[++i] == '0') {
          if (i == mod + 3) {
            ++i;
            break;
          }
        }
        --i;
        if (i != mod+3) {
          strcat(s, "零");
        }
      }
    }
    strcat(s, "万");
    if (buf[mod + 3] == '0') {
      strcat(s, "零");
    }
    for (i=mod+4; i<mod+8; i++) {
      if (buf[i] != '0') {
        temp[0] = buf[i];
        string_ntotc(temp);
        strcat(s, temp);
        strcat(s, unit[len - 2 - i]);
      }
      else {
        while (buf[++i] == '0') {
          if (i == mod + 7) {
            ++i;
            break;
          }
        }
        --i;
        if (i != mod+7) {
          strcat(s, "零");
        }
        else
          strcat(s, "元");
      }
    }
  }

  if (sn == 2) {
    for (i=0; i<mod; i++) {
      if (buf[i] != '0') {
        temp[0] = buf[i];
        string_ntotc(temp);
        strcat(s, temp);
        if (i != mod-1)
          strcat(s, unit[len - 6 - i]);
      }
      else {
        while (buf[++i] == '0') {
          if (i == mod-1) {
            ++i;
            break;
          }
        }
        --i;
        if (i != mod-1) {
          strcat(s, "零");
        }
      }
    }
    strcat(s, "万");
    if (buf[mod-1] == '0') {
      strcat(s, "零");
    }
    for (i=mod; i<mod+4; i++) {
      if (buf[i] != '0') {
        temp[0] = buf[i];
        string_ntotc(temp);
        strcat(s, temp);
        strcat(s, unit[len - 2 - i]);
      }
      else {
        while (buf[++i] == '0') {
          if (i == mod + 3) {
            ++i;
            break;
          }
        }
        --i;
        if (i != mod+3) {
          strcat(s, "零");
        }
        else
          strcat(s, "元");
      }
    }
  }

  if (sn == 1) {
    for (i=0; i<mod; i++) {
      if (buf[i] != 0) {
        temp[0] = buf[i];
        string_ntotc(temp);
        strcat(s, temp);
        strcat(s, unit[len - 2 - i]);
      }
      else {
        while (buf[++i] == 0) {
          if (i == mod-1) {
            ++i;
            break;
          }
        }
        --i;
        if (i != mod-1) {
          strcat(s, "零");
        }
        else
          strcat(s, "元");
      }
    }
  }

/***********************************
 * complete both the fraction part *
 * and the integer part! join both *
 * *********************************/


  strcat(s, frac);

  return 0;
}


阅读(984) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:文件I/O 函数

给主人留下些什么吧!~~