几道C++题目,谁来帮帮我啊~~~~ 20
1)Writeaprogramthattakesin2integersfromtheuser,andcomputestheirgcd(greatestcommondivi...
1) Write a program that takes in 2 integers from the user, and computes their gcd (greatest
common divisor).
The gcd of two integers is the largest integer that divides both numbers. For instance,
the gcd of 15 and 25 is 5.
Implement the followng method to compute the gcd. Let the two numbers be x, y. Take the
larger of the two numbers (let that be x), and divide x by the smaller number (y).
Let there be a remainder of z. Now you divide y by z, compute the remainder and so on.
Keep doing this until you get a remainder of 0. The smaller number when we get the
remainder of 0 is the gcd.
For example, let us find the gcd of 735 and 252.
You divide 735 by 252. You get a remainder of 231.
Now you divide 252 by 231. You get a remainder of 21.
Now you divide 231 by 21. You get a remainder of 0.
Therefore the gcd is 21.
2) Write a simple arithmetic expression translator that reads in expressions such as
25.5 + 34.2, and displays their value. Each expression consists of a number (type: float)
followed by a space followed by an operator (type: char) followed by
another number
(type: float)
. The operator you support must include + - * / You must keep evaluating
the expressions until the user enters an expression such as 0 + 0
--------------
Test Data:
Enter expression to be evaluated: 3 * 5
3 * 5 = 15
Enter expression to be evaluated: 4 + 9
4 + 9 = 13
Enter expression to be evaluated: 0 + 0
Thanks for using our calculator !!!
3)
Drivers are concerned with the mileage obtained by their cars. One driver has kept track
of several tankful of gasoline by recording miles driven and gallons used for each tankful.
Develop a c++ program that uses a while statement to input the miles driven and gallons
used for each tankful. The program should calculate and display the miles per gallon
obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up
to that point. If the mileage is -1, the program quits.
---------------------
Test Data:
Enter Mileage for Trip #1 : 200
Enter Gallons for Trip #1 : 10
MPG for Trip #1 : 20
MPG for all trips so far is 20
Enter Mileage for Trip #2 : 300
Enter Gallons for Trip #2 : 20
MPG for Trip #2 : 15
MPG for all trips so far is 16.6667
Enter Mileage for Trip #3 : -1
Thank you for using our MPG program!
4) Input an integer containing only 0s and 1s (i.e. binary integer) and prints its decimal
equivalent. Use the modulus and division operators to pick off the binary digits as before.
You only need to handle a fixed-length, 5-digit binary integer for this question. Assume that the
user will always enter a 5-digit integer.
------------------
Test Data:
Enter a binary number : 11011
Decimal equivalent of 11011 is 27.
(高手帮我解答啊,高分回报!)
烦请高手把详细算法写出来,我也可以比较出我哪里写错了。谢谢 展开
common divisor).
The gcd of two integers is the largest integer that divides both numbers. For instance,
the gcd of 15 and 25 is 5.
Implement the followng method to compute the gcd. Let the two numbers be x, y. Take the
larger of the two numbers (let that be x), and divide x by the smaller number (y).
Let there be a remainder of z. Now you divide y by z, compute the remainder and so on.
Keep doing this until you get a remainder of 0. The smaller number when we get the
remainder of 0 is the gcd.
For example, let us find the gcd of 735 and 252.
You divide 735 by 252. You get a remainder of 231.
Now you divide 252 by 231. You get a remainder of 21.
Now you divide 231 by 21. You get a remainder of 0.
Therefore the gcd is 21.
2) Write a simple arithmetic expression translator that reads in expressions such as
25.5 + 34.2, and displays their value. Each expression consists of a number (type: float)
followed by a space followed by an operator (type: char) followed by
another number
(type: float)
. The operator you support must include + - * / You must keep evaluating
the expressions until the user enters an expression such as 0 + 0
--------------
Test Data:
Enter expression to be evaluated: 3 * 5
3 * 5 = 15
Enter expression to be evaluated: 4 + 9
4 + 9 = 13
Enter expression to be evaluated: 0 + 0
Thanks for using our calculator !!!
3)
Drivers are concerned with the mileage obtained by their cars. One driver has kept track
of several tankful of gasoline by recording miles driven and gallons used for each tankful.
Develop a c++ program that uses a while statement to input the miles driven and gallons
used for each tankful. The program should calculate and display the miles per gallon
obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up
to that point. If the mileage is -1, the program quits.
---------------------
Test Data:
Enter Mileage for Trip #1 : 200
Enter Gallons for Trip #1 : 10
MPG for Trip #1 : 20
MPG for all trips so far is 20
Enter Mileage for Trip #2 : 300
Enter Gallons for Trip #2 : 20
MPG for Trip #2 : 15
MPG for all trips so far is 16.6667
Enter Mileage for Trip #3 : -1
Thank you for using our MPG program!
4) Input an integer containing only 0s and 1s (i.e. binary integer) and prints its decimal
equivalent. Use the modulus and division operators to pick off the binary digits as before.
You only need to handle a fixed-length, 5-digit binary integer for this question. Assume that the
user will always enter a 5-digit integer.
------------------
Test Data:
Enter a binary number : 11011
Decimal equivalent of 11011 is 27.
(高手帮我解答啊,高分回报!)
烦请高手把详细算法写出来,我也可以比较出我哪里写错了。谢谢 展开
1个回答
展开全部
都不是很难。 说下我自己的思路。有问题可以讨论下。
1.是利用辗转相除法求最大公约数。
2.就是一个简单的计算器,让输入一个数 一个字符 一个数,只要记录下来两个输入的数,并且判断下这个字符是什么运算符就行了。
3.
Enter Mileage for Trip #1 : 200//第一组数据 中的公里数
Enter Gallons for Trip #1 : 10// 汽油数
MPG for Trip #1 : 20// 第一次TRIP 中每公里用油数
MPG for all trips so far is 20//总的平均每公里用油数
4.题中已经说明这个二进制数是5位的,要转换为十进制数。
如果可以的话,你把你的代码发出来,大家帮你看看O(∩_∩)O~
1.是利用辗转相除法求最大公约数。
2.就是一个简单的计算器,让输入一个数 一个字符 一个数,只要记录下来两个输入的数,并且判断下这个字符是什么运算符就行了。
3.
Enter Mileage for Trip #1 : 200//第一组数据 中的公里数
Enter Gallons for Trip #1 : 10// 汽油数
MPG for Trip #1 : 20// 第一次TRIP 中每公里用油数
MPG for all trips so far is 20//总的平均每公里用油数
4.题中已经说明这个二进制数是5位的,要转换为十进制数。
如果可以的话,你把你的代码发出来,大家帮你看看O(∩_∩)O~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询