我有一个带有7个参数的程序。现在,第一个参数被忽略。我的主要函数fcfsa具有8个参数:s1,s2,x1,y1,z1,x2,y2,z2。 s1和s2是char指针变量,而x1..z2是argv中连续顺序的最后6个整数参数。

fcfsa应该这样做:
第一个字符串s1将由x1 R组成,其后是y1 w,再由z1 R组成。
第二个字符串s2将由x1 r,x2 R和y2 w,z2 R组成。

但是用./main执行程序时,我没有得到正确的输出0 4 2 7 3 6 5
再次将第一个参数0暂时忽略。

这是我的输出:

inputs: 0 4 2 7 3 6 5
maxSize=27

Part 1

RRRRwwRRRRRRRRRRRR+Y?
rrrrRRRwwwwww

0 4 2.0 0.86364


而我的main.c:

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

void part0(char *s1, char *s2);
void display(char *heading, char *s1, char *s2);
void fcfsa(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2);

int main(int argc, char **argv) {
    int i;
    printf("Assignment 0 program was written by Marcus Lorenzana\n");
    if (argc != 8) {
        printf("Error. Wrong number of arguments\n");
        return 1;
    }
    printf("inputs: ");
    for (i = 1; i < 8; i++) {
        printf("%s ",argv[i]);
    }
    printf("\n");

    //Get maximum string size
    int maxSize=0;
    for (i = 1; i < 8; i++) {
        maxSize+=atoi(argv[i]);
    }
    printf("maxSize=%d\n",maxSize);

    char str1[maxSize],str2[maxSize];

    fcfsa(str1,str2,atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]),atoi(argv[6]),atoi(argv[7]));
   display("Part 1\n",str1,str2);

        return 0;
}


而我的程序包含fcfsa:

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

void part0(char *s1, char *s2){
    strcpy(s1,"RRwwwwwRRRRRRRRR");
    strcpy(s2,"rrRRRRwwwwwwwwrrRRRRRRR");
}

void display(char *heading, char *s1, char *s2){
    printf("\n");
    printf("%s\n",heading);
    printf("%s\n",s1);
    printf("%s\n",s2);
    printf("\n");
    int s1len = strlen(s1);
    int s2len = strlen(s2);
    int i,s1cnt,s2cnt,s1cnt2,s2cnt2;
    s1cnt=s2cnt=0;
    s1cnt2=s2cnt2=0;
    for (i = 0; i < s1len; i++) {
        if (s1[i]=='r')
            s1cnt++;
    }
    for (i = 0; i < s2len; i++) {
        if (s2[i]=='r')
            s2cnt++;
    }
    float average_r = (s1cnt+s2cnt)/2;

    for (i = 0; i < s1len; i++) {
        if (s1[i]=='R')
            s1cnt2++;
    }
    for (i = 0; i < s2len; i++) {
        if (s2[i]=='R')
            s2cnt2++;
    }

    int longest;
    if (s2len > s1len) {
        longest = s2len;
    } else {
        longest = s1len;
    }

    float average_R = (float)(s1cnt2+s2cnt2)/longest;

    printf("%d %d %.1f %.5f\n",s1cnt,s2cnt,average_r,average_R);
}

void fcfsa(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2){
    //s1: x1 R's, y1 w's, 0 or more r's, z1 R's
    //s2: x1 r's, x2 R's, y2 w's, 0 or more r's, z2 R's
    int i;
        //s1 fill
    int s1_start=0;
        int s1_end=x1;
    for (i = s1_start; i < s1_end; i++) {
        s1[i]='R';
    }
    s1_start=s1_end;
    s1_end+=y1;
    for (i = s1_start; i < s1_end; i++) {
        s1[i]='w';
    }
    s1_start=s1_end;
    s1_end+=z1;
    for (i = s1_start; i < s1_end; i++){
        s1[i]='R';
    }
    s1[s1_end]='\0';
        //printf("s1:%s\n",s1);
    //s2 fill
    int s2_start=0;
    int s2_end=x1;
    for (i = s2_start; i < s2_end; i++) {
        s2[i]='r';
    }
    s2_start=s2_end;
    s2_end+=x2;
    for (i = s2_start; i < s2_end; i++) {
        s2[i]='R';
    }
    s2_start=s2_end;
    s2_end+=y2;
    for (i = s2_start; i < s2_end; i++) {
        s2[i]='w';
    }
    s2_start=s2_end;
    s2_end+=z2;
    for (i = s2_start; i < s2_end; i++) {
        s1[i]='R';
    }
    s2[s2_end]='\0';
    //printf("s2:%s\n",s2);
}

最佳答案

您代码中的错误:

s2_start=s2_end;
s2_end+=z2;
for (i = s2_start; i < s2_end; i++) {
    s1[i]='R';
 // ^^^^^^^  error
}
s2[s2_end]='\0';


该行应为:

    s2[i]='R';


您可以更改自己的编码习惯,以避免犯此类错误。例如,将fcfsa分为两个函数。

void fcfsa(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2){
   fcfsa1(s1, x1, y1, z1);
   fcfsa2(s2, x1, x2, y2, z2);
}


哪里

void fcfsa1(char *s, int x1, int y1, int z1){
   //s: x1 R's, y1 w's, 0 or more r's, z1 R's
   int i;
   int start=0;
   int end=x1;
   for (i = start; i < end; i++) {
      s[i]='R';
   }

   start=end;
   end+=y1;
   for (i = start; i < end; i++) {
      s[i]='w';
   }
   start=end;
   end+=z1;
   for (i = start; i < end; i++){
      s[i]='R';
   }
   s[end]='\0';
}

void fcfsa2(char *s, int x1, int x2, int y2, int z2){
    //s: x1 r's, x2 R's, y2 w's, 0 or more r's, z2 R's
    int i;
    int start=0;
    int end=x1;
    for (i = start; i < end; i++) {
        s[i]='r';
    }
    start=end;
    end+=x2;
    for (i = start; i < end; i++) {
        s[i]='R';
    }
    start=end;
    end+=y2;
    for (i = start; i < end; i++) {
        s[i]='w';
    }
    start=end;
    end+=z2;
    for (i = start; i < end; i++) {
        s[i]='R';
    }
    s[end]='\0';
}


您可以通过编写辅助函数以用字符填充数组来进一步简化代码。

void fill(char *s, int start, int end, char c)
{
   int i;
   for (i = start; i < end; i++) {
      s[i]=c;
   }
}


然后,fcfsa1fcfsa2可以简化为:

void fcfsa1(char *s, int x1, int y1, int z1){
   //s: x1 R's, y1 w's, 0 or more r's, z1 R's
   int start=0;
   int end=x1;
   fill(s, start, end, 'R');

   start=end;
   end+=y1;
   fill(s, start, end, 'w');

   start=end;
   end+=z1;
   fill(s, start, end, 'R');

   s[end]='\0';
}

void fcfsa2(char *s, int x1, int x2, int y2, int z2){
    //s: x1 r's, x2 R's, y2 w's, 0 or more r's, z2 R's
    int start=0;
    int end=x1;
    fill(s, start, end, 'r');

    start=end;
    end+=x2;
    fill(s, start, end, 'R');

    start=end;
    end+=y2;
    fill(s, start, end, 'w');

    start=end;
    end+=z2;
    fill(s, start, end, 'R');

    s[end]='\0';
}

10-06 15:30