按字母顺序列出C中的列表?

时间:2018-12-06 19:11:37

标签: c

程序收到以下列表:

// Setting preferred inference device given user's intent  
m_inferenceDeviceSelected = m_useGPU ? LearningModelDeviceKind.DirectX : LearningModelDeviceKind.Cpu;   
m_device = new LearningModelDevice(m_inferenceDeviceSelected); // Executes w/ CPU or DirectX specified.
m_session = new LearningModelSession(m_model, m_device); // Access Violation Error w/ DirectX device

我想按字母顺序对它进行排序。

我编写了以下程序,但运行时遇到分段错误。

#!/bin/bash
#SBATCH --nodes=1 
#SBATCH --tasks-per-node=1
#SBATCH --time=24:00:00
#SBATCH --mem=40GB?
#SBATCH --job-name=Master_script
#call first slurm
sbatch a.slurm
#if it completes successfully
if a.slurm:
   sbatch b.slurm
   if b.slurm:
      sbatch c.slurm
      sbatch d.slurm
      if c.slurm:
          sbatch e.slurm
      else:
         echo "c.slurm did not complete successfully"
   else:
      echo "b.slurm did not complete successfully"
else:
    echo "a.slurm did not complete successfully"

1 个答案:

答案 0 :(得分:3)

正如bruceg建议的那样,您的for循环需要工作。而且,您可能要删除换行符。

还要记录行数。

我也做了进一步的清理:

#include <stdio.h>
#include <string.h>

#define NLINES  20
#define LWID    20

int
main()
{
    char a[NLINES][LWID];
    char tmp[LWID];
    char *cp;
    int count;
    int i;
    int j;
    FILE *file;

    file = fopen("./test.txt", "r");
    if (file == NULL)
        printf("FILE NOT Opened...\n");

    for (count = 0; count < NLINES; count++) {
        if (fgets(tmp, LWID, file) == NULL)
            break;

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

        strcpy(a[count], tmp);
        printf(" %s", a[count]);
    }
    printf("\n");

    fclose(file);

    for (i = 0; i < count; i++) {
        for (j = 0; j < count; j++) {
            if (strcmp(a[i], a[j]) < 0) {
                strcpy(tmp, a[i]);
                strcpy(a[i], a[j]);
                strcpy(a[j], tmp);
            }
        }
    }

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

    return 0;
}

以下是输出:

 Google Apple Microsoft Samsung
Apple
Google
Microsoft
Samsung