Advertisement
nguyenvanquan7826

tinh toan

Oct 8th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int cal(char s[])
  5. {
  6.     char str [1000];
  7.     int a, sum = 0;
  8.     int i=0, l= strlen(s);
  9.     while (i < l)
  10.     {
  11.         if (s[i] == '+') i++;
  12.         sscanf(s + i, "%d", &a);    // lay tung so trong chuoi
  13.         sum += a;
  14.         sprintf(str, "%d", a);      // doi so a ra chuoi str de xac dinh vi tri i
  15.         i += strlen(str);
  16.     }
  17.     return sum;
  18. }
  19.  
  20. int main()
  21. {
  22.     char s[1000], ch;
  23.     int result;
  24.     FILE *f = NULL;
  25.     f=fopen("input.inp","rt");
  26.     if (f==NULL)
  27.     {
  28.         printf("Error opening file");
  29.         return 0;
  30.     }
  31.     while((fscanf(f,"%s%c",&s,&ch)!=EOF))
  32.     {
  33.         result = cal(s);
  34.         printf("%s = %d\n", s, result);
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement