| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2463 人关注过本帖
标题:十万火急! 哪位高手能提供一个100行带注释的C语言程序啊?
只看楼主 加入收藏
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 


/* 一个简易的用文件存储数据的程序 */

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

struct pcb
{
    int  num;                   /* 编号   */
    char kehu[40];              /* 客户   */
    char gongyi[30];            /* 工艺   */
    char kebian[50];            /* 客编   */
    char beizhu[200];           /* 备注   */
};

int write_pcb(FILE * pf);            /* 修改 (edit_file()函数访问此函数)  */
int show_file(FILE * pf);           /* 显示文件内容        */
int edit_file(FILE * pf);            /* 修改、查找、显示    */
FILE * create_file(char * fname);    /* 打开 fname 文件     */

int main(void)
{
    FILE * pf;
    char fname[125] = {'\0'};
   
    while(fname[0] == '\0')
    {
        printf("Please enter the file name: ");
        gets(fname);
    }
   
    pf = create_file(fname);
   
    while(edit_file(pf))
        continue;

    fclose(pf);             /* 关闭文件                        */
   
    puts("\nThe End!");
    system("Pause");
    return 0;
}


/* 打开文件 ****************************/
FILE * create_file(char * fname)
{
    FILE * pf = NULL;
    int ch;

    if((pf = fopen(fname, "r+b")) == NULL)   /* 文件不存在则创建 */
    {
        printf("The %s does not exist!\n", fname);

        printf("Whether the creation of the document? (Q to quit)\n");
        printf("[Y / N]... ");
        while((ch = getchar()) != 'y'&& ch != 'Y'
                       && ch != 'n' && ch != 'N'  )
        {
            while(getchar() != '\n')
                ;
            printf("[Y / N]... ");
        }
        while(getchar() != '\n')
            ;

        switch(ch)
        {
            case 'y':
            case 'Y':
                if((pf = fopen(fname, "w+b")) == NULL)
                {
                    puts("Failure to create documents!");
                    system("Pause");
                    exit(1);
                }
                break;

            case 'n':
            case 'N':
                puts("Goodbye!");
                system("Pause");
                exit(1);
                break;
            default :
                puts("Goodbye!");
                system("Pause");
                exit(1);
                break;
        }
    }
    else
        printf("%s to open!\n", fname);

    return pf;
}


/* 显示文件内容 ******************/
int show_file(FILE * pf)
{
    struct pcb temp;
    int count = 0;

    if(pf == NULL)
    {
        fprintf(stderr, "Error! [enter]...");
        getchar();
        exit(1);
    }

    rewind(pf);   /* 回到文件开始处 */
   
    system("CLS");
    puts("============== File ==================");
   
    while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
    {
        ++count;
        printf("\nId: %d\n  kehu: %s\n  gongyi%s\n  kebian%s\n  beizhu%s\n"
            ,temp.num, temp.kehu, temp.gongyi, temp.kebian, temp.beizhu);

        if(count % 3 == 0)
        {
            system("Pause");
            system("CLS");
        }
    }

    if(count == 0)
        printf("\nfile is null!\n\n");
    else
        printf("\n                     Count: %d\n\n", count);
        
    rewind(pf);   /* 回到文件开始处 */
   
    return count;
}


/* 项目写入操作 ****************************/
int write_pcb(FILE * pf)
{
    struct pcb temp;

    printf("Id: ");
    while(scanf("%d", &temp.num) != 1)
    {
        while(getchar() != '\n')
            continue;
        printf("Id: ");
    }
    while(getchar() != '\n')
        continue;
    printf("kehu: ");
    gets(temp.kehu);
    printf("gongyi: ");
    gets(temp.gongyi);
    printf("kebian: ");
    gets(temp.kebian);
    printf("beizhu: ");
    gets(temp.beizhu);

    if(fwrite(&temp, sizeof(struct pcb), 1, pf) != 1)
    {
        fprintf(stderr, "Error! [enter]...\n");
        getchar();
        exit(1);
    }
    return 1;
}

/* 编辑文件 **********************************/
int edit_file(FILE * pf)
{
    int ch = '\0';
    int num;
    int i, k;
    struct pcb temp;

    if(pf == NULL)
    {
        fprintf(stderr, "Error! [enter]...");
        getchar();
        exit(1);
    }

    rewind(pf);   /* 回到文件开始处 */

    puts("================== Menu ==================\n");
   
    while(ch != 'e' && ch != 'f' && ch != 'a' && ch != 'q' && ch != 's')
    {
        printf("\na)add    e)edit\n"
               "f)find   s)show\n"
               "q)quit\n\n"          );
        ch = getchar();
        if(ch != '\n')
            while(getchar() != '\n');
    }

    system("CLS");
   
    switch(ch)
    {
        case 'a':
            puts("Add ---------------------------\n");
            fseek(pf, 0l, SEEK_END);    /* 到文件结尾 */
            write_pcb(pf);
            break;
            
        case 'e':
            puts("Edit ---------------------------\n");
            
            printf("Edit Id: ");
            while(scanf("%d", &num) != 1)
            {
                while(getchar() != '\n')
                    continue;
                printf("Edit Id: ");
            }
            while(getchar() != '\n')
                continue;

            rewind(pf);  /* 回到文件开始处 */

            k = 0;
            while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
            {
                if(num == temp.num)
                {
                    k = 1;
                    i = ftell(pf) - sizeof(struct pcb);
                    fseek(pf, (long)i, SEEK_SET);
                    write_pcb(pf);
                    break;
                }
            }
            if(k < 1)
            {
                printf("不存在!\n");
                system("Pause");
            }
            break;
            
        case 'f':
            puts("Find ---------------------------\n");
            printf("Find Id: ");
            while(scanf("%d", &num) != 1)
            {
                while(getchar() != '\n')
                    continue;
                printf("Find Id: ");
            }
            while(getchar() != '\n')
                continue;

            rewind(pf);  /* 回到文件开始处 */
            
            k = 0;
            while(fread(&temp, sizeof(struct pcb), 1, pf) == 1)
            {
                if(num == temp.num)
                {
                    k = 1;
                    printf("记录存在!\n");
                    break;
                }
            }
            if(k < 1)
                printf("不存在!\n");
            system("Pause");
            break;
            
        case 's':
            show_file(pf);
            break;
            
        default :
            return 0;
            break;
    }

    rewind(pf);   /* 回到文件开始处 */
   
    return 1;
}

—>〉Sun〈<—
2007-12-16 21:07
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 

/* 简单的队列程序 */

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

#define QUEUEMAX 100    // 队列最大长度

typedef struct {
    int data;
    int pri;
} Item;

typedef struct node
{
    Item item;
    struct node * next;
} Node;

typedef struct queue
{
    Node * front;
    Node * rear;
    int count;
} Queue;


void Itin(Queue * pq)            // 初始化队列 */
{
    pq->front = pq->rear = NULL;
    pq->count = 0;
}

int FullQueue(Queue * pq)        // 队列是否已满
{
    return (pq->count >= QUEUEMAX);
}

int EmptyQueue(Queue * pq)       // 队列是否为空
{
    return (pq->count == 0);
}

int QueueCount(Queue * pq)       // 返回队列长度
{
    return pq->count;
}

int DelQueue(Queue * pq, Item * item)         // 删除节点
{
    Node * temp;
    if(EmptyQueue(pq))
        return 0;
    *item = pq->front->item;
    temp = pq->front;
    pq->front = pq->front->next;
    free(temp);
    pq->count--;
    if(pq->count == 0)
        pq->rear = NULL;
    return 1;
}

int AddQueue(Queue * pq, Item item)            // 添加节点
{
    Node * qnew = NULL;
    if(FullQueue(pq))
        return 0;
    qnew = (Node *)malloc(sizeof(Node));
    if(qnew == NULL)
    {
        fputs("无法分配足够内存!\n", stderr);
        exit(1);
    }
    qnew->next = NULL;
    qnew->item = item;
    if(EmptyQueue(pq))
    {
        pq->front = qnew;
        pq->rear = qnew;
    }
    else
    {
        pq->rear->next = qnew;
        pq->rear = qnew;
    }
    pq->count++;
    return 1;
}

void EndQueue(Queue * pq)             // 结束,删除所有节点
{
    Item * p;
    while(DelQueue(pq, p));
}

int main(void)
{
    Queue line;
    Item item;

    Itin(&line);  // 初始化队列

    puts("请添加内容: ");
    while(!FullQueue(&line))
    {
        printf("请输入2个整数: ");
        if(scanf("%d %d", &item.data, &item.pri) != 2)
        {
            while(getchar() != '\n');
            break;
        }
        else
        {
            while(getchar() != '\n');
            AddQueue(&line, item);
        }
    }

    printf("队列中有 %d 项。\n", QueueCount(&line));

    while(DelQueue(&line, &item))
        printf("data: %d  pri: %d\n", item.data, item.pri);

    EndQueue(&line);  // 清除队列,结束工作
    getchar();
    return 0;
}

—>〉Sun〈<—
2007-12-16 21:16
nanvzi
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-12-15
收藏
得分:0 
回复 10# 的帖子
十分感谢~
2007-12-16 22:21
逍遥林下
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-11-6
收藏
得分:0 
我现在心有余而力不足啊

天下事有难易乎?为之,则难者亦易矣;不为,则易者亦难矣!
2008-11-06 13:11
buaa_wangm
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2008-11-14
收藏
得分:0 
2008-11-15 09:54
洛fei
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-9-17
收藏
得分:0 
回复 10楼 cosdos
能稍稍讲下吗?我没怎么看懂谢谢啊!
2009-09-17 14:34
快速回复:十万火急! 哪位高手能提供一个100行带注释的C语言程序啊?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018849 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved