Tarango

Input Output & Type Conversions in C++

Jul 16th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. //============================================================================
  2. // Name        : Input Output and Type Conversions in C++
  3. // Author      : Tarango Khan
  4. // Team        : BRACU Byteheads
  5. //============================================================================
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. int str2int(string s) {
  11.     stringstream ss(s);
  12.     int x;
  13.     ss >> x;
  14.     return x;
  15. }
  16.  
  17. string int2str(int a) {
  18.     stringstream ss;
  19.     ss << a;
  20.     string str = ss.str();
  21.     return str;
  22. }
  23.  
  24. string char2str(char a) {
  25.     stringstream ss;
  26.     ss << a;
  27.     string str = ss.str();
  28.     return str;
  29. }
  30.  
  31. string reverse(string s) {
  32.     for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
  33.         swap(s[i], s[j]);
  34.     }
  35.     return s;
  36. }
  37.  
  38. int binary2int(int num) {
  39.     int bin = num, dec = 0, rem, base = 1;
  40.     cin >> num;
  41.     bin = num;
  42.     while (num > 0) {
  43.         rem = num % 10;
  44.         dec = dec + rem * base;
  45.         base = base * 2;
  46.         num = num / 10;
  47.     }
  48.     cout << "The decimal equivalent of " << bin << " : " << dec << endl;
  49.     return dec;
  50. }
  51.  
  52. string int2binary(unsigned int val) {
  53.     unsigned int mask = 1 << (sizeof(int) * 8 - 1);
  54.     string s = "";
  55.     for (int i = 0; i < (int) (sizeof(int) * 8); i++) {
  56.         if ((val & mask) == 0) {
  57.             s.append("0");
  58.         } else {
  59.             s.append("1");
  60.         }
  61.         mask >>= 1;
  62.     }
  63.     cout << "The binary equivalent of " << val << " : " << s << endl;
  64.     return s;
  65. }
  66.  
  67. bool is_the_double_is_integer(double value) {
  68.     int num = ceil(value);
  69.     double newValue = (double) num;
  70.     if (abs(value - newValue) <= 0.00000000000001) {
  71.         return true;
  72.     } else {
  73.         return false;
  74.     }
  75. }
  76.  
  77. string char_array_2_string(char * a) {
  78.     int Len = strlen(a);
  79.     string s = "";
  80.     for (int i = 0; i < Len; i++) {
  81.         s = s.append(char2str(a[i]));
  82.     }
  83.     return s;
  84. }
  85.  
  86. char * string_2_char_array(string s) {
  87.     int Len = s.length();
  88.     char a[Len + 1];
  89.     for (int i = 0; i < Len; i++) {
  90.         a[i] = s[i];
  91.     }
  92.     a[Len] = '\0';
  93.     return a;
  94. }
  95.  
  96. string remove_leading_space(string s) {
  97.     string t = "";
  98.     int Len = s.length();
  99.     int i = 0;
  100.     while (i < Len && s[i] == ' ')
  101.         i++;
  102.     for (int c = i; c < Len; c++) {
  103.         t = t.append(char2str(s[c]));
  104.     }
  105.     return t;
  106. }
  107.  
  108. string remove_trailing_space(string s) {
  109.     string t = "";
  110.     int Len = s.length();
  111.     int i = Len - 1;
  112.     while (i >= 0 && s[i] == ' ')
  113.         i--;
  114.     for (int c = 0; c <= i; c++) {
  115.         t = t.append(char2str(s[c]));
  116.     }
  117.     return t;
  118. }
  119.  
  120. void string_input_coma_delimetre() {
  121.     char tokenstring[] = "Hello,25.5,World,15";
  122.     int i;
  123.     double fp;
  124.     char o[10], f[10], s[10], t[10];
  125.  
  126.     int result = sscanf(tokenstring, "%[^','],%[^','],%[^','],%s", o, s, t, f);
  127.     fp = atof(s); //Convert from string to float;
  128.     i = atoi(f); //Convert from string to int;
  129.     printf(" %s\n %lf\n %s\n %d\n", o, fp, t, i);
  130. }
  131.  
  132. void string_input_other_delimetre() {
  133.     char text[120] = "hello.123.234.223";
  134.     int a[3] = { -1, -1, -1 };
  135.     char str[120];
  136.     sscanf(text, "%[a-z-A-Z].%d.%d.%d", &str, &a[0], &a[1], &a[2]);
  137.  
  138.     //%[a-z-A-Z] indicate to store all characters from a to z, A to Z.
  139.     //And you can add in any symbol and numbers such as add in 0-9 and underscore.
  140.     //Example : %[a-z-A-Z-0-9-_]
  141.     printf("%s, %d, %d, %d\n", str, a[0], a[1], a[2]);
  142. }
  143.  
  144. void sscanf_known_no_of_inputs_in_a_line() {
  145.     char sentence[] = "Rudolph is 12 years old";
  146.     char s1[20], s2[20], s3[20], s4[20];
  147.     int i;
  148.     sscanf(sentence, "%s %s %d %s %s", &s1, &s2, &i, &s3, &s4);
  149.     printf("%s\n%s\n%d\n%s\n%s\n", s1, s2, i, s3, s4);
  150. }
  151.  
  152. void stringstream_unknown_no_of_integer_inputs_in_a_line() {
  153.     stringstream ss;
  154.     string input = "a b c 4 e 10 15 frd 20 50 hh 100";
  155.     ss << input;
  156.     int found;
  157.     string temp;
  158.  
  159.     while (getline(ss, temp, ' ')) {
  160.         if (stringstream(temp) >> found) {
  161.             cout << found << endl;
  162.         }
  163.     }
  164. }
  165.  
  166. void unknown_no_of_any_inputs_in_a_line() {
  167.     char input[] = "10 12 hello 15 20 hahaha 30 Good 50";
  168.     int Len = strlen(input);
  169.     string s = "";
  170.     for (int i = 0; i < Len; i++) {
  171.         if (input[i] == ' ') {
  172.             if (s.length() != 0) {
  173.                 if (s[0] >= '0' && s[0] <= '9') {
  174.                     int int_value = str2int(s);
  175.                     cout << "The int value is: " << int_value << endl;
  176.                 } else {
  177.                     cout << "The string value is: " << s << endl;
  178.                 }
  179.             }
  180.             s = "";
  181.         } else {
  182.             s = s.append(char2str(input[i]));
  183.         }
  184.     }
  185.  
  186.     //For the last value:
  187.     if (s.length() != 0) {
  188.         if (s[0] >= '0' && s[0] <= '9') {
  189.             int int_value = str2int(s);
  190.             cout << "The int value is: " << int_value << endl;
  191.         } else {
  192.             cout << "The string value is: " << s << endl;
  193.         }
  194.     }
  195. }
  196.  
  197. int main() {
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment