Guest User

Untitled

a guest
Dec 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. double get_res(char *buff) {
  7. double arg;
  8. if (sscanf(buff, "sin(%lf)%*[ ]s=", &arg)) {
  9. char *nxt = strchr(buff, '=') + 1;
  10. *nxt = 0;
  11. return sin(arg);
  12. }
  13. if (sscanf(buff, "cos(%lf)%*[ ]s=", &arg)) {
  14. char *nxt = strchr(buff, '=') + 1;
  15. *nxt = 0;
  16. return cos(arg);
  17. }
  18. if (sscanf(buff, "tg(%lf)%*[ ]s=", &arg)) {
  19. char *nxt = strchr(buff, '=') + 1;
  20. *nxt = 0;
  21. return tan(arg);
  22. }
  23. if (sscanf(buff, "ctg(%lf)%*[ ]s=", &arg)) {
  24. char *nxt = strchr(buff, '=') + 1;
  25. *nxt = 0;
  26. return 1. / tan(arg);
  27. }
  28. return NAN;
  29. }
  30.  
  31. int process(const char *in, const char *out) {
  32. FILE *fin = fopen(in, "r"),
  33. *fout = fopen(out, "w");
  34.  
  35. if (!fin) {
  36. fclose(fout);
  37. return 0;
  38. }
  39.  
  40. char buff[100];
  41.  
  42. while (fgets(buff, 100, fin)) {
  43. double res = get_res(buff);
  44. if (isnan(res)) {
  45. fclose(fout);
  46. return 0;
  47. }
  48. fprintf(fout, "%s %lf\n", buff, res);
  49. }
  50.  
  51. fclose(fin);
  52. fclose(fout);
  53. return 1;
  54. }
  55.  
  56. int main(void) {
  57. if (!process("input.txt", "output.txt")) {
  58. printf("Error!\n");
  59. }
  60.  
  61. return 0;
  62. }
Add Comment
Please, Sign In to add comment