2012-02-14 62 views
2

我一直抨击我的头反对这些演习几天,我无法把握他们。 我必须在我的号码上添加一个单位(即m,ft,in,cm)并拒绝无效单位,然后将数字和单位转换为米。
我有我的代码,显示如何选择并保持最高和最低的数字等,但我完全丧失如何添加单位的一部分,我试过如果语句和他们看起来像他们会工作,但他们不会,我会感到沮丧,并且诚实地说我错过了一些基本观点。 这里是迄今为止从编程:使用C++的原理和实践的第4章着手。 Bjarne Stroustrup

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

Bjarne通过一些涉及用户定义文字的单元在最近的演示中涉及一些代码http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Keynote-Bjarne-Stroustrup-Cpp11-Style从大约23分钟开始。这可能对你有帮助:-) – 2012-02-14 14:37:47

+2

'然后把数字和单位转换成metres',在哪里?我看不到你在转换。 – atoMerz 2012-02-14 14:38:34

+0

你似乎没有保持最小的距离。你应该读第一个距离,使其成为最大*和*最小,然后继续循环。 – Beta 2012-02-14 14:43:35

回答

-1

好吧,我想我明白了。请记住,我只从前四章知道这些基础知识,因此考虑到迄今为止的工具,这是否是这样做的正确方法?

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; 
} 
+0

也许这只是我,但我认为你应该缩进你的代码更好(例如一切在主函数的范围内应该缩进,while循环的范围内的所有内容都应该缩进)。您还应该使用std :: endl而不是手动放置换行符(例如'cout <<“bla”<< endl;')。 – tr9sh 2012-02-14 23:41:04

+0

是的,我的缩进需要注意,我不知道最终的东西,谢谢 – 2012-02-14 23:53:39

1

1.通过第4章,您已经了解常量和“魔术常量或数字”
What is a magic number, and why is it bad?)。

2。 cout < <“请输入1个数字和一个单位(cm,M,Ft,In):”< < endl; 这是错误的,因为您使用大写字母并且测试小写字母。



3。 if (small == 0 && val1 > large)

,如果我进入0米它说,它“既不是其最大的NOR最小的”像那些已经设置,更好地运用

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;

它保持原来的价值和单位使用。

+0

起初我不明白你的答案#2,因为它不符合原始帖子中的代码。然而,当我从写同样问题的人那里找到答案时,这是有道理的。 – drescherjm 2014-03-21 12:59:40

0

这个我如何解决它

#include "../../std_lib_facilities.h" 
double unitconv(double x, string y) // a function to convert units. 
{ 
    if (y == "in") 
    { 
     x = x/39.37; 
     y = "m"; 
    } 
    else if (y == "ft") 
    { 
     x = x/3.28; 
     y = "m"; 
    } 
    else if (y == "cm") 
    { 
     x = x/100; 
     y = "m"; 
    } 
    else if (y == "m") 
    { 
     x = x; 
     y = y; 
    } 
    else 
     cout << "unrecognised unit"; 
    return x; 

} 

int main() 
{ 
    double value = 0; 
    string unit = " "; 
    double sum = 0; 
    int iter = 0; 
    vector<double>values; 
    cin >> value >> unit; 
    value = unitconv(value, unit); 
    values.push_back(value); 
    sum = sum + value; 
    ++iter; 
    double largest = value; 
    double smallest = value; 
    while (cin >> value >> unit) 
    { 
     value = unitconv(value, unit); 
     if (value > largest) 
     { 
      largest = value; 
     } 
     if (value < smallest) 
     { 
      smallest = value; 
     } 
     values.push_back(value); 
     sum = sum + value; 
     ++iter; 
     cout << "\nLargest Value = " << largest << "\nSmallest Value = " << smallest << "\nSum of Values = " << sum << "\nNumber of Values = " << iter << "\n"; 
    } 
    sort(values.begin(),values.end()); 
    for (int i = 0; i < values.size(); ++i) 
     cout << values[i] << '\n'; 
    keep_window_open(); 
} 
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章不讨论载体,有可以添加到执行上的所有值测试了一些修改。此程序适用于大多数,但不是全部值。

0

这是我的2美分(可以这么说)。同一本书,同一钻,使用知识到目前为止解释...

/* The problem: 
    Add a unit to each double entered; that is, enter values such as 10cm, 
    2.5in, 5ft, or 3.33m. Accept the four units: m, cm, in, ft. Assume 
    conversion factors 1m == 100cm, l in == 2.54cm, ft == 12in. Read the unit 
    indicator into a string. */ 


#include "../../std_lib_facilities.h" 

int main() 
{ 
    double a; 
    string unit = ""; 
    double smallest = 0; 
    double largest = 0; 
    bool initialize = false; 

    while (cin >> a >> unit) 
    { 
     cout << "You have entered: " << a << unit << endl; 

     if (unit == "m") 
      a = a * 100; 
     else if (unit == "cm") 
      a = a * 1; 
     else if (unit == "ft") 
      a = a * 12 * 2.54; 
     else if (unit == "in") 
      a = a * 2.54; 

     /*no comparison until second set of values are accepted. That is when 
      trackers of largest and smallest numbers are initialized*/ 

     if (!initialize) 
     { 
      smallest = a; 
      largest = a; 
      initialize = true; 
     } 
     else 
     { 
      if (a > largest) 
      { 
       cout << "The largest so far" << endl; 
       largest = a; 
      } 

      if (a < smallest) 
      { 
       cout << "The smallest so far" << endl; 
       smallest = a; 
      } 
     } 
    } 
    return 0; 
} 

输出

10 m 
You have entered: 10m 
40 ft 
You have entered: 40ft 
The largest so far 
500 in 
You have entered: 500in 
The largest so far 
39 ft 
You have entered: 39ft 
30 ft 
You have entered: 30ft 
The smallest so far 
1 m 
You have entered: 1m 
The smallest so far 
1 in 
You have entered: 1in 
The smallest so far 

PS:由于我还是个新手,我真的很感激评论代码的任何反馈,格式化,逻辑和其他一切。

相关问题