Advertisement
a53

Ecuatii

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