发新话题
打印

[TIPS]C学习笔记 发少量谭浩强C程序设计SE课后题答案

简单结构
复制内容到剪贴板
代码:
#include <stdio.h>

int length, width;
long area;

struct coord{
    int x;
    int y;
};

struct rectangle{
    struct coord topleft;
    struct coord bottomrt;
} mybox;

int main(void)
{
    printf("top left x: ");
    scanf("%d", &mybox.topleft.x);

    printf("top left y: ");
    scanf("%d", &mybox.topleft.y);

    printf("bottom right left x: ");
    scanf("%d", &mybox.bottomrt.x);

    printf("bottom right left y: ");
    scanf("%d", &mybox.bottomrt.y);

    width = mybox.bottomrt.x - mybox.topleft.x;
    length = mybox.bottomrt.y - mybox.topleft.y;

    area = width * length;
    printf("\nThe area is %ld units.\n", area);

    return 0;
}

TOP

数组包含结构
复制内容到剪贴板
代码:
//终于进入结构的学习了
#include <stdio.h>

struct entry {
    char fname[20];
    char lname[20];
    char phone[10];
}list[4];

int i;

int main(void)
{
    for(i = 0; i < 4; i++)
    {
        printf("Enter first name: ");
        scanf("%s", list[i].fname);
        printf("Enter last name: ");
        scanf("%s", list[i].lname);
        printf("Enter phone in 123-4567 format: ");
        scanf("%s", list[i].phone);
    }

    printf("\n\n");

    for (i = 0; i < 4; i++)
    {
        printf("Name: %s %s", list[i].fname, list[i].lname);
        printf("\t\tPhone: %s\n", list[i].phone);
    }

    return 0;
}

TOP

复制内容到剪贴板
代码:
//本来考虑用指针代替name的 后来不行 因为指针必须初始化 否则很危险
//如果没初始化我直接把内容给他 那就不知道他放到什么地方去了
#include <stdio.h>

#define MAX 4

struct part {
    short number;
    char name[10];
} data[MAX] = {1, "Smith", 2, "Jones", 3, "Adams", 4, "Wilson"};

struct part *p_part;
int count;

int main(void)
{
    p_part = data;

    for( count = 0; count < 4; count++)
    {
        printf("At address %d: %d %s\n", p_part, p_part->number, p_part->name);
        p_part++;
    }

    return 0;
}

TOP

复制内容到剪贴板
代码:
//系统命令调用练习
//一个小程序 省去打命令的辛苦哟 by 冰血封情
#include <stdio.h>
#include <stdlib.h>

int menu(void);

int main(void)
{
     char input0[] = "ping [url]www.bbnl.org[/url]";
     char input1[] = "netstat -an";
     char input2[] = "tasklist";

     int x;

     x = menu();

     while (x > 1 || x < 4)
     {

          switch(x)
          {
          case 1:
              {
                   puts("Now pinging [url]www.bbnl.org:[/url] ");
                   system(input0);
                   break;
              }
          case 2:
              {
                   puts("Now nestat to c the port: ");
                   system(input1);
                   break;
              }
          case 3:
              {
                   puts("Now run tasklist to c task: ");
                   system(input2);
                   break;
              }
          case 4:
              {
                   puts("Now exit to win program...");
                   exit(0);
              }
          default:
              {
                   puts("Error parameters: ");
              }

          }
          x = menu();
     }

     return 0;
}

int menu(void)
{
     int fx = 0;

     while (fx < 1 || fx > 4)
     {
          printf("This is a program for EvilOctal[E.S.T] private.\n");
          printf("Please input a number between 1 and 4\n");
          printf("\tEnter 1 to ping bbnl ip\n");
          printf("\tEnter 2 to show port list\n");
          printf("\tEnter 3 to show the task\n");
          printf("\tEnter 4 to exit the program\n");
          printf("Enter a munber: ");
          scanf("%d", &fx);

          if (fx < 1 || fx > 4)
              printf("Please input a right parameters: \n");
     }
     return fx;
}
晕自己想做来玩的 哈哈 没想到 最后的选做题竟然和这个一样 省了

TOP

复制内容到剪贴板
代码:
#include <stdio.h>
#include <stdlib.h>

/*
int inc(int x, int y);
int dec(int x, int y);
int mul(int x, int y);
int div(int x, int y);
printf("");
这个么 是一个可以进行加减乘除运算的小计算器
是13天课程的一个选做题目
以前的选做都做了 此次自然也不例外了
By 冰血封情[E.S.T]
*/
int menu(void);

int main(void)
{
    int count, x, y;

    printf("This is a counter made by EvilOctal[E.S.T]: \n");
    printf("He can help you to finish some simple count.\n");
   
    count = menu();
    while(1)
    {
        switch(count)
        {
        case 1:
            {
                printf("Please input 2 number for inc by space: ");
                scanf("%d %d", &x, &y);
                printf("\n%d + %d = %d", x, y, (x + y));
                break;
            }
        case 2:
            {
                printf("Please input 2 number for dec by space: ");
                scanf("%d %d", &x, &y);
                printf("\n%d - %d = %d", x, y, (x - y));
                break;
            }
        case 3:
            {
                printf("Please input 2 number for mul by space: ");
                scanf("%d %d", &x, &y);
                printf("\n%d x %d = %d", x, y, (x * y));
                break;
            }
        case 4:
            {
                printf("Please input 2 number for div by space: ");
                scanf("%d %d", &x, &y);
                printf("\n%d / %d = %d", x, y, (x / y));
                break;
            }
        case 5:
            {
                printf("\nOk! You chose EXIT...");
                printf("\nSee you!");
                exit(0);
            }
        default:
            {
                printf("You will never see me in the world!");
            }
        }
        count = menu();
    }
    return 0;
}

int menu(void)
{
    int choose = 0;
   
    while(choose < 1 || choose > 5)
    {
        puts("Choose the number for inc dec mul or div:");
        printf("1 is inc;2 is dec;3 is mul;4 is div;5 to escape\n");
        printf("Please input a number between 1 and 5: ");
        scanf("%d", &choose);
    }
    return choose;
}

TOP

复制内容到剪贴板
代码:
//娘的 这程序看起来短 但是不经意的时候十分容易出问题
#include <stdio.h>

#define MAX 10

int main(void)
{
    char ch, buffer[MAX+1];
    int x = 0; //要注意 这里是从0开始 所以11个只到10

    while ((ch =getchar()) != &#39;\n&#39; && x < MAX) //一次反馈一个ch MAX之后就不管了
        buffer[x++] = ch; //每次存储一个 最后一次x是10

    buffer[x] = &#39;\0&#39;; //把第10个放成空字符

    puts(buffer);
    printf("%s\n", buffer);

    return 0;
}

TOP

昨天看了 53楼的调用简单系统命令的第一个char input0[] = "ping www.bbnl.org";
的url要去掉啦,下面的 case 1  输出同理。

54楼的程序声明了几个加减乘除的无用函数但是没有使用.....还有在输出 printf("\n%d + %d = %d", x, y, (x + y)); 的时候最好把\n放后面这样美观点。

还有一点想说说的是53楼的第一个循环判断while (x > 1 || x < 4) 虽然这样是可以执行的但总觉得有点....  menu返回1虽然也能正确判断下面的switch,但个人觉得改为while (x > 0 && x < 5)在逻辑上要严密点。
BLOG: http://blog.csdn.net/hkbyest

TOP

引用:
下面是引用evilbogy于2005-03-17 13:32发表的:
昨天看了 53楼的调用简单系统命令的第一个char input0[] = "ping www.bbnl.org";
的url要去掉啦,下面的 case 1  输出同理。

54楼的程序声明了几个加减乘除的无用函数但是没有使用.....还有在输出 printf("n%d + %d = %d", x, y, (x + y)); 的时候最好把n放后面这样美观点。

.......
53楼那个有问题 具体我已经在另外一个程序更正了一下
其实应该是while(1)
用死循环实现
曾几何时,有人对我说:装B遭雷劈。我说:去你妈的。于是,这个人又对我说:如果再说脏话,上帝会惩罚你的。我说:我操上帝。结论:彪悍的人生不需要上帝。

TOP

要开始着手培养阅读代码的能力了
复制内容到剪贴板
代码:
//程序分析
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAXLINES 25

int get_lines(char *lines[]);
void sort(char *p[], int n);
void print_strings(char *p[], int n);

char *lines[MAXLINES];  //定义了一个25个元素的指针数组

int main( void )
{
  int number_of_lines;  //输入了多少行

  number_of_lines = get_lines(lines);  //接受字符串输入的函数

  if ( number_of_lines < 0 )  //返回值小于0就说空间分配失败
  {
     puts(" Memory allocation error");
     exit(-1);
  }

  sort(lines, number_of_lines);  //排序函数
  print_strings(lines, number_of_lines);
  return 0;
}

int get_lines(char *lines[])  //接受一个指向数组元素的指针
{
  int n = 0;
  char buffer[80];  //每行最多输入80个字符

  puts("Enter one line at time; enter a blank when done.");

  while ((n < MAXLINES) && (gets(buffer) != 0) && (buffer[0] != &#39;\0&#39;))  //进入循环的条件 必须全部满足
  {
     if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)  //动态分配空间
        return -1;  //分配失败返回-1
     strcpy( lines[n++], buffer );  //空间分配成功了就把字符串拷贝到数组里
  }
  
  return n;  //n是返回的字符串数量 就是你输入了几行 可以通过printf("%d", n);来验证

}

void sort(char *p[], int n)  //排序函数接受指向数组的指针和行数
{
  int a, b;
  char *x;

  for (a = 0; a < n-1; a++)  //遍历的次数 明显一次只能移动一个单位
  {
     for (b = 0; b < n-1; b++)  //遍历所有字符串一次
     {
        if (strcmp(p[b], p[b+1]) > 0)  //比较首字符的大小
        {
          x = p[b];  //如果前面的比后面的数字大就调换位置 越小越前面么
          p[b] = p[b+1];
          p[b+1] = x;  //x是用来暂时存储的
        }
     }
  }
}

void print_strings(char *p[], int n)  //打印字符串
{
  int count;

  for (count = 0; count < n; count++)
     printf("%s\n", p[count]);
}

TOP

复制内容到剪贴板
代码:
#include <stdio.h>

int main(void)
{
    int a=5, b=7;
    double x=67.8564,y=-789.124;
    char c=&#39;A&#39;;
    long n=1234567;
    unsigned u=65535;

    printf("%d%d\n",a,b);
    printf("%3d%3d\n",a,b);
    printf("%f,%f\n",x,y);
    printf("%-10f,%-10f\n",x,y);
    printf("%8.2f,%8.2f,%.4f,%.4f,%3f,%3f\n",x,y,x,y,x,y);
    printf("%e,%10.2e\n",x,y);
    printf("%c,%d,%o,%x\n",c,c,c,c);
    printf("%ld,%lo,%lx\n",n,n,n);
    printf("%u,%o,%x,%d\n",u,u,u,u);
    printf("%s,%5.3s\n","COMPUTER","COMPUTER");

    return 0;
}

TOP

复制内容到剪贴板
代码:
#include <stdio.h>

int square(int x);

int (*ptr)(int x);

int main(void)
{
    ptr = square;

    printf("%d %d\n", square(6), ptr(7));
    return 0;
}

int square(int x)
{
    return x * x;
}

TOP

  缺点 输入字母死循环 fuck
使用函数指针来调用不同的函数
复制内容到剪贴板
代码:
#include <stdio.h>

void func1(int x);
void one(void);
void two(void);
void other(void);

int main(void)
{
     int nbr;

     for(;;)
     {
          puts("Enter an integer between 1 and 10, 0 to exit:");
          scanf("%d", &nbr);

          if(nbr == 0)
              break;
          func1(nbr);
     }
     return 0;
}

void func1(int x)
{
     void (*ptr)(void);

     if(x == 1)
          ptr = one;
     else if(x == 2)
          ptr = two;
     else
          ptr = other;

     ptr();
}

void one(void)
{
     puts("You entered 1.");
}

void two(void)
{
     puts("You entered 2.");
}

void other(void)
{
     puts("You entered something other than 1 or 2.");
}

TOP

将指针作为参数传递给函数
高级指针真吐血
复制内容到剪贴板
代码:
#include <stdio.h>

void func1(void (*p)(void));  //函数接受一个指向函数的指针
void one(void);
void two(void);
void other(void);

int main(void)
{
     int nbr;
      void (*ptr)(void);  //在函数体里定义的函数指针

     for(;;)  //死循环来处理直到0后退出
     {
          puts("Enter an integer between 1 and 10, 0 to exit:");
          scanf("%d", &nbr);

          if(nbr == 0)  //等于0退出
              break;
              else if(nbr == 1)  //等于1的话
                  ptr = one;  //就把指针指向函数one
              else if(nbr == 2)
                  ptr = two;
              else
                  ptr = other;
          func1(ptr);  //调用函数 传递指针
     }
     return 0;
}

void func1(void (*p)(void))
{
     p();  //通过指针执行相应函数
}

void one(void)
{
     puts("You entered 1.");
}

void two(void)
{
     puts("You entered 2.");
}

void other(void)
{
     puts("You entered something other than 1 or 2.");
}

TOP

  字符串排序
复制内容到剪贴板
代码:
/* Inputs a list of strings from the keyboard, sorts them, */
/* and then displays them on the screen. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAXLINES 25

int get_lines(char *lines[]);
void sort(char *p[], int n);
void print_strings(char *p[], int n);

char *lines[MAXLINES];

int main( void )
{
  int number_of_lines;

  /* Read in the lines from the keyboard. */

  number_of_lines = get_lines(lines);

  if ( number_of_lines < 0 )
  {
     puts(" Memory allocation error");
     exit(-1);
  }

  sort(lines, number_of_lines);
  print_strings(lines, number_of_lines);
  return 0;
}

int get_lines(char *lines[])
{
  int n = 0;
  char buffer[80];  /* Temporary storage for each line. */

  puts("Enter one line at time; enter a blank when done.");

  while ((n < MAXLINES) && (gets(buffer) != 0) &&
       (buffer[0] != &#39;\0&#39;))
  {
     if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
        return -1;
     strcpy( lines[n++], buffer );
  }
  return n;

} /* End of get_lines() */

void sort(char *p[], int n)
{
  int a, b;
  char *x;

  for (a = 1; a < n; a++)
  {
     for (b = 0; b < n-1; b++)
     {
        if (strcmp(p[b], p[b+1]) > 0)
        {
          x = p[b];
          p[b] = p[b+1];
          p[b+1] = x;
        }
     }
  }
}

void print_strings(char *p[], int n)
{
  int count;

  for (count = 0; count < n; count++)
     printf("%s\n", p[count]);
}
函数指针实现 直接通过指针调用
复制内容到剪贴板
代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAXLINES 25

int get_lines(char *lines[]);

void sort1(char *p[], int n);
void sort2(char *p[], int n);
void print_strings(char *p[], int n);

char *lines[MAXLINES];

int main( void )
{
  int number_of_lines, x;
  void (*ptr)(char *p[], int n);  //定义在这里没有问题的

  number_of_lines = get_lines(lines);

  if ( number_of_lines < 0 )
  {
     puts(" Memory allocation error");
     exit(-1);
  }
  printf("Forward 1, Backword 2: ");
  scanf("%d", &x);

  if(x == 1)
  {
       ptr = sort1;  //原来我这SB竟然把整个函数连同参数部分都写右边了
  }
  else if(x == 2)
  {
       ptr = sort2;
  }
  ptr(lines, number_of_lines);  //原来我这SB竟然直接调用没给参数
  print_strings(lines, number_of_lines);
  return 0;
}

int get_lines(char *lines[])
{
  int n = 0;
  char buffer[80];

  puts("Enter one line at time; enter a blank when done.");

  while ((n < MAXLINES) && (gets(buffer) != 0) &&
       (buffer[0] != &#39;\0&#39;))
  {
     if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
        return -1;
     strcpy( lines[n++], buffer );
  }
  return n;

}

void sort1(char *p[], int n)
{
  int a, b;
  char *x;

  for (a = 1; a < n; a++)
  {
     for (b = 0; b < n-1; b++)
     {
        if (strcmp(p[b], p[b+1]) > 0)
        {
          x = p[b];
          p[b] = p[b+1];
          p[b+1] = x;
        }
     }
  }
}

void sort2(char *p[], int n)
{
  int a, b;
  char *x;

  for (a = 1; a < n; a++)
  {
     for (b = 0; b < n-1; b++)
     {
        if (strcmp(p[b], p[b+1]) < 0)
        {
          x = p[b];
          p[b] = p[b+1];
          p[b+1] = x;
        }
     }
  }
}

void print_strings(char *p[], int n)
{
  int count;

  for (count = 0; count < n; count++)
     printf("%s\n", p[count]);
}
将指针做为参数传递给函数 其实我都可以做到 只是麻烦 也是第一个函数修改的 不过这个是我自己修改的 后面可以看见书上修改的 可以对比
复制内容到剪贴板
代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAXLINES 25

int get_lines(char *lines[]);

void func1(void (* ptr_p)(char *p[], int n));
void sort1(char *p[], int n);
void sort2(char *p[], int n);
void print_strings(char *p[], int n);

char *lines[MAXLINES];
int number_of_lines;  //子函数要用 所以全局了

int main( void )
{
  int x;
  void (*ptr)(char *p[], int n);

  number_of_lines = get_lines(lines);

  if ( number_of_lines < 0 )
  {
     puts(" Memory allocation error");
     exit(-1);
  }
  printf("Forward 1, Backword 2: ");
  scanf("%d", &x);

  if(x == 1)
  {
       ptr = sort1;  //原来我这SB竟然把整个函数连同参数部分都写右边了
  }
  else if(x == 2)
  {
       ptr = sort2;
  }
  func1(ptr);
  print_strings(lines, number_of_lines);
  return 0;
}

void func1(void (* ptr_p)(char *p[], int n))
{
     ptr_p(lines, number_of_lines);  //在这里卡了很长时间 闹了半天参数作用域要全局 我操 那我还用你干个P
}

int get_lines(char *lines[])
{
  int n = 0;
  char buffer[80];

  puts("Enter one line at time; enter a blank when done.");

  while ((n < MAXLINES) && (gets(buffer) != 0) &&
       (buffer[0] != &#39;\0&#39;))
  {
     if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)
        return -1;
     strcpy( lines[n++], buffer );
  }
  return n;

}

void sort1(char *p[], int n)
{
  int a, b;
  char *x;

  for (a = 1; a < n; a++)
  {
     for (b = 0; b < n-1; b++)
     {
        if (strcmp(p[b], p[b+1]) > 0)
        {
          x = p[b];
          p[b] = p[b+1];
          p[b+1] = x;
        }
     }
  }
}

void sort2(char *p[], int n)
{
  int a, b;
  char *x;

  for (a = 1; a < n; a++)
  {
     for (b = 0; b < n-1; b++)
     {
        if (strcmp(p[b], p[b+1]) < 0)
        {
          x = p[b];
          p[b] = p[b+1];
          p[b+1] = x;
        }
     }
  }
}

void print_strings(char *p[], int n)
{
  int count;

  for (count = 0; count < n; count++)
     printf("%s\n", p[count]);
}
下面是书上写的 我详细阅读加了注释
复制内容到剪贴板
代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAXLINES 25

int get_lines(char *lines[]);  //管理输入的函数
void sort(char *p[], int n, int sort_type);  //排序
void print_strings(char *p[], int n);  //打印字符的函数
int alpha(char *p1, char *p2);  //顺序排列
int reverse(char *p1, char *p2);  //反序排列

char *lines[MAXLINES];  //字符型指针数组

int main( void )
{
  int number_of_lines, sort_type;

  number_of_lines = get_lines(lines);  //调用函数输入

  if ( number_of_lines < 0 )  //如果返回值小于0
  {
     puts("Memory allocation error");  //报错并退出
     exit(-1);
  }

  puts("Enter 0 for reverse order sort, 1 for alphabetical:" );  //0反序1顺序
  scanf("%d", &sort_type);  //输入值

  sort(lines, number_of_lines, sort_type);  //调用排序的函数 传递行数
  print_strings(lines, number_of_lines);  //把函数和行书传递过去打印 打印排列好的字符串
  return 0;  //返回
}

int get_lines(char *lines[])  //输入函数
{
   int n = 0;
   char buffer[80];

   puts("Enter one line at time; enter a blank when done.");

   while (n < MAXLINES && gets(buffer) != 0 && buffer[0] != &#39;\0&#39;)  //输入的条件 gets()的返回值以前学过
   {
      if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)  //为每行字符传分配空间
      return -1;  //分配空间出错返回的值
      strcpy( lines[n++], buffer );  //成功则把字符串拷贝进空间 然后n++ 继续循环直到退出
   }
   return n;  //反回n的值

}

void sort(char *p[], int n, int sort_type)  //接受一个指针数组指针 行数 和排序的类型
{
   int a, b;
   char *x;

   int (*compare)(char *s1, char *s2);  //定义一个指向指针的函数

  compare = (sort_type) ? reverse : alpha;  //通过判断来决定是使指针指向哪个函数

   for (a = 1; a < n; a++) //n-1次循环
   {
      for (b = 0; b < n-1; b++)  //两层循环才能完成所有的排序 这行是确定从头到尾遍历一次
      {
        if (compare(p[b], p[b+1]) > 0)  //调用前面指向的函数
        {
           x = p[b];  //用x来做中转
           p[b] = p[b+1];  //如果是满足条件就交换
           p[b+1] = x;
        }
      }
   }
}

void print_strings(char *p[], int n)  //n是接受的行数 p是指针数组
{
   int count;  //定义循环的次数

   for (count = 0; count < n; count++)  //因为n行 所以从0到n-1
      printf("%s\n", p[count]);  //直接调用接受的参数打印
}

int alpha(char *p1, char *p2)  //比较函数 接受两个数组指针 顺序排列
{
   return(strcmp(p2, p1));  //进行比较
}

int reverse(char *p1, char *p2)  //反序排列 接受的都一样是没关系的
{
    return(strcmp(p1, p2));  //其实都是传给它 只不过是谁减谁而已
}
曾几何时,有人对我说:装B遭雷劈。我说:去你妈的。于是,这个人又对我说:如果再说脏话,上帝会惩罚你的。我说:我操上帝。结论:彪悍的人生不需要上帝。

TOP

  链表的基础
复制内容到剪贴板
代码:
#include <stdio.h>
#include <stdlib.h>

#ifndef NULL
  #define NULL 0  //检查是否定义了NULL 如果没有就将其定义为0
#endif

struct list  //定义一个结构
{
  int   ch;  //结构成员
  struct list *next_rec;  //链表指针
};


typedef struct list LIST;
typedef LIST *LISTPTR;  //指向LIST结构类型的指针

//函数原型
LISTPTR add_to_list( int, LISTPTR );
void show_list(LISTPTR);
void free_memory_list(LISTPTR);

int main( void )
{
  LISTPTR first = NULL;  //首先把首指针初始化
  int i = 0;
  int ch;
  char trash[256];

  while ( i++ < 5 )  //输入5次 先比较后加加 ++优先级是高 但是要看在前在后
  {
    ch = 0;
    printf("\nEnter character %d: ", i);

    do
    {
       printf("\nMust be a to z: ");
       ch = getc(stdin);  //读取一个字符
       gets(trash);  //把余下的字符处理掉 但是有溢出隐患
    } while( (ch < &#39;a&#39; || ch > &#39;z&#39;) && (ch < &#39;A&#39; || ch > &#39;Z&#39;));  //限制字母必须的范围

    first = add_to_list( ch, first );  //调用函数实现链表 传给它字母和指针的头
  }

  show_list( first );
  free_memory_list( first );
  return 0;
}

LISTPTR add_to_list( int ch, LISTPTR first )
{
  LISTPTR new_rec = NULL;  //指向要加入的新节点
  LISTPTR tmp_rec = NULL;  //指向当前的节点
  LISTPTR prev_rec = NULL;  //指向当前节点的前一个节点

  new_rec = (LISTPTR)malloc(sizeof(LIST));  //为新节点分配空间
  if (!new_rec)  //如果是空 非了就是1
  {
    printf("\nUnable to allocate memory!\n");
    exit(1);  //报错并退出
  }

  new_rec->ch = ch;  //成功分配空间后 先把ch给新节点的ch
  new_rec->next_rec = NULL;  //新节点的指针先放空

  if (first == NULL)  //如果第一个节点等于空
  {
     first = new_rec;  //第一个节点就是新节点
     new_rec->next_rec = NULL;  //但是新节点的下面还是没有东西
  }
  else  //如果新节点不为空 那就是原来有东西
  {
    if ( new_rec->ch < first->ch )  //看看节点内容谁大 谁就靠后
    {
      new_rec->next_rec = first;  //如果原来的大 新来的就指向原来的 保证链表不丢失
      first = new_rec;  //而新的就是在第一个了
    }
    else  //如果原来的小
    {
      tmp_rec = first->next_rec;  //那就把原来的下一个指针指向一个当前节点
      prev_rec = first;  //把头给当前节点的上一个节点

      if ( tmp_rec == NULL )  //当前节点如果是空的
      {
         prev_rec->next_rec = new_rec;  //而前一个节点的指针指向新来的
      }
      else  //非空
      {
        while (( tmp_rec->next_rec != NULL))  //当下面也不为空
        {
          if( new_rec->ch < tmp_rec->ch )  //就把新的和下面的比 如果当前的大
          {
            new_rec->next_rec = tmp_rec;  //那么当前的就放到后面去了
            if (new_rec->next_rec != prev_rec->next_rec)  //如果指针指向的地址不等就报错
            {
              printf("ERROR");
              getc(stdin);
              exit(0);
            }
            prev_rec->next_rec = new_rec;  //则前一个的指针指向新的
            break;
          }
          else  //如果当前的小 则说明应该放到后面
          {
            tmp_rec = tmp_rec->next_rec;  //分别将指针指向下面的节点
            prev_rec = prev_rec->next_rec;  //这里真是多亏了书上的注释 被卡住了
          }
        }
        if (tmp_rec->next_rec == NULL)  //如果下面为空了 那么就到末尾了
        {
          if (new_rec->ch < tmp_rec->ch )  //如果新来的应该在末尾的前面
          {
            new_rec->next_rec = tmp_rec;  //那么就把新的指针指向末尾的头
            prev_rec->next_rec = new_rec;  //把前面一个的指针指向新来的
          }
          else  //如果比原来所有的都大
          {
            tmp_rec->next_rec = new_rec;  //那就应该放到末尾
            new_rec->next_rec = NULL;  //下面就是空空了
          }
        }
      }
    }
  }
  return(first);  //排序完成后把链表头返回
}

void show_list( LISTPTR first )  //可以看到函数接受整个链表
{
  LISTPTR cur_ptr;  //定义一个当前节点
  int counter = 1;  //用来计数

  printf("\n\nRec addr  Position  Data  Next Rec addr\n");
  printf("========  ========  ====  =============\n");

  cur_ptr = first;  //把接受到的链表头给指针
  while (cur_ptr != NULL )  //如果不等于空
  {
    printf("    %X    ", cur_ptr );
    printf("%2d     %c", counter++, cur_ptr->ch);
    printf("    %X  \n",cur_ptr->next_rec);
    cur_ptr = cur_ptr->next_rec;  //轮番打印直到结尾
  }
}


void free_memory_list(LISTPTR first)  //接受链表释放空间
{
  LISTPTR cur_ptr, next_rec;  //和刚才一样
  cur_ptr = first;  //先找到链表头

  while (cur_ptr != NULL)  //遍历
  {
    next_rec = cur_ptr->next_rec;
    free(cur_ptr);  //打印完了 要释放内存的
    cur_ptr = next_rec;
  }
}

TOP

作业6.1 最大公约 和最小公倍
复制内容到剪贴板
代码:
#include <stdio.h>
#include <stdlib.h>

int submultiple(int x, int y);
int multiple(int x, int y);

int count = 0;

int main(void)
{
int a = 0, b = 0;
int sm = 0, mm = 0;

printf("Please input 2 number by space: ");
scanf("%d %d", &a, &b);

printf("Select the list!\n");
printf("\tSubmultiple is 1, Multiple is 2, please select one: ");
scanf("%d", &count);

if ( count == 1 )
{
       sm = submultiple(a, b);
       if ( sm == -1 )
       {
        printf("Exit!");
        exit (1);
       }
       printf("submutiple is %d!", sm);
}
else if ( count == 2 )
{
       mm = multiple(a, b);
       if ( mm == -1 )
       {
        printf("Exit!");
        exit (1);
       }
       printf("mutiple is %d!", mm);
}
else
{
       printf("Error input!");
       exit (1);
}
getchar();
return 0;
}

int submultiple(int x, int y)
{
for(count = ( x >= y ? y : x ); count > 0; count --)
{
       if ( x % count == 0 && y % count == 0 )
        return count;
}
printf("No submultiple!\n");
return (-1);
}

int multiple(int x, int y)
{
for( count = 1; count <= ( x * y ); count++ )
{
       if ( count % x == 0 && count % y==0 )
        return count;
}
printf("No multiple!\n");
return (-1);
}
作业6.3 求 a+aa+aaa+aaaa+.....+aaaaaaaaaaaaaaaaaaaaaaaaaaaa 多少个a可以自己定义
复制内容到剪贴板
代码:
#include <stdio.h>
#include <math.h>

int main(void)
{
int a = 0, n = 0, i = 0;
double x = 0, s = 0;

printf("Please input a and n separate by space: ");
scanf("%d %d", &a, &n);

for(;i < n; i++ )
{
x = x + a * (pow( 10, i));
s += x;
}

printf("%f", s);
return 0;
}
作业6.8 求2/1+3/2+5/3+8/5+....求前20项的和 我只求前6
复制内容到剪贴板
代码:
#include <stdio.h>

int main(void)
{
float f1 = 1, f2 = 2, f3 = 3, f4 = 0, s = 0;

for(int i = 0; i < 3; i++ )
{
f4 = (f2/f1) + (f3/f2);
s += f4;
f1 = f3;
f2 = f2 + f3;
f3 = f1 + f2;
}
f4 = (2.0/1)+(3.0/2)+(5.0/3)+(8.0/5)+(13.0/8)+(21.0/13);  //验证用
printf("%f\n", s);
printf("%f\n", f4);  //验证用
return 0;
}