Can someone please explain why my program is wrong? It works correctly only for the last two test datas.
[cpp]
/*@BEGIN_OF_SOURCE_CODE*/
#include <stdio.h>
#include <string.h>
int len;
char strand[256]={'\0'};
char *seq[4][4][4]={
{ {"Phe","Phe","Leu","Leu"},{"Ser","Ser","Ser","Ser"},{"Tyr","Tyr","END","END"},{"Cys","Cys","END","Trp"} },
{ {"Leu","Leu","Leu","Leu"},{"Pro","Pro","Pro","Pro"},{"His","His","Gln","Gln"},{"Arg","Arg","Arg","Arg"} },
{ {"Ile","Ile","Ile","Met"},{"Thr","Thr","Thr","Thr"},{"Asn","Asn","Lys","Lys"},{"Ser","Ser","Arg","Arg"} },
{ {"Val","Val","Val","Val"},{"Ala","Ala","Ala","Ala"},{"Asp","Asp","Glu","Glu"},{"Gly","Gly","Gly","Gly"} }
};
int val(char a){
switch(a){
case 'T': return 0;
case 'C': return 1;
case 'A': return 2;
case 'G': return 3;
}
return 0;
}
bool check(){
int i;
char codon[4]={'\0'},amino[4]={'\0'},out[350]={'\0'};
bool start=false,firstcodon=false;
for(i=0;i<len;i++){
codon[i%3]=strand;
if(i%3==2){
strcpy(amino,seq[val(codon[0])][val(codon[1])][val(codon[2])]);
if(strcmp(amino,"Met")==0) start=true;
else if(strcmp(amino,"END")==0) break;
if(start){
if(firstcodon){
if(out[0]!='\0') strcat(out,"-");
strcat(out,amino);
}
firstcodon=true;
}
}
}
if(start) printf("%s\n",out);
return start;
}
void reverse(){
int i;
char a;
for(i=len/2;i>=0;i--){
a=strand;
strand=strand[len-i-1];
strand[len-i-1]=a;
}
return;
}
void complement(){
int i;
for(i=0;i<len;i++){
switch(strand){
case 'T': strand='A'; break;
case 'C': strand='G'; break;
case 'A': strand='T'; break;
case 'G': strand='C'; break;
}
}
return;
}
int main(){
while(gets(strand)){
if(strand[0]=='*') break;
len=strlen(strand);
complement();
if(!check()){
complement();
if(!check()){
reverse();
if(!check()){
complement();
if(!check()) printf("*** No translatable DNA found ***\n");
}
}
}
}
return 0;
}
/*@END_OF_SOURCE_CODE*/
[/cpp]
Thanks a lot.
