先给出第三章习题的程序,注释还是暂缓
程序在visual studio下编译,正常运行。如有问题请不吝赐教
第一题
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;
#define NUM_HEIGHT 10
void Input(int & num, int & count);
int Deal(int num, int count);
void Convert(const int & trans, int result, int * inch, int * feet);
int main()
{
int num = 0;
int result = 0;
int inch, feet;
int count = 0;
const int trans = 12;
Input(num, count);
result = Deal(num, count);
cout << "Your height is " << result << " in inch." << endl;
Convert(trans, result, &inch, &feet);
cout << "After dealing with the data, your height is " << feet << " feet and " << inch << " inches." << endl;
return 0;
}
void Input(int & num, int & count)
{
int a;
int temp[NUM_HEIGHT];
cout << "Please input your height in inch: ";
cout << "_\b" ;
while(a = getch())
{
if(a > 47 && a < 58)
{
a = a - 48;
cout << a;
cout << "_";
cout << "\b";
temp[count] = a * (int)pow((double)10,(double)count);
num = num + temp[count];
count ++;
}
else if(a == 13)
{
cout << " " << endl;
break;
}
else if(a == 8)
{
cout << "\b_ " << "\b\b";
num = num - temp[--count];
}
else
continue;
}
}
int Deal(int num, int count)
{
int re = 0;
int m_count = 0;
while(count != 0)
{
int temp;
temp = num / (int)pow((double)10, count-1);
re = re + temp * (int)pow((double)10, m_count);
num = num - temp * (int)pow((double)10, count-1);
count --;
m_count ++;
}
return re;
}
void Convert(const int & trans, int result, int * inch, int * feet)
{
* inch = result % trans;
* feet = result / trans;
}
第二题
#include <iostream>
using namespace std;
const int Conv_inch = 12;
const double Conv_meter = 0.0254;
const double Conv_weight = 2.2;
void Convert(const int *in, const int *fe, const int *wei, double & meter, double & kg);
int main()
{
int inch, feet;
int weight;
double meter;
double kg;
double result;
cout << "Enter your height in feet and inches: ";
cin >> feet >> inch;
cout << "Enter your weight in pound: ";
cin >> weight;
Convert(&inch, &feet, &weight, meter, kg);
result = kg / pow(meter, 2);
cout << "BMI is " << result << endl;
return 0;
}
void Convert(const int *in, const int *fe, const int *wei, double & meter, double & kg)
{
int inches;
inches = *in + *fe * Conv_inch;
meter = (double)inches * Conv_meter;
kg = *wei / Conv_weight;
}
第三题
#include <iostream>
using namespace std;
#define CONV_D_M 60
#define CONV_M_S 60
int main()
{
int degree, minute, second;
double hd_minute, hd_degree;
cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
cout << "First, enter the degrees: ";
cin >> degree;
cout << "Next, enter the minutes of arc: ";
cin >> minute;
cout << "Finally, enter the seconds of arc: ";
cin >> second;
hd_minute = (double)second / CONV_M_S;
hd_degree = (double)degree + ((double)minute + hd_minute) / CONV_D_M;
cout << degree << " degrees, " << minute << " minutes, " << second << " seconds = ";
cout << hd_degree << " degrees" << endl;
return 0;
}
第四题
#include <iostream>
#define CONV_M_S 60
#define CONV_H_M 60
#define CONV_D_H 24
int main()
{
using namespace std;
long int seconds;
int second, minute, hour, day;
cout << "Enter the number of seconds: ";
cin >> seconds;
second = seconds % CONV_M_S;
minute = seconds / CONV_M_S;
hour = minute / CONV_H_M;
minute = minute % CONV_H_M;
day = hour / CONV_D_H;
hour = hour % CONV_D_H;
cout << seconds << " seconds = ";
cout << day << " days, " << hour << " hours, ";
cout << minute << " minutes, " << second << " seconds" << endl;
return 0;
}
第五题
#include <iostream>
using namespace std;
int main()
{
long long people_US, people_W;
double proportion;
cout << "Enter the world's population: ";
cin >> people_W;
cout << "Enter the population of the US: ";
cin >> people_US;
proportion = (double)people_US / people_W;
cout << "The population of the US is " << proportion * 100 << "% of the world population." << endl;
return 0;
}
第六题
#include <iostream>
using namespace std;
int main()
{
double kilometer, litre;
double per_consumption;
cout << "Enter the kilometers: ";
cin >> kilometer;
cout << "Enter the litre: ";
cin >> litre;
per_consumption = litre * 100 / kilometer;
cout << "Every 100 kilometers consumpt " << per_consumption << " litres oil" << endl;
return 0;
}
第七题
#include <iostream>
using namespace std;
int main()
{
const double HKM2MI = 62.14;
const double GL2LI = 3.875;
double l_km, mpg;
cout << "Enter the oil consumption in European style: ";
cin >> l_km;
mpg = l_km / HKM2MI / GL2LI;
mpg = 1 / mpg;
cout << l_km << "/100km equals to " << mpg << "mpg" << endl;
return 0;
}