fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. std::string & ltrim(std::string & str)
  7. {
  8. auto it2 = std::find_if( str.begin() , str.end() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
  9. str.erase( str.begin() , it2);
  10. return str;
  11. }
  12.  
  13. std::string & rtrim(std::string & str)
  14. {
  15. auto it1 = std::find_if( str.rbegin() , str.rend() , [](char ch){ return !std::isspace<char>(ch , std::locale::classic() ) ; } );
  16. str.erase( it1.base() , str.end() );
  17. return str;
  18. }
  19.  
  20. string add(string n1, string n2)
  21. {
  22. string sum = "";
  23.  
  24. int temp;
  25. int len;
  26. int carry = 0;
  27.  
  28. int diff = n1.length() - n2.length();
  29.  
  30. if (diff == 0)
  31. len = n1.length();
  32. else if (diff > 0)
  33. len = n2.length();
  34. else
  35. len = n1.length();
  36.  
  37. for (int i = 0; i<len; i++)
  38. {
  39. temp = n1[i] + n2[i] - 2 * '0' + carry;
  40.  
  41. if (temp > 9) {
  42. sum += (temp % 10) + '0';
  43. carry = 1;
  44. }
  45. else {
  46. sum += temp + '0';
  47. carry = 0;
  48. }
  49. }
  50.  
  51. if (len < n1.length())
  52. {
  53. for (int i = len; i< n1.length(); i++)
  54. {
  55. temp = n1[i] + carry - '0';
  56.  
  57. if (temp > 9) {
  58. sum += (temp % 10) + '0';
  59.  
  60. carry = 1;
  61. }
  62. else {
  63. sum += temp + '0';
  64. carry = 0;
  65. }
  66. }
  67. }
  68. else if (len > n1.length())
  69. {
  70. for (int i = len; i< n2.length(); i++)
  71. {
  72. temp = n2[i] + carry - '0';
  73.  
  74. if (temp > 9) {
  75. sum += (temp % 10) - '0';
  76. carry = 1;
  77. }
  78. else {
  79. sum += temp + '0';
  80. carry = 0;
  81. }
  82. }
  83. }
  84.  
  85.  
  86.  
  87. if (carry)
  88. return sum + '1';
  89. else
  90. return sum;
  91.  
  92. return ltrim(rtrim("0"));
  93. }
  94.  
  95. int main() {
  96.  
  97. int n;
  98. string n1, n2;
  99.  
  100. cin >> n;
  101.  
  102. while (n>0)
  103. {
  104. cin >> n1;
  105. cin >> n2;
  106.  
  107. n--;
  108.  
  109. cout << add(n1, n2) << endl;
  110. }
  111.  
  112.  
  113. return 0;
  114. }
Compilation error #stdin compilation error #stdout 0s 2824KB
stdin
4
24 100
4358 754
305 794
6009 7001
compilation info
prog.cpp: In function 'std::string& ltrim(std::string&)':
prog.cpp:8: error: ISO C++ forbids declaration of 'it2' with no type
prog.cpp:8: error: 'find_if' is not a member of 'std'
prog.cpp:8: error: expected primary-expression before '[' token
prog.cpp:8: error: expected primary-expression before ']' token
prog.cpp:8: error: expected primary-expression before 'char'
prog.cpp:9: error: no matching function for call to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::erase(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, int&)'
/usr/include/c++/4.3/bits/basic_string.h:1131: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::erase(typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/include/c++/4.3/bits/basic_string.h:1147: note:                 __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> > std::basic_string<_CharT, _Traits, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/include/c++/4.3/bits/basic_string.h:1167: note:                 __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> > std::basic_string<_CharT, _Traits, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::rebind<_CharT>::other::pointer, std::basic_string<_CharT, _Traits, _Alloc> >) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
prog.cpp: In function 'std::string& rtrim(std::string&)':
prog.cpp:15: error: ISO C++ forbids declaration of 'it1' with no type
prog.cpp:15: error: 'find_if' is not a member of 'std'
prog.cpp:15: error: expected primary-expression before '[' token
prog.cpp:15: error: expected primary-expression before ']' token
prog.cpp:15: error: expected primary-expression before 'char'
prog.cpp:16: error: request for member 'base' in 'it1', which is of non-class type 'int'
prog.cpp: In function 'std::string add(std::string, std::string)':
prog.cpp:92: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'const char*'
prog.cpp:13: error: in passing argument 1 of 'std::string& rtrim(std::string&)'
stdout
Standard output is empty