一个编程问题(给满意答案分)

来源:百度知道 编辑:UC知道 时间:2024/05/17 00:19:29
#include<iostream.h>
#include<stdlib.h>

void GetTime(int*,int*,int*);
int CalcSeconds(int,int,int);

int main()
{
int hr,min,sec;
char another;
int total_sec;

cout<<"\n\n欢迎使用时间转换程序\n\n";

do
{

GetTime(&hr,&min,&sec);
total_sec=CalcSeconds(hr,min,sec);

cout<<"\n\n你输入的时间是"<<hr<<":"<<min<<":"<<sec;
cout<<"\n等于"<<total_sec<<"秒";

cout<<"\n\n再运行一次吗?y或n";
cin>>another;

}while(another=='y'||another=='Y');

cout<<"\n谢谢你的使用,欢迎下次使用";

return 0;
}

void GetTime(int*h_ptr,int*m_ptr,int*s_ptr)

{
int ask_them_again=1;

while(ask_them_again==1)
{

1)为什么传递的是地址GetTime(&hr,&min,&sec);!!
因为定义的函数参数是指针类型,而hr,min,sec只是基本类型,所以要对它们取地址。
2)cin>>*h_ptr>>*m_ptr>>*m_ptr;前面没有给指针赋值,为什么就可用!!
因为在函数Gettime被调用的时候,h_ptr,m_ptr,m_ptr这些形参会被给予实参,在本例子中,它们指的就是&hr,&min,&sec,也就是说,这些指针在调用的时候已经被赋值了。并且,*h_ptr,*m_ptr,*m_ptr分别标识hr,min,sec

为什么传递的是地址GetTime(&hr,&min,&sec);!!
因为定义的函数GetTime用的形参是指针.
cin>>*h_ptr>>*m_ptr>>*m_ptr;前面没有给指针赋值,为什么就可用!!
因为函数GetTime用的形参是指针h_ptr,m_ptr,m_ptr,在调用函数GetTime时会给予实参,届时形参会自动被实参取代.例如程序中GetTime(&hr,&min,&sec);

GetTime(&hr,&min,&sec);!!

因为函数只能返回一个值,而这个函数想返回三个值,只能用指针的方法,传进三个地址,然后改变地址的内容。

cin>>*h_ptr>>*m_ptr>>*s_ptr;
h_ptr,m_ptr,s_ptr是三个形参,调用者已经通过实参对它们赋值了。