版本68和71间的区别 (跳过第3版)
于2007-10-22 10:41:04修订的的版本68
大小: 1028
编辑: czk
备注:
于2007-10-22 15:09:30修订的的版本71
大小: 4443
编辑: 218
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 27: 行号 27:
=== 实验3 ===
 1. 拷贝5.4节中的程序:{{{#!cplusplus
   #define ALLOCSIZE 10000 /* size of available space */
行号 28: 行号 31:
   static char allocbuf[ALLOCSIZE]; /* storage for alloc */
   static char *allocp = allocbuf; /* next free position */

   char *alloc(int n) /* return pointer to n characters */
   {
       if (allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */
           allocp += n;
           return allocp - n; /* old p */
       } else /* not enough room */
           return 0;
   }

   void afree(char *p) /* free storage pointed to by p */
   {
       if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
           allocp = p;
   }

}}}
 1. 1.9节getline{{{#!cplusplus
  /* getline: read a line into s, return length */
   int getline(char s[],int lim)
   {
       int c, i;

       for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
           s[i] = c;
       if (c == '\n') {
           s[i] = c;
           ++i;
       }
       s[i] = '\0';
       return i;
   }
   
}}}
 1. 5.6节{{{#!cplusplus
   #include <stdio.h>
   #include <string.h>

   #define MAXLINES 5000 /* max #lines to be sorted */

   char *lineptr[MAXLINES]; /* pointers to text lines */

   int readlines(char *lineptr[], int nlines);
   void writelines(char *lineptr[], int nlines);

   void qsort(char *lineptr[], int left, int right);

   /* sort input lines */
   main()
   {
       int nlines; /* number of input lines read */

       if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
           qsort(lineptr, 0, nlines-1);
           writelines(lineptr, nlines);
           return 0;
       } else {
           printf("error: input too big to sort\n");
           return 1;
       }
   }

   #define MAXLEN 1000 /* max length of any input line */
   int getline(char *, int);
   char *alloc(int);

   /* readlines: read input lines */
   int readlines(char *lineptr[], int maxlines)
   {
       int len, nlines;
       char *p, line[MAXLEN];

       nlines = 0;
       while ((len = getline(line, MAXLEN)) > 0)
           if (nlines >= maxlines || (p = alloc(len)) == NULL)
               return -1;
           else {
               line[len-1] = '\0'; /* delete newline */
               strcpy(p, line);
               lineptr[nlines++] = p;
           }
       return nlines;
   }

   /* writelines: write output lines */
   void writelines(char *lineptr[], int nlines)
   {
       int i;

       for (i = 0; i < nlines; i++)
           printf("%s\n", lineptr[i]);
   }

}}} 及qsort和swap{{{#!cpluscplus
void qsort(char *v[], int left, int right)
{
    int i, last;
    void swap(char *v[], int i, int j);

    if (left >= right) /* do nothing if array contains */
        return; /* fewer than two elements */
    swap(v, left, (left + right)/2);
    last = left;
    for (i = left+1; i <= right; i++)
        if (strcmp(v[i], v[left]) < 0)
            swap(v, ++last, i);
    swap(v, left, last);
    qsort(v, left, last-1);
    qsort(v, last+1, right);
}
/* swap: interchange v[i] and v[j] */
void swap(char *v[], int i, int j)
{
    char *temp;

    temp = v[i];
    v[i] = v[j];
    v[j] = temp;
}
}}}
 1. 所有的qsort换一个名字,比如qsort2
 1. 改成readlines{{{
    while(scanf("%s", line)!=EOF) {
     len = strlen(line)+1;
}}}

TableOfContents

实验基本要求

  • 用标准C++完成程序
  • 按照传统C语言风格进行命名和排版
  • 在程序必要的地方进行注释

集成开发环境指导

  • 参见:["C++集成开发环境"]

实验内容

1. 05瓯信算

先打开实验报告模板,查看需要完成哪些题目,然后打开判题系统地址,在判题系统上做对相应题目,最后将正确的程序复制到实验报告中,发到邮箱。

判题系统地址:[http://wzuacm.3322.org/]

完成如下实验报告:

  • attachment:面向对象实验1.doc
  • attachment:面向对象实验2.doc
  • attachment:面向对象实验3.doc

在实验报告文件名后添加你的学号姓名,比如“面向对象实验1-0530215288-张三.doc”,然后email到: [email protected]

1.1. 实验3

  1. 拷贝5.4节中的程序:

       1    #define ALLOCSIZE 10000 /* size of available space */
       2 
       3    static char allocbuf[ALLOCSIZE]; /* storage for alloc */
       4    static char *allocp = allocbuf;  /* next free position */
       5 
       6    char *alloc(int n)    /* return pointer to n characters */
       7    {
       8        if (allocbuf + ALLOCSIZE - allocp >= n) {  /* it fits */
       9            allocp += n;
      10            return allocp - n; /* old p */
      11        } else      /* not enough room */
      12            return 0;
      13    }
      14 
      15    void afree(char *p)  /* free storage pointed to by p */
      16    {
      17        if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
      18            allocp = p;
      19    }
    
  2. 1.9节getline

       1   /* getline:  read a line into s, return length  */
       2    int getline(char s[],int lim)
       3    {
       4        int c, i;
       5 
       6        for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
       7            s[i] = c;
       8        if (c == '\n') {
       9            s[i] = c;
      10            ++i;
      11        }
      12        s[i] = '\0';
      13        return i;
      14    }
    
  3. 5.6节

       1    #include <stdio.h>
       2    #include <string.h>
       3 
       4    #define MAXLINES 5000     /* max #lines to be sorted */
       5 
       6    char *lineptr[MAXLINES];  /* pointers to text lines */
       7 
       8    int readlines(char *lineptr[], int nlines);
       9    void writelines(char *lineptr[], int nlines);
      10 
      11    void qsort(char *lineptr[], int left, int right);
      12 
      13    /* sort input lines */
      14    main()
      15    {
      16        int nlines;     /* number of input lines read */
      17 
      18        if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
      19            qsort(lineptr, 0, nlines-1);
      20            writelines(lineptr, nlines);
      21            return 0;
      22        } else {
      23            printf("error: input too big to sort\n");
      24            return 1;
      25        }
      26    }
      27 
      28    #define MAXLEN 1000  /* max length of any input line */
      29    int getline(char *, int);
      30    char *alloc(int);
      31 
      32    /* readlines:  read input lines */
      33    int readlines(char *lineptr[], int maxlines)
      34    {
      35        int len, nlines;
      36        char *p, line[MAXLEN];
      37 
      38        nlines = 0;
      39        while ((len = getline(line, MAXLEN)) > 0)
      40            if (nlines >= maxlines || (p = alloc(len)) == NULL)
      41                return -1;
      42            else {
      43                line[len-1] = '\0';  /* delete newline */
      44                strcpy(p, line);
      45                lineptr[nlines++] = p;
      46            }
      47        return nlines;
      48    }
      49 
      50    /* writelines:  write output lines */
      51    void writelines(char *lineptr[], int nlines)
      52    {
      53        int i;
      54 
      55        for (i = 0; i < nlines; i++)
      56            printf("%s\n", lineptr[i]);
      57    }
    

    及qsort和swap

    void qsort(char *v[], int left, int right)
    {
        int i, last;
        void swap(char *v[], int i, int j);
    
        if (left >= right)  /* do nothing if array contains */
            return;         /* fewer than two elements */
        swap(v, left, (left + right)/2);
        last = left;
        for (i = left+1; i <= right; i++)
            if (strcmp(v[i], v[left]) < 0)
                swap(v, ++last, i);
        swap(v, left, last);
        qsort(v, left, last-1);
        qsort(v, last+1, right);
    }
    /* swap:  interchange v[i] and v[j] */
    void swap(char *v[], int i, int j)
    {
        char *temp;
    
        temp = v[i];
        v[i] = v[j];
        v[j] = temp;
    }
  4. 所有的qsort换一个名字,比如qsort2
  5. 改成readlines

        while(scanf("%s", line)!=EOF) {
            len = strlen(line)+1;

2. 05计本

在判题系统[http://czk.8866.org/oj/]上完成3题,并提交实验报告:

  • attachment:面向对象实验模板.doc

实验报告发送到: [email protected]

C++实验 (2020-04-25 09:16:09由czk编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.