Advertisement
a53

Ecuatii

a53
Jan 22nd, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #define Lmax 256
  5. using namespace std;
  6. char s[Lmax],s1[Lmax],s2[Lmax];
  7. int n;
  8. long nr1,nr2,nrx1,nrx2;
  9.  
  10. void Solve(char *s,long &nr,long &nrx)
  11. {
  12. char *p,ss[Lmax];
  13. long v;
  14. int semn=1,L;
  15. strcpy(ss,s);
  16. p=strtok(ss,"+-");
  17. nr=0;
  18. nrx=0;
  19. while(p)
  20. {
  21. L=strlen(p);
  22. if(p[0]=='x')
  23. nrx+=semn;
  24. else
  25. if(p[L-1]=='x')
  26. p[L-1]=NULL,v=atol(p),nrx=nrx+semn*v;
  27. else
  28. v=atol(p),nr=nr+semn*v;
  29. if(s[p+L-ss]=='+')
  30. semn=1;
  31. else
  32. semn=-1;
  33. p=strtok(NULL,"+-");
  34. }
  35. }
  36.  
  37. int main()
  38. {
  39. FILE *f=fopen("ecuatii.in","r");
  40. FILE *g=fopen("ecuatii.out","w");
  41. int i;
  42. char *p;
  43. fscanf(f,"%d",&n);
  44. for(i=0;i<n;++i)
  45. {
  46. fscanf(f,"%s",s1);
  47. p=strchr(s1,'=');
  48. strcpy(s2,p+1);
  49. *p=NULL;
  50. Solve(s1,nr1,nrx1);
  51. Solve(s2,nr2,nrx2);
  52. if(nrx1==nrx2)
  53. if(nr1==nr2)
  54. fprintf(g,"infit\n");
  55. else
  56. fprintf(g,"imposibil\n");
  57. else
  58. fprintf(g,"%.4lf\n",((double)(nr2-nr1)/(nrx1-nrx2)));
  59. }
  60. fclose(f);
  61. fclose(g);
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement