在C

时间:2018-11-10 20:18:06

标签: c file bubble-sort

我试图弄清楚为什么我会收到错误消息,从文件创建气泡排序。它可以是来自文件的任何文件,具体取决于我执行labcheck命令的时间。这已经到期了,但是我真的很想知道为什么。 C编程中的注释是CS讲师的指示。任何人都可以帮助修复C程序吗?

这是我进行实验室检查的时间:

p8.c: In function ‘main’:
p8.c:63:24: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
       printf ("Usage: %s <file>\n");/*this should be followed as from
                       ~^
p8.c:67:22: warning: implicit declaration of function ‘atof’; did you mean ‘feof’? [-Wimplicit-function-declaration]
       char array[] = atof (argv[2]);
                      ^~~~
                      feof
p8.c:67:22: error: invalid initializer
p8.c:68:18: warning: passing argument 1 of ‘bubblesort’ from incompatible pointer type [-Wincompatible-pointer-types]
       bubblesort(array, 100);
                  ^~~~~
p8.c:25:24: note: expected ‘char **’ but argument is of type ‘char *’
 void bubblesort (char *A[], int n)
                  ~~~~~~^~~
make: *** [Makefile:37: p8.o] Error 1
-4.0 p8 failed to compile.

这是我的代码分配:

/* 4 points */
#include <stdio.h>
#include <string.h>

#define MAXLEN 1000

/**
 * Fill in the bubblesort function: https://en.wikipedia.org/wiki/Bubble_sort
 * Using this pseudo-code, convert to C code:
 * procedure bubbleSort( A : list of sortable items, n : length(A) )
 *     repeat
 *         newn = 0
 *         for i = 1 to n-1 inclusive do
 *             if A[i-1] > A[i] then
 *  

                swap(A[i-1], A[i])
 *                 newn = i
 *             end if
 *         end for
 *         n = newn
 *     until n <= 1
 * end procedure
 */
void bubblesort (char *A[], int n)
{
  int i, j, temp;
  int length;

  for (i = 0; i < length; i++)
    {
      for (j = 0; j < length - 1; j++)
  {
    if (*A[j + 1] < *A[j])
      {
        temp = *A[j];
        *A[j] = *A[j + 1];
        *A[j + 1] = temp;
      }
  }
    }
}
/**
 * Create a main function that opens a file composed of words, one per line
 * and sorts them using the bubble-sort algorithm.
 * Usage: "Usage: %s <file>\n"
 * Example input/output:
 * ./p8 testfile
 * Bob
 * Bubbles
 * Butters
 * Dave
 * ...
 */

int main (int argc, char *argv[]) {
  int rc, i;//rc is Read Character 

  if (argc < 2)
    {
      printf ("Usage: %s <file>\n");/*this should be followed as from 
      instruction, nothing can be change due to error message.*/
      rc = -1;
    } else {
      char array[] = atof (argv[2]);
      bubblesort(array, 100);
      rc = 0;
      while (rc < i)
  {
    printf ("%c\n", array[rc]);
    rc++;
  }
    }
  // prints print new lines from file 
  return 0;
}

1 个答案:

答案 0 :(得分:2)

有很多错误。我创建了两个版本的代码。其中一个带有注释的错误。还有另一个清理工作的版本。


这是带注释的版本:

/* 4 points */
#include <stdio.h>
#include <string.h>

// NOTE/BUG: we need this for atof
#if 1
#include <stdlib.h>
#include <errno.h>
#endif

#define MAXLEN 1000

/**
 * Fill in the bubblesort function: https://en.wikipedia.org/wiki/Bubble_sort
 * Using this pseudo-code, convert to C code:
 * procedure bubbleSort( A : list of sortable items, n : length(A) )
 *     repeat
 *         newn = 0
 *         for i = 1 to n-1 inclusive do
 *             if A[i-1] > A[i] then
 *

                swap(A[i-1], A[i])
 *                 newn = i
 *             end if
 *         end for
 *         n = newn
 *     until n <= 1
 * end procedure
 */
void
bubblesort(char *A[], int n)
{
    int i,
     j,
     temp;
    int length;

// NOTE/BUG: temp needs to be "char *temp"
// NOTE/BUG: "length" should be "n" here
// NOTE/BUG: replace all "*A[whatever]" with "A[whatever]"
    for (i = 0; i < length; i++) {
        for (j = 0; j < length - 1; j++) {
// NOTE/BUG: we need strcmp here
            if (*A[j + 1] < *A[j]) {
                temp = *A[j];
                *A[j] = *A[j + 1];
                *A[j + 1] = temp;
            }
        }
    }
}

/**
 * Create a main function that opens a file composed of words, one per line
 * and sorts them using the bubble-sort algorithm.
 * Usage: "Usage: %s <file>\n"
 * Example input/output:
 * ./p8 testfile
 * Bob
 * Bubbles
 * Butters
 * Dave
 * ...
 */

int
main(int argc, char *argv[])
{
    int rc,
     i;                                 // rc is Read Character

    if (argc < 2) {
/* this should be followed as from instruction, nothing can be change due to
error message. */
// NOTE/BUG: this is missing the argv[0] argument
#if 0
        printf("Usage: %s <file>\n");
#else
        printf("Usage: %s <file>\n",argv[0]);
#endif
        rc = -1;
    }
    else {
// NOTE/BUG: this is too short (i.e. it needs to be 100)
// NOTE/BUG: we need to read in the values
#if 0
        char array[] = atof(argv[2]);
        bubblesort(array, 100);
#else
        char *array[100];

// NOTE/BUG: this should be argv[1]
        FILE *fi = fopen(argv[2],"r");
        if (fi == NULL) {
            printf("unable to open '%s' -- %s\n",argv[2],strerror(errno));
            exit(1);
        }

        int arrcnt = 0;
        while (1) {
            char buf[1000];
            char *cp = fgets(buf,sizeof(buf),fi);
            if (cp == NULL)
                break;

            cp = strchr(buf,'\n');
            if (cp != NULL)
                *cp = 0;

            cp = strdup(cp);
            array[arrcnt++] = cp;
        }

        fclose(fi);

        bubblesort(array,arrcnt);
#endif

        rc = 0;
        while (rc < i) {
// NOTE/BUG: this should be "%s"
            printf("%c\n", array[rc]);
            rc++;
        }
    }

    // prints print new lines from file
    return 0;
}

这是清理后的工作版本:

/* 4 points */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#define MAXLEN 1000

/**
 * Fill in the bubblesort function: https://en.wikipedia.org/wiki/Bubble_sort
 * Using this pseudo-code, convert to C code:
 * procedure bubbleSort( A : list of sortable items, n : length(A) )
 *     repeat
 *         newn = 0
 *         for i = 1 to n-1 inclusive do
 *             if A[i-1] > A[i] then
 *

                swap(A[i-1], A[i])
 *                 newn = i
 *             end if
 *         end for
 *         n = newn
 *     until n <= 1
 * end procedure
 */
void
bubblesort(char *A[], int n)
{
    int i, j;
    char *temp;

    for (i = 0; i < n; i++) {
        for (j = 0; j < n - 1; j++) {
            if (strcmp(A[j + 1],A[j]) < 0) {
                temp = A[j];
                A[j] = A[j + 1];
                A[j + 1] = temp;
            }
        }
    }
}

/**
 * Create a main function that opens a file composed of words, one per line
 * and sorts them using the bubble-sort algorithm.
 * Usage: "Usage: %s <file>\n"
 * Example input/output:
 * ./p8 testfile
 * Bob
 * Bubbles
 * Butters
 * Dave
 * ...
 */

int
main(int argc, char *argv[])
{
    int rc = 0;

    if (argc < 2) {
        /* this should be followed as from instruction, nothing can be change
        due to error message. */
        printf("Usage: %s <file>\n",argv[0]);
        rc = 1;
    }
    else {
        char *array[100];
        int arrmax = sizeof(array) / sizeof(array[0]);

        FILE *fi = fopen(argv[1],"r");
        if (fi == NULL) {
            printf("unable to open '%s' -- %s\n",argv[1],strerror(errno));
            exit(1);
        }

        int arrcnt = 0;
        while (1) {
            char buf[MAXLEN];
            char *cp;

            cp = fgets(buf,sizeof(buf),fi);
            if (cp == NULL)
                break;

            if (arrcnt >= arrmax) {
                printf("too many input lines\n");
                exit(1);
            }

            cp = strchr(buf,'\n');
            if (cp != NULL)
                *cp = 0;

            cp = strdup(buf);
            array[arrcnt++] = cp;
        }

        fclose(fi);

        bubblesort(array,arrcnt);

        for (int arridx = 0;  arridx < arrcnt;  ++arridx)
            printf("%s\n", array[arridx]);
    }

    // prints print new lines from file
    return rc;
}