fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. FILE * input;
  5. int errorFlag = 0;
  6.  
  7. int isDigit(int c)
  8. {
  9. /*do{
  10. c = getc(input);
  11. printf("%c", c);
  12. }
  13. while (c == ' ' || c == '\t' || c == '\n');
  14. ungetc(c, input);*/
  15. if (c >= '0' && c <= '9')
  16. return 1;
  17. else
  18. return 0;
  19. }
  20.  
  21. int op()
  22. {
  23. int a;
  24.  
  25. do
  26. a = getc(input);
  27. while (a == ' ' || a == '\t' || a == '\n');
  28. ungetc(a, input);
  29. if (a == '+')
  30. return 1;
  31. else if (a == '-')
  32. return 0;
  33. }
  34.  
  35. int oper(int a, int b, char c)
  36. {
  37. if (c == '+')
  38. return a + b;
  39. if (c == '-')
  40. return a - b;
  41. }
  42.  
  43. int main(){
  44. FILE *input, *output;
  45.  
  46. int num1 = 0;
  47. int num2 = 0;
  48. char op = '\0';
  49.  
  50. int value;
  51.  
  52. int varValue[26];
  53. char varName;
  54. int result;
  55.  
  56. char c,d;
  57. int cnt = 0;
  58. int cnt2 = 0;
  59.  
  60. input = fopen("input.txt", "r");
  61. output = fopen("output.txt", "w");
  62.  
  63. //No input file error
  64. if (input == NULL){
  65. printf("Missing input file.\n");
  66. return;
  67. }
  68.  
  69.  
  70.  
  71.  
  72. else {
  73. do {
  74. c = getc(input);
  75.  
  76. //printf("%c\n",c);
  77. if (c == ' ' || c == '\t' || c == '\n')
  78. continue;
  79.  
  80. if (isDigit(c)) {
  81. cnt2 += 1;
  82. if (cnt == 0) {
  83. d = c-48;
  84. cnt = 1;
  85. }
  86. else {
  87.  
  88. // call operator function
  89. d = oper(d, c-48, op);
  90. }
  91. }
  92. else {
  93. if (c == ';') {
  94. printf("%d\n", d);
  95. cnt = 0;
  96. cnt2 = 0;
  97. }
  98. else {
  99. op = c;
  100. }
  101. }
  102. }
  103. while (c != EOF);
  104. }
  105.  
  106.  
  107. if (fscanf(input, "%d", &num2) == EOF)
  108. printf("Empty input file. \n");
  109.  
  110.  
  111. fclose(input);
  112. fclose(output);
  113.  
  114. system("pause");
  115. return 0;
  116. }
  117.  
Runtime error #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
Missing input file.