Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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_String_Memory_Concurrency_Buffer - Fatal编程技术网

C 我的字符串生成程序有什么问题?

C 我的字符串生成程序有什么问题?,c,string,memory,concurrency,buffer,C,String,Memory,Concurrency,Buffer,我有一个接受7个参数的程序。目前,第一个参数被忽略。我的主要函数fcfsa接受8个参数:s1、s2、x1、y1、z1、x2、y2、z2。s1和s2是字符指针变量,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 这是我的输出

我有一个接受7个参数的程序。目前,第一个参数被忽略。我的主要函数fcfsa接受8个参数:s1、s2、x1、y1、z1、x2、y2、z2。s1和s2是字符指针变量,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
我的主要任务是:

#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;
}
#包括
#包括
#包括
#包括“pslibrary.h”
无效部分0(字符*s1,字符*s2);
无效显示(字符*标题、字符*s1、字符*s2);
无效fcfsa(字符*s1,字符*s2,字符x1,字符y1,字符z1,字符x2,字符y2,字符z2);
int main(int argc,字符**argv){
int i;
printf(“作业0程序由Marcus Lorenzana编写\n”);
如果(argc!=8){
printf(“错误。参数数目错误\n”);
返回1;
}
printf(“输入:”);
对于(i=1;i<8;i++){
printf(“%s”,argv[i]);
}
printf(“\n”);
//获取最大字符串大小
int maxSize=0;
对于(i=1;i<8;i++){
maxSize+=atoi(argv[i]);
}
printf(“maxSize=%d\n”,maxSize);
字符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]);
显示(“第1部分”,str1、str2);
返回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); 
}
#包括
#包括
#包括“pslibrary.h”
无效部分0(字符*s1,字符*s2){
strcpy(s1,“rrwwwwrrrrrrrrrrrrr”);
strcpy(s2,“rrrrrrwwwwwwrrrrrrrrrrr”);
}
无效显示(字符*标题、字符*s1、字符*s2){
printf(“\n”);
printf(“%s\n”,标题);
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;
对于(i=0;is1len){
最长=s2len;
}否则{
最长=s1len;
}
浮动平均值R=(浮动)(s1cnt2+s2cnt2)/最长;
printf(“%d%d%.1f%.5f\n”,s1cnt,s2cnt,average\u r,average\u r);
}
无效fcfsa(字符*s1、字符*s2、整数x1、整数y1、整数z1、整数x2、整数y2、整数z2){
//s1:x1 R's、y1 w's、0个或更多R's、z1 R's
//s2:x1 r's、x2 r's、y2 w's、0或更多r's、z2 r's
int i;
//s1填充
int s1_start=0;
int s1_end=x1;
对于(i=s1\u开始;i
您偶尔会遇到一些故障问题吗?我认为在填充之前,您应该尝试使用x1+y1+z1作为第一个字符串的大小,x2+y2+z2作为第二个字符串的大小来填充两个新字符串

char *str1 = (char *)malloc(sizeof(char) * (x1 + y1 + z1 + 1)); // +1 for the '\0'
// here goes the code to fill str1
printf("str1:%s\n", str1);
free(str1);

不要忘记在代码中包含错误:

s2_start=s2_end;
s2_end+=z2; 
for (i = s2_start; i < s2_end; i++) {
    s1[i]='R';
 // ^^^^^^^  error
}
s2[s2_end]='\0';
你可以改变你的编码习惯来避免犯这样的错误。例如,将
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);
}
在哪里


}

使用所有警告和调试信息(例如,
gcc-Wall-g
)编译,然后使用调试器(例如,
gdb
),您可能希望自己尝试调试。请阅读此处的操作方法:当您要追加第二批
R
s时,在
fcfsa
中有
s1
。最好只调用一个函数两次,即每个字符串调用一次(对于
s1
,rs的数量为零);谢谢,这可能是一个很好的想法,用于获得分割错误,但这是由于我的for循环没有获得正确的索引,但我认为我修复了它。但我不认为我需要为字符串分配malloc空间,因为在调用fcfsa之前,两个字符串的最大字符串长度已经是所有6个参数加在一起的最大大小。很好,哈哈,我看了一段时间相同的代码,出于某种原因没有看到。请不要只是张贴代码墙而不告诉我们什么我们看到了。
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;
   } 
}
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';
}
void change(char* s,int position,char nChar);
void fcfsa(char *s1, char *s2,int x1,int y1,int z1,int x2,int y2,int z2){
    int i,position=0;
    //*s1=(char*)calloc(x1+y1+z1+1,sizeof(char));
    //*s2=(char*)calloc(x1+x2+y2+z2+1,sizeof(char));

    //initialization of s1
    for(i=0;i<x1;i++){
          s1[position]='R';
          position++;
    }
    for(i=0;i<y1;i++){
          s1[position]='w';
          position++;
    }
    for(i=0;i<z1;i++){
          s1[position]='R';
          position++;
    }
    s1[position]='\0';


    //initialization of s2
    position=0;
    for(i=0;i<x1;i++){
          s2[position]='r';
          position++;
    }
    for(i=0;i<x2;i++){
          s2[position]='R';
          position++;
    }
    for(i=0;i<y2;i++){
          s2[position]='w';
          position++;
    }
    for(i=0;i<z2;i++)
    {
          s2[position]='R';
          position++;
    }
    s2[position]='\0';

    //printf("\ns1=%s",s1);
    //printf("\ns2=%s",s2);

    i=0;
    while(s1[i]!='\0' && s2[i]!='\0')
    {
            if(s1[i]=='R' && s2[i]=='R')
            {
                    if(s2[i]==s2[i-1])
                    {
                         change(s1,i,'r');

                    }
                    else
                    {
                     change(s2,i,'r');
                    }
            }
            i++;
    }
void change(char* s,int position,char nChar){

int i,length;
//char *ptr;
length=strlen(s);
//ptr=(char*)calloc(length+2,sizeof(char));
//strcpy(ptr,s);

for(i=length;i>=position;i--)
{
         s[i+1]=s[i];
}
s[position]=nChar;