我一直在抨击这些演习数天,我无法掌握它们。 我必须在我的数字中添加一个单位(即m,ft,in,cm。)并拒绝无效单位,然后将数字和单位转换为米。 我有我的代码,显示如何选择并保持最高和最低的数字等,但我完全失去了如何添加单位部分,我已经尝试过if语句,他们似乎看起来像他们会工作,但他们没有,我感到沮丧,老实说,我错过了一些基本点。 这是我到目前为止的代码
int main(){
double val1=0;
string unit;
double large=0;
double small=0;
cout<<"please Enter 1 number and a unit (cm, M, Ft, In):\n";
while (cin>>val1>>unit){
if(val1<small) {small=val1; cout<<"smallest so far\n";}//find smallest number
else if(val1>large) {large=val1; cout<<"largest so far\n";}//Find largest number
else cout<<"Neither largest nor smallest\n";
}
cout<<"The smaller value so far is (in metres): "<<small<<"\n";
cout<<"The larger value so far is (in metres): "<<large<<"\n";
keep_window_open("~");
return 0;
}
这不是家庭作业,我只是为了自己的利益而这样做。任何帮助将不胜感激。
答案 0 :(得分:2)
1.从第4章开始,您已经了解了常量和“魔术常数或数字”(What is a magic number, and why is it bad?)。
2。 cout&lt;&lt; “请输入1个数字和一个单位(cm,M,Ft,In):”&lt;&lt; ENDL; 这是错误的,因为你使用大写并测试小写字母。
3。if (small == 0 && val1 > large)
如果我输入0米它表示“它既不是最大也不是最小”,就像已经设定的那样,更好地使用
if(small=0 && large==0)
4.Personaly我不喜欢转换成米我宁愿使用
if(unit=="m")
{
if( (val1*=m_in_cm) <= small)
{
std::cout<<"\nThe smallest value so far "<<val1<<" "<<unit<<" \n";
small=val1;
}
if( (val1*=m_in_cm) >= large)
{
std::cout<<"\nThe largest value so far "<<val1<<" "<<unit<<" \n";
large=val1;
}
}
else if(unit=="in"){....}
其中“m_in_cm”为
const double m_in_cm=100;
保持原有价值和单位使用。
答案 1 :(得分:2)
这是我的解决方案。我宁愿只检查向量的最高和最低值,但这不是演习的要求吗?似乎是一个漂亮的程序,Bjarne希望我们用这个演练来做。
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
答案 2 :(得分:1)
答案 3 :(得分:0)
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
double a = 0.0;
vector<double> double_vec;
int iterator = 0;
double temp_smallest = 0;
double temp_largest = 0;
int count = 0;
while (cin >> a)
{
// add i to the vector;
double_vec.push_back(a);
for (int i = 0; i < count; i++)
{
if (double_vec[i] > temp_largest)
{
temp_largest = double_vec[i];
}
else if (double_vec[i] < temp_smallest)
{
temp_smallest = double_vec[i];
}
}
if (temp_smallest > a)
cout << "smallest so far" << endl;
else if (temp_largest < a)
cout << "largest so far";
count++;
}
system("PAUSE");
return 0;
}
第4章确实讨论了向量,可以添加一些修改来对所有值执行测试。此程序适用于大多数,但不是所有值。
答案 4 :(得分:0)
这是我的解决方案。我发现关于所有其他解决方案的问题是它们如何处理无效输入(即具有无效单元的输入)。他们中的大多数只是将值添加到矢量中。有些只是突破while循环而不给用户提供继续添加值的机会。
我也使用Stroustrup建议的约定,例如for
常量和范围valid_entry
循环。
我的版本中的额外复杂性是在每个循环上设置的#include "std_lib_facilities.h"
int main()
{
constexpr double METERS_PER_CM = 1/100.0;
constexpr double METERS_PER_IN = 1/39.37; //avoid magic #s!
constexpr double METERS_PER_FT = 1/3.28;
double smallest_so_far = 0;
double largest_so_far = 0;
double current_val = 0;
double current_val_meters = 0;
vector<double> all_distances;
double values_sum = 0;
string units;
bool valid_entry = true; //if they enter some bs, set to false
cout << fixed << showpoint << setprecision(3);
cout << "Please enter length and unit. Enter single char if done.\n"
<< "Assume 0 was first. Units are cm, m, in, ft.\n\n";
while ( cin >> current_val >> units)
{
cout << "You entered " << current_val << units << ".\n";
//process units: convert to meters
if (units == "m")
current_val_meters = current_val;
else if (units == "cm")
current_val_meters = current_val * METERS_PER_CM;
else if (units == "in")
current_val_meters = current_val * METERS_PER_IN;
else if (units == "ft")
current_val_meters = current_val * METERS_PER_FT;
else
{
cout << "You done messed up A-aron!\n";
valid_entry = false;
}
if (valid_entry)
{
if (units != "m")
cout << "\nThat is " << current_val_meters << " meter(s)!\n";
//add to vector
all_distances.push_back(current_val_meters);
//deal with relative sizes
if (current_val_meters < smallest_so_far)
{
cout << "Smallest so far!\n";
smallest_so_far = current_val;
}
if (current_val_meters > largest_so_far)
{
cout << "Largest so far!\n";
largest_so_far = current_val_meters;
}
} // valid_entry
valid_entry = true;
cout << "\nAnother...?\n";
} // while cin
//When done, present summary statistics
cout << "You entered " << all_distances.size() << " values.\n";
//Get sum
sort(all_distances);
cout << "All the values you entered: \n";
for (double x: all_distances)
{
values_sum += x;
cout << x << "\n";
}
cout << "Sum of all meters: " << values_sum << "\n";
//show min and max
cout << "The max was " << largest_so_far << ", and the min was "
<< smallest_so_far << ".\n";
cout << "\nHave a good day!\n";
return 0;
};
标志。如果输入无效,程序不会停止,它只是再次循环。
<html><head>
<title>Creating ID</title></head>
<style>
p {font-size: 12pt }
</style>
<body>
<p>Your ID is going to be created. Please press the submitt button</p>
<form action=atmos.cgi method=get><p>
<input type=hidden name=ID value=202.166.76.23.deY7hZxSeq>
<input type=submit />
</form>
</body></html>
答案 5 :(得分:-1)
好的,我想我明白了。请记住,我只从前4章中了解这些基础知识,所以考虑到目前为止的工具,这是一种正确的方法吗?
int main(){
double val1 = 0;
string unit;
double large = 0;
double small = 0;
cout << "please Enter 1 number and a unit (cm, M, Ft, In):" << endl;
while (cin >> val1 >> unit) {
if (unit == "cm") val1 = val1/100;
else if (unit == "m") val1 = val1;
else if (unit == "in") val1 = val1 * 0.0254;
else if (unit == "ft") val1 = val1 * 0.3048;
else {
cout << endl << "dont understand " << unit;
break;
}
if (small == 0 && val1 > large){
small = val1;
large = val1;
cout << val1 << "metres is both the largest and smallest" << endl;
} else if (val1 < small) {
small = val1;
cout << small << " metres is the smallest so far" << end;
} else if (val1 > large){
large = val1;
cout << large <<" metres is the largest so far" << endl;
} else {
cout << val1 << " metres is neither largest nor smallest" << endl;
}
}
cout << endl << small << " metres" << "\t" << "is the smallest number" << endl << large << " metres" << "\t" << "is the largest number" << endl;
keep_window_open("~");
return 0;
}