经典编程900例(c语言)(第十篇)

例110:实现链表

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

int main(int argc, char const *argv[])
{
    int i;

    // 定义结构、声明头指针和临时节点
    struct ListEntry
    {
        int number;
        struct ListEntry *next;
    } start, *node;

    // 初始化链表
    start.next = NULL;
    node = &start;
    for (i = 1; i <= 10; i++)
    {
        node->next = (struct ListEntry *) malloc(sizeof(struct ListEntry));
        node = node->next;
        node->number = i;
        node->next = NULL;
    }

    // 节点指向头节点
    node = start.next;

    while (node)
    {
        printf("%d ", node->number);
        node = node->next;
    }

    return 0;
}

例111:双向链表的插入元素

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

int main(int argc, char const *argv[])
{
    int i;

    struct ListEntry
    {
        int number;
        struct ListEntry *next;
        struct ListEntry *previous;
    } start, *node, *new;

    start.next = NULL;
    start.previous = NULL;
    node = &start;

    // 生成链表节点为1,3,5,7,9
    for (i = 1; i < 10; i += 2)
    {
        node->next = (struct ListEntry *) malloc(sizeof(struct ListEntry));
        node->next->previous = node;
        node = node->next;
        node->number = i;
        node->next = NULL;
    }

    for (i = 2; i <= 10; i += 2)
    {
        int found = 0;

        new = (struct ListEntry *) malloc(sizeof(struct ListEntry));
        new->number = i;
        node = start.next;

        do
        {
            // 如果原链表节点大于新节点的数就将新节点插入该节点前面
            if (node->number > new->number)
            {
                new->next = node;
                new->previous = node->previous;
                node->previous->next = new;
                node->previous = new;
                found = 1;
            }
            else
                node = node->next;
        }while ((node->next) && (! found));

        // 最后一个节点
        if (! found)
            if (node->number > new->number)
            {
                new->next = node;
                new->previous = node->previous;
                node->previous->next = new;
                node->previous = new;
            }
            else
            {
                new->next = NULL;
                new->previous = node;
                node->next = new;
            }
    }

    /* Display the list*/
    node = start.next;
    do
    {
        printf("%d ", node->number);
        node = node->next;
    }while (node);

    return 0;
}

例112:main函数的参数

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

/**
 * main函数 - 程序入口
 * @Author dust_fall
 * @param  argc      参数的个数,这个在程序运行时输入的命令行中字符串的个数决定。
 * @param  argv      具体的参数,每个参数是一个字符数组指针。
 * @param  env       环境变量参数,由系统的当前环境变量的设置决定。
 * @return           返回给系统的值
 */
int main(int argc, char *argv[], char *env[])
{
    printf("Command line\n");
    while (*argv)
        puts(*argv++);

    printf("Environment entries\n");
    while (*env)
        puts(*env++);

    return 0;
}

例113:一段我编译不了的代码

#include <stdio.h>
#include <dos.h>

int main(int argc, char const *argv[])
{
    if (_8087)
        printf("Math coprocessor found\n");
    else
        printf("No math coprocessor\n");
    return 0;
}

例114:switch选择结构的使用

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int a = 1, b, c, d;

    switch (a) 
    {
        case 1: a = 5;
                b = 6;
                c = 7;
                d = 8;
                break;
        case 2: b = 6;
                c = 7;
                d = 8;
                break;
    };

    printf("a=%d b=%d c=%d d=%d\n", a, b, c, d);    // a=5 b=6 c=7 d=8

    return 0;
}

例115:又一段我编译不了的代码

#include <stdio.h>
#include <dos.h>
#include <dir.h>


int function[255];  /* DOS services*/

void interrupt far (*original_handler)();


void interrupt far handler(void)
 {
   char i;

   asm { mov i, ah }

   function[i]++;

   _chain_intr(original_handler);
 }


void main(void)
 {
   int i;

   // Zero the function counts
   for (i = 0; i < 255; i++)
     function[i] = 0;

   original_handler = _dos_getvect(0x21);
   _disable();
   _dos_setvect(0x21, handler);
   _enable();

   printf("This is a message\n");
   fprintf(stdout, "This is a second message\n");

   printf("Current disk is %c\n", getdisk() + 'A');

   _disable();
   _dos_setvect(0x21, original_handler);
   _enable();


   for (i = 0; i <= 255; i++)
     if (function[i])
       printf("Function %x called %d times\n", i, function[i]);
 }

例116:还有一段我编译不了的代码

#include <stdio.h>
#include <dos.h>

void main(void)
 {
   printf("Previous extended Ctrl-Break status %s\n",
    (getcbrk()) ? "On": "Off");

   setcbrk(0);  /* Turn if off*/
    }

例117:还有。。。

#include <stdio.h>
#include <dos.h>
#include <conio.h>

void far handler(unsigned device_error, unsigned error_code,
  unsigned far *device_header)
 {
   cputs("Critical error ending program\n");
   _hardresume(_HARDERR_ABORT);   /* Abort*/
 }

void main(void)
 {
   FILE *fp;

   _harderr(handler);

   fp = fopen("A:SOMEFILE.EXT", "r");

   printf("Program message...\n");
   fclose(fp);
 }

例118:区分ASCII码表中的不可打印字符和可打印字符

#include <stdio.h>
#include <ctype.h>

int main(int argc, char const *argv[])
{
    int ascii_char;

    // 区别ASCII码表的可打印字符
    for (ascii_char = 0; ascii_char < 128; ascii_char++)
        // int isprint(int c) 检查所传的字符是否是可打印的。可打印字符是非控制字符的字符
        // 如果 c 是一个可打印的字符,则该函数返回非零值(true),否则返回 0(false)
        if (isprint(ascii_char))
            printf("ASCII value %d setting (hex) %x ASCII %c\n", ascii_char, _ctype[ascii_char], ascii_char);
        else
            printf("ASCII value %d setting (hex) %x ASCII %c\n",  ascii_char, _ctype[ascii_char], ascii_char);
    
    return 0;
}

例119:双向链表的遍历

#include <stdio.h>
#include <malloc.h>

int main(int argc, char const *argv[])
{
    int i;

    struct ListEntry {
        int number;
        struct ListEntry *next;
        struct ListEntry *previous;
    } start, *node;

    // 建立并初始化双向链表
    start.next = NULL;
    start.previous = NULL;
    node = &start;    
    for (i = 1; i <= 10; i++)
    {
        node->next = (struct ListEntry *) malloc(sizeof(struct ListEntry));
        node->next->previous = node;
        node = node->next;
        node->number = i;
        node->next = NULL;
    }
    node = start.next;

    // 正向遍历链表
    do {
        printf("%d ", node->number);
        node = node->next;
    } while (node->next);

    // 逆向遍历链表
    do {
        printf("%d ", node->number);
        node = node->previous;
    } while (node->previous);

    return 0;
}

例120:打印错误信息

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

int main(int argc, char const *argv[])
{
    int error;

    for (error = 0; error < sys_nerr; error++)
        printf("Error %d %s\n", error, sys_errlist[error]);

    return 0;
}

例121:调用子进程

#include <process.h>
#include <stdio.h>

int main(int argc, char const *argv[])
{
    printf("About to call child process\n\n");

	// int execl(char *pathname, char *arg0, arg1, ..., argn, NULL); 
    // execl()用来执行参数pathname字符串所代表的文件路径, 
    // 接下来的参数代表执行该文件时传递过去的argv(0),argv[1], ..., 最后一个参数必须用空指针(NULL)作结束
    execl("CHILD.EXE", "CHILD.EXE", "AAA", "BBB", "CCC", NULL);
    printf("\n\nBack from child process--SHOULD NOT APPEAR\n");

    return 0;
}

例122:调用子进程2

#include <process.h>
#include <stdio.h>

int main(int argc, char const *argv[])
{
	// 这里定义的数组最后一个元素是NULL, 加入参数列表后, 最后一个参数是NULL
    char *env[] = {"FILE=EXECLPE.C", "LANGUAGE=C", "OS=DOS", NULL};

    // int execple(char *pathname, char *arg0, arg1, ..., NULL, char *envp[]); 
    // execple()会从PATH 环境变量所指的目录中查找符合参数file 的文件名, 找到后便执行该文件, 
    // 然后将第二个以后的参数当做该文件的argv[0], argv[1], ..., 最后一个参数必须用空指针(NULL)作结束.
    execlpe("CHILD.EXE", "CHILD.EXE", "Using-spawnlpe", "BBB", NULL, env);

    return 0;
}

例123:调用子进程3

#include <stdio.h>
#include <process.h>

int main(int argc, char const *argv[])
{
    char *env[] = { "FILENAME=SPAWNVXX.C", "OS=DOS", 
                   "ROUTINE=EXECVPE", NULL };

    char *argv[] = { "CHILD.EXE", "AAA", "BBB", NULL };
	
	// int execvpe(char *pathname, char *argv[], char *envp[]); 
    execvpe("CHILD.EXE", argv, env);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值