C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

常用链接

留言簿(8)

搜索

  •  

最新评论

阅读排行榜

评论排行榜


第一个程序:      
#include "stdafx.h"
#include 
<iostream>
using namespace std;
struct travel_time
{
    
int hours;
    
int mines;
}
;
const int Mines_per_hr = 60;
travel_time sum(travel_time t1, travel_time t2);
void show_time(travel_time t);
int main(int argc, char* argv[])
{
    travel_time day1 
= {545}
    travel_time day2 
= {455};
    travel_time trip 
= sum (day1, day2);
    cout
<<"Two-day total: ";
    show_time(trip);
    
    travel_time day3
={4,32};
    cout
<<"Three-day total: ";
    show_time(sum(trip,day3));
    
return 0;
}

travel_time sum(travel_time t1, travel_time t2)
{
    travel_time total;
    total.mines
=(t1.mines+t2.mines)%Mines_per_hr;
    total.hours
=t1.hours+t2.hours+(t1.mines+t2.mines)/Mines_per_hr;
    
return total;
}

void show_time(travel_time t)
{
    cout
<<t.hours<<" hours, "<<t.mines<<" minutes\n";
}

第二个程序:
      这个例子将定义两个结构,用于表示两种不同的描述位置的方法,然后开发一个函数,将一种格式转换为另一种格式,并显示结果。
#include "stdafx.h"
#include 
<iostream>
#include 
<cmath>
using namespace std;
//structure declarations
struct polar
{
    
double distance;    //distance from origin
    double angle;        //direction from origin
}
;
struct rect
{
    
double x;        //horizontal distance from origin
    double y;        //vertical distance from origin
}
;

// prototypes
polar rect_to_polar(rect xypos);
void show_polar(polar dapos);
int main(int argc, char* argv[])
{
    rect rplace;
    polar pplace;
    cout
<<"Enter the x and y values: ";
    
// 将cin>>用作测试条件消除了这种限制,因为它接受任何有效地数字输入。
    
// 在需要使用循环来输入数字时候,别忘了考虑使用这种方式。另外请记住,
    
// 非数字输入将设置一个错误条件,禁止进一步读取输入。如果程序在输入
    
// 循环后还需要进行输入,则必须使用cin.clear()重置输入,然后可能需
    
// 要通过读取不合法的输入来丢弃它们。
    while(cin>>rplace.x>>rplace.y)    //slick use of cin
    {
        pplace 
= rect_to_polar(rplace);
        show_polar(pplace);
        cout
<<"Next two numbers(q to quit): ";
    }

    cout
<<"Done.\n";
    
return 0;
}


//convert rectangular to polar coordinates
polar rect_to_polar(rect xypos)
{
    polar answer;
    answer.distance
=sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
    answer.angle
=atan2(xypos.y, xypos.x);
    
return answer;    
}

//show polar coordinates, converting angle to degrees
void show_polar(polar dapos)
{
    
const double Rad_to_deg=57.29577951;
    cout
<<"distance = "<<dapos.distance;
    cout
<<", angle = "<<dapos.angle * Rad_to_deg;
    cout
<<" degrees\n";
}

第三个程序
      传递结构的地址。假设要传递结构的地址而不是整个结构以节省时间和空间,则需要重新编写前面的函数,使用指向结构的指针。对于重新编写show_polar()函数,需要修改3个地方:1、调用函数时,将结构的地址(&pplace)而不是结构本身(pplace)传递给它。2、将形参声明为指向polar的指针,即polar*类型。由于函数不应该修改结构,因此使用const修饰。3、由于形参是指针而不是结构,因此应使用间接成员操作符(->),而不是成员操作符(句点)。
#include "stdafx.h"
#include 
<iostream>
#include 
<cmath>
using namespace std;
//structure declarations
struct polar
{
    
double distance;    //distance from origin
    double angle;        //direction from origin
}
;
struct rect
{
    
double x;        //horizontal distance from origin
    double y;        //vertical distance from origin
}
;

// prototypes
void rect_to_polar(const rect* pxy, polar* pda);
void show_polar(const polar *pda);
int main(int argc, char* argv[])
{
    rect rplace;
    polar pplace;
    cout
<<"Enter the x and y values: ";
    
while(cin>>rplace.x>>rplace.y)    //slick use of cin
    {
        rect_to_polar(
&rplace, &pplace);
        show_polar(
&pplace);
        cout
<<"Next two numbers(q to quit): ";
    }

    cout
<<"Done.\n";
    
return 0;
}

void rect_to_polar(const rect* pxy, polar* pda)
{
    pda
->distance=sqrt(pxy->* pxy->+ pxy->* pxy->y);
    pda
->angle=atan2(pxy->y, pxy->x);
}

void show_polar(const polar *pda)
{
    
const double Rad_to_deg=57.29577951;
    cout
<<"distance = "<<pda->distance;
    cout
<<", angle = "<<pda->angle * Rad_to_deg;
    cout
<<" degrees\n";
}
posted on 2010-02-12 19:02 烟皑 阅读(323) 评论(0)  编辑 收藏 引用 所属分类: C++ primer plus学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理