0

给定一个数字列表和一个正整数 k,我必须一次反转列表的元素,k 项。如果元素个数不是k的倍数,那么最后剩下的项应该保持原样:

例如:输入:1,2,3,4,5;2

输出:2,1,4,3,5

我使用了一个非常简单的概念,我在两端取两个指针 a&b 进行交换,并在整个循环中迭代它们。

我遇到了分段错误。我应该如何纠正它?

我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reverse(char str[],int p)
{
    int i,j,k,a,b;
    int len=strlen(str);
    printf("%d\n",len);
    a=0;
    b=p-1;
    while(1)
    {
        i=a;j=b;
        while(j>i)
        {
          k=str[i];
          str[i]=str[j];
          str[j]=k;
          i++;
          j--;
        }
        a=a+p;
        b=b+p;
        if(b>=len)
        break;  
    }
    for(i=0;i<len-1;i++)
    printf("%c,",str[i]);
    if(i==len-1)
    printf("%c",str[i]);
    printf("\n");
}
int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        printf("Wrong number of arguments\n");
        exit(0);
    }
    FILE *fp;
    fp=fopen(argv[1],"r");
    if(fp==NULL)
    {
        printf("File can't be openend:\n");
        exit(0);
    }
    char c;
    c=fgetc(fp);
    char str[100];
    memset(str,'0',100);
    int k,i;
    while(1)
    {
        k=0;
        while(c!=';')
        {
          if(c!=',')
          {
            str[k]=c;
            k++;
            c=fgetc(fp);
          }
          else
          c=fgetc(fp);
        }
        str[k]='\0';
        c=fgetc(fp);
        int p=c-'0';
        reverse(str,p);
        c=fgetc(fp);
        if(c==EOF)
        break;
        else
        c=fgetc(fp);
    }
    return 0;
}

谢谢!

4

1 回答 1

0

可能还有其他方法可以做到这一点,但这段代码有效。你在算法上做得很好reverse()。在reverse()函数中,唯一的变化是代码的格式和添加更完整的诊断打印。该main()函数被重写以读取一行fgets(),然后解析该行。如果不出意外,这样做可以更容易地报告错误输入(例如,尽管代码在不包含分号的行上不能正确报告,并且代码假定k一次项目始终是单个数字,并且它不坚持用逗号分隔的单个数字,并且...)。

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

static void reverse(char str[], int p)
{
    int i, j, k, a, b;
    int len = strlen(str);
    printf("Input:  %d: %s (%d)\n", len, str, p);
    a = 0;
    b = p - 1;
    while (1)
    {
        i = a;
        j = b;
        while (j > i)
        {
          k = str[i];
          str[i] = str[j];
          str[j] = k;
          i++;
          j--;
        }
        a = a + p;
        b = b + p;
        if (b >= len)
            break;  
    }
    printf("Output: ");
    for (i = 0; i < len - 1; i++)
        printf("%c, ", str[i]);
    if (i == len - 1)
        printf("%c", str[i]);
    printf("\n");
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        printf("Wrong number of arguments\n");
        exit(1);
    }
    FILE *fp = fopen(argv[1], "r");
    if (fp == NULL)
    {
        printf("File can't be openend:\n");
        exit(1);
    }
    char line[100];
    while (fgets(line, sizeof(line), fp) != 0)
    {
        printf("Read:   %s", line);
        fflush(0);
        char str[100];
        int k = 0;
        int i = 0;
        char c;
        while ((c = line[i++]) != '\0')
        {
            if (c == ';')
                break;
            else if (c == ',')
                continue;
            else
                str[k++] = c;
        }
        str[k] = '\0';
        printf("String: %s\n", str);
        if (c == ';')
        {
            c = line[i];
            int p = c - '0';
            reverse(str, p);
        }
    }
    fclose(fp);
    return 0;
}

样本输入数据

1,2,3,4,5;2
1,2,3,4,5,6,7;3
1,2,3,4,5,6,7,8,9;2
1,2,3,4,5,6,7,8,9;3
1,2,3,4,5,6,7,8,9;4
1,2,3,4,5,6,7,8,9;5

样本输出

Read:   1,2,3,4,5;2
String: 12345
Input:  5: 12345 (2)
Output: 2, 1, 4, 3, 5
Read:   1,2,3,4,5,6,7;3
String: 1234567
Input:  7: 1234567 (3)
Output: 3, 2, 1, 6, 5, 4, 7
Read:   1,2,3,4,5,6,7,8,9;2
String: 123456789
Input:  9: 123456789 (2)
Output: 2, 1, 4, 3, 6, 5, 8, 7, 9
Read:   1,2,3,4,5,6,7,8,9;3
String: 123456789
Input:  9: 123456789 (3)
Output: 3, 2, 1, 6, 5, 4, 9, 8, 7
Read:   1,2,3,4,5,6,7,8,9;4
String: 123456789
Input:  9: 123456789 (4)
Output: 4, 3, 2, 1, 8, 7, 6, 5, 9
Read:   1,2,3,4,5,6,7,8,9;5
String: 123456789
Input:  9: 123456789 (5)
Output: 5, 4, 3, 2, 1, 6, 7, 8, 9

调试原件

如果您必须调试原始代码,那么reverse()我添加的诊断打印将对此有所帮助。在main()代码中,您需要考虑在每个字符到达时对其进行回显,并可能打印str数组。你需要小心,因为你用零填充了它。您可以使用printf("String: %.*s\n", k, str);将输出限制kstr.

于 2013-04-11T12:13:20.720 回答