

 才第一章看完。。。
才第一章看完。。。能解出来不???
我看晕了
 程序代码:
程序代码:
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
char* TransForm(char cBuffer[], int iVar);
void main(void)
{
    char cString[1024];
    // Hello,world!的翻版
    // 原先是这样写的
    // char cString[] = "Hello,world!";
    // 即printf("%s\n", cString);
    // 套上面的格式, 成下面这样, 这种套换方式, 是程序语言的基本用法
    printf("%s\n", TransForm(cString, 1234567));
    printf("%s\n", TransForm(cString, 0));
    printf("%s\n", TransForm(cString, -1234567));
    // 注意与下面变换对比:
    // printf("%d\n", 1234567);
    // 这样就可以猜到printf()内部的动作了
    // 如果要回避printf(), 可以
    // puts(TransForm(cString, 1234567));
    // 不妨比较前后两种版本(printf()版和puts()版)EXE文件的大小
}
char* TransForm(char cBuffer[], int iVar)
{
    int iCount;            // 千位计数器
    bool lNegative;        // 是否负数的标志
    char* p = cBuffer;    // 记录缓冲区的起始地址(p是动指针, cBuffer是不动的, 用于返回)
    if (iVar == 0)
    {
        // 如果传入的数值为0直接返回"0"
        *p++ = '0';        // 数组版本可写成cBuffer[0] = '0';
        *p = '\0';        // 数组版本可写成cBuffer[1] = '\0';
        return cBuffer;
    }
    lNegative = (iVar < 0);
    if (lNegative)
    {
        iVar = -iVar;    // 取正数计算
    }
    iCount = 0;
    while (iVar > 0)
    {
        if (iCount < 3)
        {
            iCount++;
        }
        else
        {
            *p++ = ',';
            iCount = 1;
        }
        *p++ = '0' + (iVar % 10);        // 数组版本应建立一个下标计数器写入数据
        iVar /= 10;
    }
    if (lNegative)        // 补回负数的标识
    {
        *p++ = '-';
    }
    *p = '\0';            // 字符串结束符
    return _strrev(cBuffer);    // 反转缓冲区之后返回
}
// 其实可以不返回缓冲区的地址, 改为返回字符串的字符数, 为0时失败.
										
					
	

 程序代码:
程序代码:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node NODE, * PNODE;
typedef const PNODE PCNODE;
struct node {
    char bits[4];
    PNODE next;
};
void *         check(void * p);
PCNODE         init_stack(void);
PCNODE         push(PNODE pTop, const char * bits);
const char *   pop(PNODE pTop);
int            is_empty(PCNODE pTop);
void           kill_stack(PNODE top);
char *         number_format(long num, char * buf);
void * check(void * p) {
    if(!p) {
        fprintf(stderr, "There's a error in program and then program will to exit.\n");
        system("pause");
        exit(1);
    }
    return p;
}
PCNODE init_stack(void) {
    PNODE pTop = (PNODE)malloc(sizeof(NODE));
    check(pTop);
    pTop->next = NULL;
    return pTop;
}
PCNODE push(PNODE pTop, const char * bits) {
    PNODE pNewNode = (PNODE)malloc(sizeof(NODE));
    check(pNewNode);
    strcpy(pNewNode->bits, bits);
    pNewNode->next = pTop->next;
    pTop->next = pNewNode;
    return pNewNode;
}
const char * pop(PNODE pTop, char * bits) {
    PNODE pTemp = pTop->next;
    if(is_empty(pTop))
        return NULL;
    strcpy(bits, pTemp->bits);
    pTop->next = pTemp->next;
    free(pTemp);
    return bits;
}
void kill_stack(PNODE pTop) {
    char buf[4];
    while(pop(pTop, buf))
        ;
    free(pTop);
}
int is_empty(PCNODE pTop) {
    return pTop->next == NULL;
}
char * number_format(long num, char * buf) {
    int             isNage = num < 0;
    char *          pTemp = buf;
    char            bits[4];
    PNODE           pTop = init_stack();
    unsigned long   temp = isNage ? ~(unsigned long)num + 1 : (unsigned long)num;
    while(temp) {
        sprintf(bits, "%d", temp % 1000);
        push(pTop, bits);
        temp /= 1000;
    }
    if(isNage) {
        strcpy(buf, "-");
        strcat(buf, pop(pTop, bits));
    } else
        strcpy(buf, pop(pTop, bits));
    while(pop(pTop, bits)) {
        strcat(buf, ",");
        strcat(buf, bits);
    }
    kill_stack(pTop);
    return buf;
}
int main(void) {
    char    fileName[128];
    char    buf[100];
    long    size;
    long    value;
    FILE *  fp;
   
    scanf("%s", fileName);
    check(fp = fopen(fileName, "rb"));
    fseek(fp, 0L, SEEK_END);
    size = ftell(fp);
    printf("Size of \"%s\" is %s bytes.\n", fileName, number_format(size, buf));
    scanf("%ld", &value);
    printf("%s\n", number_format(value, buf));
    system("pause");
    return 0;
}
	
		
			
		
	
