fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. #define CONSTANT1 (85e-6)
  7. #define CONSTANT2 (2e8)
  8. #define CONSTANT3 (40.0 / 6.0)
  9. #define CONSTANT4 (48.0 / 6.0)
  10. #define CONSTANT5 (136.67)
  11.  
  12. int main() {
  13. double deflection_values[1000]; // Assuming a fixed size or use dynamic allocation
  14. int index = 0;
  15. double x, y, result, term1, term2, term3, term4;
  16. char input[100];
  17.  
  18. // Open gnuplot in a pipe
  19. FILE *gp = popen("gnuplot -persist", "w");
  20. if (gp == NULL) {
  21. fprintf(stderr, "Error opening gnuplot\n");
  22. return 1;
  23. }
  24.  
  25. while (1) {
  26. printf("Enter a value for x (or type 'exit' to stop): ");
  27. fgets(input, sizeof(input), stdin);
  28.  
  29. if (strcmp(input, "exit\n") == 0) {
  30. break;
  31. }
  32.  
  33. if (sscanf(input, "%lf", &x) != 1) {
  34. printf("Invalid input. Please enter a valid numeric value for x.\n");
  35. continue;
  36. }
  37.  
  38. term1 = (28 * pow(x, 3)) / 6;
  39. term2 = CONSTANT3 * pow((x - 3), 3);
  40. term3 = CONSTANT4 * pow((x - 5), 3);
  41. term4 = CONSTANT5 * x;
  42.  
  43. // Check if term2 and term3 are negative
  44. if (term2 <= 0) {
  45. term2 = 0;
  46. }
  47. if (term3 < 0) {
  48. term3 = 0;
  49. }
  50.  
  51. result = term1 - term2 - term3 - term4;
  52. y = result / (CONSTANT1 * CONSTANT2);
  53. deflection_values[index++] = y;
  54.  
  55. printf("Deflection (y) for x = %.6f: %.6f m\n", x, y);
  56. }
  57.  
  58. // Plot deflection values using gnuplot
  59. fprintf(gp, "set title 'Deflection vs. Input Value'\n");
  60. fprintf(gp, "set xlabel 'Input Value (x)'\n");
  61. fprintf(gp, "set ylabel 'Deflection (y)'\n");
  62. fprintf(gp, "plot '-' with lines title 'Deflection (y)'\n");
  63.  
  64. for (int i = 0; i < index; i++) {
  65. fprintf(gp, "%.6f\n", deflection_values[i]);
  66. }
  67.  
  68. fprintf(gp, "e\n");
  69.  
  70. // Close gnuplot pipe
  71. pclose(gp);
  72.  
  73. return 0;
  74. }
  75.  
Success #stdin #stdout #stderr 0s 5308KB
stdin
9
20
35
45
67
exit
stdout
Enter a value for x (or type 'exit' to stop): Deflection (y) for x = 9.000000: 0.012939 m
Enter a value for x (or type 'exit' to stop): Deflection (y) for x = 20.000000: -1.479612 m
Enter a value for x (or type 'exit' to stop): Deflection (y) for x = 35.000000: -14.067850 m
Enter a value for x (or type 'exit' to stop): Deflection (y) for x = 45.000000: -34.518832 m
Enter a value for x (or type 'exit' to stop): Deflection (y) for x = 67.000000: -132.932170 m
Enter a value for x (or type 'exit' to stop): 
stderr
sh: 1: gnuplot: not found