Cod sursa(job #933015)

Utilizator bggunaBogdan Guna bgguna Data 29 martie 2013 15:14:48
Problema A+B Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <stdio.h>
#include <stdlib.h>

int main()
{
  // open the file "adunare.in" for reading
  FILE *input = fopen("adunare.in", "rt");
  if (input == NULL) {
    fprintf(stderr, "Failed to open input file\n");
    exit(1);
  } // if
  
  // read the first line of the file
  char first_line[10];
  fgets(first_line, 10, input);
  
  // read the second line of the file
  char second_line[10];
  fgets(second_line, 10, input);
  
  // open the file "adunare.out" for writing
  FILE *output = fopen("adunare.out", "wt");
  if (output == NULL) {
    fprintf(stderr, "Failed to open output file\n");
    exit(1);
  } // if
  
  // convert the strings into integers
  int a = atoi(first_line);
  int b = atoi(second_line);
  
  // output a + b
  if (a + b < 2000000)
    fprintf(output, "%d\n", a + b);
  else {
    fprintf(stderr, "Sum is over the limit\n");
    exit(1);
  } // else
  
  // close the output file
  fclose(output);
  
  // close the input file
  fclose(input);
  
  return 0;
}