Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int cal(char s[])
- {
- char str [1000];
- int a, sum = 0;
- int i=0, l= strlen(s);
- while (i < l)
- {
- if (s[i] == '+') i++;
- sscanf(s + i, "%d", &a); // lay tung so trong chuoi
- sum += a;
- sprintf(str, "%d", a); // doi so a ra chuoi str de xac dinh vi tri i
- i += strlen(str);
- }
- return sum;
- }
- int main()
- {
- char s[1000], ch;
- int result;
- FILE *f = NULL;
- f=fopen("input.inp","rt");
- if (f==NULL)
- {
- printf("Error opening file");
- return 0;
- }
- while((fscanf(f,"%s%c",&s,&ch)!=EOF))
- {
- result = cal(s);
- printf("%s = %d\n", s, result);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement