ch4

    技术2022-11-22  40

    Drill 01

    //Write a program that consists of a while-loop that (each time around // the loop) reads in two ints and then prints them. Exit the program when //a terminating '|' is entered. #include "std_lib_facilities.h" int main() { int val1, val2; while(cin >> val1 >> val2) cout << val1 << '/t' << val2 << '/n'; }

     

    Drill 02

    //Change the program to write out the smaller value is: followed //by the smaller of the numbers and the larger value is: followed //by the larger value. #include "std_lib_facilities.h" int main() { int val1, val2; while(cin >> val1 >> val2) if( val1 < val2) cout << "the smaller value is: " << val1 << '/n' << "the larger value is: " << val2 << '/n'; else cout << "the smaller value is: " << val2 << '/n' << "the larger value is: " << val1 << '/n'; cout << '/n'; }

     

    Drill 03

    //Augment the program so that is writes the line the numbers are //equal(only) if they are equal #include "std_lib_facilities.h" int main() { int val1, val2; while(cin >> val1 >> val2) { if( val1 < val2) cout << "the smaller value is: " << val1 << '/n' << "the larger value is: " << val2 << '/n'; else if(val1 > val2) cout << "the smaller value is: " << val2 << '/n' << "the larger value is: " << val1 << '/n'; else cout << "the numbers are equal" << '/n'; cout << '/n'; } }

     

    Drill 04

    //Change the program so that it uses doubles instead of ints. #include "std_lib_facilities.h" int main() { double val1, val2; while(cin >> val1 >> val2) { if( val1 < val2) cout << "the smaller value is: " << val1 << '/n' << "the larger value is: " << val2 << '/n'; else if(val1 > val2) cout << "the smaller value is: " << val2 << '/n' << "the larger value is: " << val1 << '/n'; else cout << "the numbers are equal" << '/n'; cout << '/n'; } }

     

    Drill 05

    //Change the program so that it writes out the numbers are almost equal //after writing out which is the larger and the smaller if the two numbers //differ by less thann 1.0/100. #include "std_lib_facilities.h" int main() { double val1, val2, result; while(cin >> val1 >> val2) { result = val1 - val2; if( result < 1.0/100 && result >0) cout << "the numbers are almost equal"<< '/n' << "the smaller value is: " << val2 << '/n' << "the larger value is: " << val1 << '/n'; else if(result < 0 && result > -1.0/100) cout << "the numbers are almost equal"<< '/n' << "the smaller value is: " << val1 << '/n' << "the larger value is: " << val2 << '/n'; else if(result > 1.0/100) cout << "the smaller value is: " << val2 << '/n' << "the larger value is: " << val1 << '/n'; else cout << "the smaller value is: " << val1 << '/n' << "the larger value is: " << val2 << '/n'; cout << '/n'; } }

     

    Drill 06

    //Now change the body of the loop so that is reads just one double each //time around. Define two variables to keep track of which is the smallest //and which is the largest value you have seen so far. Each time through //the loop write out the value entered. If it's the smallest so far, write the //smallest so far after the number. If it is the largest so far, write the largest //so far after the number #include "std_lib_facilities.h" int main() { double length, smallest, largest; //初始化最大最小值 cin >> length; smallest = length; cin >> length; if(smallest > length) //如果名不符实,交换变量中的值 { largest = smallest; smallest = length; } else largest = length; //判断最值 while(cin >> length) { cout << "the smallest so far: " << smallest << '/n' << "the largest so far: " << largest << '/n'; if(length < smallest) smallest = length; else if(length >largest) largest = length; cout << '/n'; } }

     

    Drill 07

    //Add a unit to each double entered; that is, enter values such as 10cm, //2.5in, 5ft, or 3.33cm. Accept the four units: cm, m, in, ft. Assume conversion //factors 1m == 100cm, 1in == 2.54cm, 1ft == 12in. Read the unit indicator //into a string #include "std_lib_facilities.h" int main() { double length = 1.0; string unit = "abc"; cout << "Please enter a length followed by a unit(cm, m, in or ft): " << '/n'; while(cin >> length >> unit) { if(unit == "cm") cout << length << "cm == " << length/100 << "m/n"; else if(unit == "m") cout << length << "m == " << length*100 << "cm/n"; else if(unit == "in") cout << length << "in == " << length*2.54 << "cm/n"; else if(unit == "ft") cout << length << "ft == " << length*12 << "in/n"; else cout << "Sorry, I don't know a unit called '" << unit << "'./n"; cout << '/n'; } }

     

    Drill 09

    //Keep track of the sum of values entered (as well as the smallest and the //largest) and the number of values entered. When you see the final '|' //print the smallest, the largest, the number of values, and the sum of values. //Note that to keep the sum, you have to decide on a unit to use for //hat sum; use meters. #include "std_lib_facilities.h" int main() { double sum = 0, length = 1.0, smallest, largest; string unit = "abc"; double unify_length(double, string); cout << "Please enter a length followed by a unit(cm, m, in or ft): " << '/n'; cin >> length >> unit; smallest = unify_length(length, unit); cin >> length >> unit; if(smallest > unify_length(length, unit)) { largest = smallest; smallest = unify_length(length, unit); } else largest = unify_length(length, unit); sum = smallest + largest; for(int counter = 2; cin >> length >> unit; ++counter) { cout << "the smallest so far: " << smallest << '/n' << "the largest so far: " << largest << '/n' << "the sum so far: " << sum << '/n' << "the number so far: " << counter << '/n'; if(unify_length(length, unit) < smallest) smallest = unify_length(length, unit); else if(unify_length(length, unit) >largest) largest = unify_length(length, unit); sum += unify_length(length, unit); cout << '/n'; } } //统一单位 double unify_length(double sub_length, string sub_unit) { double u_length = 1.0; if(sub_unit == "cm") u_length = sub_length/100; else if(sub_unit == "m") u_length = sub_length; else if(sub_unit == "in") u_length = sub_length*2.54/100; else if(sub_unit == "ft") u_length = 12*sub_length*2.54/100; else cout << "Sorry, I don't know a unit called '" << sub_unit << "'./n"; return u_length; }

     

    Drill 10

    //Keep all the values entered (convered into meters) in a vector. At the //end, write out those values. #include "std_lib_facilities.h" int main() { double sum = 0, length = 1.0, smallest, largest; string unit = "abc"; vector<double>unilengths; double unify_length(double, string); cout << "Please enter a length followed by a unit(cm, m, in or ft): " << '/n'; cin >> length >> unit; smallest = unify_length(length, unit); unilengths.push_back(smallest); cin >> length >> unit; if(smallest > unify_length(length, unit)) //如果输入值比最小变量中的值小 { largest = smallest; //将最小变量中的值给最大变量 smallest = unify_length(length, unit); //输入值放入最小变量中 unilengths.push_back(smallest); } else { largest = unify_length(length, unit); //否则输入值给最大变量 unilengths.push_back(largest); } sum = smallest + largest; for(int counter = 2; cin >> length >> unit; ++counter) { cout << "the smallest so far: " << smallest << '/n' << "the largest so far: " << largest << '/n' << "the sum so far: " << sum << '/n' << "the number so far: " << counter << '/n'; if(unify_length(length, unit) < smallest) { smallest = unify_length(length, unit); unilengths.push_back(smallest); } else if(unify_length(length, unit) >largest) { largest = unify_length(length, unit); unilengths.push_back(largest); } sum += unify_length(length, unit); cout << '/n'; } for(unsigned int mark=0; mark < unilengths.size(); ++mark) cout << unilengths[mark] << '/n'; } //统一单位 double unify_length(double sub_length, string sub_unit) { double u_length = 1.0; if(sub_unit == "cm") u_length = sub_length/100; else if(sub_unit == "m") u_length = sub_length; else if(sub_unit == "in") u_length = sub_length*2.54/100; else if(sub_unit == "ft") u_length = 12*sub_length*2.54/100; else cout << "Sorry, I don't know a unit called '" << sub_unit << "'./n"; return u_length; }

     

    Drill 11

    //Before writing out the values from the vector, sort them (that'll make //them come out in increasing order). #include "std_lib_facilities.h" int main() { double sum = 0, length = 1.0, smallest, largest; string unit = "abc"; vector<double>unilengths; double unify_length(double, string); cout << "Please enter a length followed by a unit(cm, m, in or ft): " << '/n'; cin >> length >> unit; smallest = unify_length(length, unit); unilengths.push_back(smallest); cin >> length >> unit; if(smallest > unify_length(length, unit)) //如果输入值比最小变量中的值小 { largest = smallest; //将最小变量中的值给最大变量 smallest = unify_length(length, unit); //输入值放入最小变量中 unilengths.push_back(smallest); } else { largest = unify_length(length, unit); //否则输入值给最大变量 unilengths.push_back(largest); } sum = smallest + largest; for(int counter = 2; cin >> length >> unit; ++counter) { cout << "the smallest so far: " << smallest << '/n' << "the largest so far: " << largest << '/n' << "the sum so far: " << sum << '/n' << "the number so far: " << counter << '/n'; if(unify_length(length, unit) < smallest) { smallest = unify_length(length, unit); unilengths.push_back(smallest); } else if(unify_length(length, unit) >largest) { largest = unify_length(length, unit); unilengths.push_back(largest); } sum += unify_length(length, unit); cout << '/n'; } sort(unilengths.begin(), unilengths.end()); for(unsigned int mark=0; mark < unilengths.size(); ++mark) cout << unilengths[mark] << '/n'; } //统一单位 double unify_length(double sub_length, string sub_unit) { double u_length = 1.0; if(sub_unit == "cm") u_length = sub_length/100; else if(sub_unit == "m") u_length = sub_length; else if(sub_unit == "in") u_length = sub_length*2.54/100; else if(sub_unit == "ft") u_length = 12*sub_length*2.54/100; else cout << "Sorry, I don't know a unit called '" << sub_unit << "'./n"; return u_length; }

    最新回复(0)