Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. // threadtest.cc
  2. // Simple test case for the threads assignment.
  3. //
  4. // Create two threads, and have them context switch
  5. // back and forth between themselves by calling Thread::Yield,
  6. // to illustratethe inner workings of the thread system.
  7. //
  8. // Copyright (c) 1992-1993 The Regents of the University of California.
  9. // All rights reserved. See copyright.h for copyright notice and limitation
  10. // of liability and disclaimer of warranty provisions.
  11.  
  12. #include "copyright.h"
  13. #include "system.h"
  14. #include "ctype.h"
  15. #include "string.h"
  16.  
  17. //----------------------------------------------------------------------
  18. // SimpleThread
  19. // Loop 5 times, yielding the CPU to another ready thread
  20. // each iteration.
  21. //
  22. // "which" is simply a number identifying the thread, for debugging
  23. // purposes.
  24. //----------------------------------------------------------------------
  25. void InputCheck(int count);
  26. void Shout();
  27.  
  28. void
  29. SimpleThread(int which)
  30. {
  31. int num;
  32.  
  33. for (num = 0; num < 5; num++) {
  34. printf("*** thread %d looped %d times\n", which, num);
  35. currentThread->Yield();
  36. }
  37.  
  38. }
  39.  
  40.  
  41. //----------------------------------------------------------------------
  42. // ThreadTest
  43. // Invoke a test routine.
  44. //----------------------------------------------------------------------
  45.  
  46. void
  47. ThreadTest()
  48. {
  49. DEBUG('t', "Entering ThreadTest");
  50.  
  51. Thread *t = new Thread("forked thread");
  52.  
  53. //t->Fork(SimpleThread, 1);
  54. //SimpleThread(0);
  55.  
  56. t->Fork(InputCheck, 0);
  57.  
  58. //Shout();
  59.  
  60. }
  61.  
  62. void InputCheck(int count)
  63. {
  64. char input[100];
  65.  
  66. printf("Enter input: ");
  67. fgets(input, sizeof(input), stdin);
  68. int length = strlen(input);
  69. length = length - 1;
  70. printf("Your input is: %s ", input);
  71.  
  72. while (length == 0)
  73. {
  74. printf("Please re-enter a valid input: ");
  75. fgets(input, sizeof(input), stdin);
  76. length = strlen(input);
  77. length = length - 1;
  78. printf("Your input is: %s ", input);
  79. }
  80.  
  81.  
  82. bool decimal = false;
  83. bool number = false;
  84. bool negative = false;
  85. bool lastdec = false;
  86.  
  87. if (input[0] == '-')
  88. {
  89. negative = true;
  90. count = 1;
  91. }
  92.  
  93. for (count; count <= length; count++)
  94. {
  95. if(isdigit(input[count]) || input[count] == '.')
  96. {
  97. lastdec = false;
  98.  
  99. if(isdigit(input[count]))
  100. {
  101. number = true;
  102. }
  103.  
  104. if (input[count] == '.')
  105. {
  106. if (number == false)
  107. {
  108. printf("Character String \n \n");
  109. return;
  110. }
  111.  
  112. if (count == length)
  113. {
  114. printf("Character String \n \n");
  115. return;
  116. }
  117.  
  118. if (decimal == false)
  119. {
  120. decimal = true;
  121. lastdec = true;
  122. }
  123. else
  124. {
  125. printf("Character String \n \n");
  126. return;
  127. }
  128. }
  129. }
  130.  
  131. if (isalpha(input[count]))
  132. {
  133. printf("Character String \n \n");
  134. return;
  135. }
  136.  
  137. if (input[count] == ' ')
  138. {
  139. printf("Character String \n \n");
  140. return;
  141. }
  142.  
  143. }
  144.  
  145. if (decimal == false && number == true)
  146. {
  147. if (negative == false)
  148. {
  149. printf("Positive Integer \n \n");
  150. return;
  151. }
  152. else
  153. {
  154. printf("Negative Integer \n \n");
  155. return;
  156. }
  157. }
  158.  
  159. if (decimal == true && number == true && lastdec == false)
  160. {
  161. if (negative == false)
  162. {
  163. printf("Positive Decimal \n \n");
  164. return;
  165. }
  166. else
  167. {
  168. printf("Negative Decimal \n \n");
  169. return;
  170. }
  171. }
  172.  
  173. printf("Character String \n \n");
  174.  
  175. return;
  176. }
  177.  
  178.  
  179. void Shout()
  180. {
  181. int T;
  182. int S;
  183.  
  184. printf("How many threads would you like to shout? ");
  185. scanf("%d", &T);
  186.  
  187. printf("How many times should they shout? ");
  188. scanf("%d", &S);
  189.  
  190. printf("%i", T);
  191. printf("%i", S);
  192. return;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement