2014-09-10 51 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