源码

简介: 源码
1. 
import java.util.Scanner;
2. public class homework
3. {
4. public static void main(String[] args)
5. {
6. 
7. /*
8.  * name must be printed out at the beginning of your source code
9.  * */
10. //introduction of the program and  my mane
11.    printIntro();
12. 
13. 
14. Scanner kb = new Scanner(System.in);//create the scanner to get the input 
15. 
16. String answer = "yes";//set the default value of answer
17. 
18. while ("yes".equals(answer))
19. {
20. 
21. 
22.   System.out.println("Enter the list of the operands and the opration:");
23. 
24. //get the input from the use
25.   String input = kb.nextLine();
26. 
27. // separate the tokens and store them in variables
28. 
29. char op1 = ' ';
30. char op2 = ' ';
31. String operation ="";
32.      int flag = 0;
33. 
34.      for(int i = 0; i < input.length();i++)
35.      {
36. 
37.        if(input.charAt(i)!= ' ')
38.        {
39. 
40.          if(flag == 0)
41.          {
42.            op1 = input.charAt(i);
43.          }else if(flag ==1)
44.          {
45.            op2 = input.charAt(i);
46.          }else if(flag ==2)
47.          {
48.            operation = input.substring(i);
49.          }
50. 
51.          flag++;
52.        }
53. 
54. 
55.      }
56. 
57. 
58.   //(3)call different methods to carry out the task
59. 
60.      printNumeric(op1,op2,operation);
61.      printEnglish(op1,op2,operation);
62. 
63. 
64. 
65.   //(4)get the choice of the user
66.   System.out.print("Do you have another expression?");
67.   answer = kb.nextLine();
68. 
69. 
70. }
71. 
72. //when the option the user inputed is not "yes" such as "no" or other words print  this sentence
73. System.out.println("Have a good day!! BYE");
74. 
75. }
76. 
77. 
78. 
79. 
80. 
81. /*this methods gets the operands and the operation and
82. prints the English version: if we call this method
83. printEnglish(2,3, plus), then this method will output:
84. Two plus three = 5 */
85. public static void printEnglish(char op1, char op2,String operation)
86. {
87.   /*1. call the method charToString(op1) to convertlish
88.   word for example ‘1’ to one or ‘2’ to two,….*/
89.   String str_num1 = charToString(op1);
90.   String str_num2 = charToString(op2);
91. 
92. 
93. 
94.   /*
95.   2. call the method operandConversionToNumber(op1) to
96.   get its numeric value. For example if op1 is ‘1’ then
97.   this method call should return the integer value 1*/
98. 
99.   int num1 = operandConversiontoNumber(op1) ;
100.  int num2 = operandConversiontoNumber(op2) ;
101. 
102.  /*
103.  3. call the method operationConversion(operation) to
104.  convert the operation to a mathematical operation. For
105.  example if you call this method with the string plus
106.  then it will return ‘+’*/
107.  char mathematical_operation =operationConversion(operation);
108. 
109. 
110.      /*4. finally call the method calculate to get the result
111.      of the operation.*/
112.      double result = calculate(num1,num2, mathematical_operation);
113.       if(ZERO ==  result && operation.equals("divide")||mathematical_operation==' ')
114.       {
115.        //do nothing because the information has already printed when the method of printEnglish(char op1, char op2,String operation)was called
116.       }else if(mathematical_operation=='^')
117.      {
118. 
119.        System.out.println(num1+" to the power of "+num2 +" = "+result );
120.      }else
121.        {  
122.        //if the operation id not divide the result should be an integer 
123.        if(!operation.equals("divide"))
124.          {
125.            System.out.println(str_num1+" "+operation+" "+str_num2+" = "+(int)result);
126.          }else
127.           {
128.            System.out.println(str_num1+" "+operation+" "+str_num2+" = "+result);
129.           }
130.       }
131. 
132. 
133. 
134. }
135. 
136. 
137. 
138. /*this method prints the numeric version which is 2*3 =6*/
139. public static boolean printNumeric(char op1, char op2, String operation)
140. {
141. 
142. 
143. //body of this method is similar to the previous 
144.  //method
145.  int num1 = operandConversiontoNumber(op1);
146.  int num2 = operandConversiontoNumber(op2);
147. 
148.  char mathematical_operation =operationConversion(operation);
149.  if(mathematical_operation!=' ')//to make sure that the operation  has exist
150.  {
151.       double result = calculate(num1,num2, mathematical_operation);
152. 
153.       if(ZERO ==  result && operation.equals("divide"))
154.        {
155.           System.out.println("Invalid opernad, cannot divide by zero");
156.        }else
157.       {
158.          //if the operation id not divide the result should be an integer 
159.         if(!operation.equals("divide"))
160.         {
161.           System.out.println(num1+" "+mathematical_operation+" "+num2 +" = "+(int)result );
162.         }else
163.         System.out.println(num1+" "+mathematical_operation+" "+num2 +" = "+result );
164. 
165.       }
166. 
167. 
168.  }else//if the operation is not exist , print this sentence
169.  {
170.     System.out.println("Invalid operation, "+operation+" does not exist"); 
171.  }
172. 
173.  return true;
174. }
175. 
176. 
177. 
178. 
179. 
180. /*this method gets a number as a character and returns
181. its numeric value as an integer. You must use case
182. statement for this method*/
183. 
184. 
185. public static int operandConversiontoNumber(char operand)
186. {
187.  int numeric_value = 0;
188.  switch(operand)
189.  {
190. 
191.  case '0': numeric_value = 0; break;
192.  case '1': numeric_value = 1; break;
193.  case '2': numeric_value = 2; break;
194.  case '3': numeric_value = 3; break;
195.  case '4': numeric_value = 4; break;
196.  case '5': numeric_value = 5; break;
197.  case '6': numeric_value = 6; break;
198.  case '7': numeric_value = 7; break;
199.  case '8': numeric_value = 8; break;
200.  case '9': numeric_value = 9; break;
201. 
202. 
203.  }
204.  return numeric_value;
205. 
206. }
207. 
208. 
209. 
210. /*this method gets the operation as a string and
211. return the equivalent operation in math. For example
212. if it receives “plus” the it will return ‘+’ */
213. public static char operationConversion(String s)
214. {
215.  char operation = ' ';
216. 
217.  if(s.equals("plus"))
218.  {
219.    operation = '+';
220.  }else if(s.equals("minus"))
221.  {
222.    operation = '-';
223.  }else if(s.equals("multiply"))
224.  {
225.    operation = '*';
226.  }else if(s.equals("divide"))
227.  {
228.    operation = '/';
229.  }else if(s.equals("power"))
230.  {
231.    operation = '^';
232.  }
233. 
234.  return  operation;
235. 
236. }
237. /*this method receives two numbers and the operation and returns the result*/
238. public static double calculate(int operand1, int operand2, char operation)
239. {
240.  double result = 0.0;
241.  switch(operation)
242.  {
243.  case '+' :  result = operand1 + operand2; break;
244.  case '-' :  result = operand1 - operand2; break;
245.  case '*' :  result = operand1 * operand2; break;
246.  case '/' :  
247.    if(operand2==0)
248.    {
249.      result = ZERO;
250.    }else
251.    {
252.    result = (double)operand1 / operand2;}
253.    break;
254.  case '^' :  result = Math.pow(operand1, operand2); break;
255. 
256.  }
257. 
258.  /*if(operation=='+')
259.  {
260.    
261.     result = operand1 + operand2;
262.  }else if(operation=='-')
263.  {
264.     result = operand1 - operand2;
265.  }else if(operation=='*')
266.  {
267.     result = operand1 * operand2;
268.    
269.  }else if(operation=='/')
270.  {
271.     result = operand1 / operand2;
272.  }else if(operation=='^')
273.  {
274.     result = Math.pow(operand1, operand2);
275.  }*/
276. 
277.  return result ;
278. 
279. }
280. /*this method converts a number character to its 
281.  * English word for example if this method receives ‘1’
282.   it will return “one” */
283. public static String charToString(char num)
284. {
285.  String word = "";
286. 
287. 
288.  switch(num)
289.  {
290.  case '0': word = "zero";break;
291.  case '1': word = "one";break;
292.  case '2': word = "two";break;
293.  case '3': word = "three";break;
294.  case '4': word = "four";break;
295.  case '5': word = "five";break;
296.  case '6': word = "six";break;
297.  case '7': word = "seven";break;
298.  case '8': word = "eight";break;
299.  case '9': word = "nine";break;
300. 
301.  }
302. 
303.  return word;
304. 
305. 
306. }
307. /*this method prints the description of this program.*/
308. public static void printIntro()
309. {
310.  System.out.println("My name is ...,This program ...");
311. }
312. //when op2 is 0 and operation is divide return this number 
313. public static final double ZERO = -65535;
314. }


相关文章
|
8月前
|
算法 NoSQL 安全
30万的源码和300的源码有什么区别?
价格差异巨大的源码(30万对300)主要区别在于质量、完整性和技术支持。高质量源码通常有清晰结构、高效算法、良好文档,提供全面的技术支持,安全稳定且来自信誉良好的开发者。而低价源码可能存在问题、缺乏文档和支持。选择时需结合实际需求,注意测试和评估。示例中的AI导诊系统和云HIS系统源码,提供完整文档、数据库和二次开发支持,适用于不同场景,能有效提升开发效率和项目质量。
30万的源码和300的源码有什么区别?
|
8月前
看源码的方法
看源码的方法
102 1
|
JavaScript
源码
源码
|
程序员 开发工具 C++
|
消息中间件 网络协议 Java
eventMesh源码学习
eventMesh源码学习
218 0
|
安全 Java
ReentranLock源码学习
线程的三大特性:原子性、可见性、有序性。也就是说满足这个三个特性的操作都是可以保证安全的,如Atomic包、volatile、通过happensBefore原则可以进行线程的安全的判断,这个依据通常是为了避免jvm指令重排。比如通常我们知道的配置信息,如果有多个线程去进行配置信息的修改,则需要进行上锁。或者多个线程修改一个变量时,此时就需要进行上锁了,或者读写分离时,可以考虑ReentrantReadWriteLock等。其本质是解决并行中的问题,将并行转成串行问题进行解决。那怎么上锁才有用呢?锁的状态大部分情况下是互斥的。当然也有特例:ReentrantReadWriteLock的读读是不会
100 0
ReentranLock源码学习
|
存储 人工智能 安全
C++学习必备——文章中含有源码
C++学习必备——文章中含有源码
127 0
C++学习必备——文章中含有源码
|
算法 NoSQL 前端开发
为什么要看源码、如何看源码,高手进阶必看
为什么要看源码、如何看源码,高手进阶必看
285 0
|
存储 Android开发 C++
看Ogrekit源码的小结
看Ogrekit源码的小结
117 0
openFrameworks下的肤色检测源码
openFrameworks下的肤色检测源码
180 0

热门文章

最新文章