2014-09-10 54 views
-1

我有一个程序,需要7个参数。现在第一个参数被忽略。我的主要函数fcfsa需要8个参数:s1,s2,x1,y1,z1,x2,y2,z2。 s1和s2是char指针变量,x1..z2是argv中连续顺序的最后6个整数参数。我的字符串生成程序有什么问题?

fcfsa应该这样做: 第一个字符串s1将包含一个x1 R,后跟y1 w's,然后是z1 R's。 第二个字符串s2由x1 r's,后面是x2 R,后面跟着y2 w's,后面跟着z2 R's组成。

但是,当使用./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); 
} 
+0

编译所有警告和调试信息(如:'GCC -Wall -g')然后**我们e调试器**(例如'gdb') – 2014-09-10 17:04:33

+0

你可能想尝试自己调试它。在这里阅读如何做到这一点:http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – alk 2014-09-10 17:06:15

+0

当你想附加第二批时,你在'fcfsa'中有's1' 'R's。只有一个函数可以调用两次,也就是每个字符串一次('s'的'r'为零')。 – 2014-09-10 17:11:13

回答

0

的错误代码:

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'; 
} 
+0

好赶上哈哈我看了一段时间相同的代码,由于某种原因没有看到。 – MeesterMarcus 2014-09-10 17:22:28

0

你有一些问题段错误occasionaly?我认为你应该尝试用x1 + y1 + z1作为第一个大小的malloc'ing两个新字符串,并且在填充之前将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); 

不要忘了#include <stdlib.h>

+0

我用来得到分段错误,但这是由于我的循环没有得到正确的索引,但我认为我解决了这个问题。但我不认为我需要malloc空间的字符串,因为在调用fcfsa之前,两个字符串的最大字符串长度已经是所有6个参数一起添加为最大大小。 – MeesterMarcus 2014-09-10 17:20:34

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; 

}

+0

请不要在没有告诉我们我们看到的内容的情况下发布代码。 – Andrew 2014-10-08 15:30:58