execution - Adding an additional printf line affects C code -
i adding simple printf line in code , affects whether line before executed or not. restarted terminal it's still same. tried on online compiler , executor , there's no problem, in computer have problem. restarting pc doesn't help.
else { lf++; }
when put lf not increased , program outputs "0 strings" on pc, "4 strings" on http://www.tutorialspoint.com/compile_c_online.php
when replace with
else { lf++; printf("\ndo here?\n"); }
it outputs "4 strings" in both platforms (as should). can reason behind this? far know simple printf shouldn't affect whether line before executed or not, independent c compiler use.
here original code:
#include"mylib.h" #define max 67108863 char tab[100][27]; double val[100]; int l; void showtab(file *fout) { int i, j; printf("\nnumber of strings %d\n",l); for(i=0;i<l;i++) { fprintf(fout,"%lf ",val[i]); for(j=0;j<26;j++) { if(tab[i][j]) fputc('a'+j,fout); } fprintf(fout,"\n"); } } int find(char*s, int stl) { int i, j, b=1; for(i=0;i<l;i++) { if(tab[i][26]!=stl) continue; for(j=0;j<stl;j++) { if(tab[i][s[j]-'a']!=1) { b=0; break; } } if(b==0) { b=1; continue; } return i; } return -1; } void clean() { int i, j, k, lf=0; for(i=0;i<l-1 && j<l;i++) { if(j<=i) j=i+1; if(val[i]==0) { while(j<l) { if(val[j]!=0) { for(k=0;k<27;k++) tab[i][k]=tab[j][k]; val[i]=val[j]; val[j]=0; j++; lf++; break; } j++; } } else { lf++; } } l=lf; return; } int main() { char s[26], b=1, inequ; int adr, stl, i; double v; file* fin=fopen("data","r"); file* fout=fopen("canonical.form","w"); l=0; while(b) { fscanf(fin,"%s",s); if(s[0]!='<' && s[0]!='>') { fscanf(fin,"%lf",&v); stl=strlen(s); adr=find(s,stl); if(adr==-1) { for(i=0;i<26;i++) tab[l][i]=0; for(i=0;i<stl;i++) tab[l][s[i]-'a']=1; tab[l][26]=stl; val[l]=v; l++; } else val[adr]+=v; } else { if(s[0]=='<') inequ=-1; else inequ=1; b=0; } } while(fscanf(fin,"%s %lf",s,&v)==2) { stl=strlen(s); adr=find(s,stl); if(adr==-1) { for(i=0;i<26;i++) tab[l][i]=0; for(i=0;i<stl;i++) tab[l][s[i]-'a']=1; val[l]=-v; l++; } else val[adr]-=v; } clean(); if(inequ==-1) fprintf(fout,"0>=\n"); else fprintf(fout,"0<=\n"); showtab(fout); return 0; }
i don't use defined in mylib.h in piece of code except
#include<stdio.h> #include<stdlib.h> #include<string.h>
and here input file:
az 1.000000 z -1.000000 bz 1.000000 z -1.000000 abz -1.000000 z 1.000000 ab 1.000000 z 1.000000 abz -1.000000 -1.000000 b -1.000000 ab 1.000000 <= ab 1.000000 b -1.000000 zb 1.000000 b -1.000000 azb -1.000000 b 1.000000 ba 1.000000 -1.000000 za 1.000000 -1.000000 bza -1.000000 1.000000 xyab 3.000000 ab -3.000000 zab 3.000000 ab -3.000000 xyzab -3.000000 ab 3.000000
you don't initialize variables before using them invoke undefined behavior.
you need set j
, k
etc zero.
Comments
Post a Comment