月度归档:2015年06月

字符串转换浮点数函数

atof函数,支持科学计数法

/*
 * atof.c
 */
#include <stdio.h>
#include <ctype.h>

int m_getline(char line[], int max);
double atof(char s[]);
int atoi(char s[]);

#define MAX 100

int main(void)
{
        int n;
        double x;
        char str[MAX];

        while (m_getline(str, MAX) > 0) {
                x = atof(str);
                n = atoi(str);
                printf("\t%g %d\n", x, n);
        }

        return 0;
}

int m_getline(char s[], int lim)
{
        int i, c;
        i = 0;
        while (--lim > 0 && (c = getchar()) != '\n' && c != EOF)
                s[i++] = c;
        if (c == '\n')
                s[i++] = c;
        s[i] = '\0';

        return i;
}

double atof(char s[])
{
        int i, sign, n, esign, e;
        double val, power;

        for (i = 0; isspace(s[i]); i++)
                ;

        sign = (s[i] == '-') ? -1 : 1;

        if (s[i] == '-' || s[i] == '+')
                i++;

        for (val = 0.0; isdigit(s[i]); i++)
                val = 10.0 * val + (s[i] - '0');

        if (s[i] == '.')
                i++;
        for (power = 1.0; isdigit(s[i]); i++) {
                val = 10.0 * val + (s[i] - '0');
                power *= 10.0;
        }

        if (s[i] == 'e' || s[i] == 'E') {
                i++;
                esign = (s[i] == '-') ? -1 : 1;

                if (s[i] == '-' || s[i] == '+')
                        i++;
                for (n = 0; isdigit(s[i]); i++)
                        n = 10 * n + (s[i] - '0');

                e = 1;
                while (n-- > 0)
                        e *= 10;
                if (esign == 1)
                        return sign * val / power * e;
                else
                        return sign * val / power / e;
        } else {
                return sign * val / power;
        }
}

int atoi(char s[])
{
//      double atof(char s[]);
        return (int) atof(s);
}

大数阶乘C语言

算法1:

/*
 * factorial of large numbers
 */
#include <stdio.h>
#include <string.h>

#define MAX 100000

int main(void)
{
        int n, i, j, up, tmp;
        int res[MAX];
        while (scanf("%d", &n) > 0 && n >= 0) {
                memset(res, 0, sizeof res);     /* initialized to 0 */
                res[0] = 1;
                for (i = 2; i <= n; i++) {
                        up = 0;         /* carry initialized to 0 */
                        for (j = 0; j < MAX; j++) {
                                tmp = res[j] * i + up;  /* carry */
                                res[j] = tmp % 10;
                                up = tmp / 10;
                        }
                }
                for (i = MAX - 1; i >= 0; i--)  /* ignores the leading 0 */
                        if (res[i])
                                break;
                for (j = i; j >= 0; j--)
                        printf("%d", res[j]);
                printf("\n");
        }

        return 0;
}

算法2:

/*
 * factorial of large numbers
 */
#include <stdio.h>
#include <string.h>

#define CLR(arr, val) memset(arr, val, sizeof (arr))
#define MAX 10000

int main(void)
{
        int n, i, j;
        int res[MAX];

        while (scanf("%d", &n) > 0 && n >= 0) {
                CLR(res, 0);
                res[0] = 1;
                for (i = 1; i <= n; i++) {
                        for (j = 0; j < MAX; j++)
                                res[j] *= i;
                        for (j = 0; j < MAX; j++) {
                                if (res[j] > 9) {
                                        res[j + 1] += res[j] / 10;
                                        res[j] %= 10;
                                }
                        }
                }
                for (i = MAX - 1; i >= 0; i--)
                        if (res[i])
                                break;
                for (j = i; j >= 0; j--)
                        printf("%d", res[j]);
                printf("\n");
        }

        return 0;
}

TCPL 2-3 编写十六进制字符串转换为整数函数

TCPL P37 htoi.c

TCPL p37 编写htoi(s), 把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整数值。字符串中允许包括数字组成的:0~9,a~f 以及A~F.
难点为前缀的检测。程序回车退出,其他循环输入。

/*
 * htoi.c
 */
#include <stdio.h>

#define MAXLINE 1000

int m_getline(char line[], int maxline);
int htoi(char s[]);

int main(void)
{
        int hex;
        char ch[MAXLINE];

        while (m_getline(ch, MAXLINE) > 1) {
                hex = htoi(ch);
                if (hex != -1)
                        printf("%sHex -- %d\n", ch, hex);
                else
                        printf("%s\a is not a hex\n", ch);
        }

        return 0;
}

int m_getline(char s[], int n)
{
        int i, c;

        for (i = 0; i < n - 1 && (c = getchar()) != '\n' && c != EOF; i++)
                s[i] = c;
        if (c == '\n')
                s[i++] = c;
        s[i] = '\0';

        return i;
}

int htoi(char s[])
{
        int i;
        int n;
        i = 0;
        n = 0;
        while (s[i] == ' ' || s[i] == '\t')     /* blink space tab character */
                i++;
        if (s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
                for (i += 2; s[i] != '\n'; i++) {
                        if ((s[i] >= '0' && s[i] <= '9')
                                || (s[i] >= 'A' && s[i] <= 'F')
                                || (s[i] >= 'a' && s[i] <= 'f')) {
                                if (s[i] >= '0' && s[i] <= '9')
                                        n = 16 * n + s[i] - '0';
                                else if (s[i] >= 'A' && s[i] <= 'F')
                                        n = 16 * n + s[i] - 'A' + 10;
                                else if (s[i] >= 'a' && s[i] <= 'f')
                                        n = 16 * n + s[i] - 'a' + 10;
                        } else {
                                return -1;
                        }
                }
                return n;
        } else if ((s[i] >= '0' && s[i] <= '9')
                || (s[i] >= 'A' && s[i] <= 'F')
                || (s[i] >= 'a' && s[i] <= 'f')) {
                for (i = 0; s[i] != '\n'; i++) {
                        if (s[i] >= '0' && s[i] <= '9')
                                n = 16 * n + s[i] - '0';
                        else if (s[i] >= 'A' && s[i] <= 'F')
                                n = 16 * n + s[i] - 'A' + 10;
                        else if (s[i] >= 'a' && s[i] <= 'f')
                                n = 16 * n + s[i] - 'a' + 10;
                        else
                                break;
                }
                        return n;
        } else {
                return -1;
        }

//      return 0;
}

继续阅读