C++ Pimer Plus(第6版中文)课后编程练习个人代码
2.7
1.
#include <iostream>
using namespace std;
int main()
{
cout << " Name " << endl;
cout << " Address is " << endl;
return 0 ;
}
2.
#include <iostream>
using namespace std;
double Mar(int);
int main()
{
int Long;
cout << " Enter Long : " << endl;
cin >> Long ;
double mar = Mar(Long);
cout << " Mar is : " << mar << endl;
return 0;
}
double Mar(int Long)
{
double Mar;
Mar = Long /220;
return Mar ;
}
3.
#include <iostream>
using namespace std;
void fun1()
{
cout << " Three blind mice " << endl;
}
void fun2()
{
cout << " See how they run " << endl;
}
int main()
{
fun1();
fun1();
fun2();
fun2();
return 0;
}
4.
#include <iostream>
using namespace std;
int exchange(int);
int main()
{
cout << "Enter your age : " ;
int age;
cin >> age ;
int month = exchange(age);
cout << "The month of your age is : " << month << endl;
return 0;
}
int exchange(int age)
{
return age*12 ;
}
5.
#include <iostream>
using namespace std;
int exchange(int);
int main()
{
cout << "Please enter a Celsius value : " ;
int celsius;
cin >> celsius ;
int Fahrenheit = exchange(celsius);
cout << celsius << " degrees Celsius is " << Fahrenheit << " degrees Fahrenheit."<< endl;
return 0;
}
int exchange(int celsius)
{
int Fahrenheit;
Fahrenheit = 1.8 * celsius +32.0;
return Fahrenheit ;
}
6.
#include <iostream>
double exchange(double);
int main()
{
using namespace std;
cout << " Enter the number of light years: ";
double lightyears;
cin >> lightyears;
double astronomicunits = exchange(lightyears);
cout << lightyears << " light years = " << astronomicunits << " astronomic units." << endl;
return 0;
}
double exchange(double lightyears)
{
double astronomicunits = lightyears*63240 ;
return astronomicunits;
}
7.
#include <iostream>
using namespace std;
void time(int,int);
int main()
{
cout << " Enter the number of hours: ";
int hours;
cin >> hours;
cout << " Enter the number of minutes: ";
int minutes;
cin >> minutes;
time(hours,minutes);
return 0;
}
void time(int hours , int minutes)
{
cout << "Time :"<< hours << ":" << minutes << endl;
}
教训:1.一定注意函数的命名前后一致不然出错,一定要仔细;
2.函数的参数成员赋值可以直接将变量放入自动赋值如第7题