Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中文件的冒泡排序_C_File_Bubble Sort - Fatal编程技术网

C中文件的冒泡排序

C中文件的冒泡排序,c,file,bubble-sort,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:

我试图弄明白为什么从文件创建气泡排序时会收到错误消息。它可以是任何来自文件的文件,这取决于我何时执行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.
p8.c:在函数“main”中:
p8.c:63:24:警告:格式“%s”需要匹配的“char*”参数[-Wformat=]
printf(“用法:%s\n”)/*这应该从一开始就遵循
~^
p8.c:67:22:警告:函数“atof”的隐式声明;你是说“费奥夫”吗?[-Wimplicit函数声明]
char数组[]=atof(argv[2]);
^~~~
费奥夫
p8.c:67:22:错误:初始值设定项无效
p8.c:68:18:警告:从不兼容的指针类型[-Wincompatible指针类型]传递'bubblesort'的参数1
气泡排序(数组,100);
^~~~~
p8.c:25:24:注意:应为'char**',但参数的类型为'char*'
void bubblesort(字符*A[],整数n)
~~~~~~^~~
make:**[Makefile:37:p8.o]错误1
-4.0 p8未能编译。
这是我的代码分配:

/* 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;
}
/*4分*/
#包括
#包括
#定义MAXLEN 1000
/**
*填写bubblesort函数:https://en.wikipedia.org/wiki/Bubble_sort
*使用此伪代码,将其转换为C代码:
*程序bubbleSort(A:可排序项列表,n:长度(A))
*重复
*newn=0
*对于i=1到n-1(包括do)
*如果A[i-1]>A[i],那么
*  
掉期(A[i-1],A[i])
*newn=i
*如果结束
*结束
*n=newn

*在n之前,存在许多错误。我创建了两个版本的代码。一个带有错误注释的。和另一个清理和工作版本


以下是注释版本:

/* 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分*/
#包括
#包括
//注意/错误:我们需要这个用于atof
#如果1
#包括
#包括
#恩迪夫
#定义MAXLEN 1000
/**
*填写bubblesort函数:https://en.wikipedia.org/wiki/Bubble_sort
*使用此伪代码,将其转换为C代码:
*程序bubbleSort(A:可排序项列表,n:长度(A))
*重复
*newn=0
*对于i=1到n-1(包括do)
*如果A[i-1]>A[i],那么
*
掉期(A[i-1],A[i])
*newn=i
*如果结束
*结束
*n=newn

*在n之前,您是在尝试读取文件并将值转换为
float
还是在尝试读取字符串列表?还有很多其他的bug,但是在我能帮助你之前,我需要知道这一点。
/* 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;
}