Advertisement
AnhVan1712

Tính tổng các số trong mảng

Mar 21st, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <string.h>
  3. int xuLy(char s[]);
  4. void xuat(int kq);
  5. int main()
  6. {
  7. char s[100];
  8. printf("Hay nhap chuoi !\n");
  9. gets(s);
  10. int kq = xuLy(s);
  11. xuat(kq);
  12. return 0;
  13. }
  14. int xuLy(char s[])
  15. {
  16. int tong = 0;
  17. int i = 0;
  18. while (i < strlen(s))
  19. {
  20. int tongSo = 0;
  21. if (s[i] == '-')
  22. {
  23. i++;
  24. while (s[i] >= '0' && s[i] <= '9')
  25. {
  26. tongSo = tongSo * 10 + s[i] - '0';
  27. i++;
  28. }
  29. tongSo = -tongSo;// khi so la so am
  30. tong = tong + tongSo;
  31. i++;
  32. }
  33. else
  34. {
  35. while (s[i] >= '0' && s[i] <= '9')
  36. {
  37. tongSo = tongSo * 10 + s[i] - '0';
  38. i++;
  39. }
  40. tong = tong + tongSo;
  41. i++;
  42. }
  43. }
  44. return tong;
  45. }
  46. void xuat(int kq)
  47. {
  48. printf("%d\n", kq);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement