Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
2.6 KiB

  1. // vi: set ts=4
  2. #include <assert.h>
  3. #define Assert(e) assert(e)
  4. #include <string.h>
  5. #include <stdlib.h>
  6. //#include <iostream.h>
  7. #include <stdio.h>
  8. #include "vector.h"
  9. #define ELEMENTS(s) (sizeof(s) / sizeof(s[0]))
  10. void CreateString(char *str, vector<char> &s)
  11. {
  12. register char *ptr;
  13. for (ptr = str; *ptr != '\0'; ptr++)
  14. s.append(*ptr);
  15. }
  16. void StringCons(char *str, vector<char> &s)
  17. {
  18. register char *ptr;
  19. for (ptr = strchr(str, '\0') - 1; ptr >= str; --ptr)
  20. s.prepend(*ptr);
  21. }
  22. void StringPrint(vector<char> &s, FILE *o)
  23. {
  24. for (register int i = 0; i < s.length(); ++i)
  25. fputc(s[i], o);
  26. }
  27. void RemoveChar(char ch, vector<char> &s)
  28. {
  29. int i = 0;
  30. while (i < s.length())
  31. if (s[i] == ch)
  32. s.removeAt(i);
  33. else
  34. ++i;
  35. }
  36. void InsertBefore(char newchar, char findchar, vector<char> &s)
  37. {
  38. for (register int i = 0; i < s.length(); ++i)
  39. if (s[i] == findchar)
  40. s.insertAt(i++, newchar);
  41. }
  42. vector<char> reverse(vector<char> &s)
  43. {
  44. if (s.length() <= 1)
  45. return s;
  46. else
  47. return reverse(s(1, s.length())) + s[0];
  48. }
  49. void RadixSort(vector<char> &s)
  50. {
  51. vector<char> queue[256];
  52. for (register const char *ptr = s.vec() + (s.length() - 1);
  53. ptr >= s.vec();
  54. --ptr)
  55. queue[*ptr].append(*ptr);
  56. s.reshape(0);
  57. for (register int i = 0; i < ELEMENTS(queue); ++i)
  58. s += queue[i];
  59. }
  60. void main()
  61. {
  62. int i;
  63. char merge_q[2] = {'!', '?'};
  64. vector<char> string_q;
  65. CreateString("for thought", string_q);
  66. StringCons("Food ", string_q);
  67. fputs("List contents = ", stdout);
  68. StringPrint(string_q, stdout);
  69. fputc('\n', stdout);
  70. string_q = reverse(string_q);
  71. fputs("List reverse = ", stdout);
  72. StringPrint(string_q, stdout);
  73. fputc('\n', stdout);
  74. string_q += vector<char>(merge_q, ELEMENTS(merge_q));
  75. fputs("List merge = ", stdout);
  76. StringPrint(string_q, stdout);
  77. fputc('\n', stdout);
  78. RemoveChar('o', string_q);
  79. fputs("Remove 'o' = ", stdout);
  80. StringPrint(string_q, stdout);
  81. fputc('\n', stdout);
  82. InsertBefore('%', 'u', string_q);
  83. InsertBefore('&', 't', string_q);
  84. fputs("u->%u, t->&t = ", stdout);
  85. StringPrint(string_q, stdout);
  86. fputc('\n', stdout);
  87. RadixSort(string_q);
  88. fputs("Sorted List = ", stdout);
  89. StringPrint(string_q, stdout);
  90. fputc('\n', stdout);
  91. string_q.insertAt(3, '@');
  92. string_q.insertAt(0, '@');
  93. string_q.insertAt(string_q.length(), '@');
  94. fputs("@ after '!' = ", stdout);
  95. StringPrint(string_q, stdout);
  96. fputc('\n', stdout);
  97. while ((i = string_q.find('@')) >= 0)
  98. string_q[i] = '*';
  99. fputs("@ -> '*' = ", stdout);
  100. StringPrint(string_q, stdout);
  101. fputc('\n', stdout);
  102. assert (string_q.find('Z') == -1);
  103. assert (string_q.find('u') == 18);
  104. // cout << "Remove '*' = " << string_q << endl;
  105. }