C++函数和结构学习

本文探讨了C++中的字符串构建函数、结构体(如旅行时间与坐标转换)以及函数指针的使用实例。通过`buildstr`函数动态构造字符数组,展示了结构体的相加和极坐标转换。重点在于函数原型声明、内存管理与实际操作。
摘要由CSDN通过智能技术生成
#include<iostream>
using namespace std;
char *buildstr(char c,int n);  //prototype

int main()
{
    int times;
    char ch;
    cin>>ch;
    cout<<"Enter a character: "<<ch<<endl;
    cin>>times;
    cout<<"Enter an integer: "<<times<<endl;

    
    char *ps = buildstr(ch,times);//注意第一次要将函数返回到ps的地址上面
    cout<<ps<<endl;
    delete [] ps;  //free memory
    
    ps=buildstr('+',20);//第二次修改ps的数值就可以
    cout<<ps<<"-DONE-"<<ps<<endl;
    delete [] ps; //free memory
    
    ps=buildstr('-',20);//第三次修改ps的数值就可以
    cout<<ps<<"+done+"<<ps<<endl;
    delete [] ps; //free memory
    return 0;
}

//builds string made of n c characters
char *buildstr(char c,int n){
    char *pstr = new char[n+1];
    pstr[n] = '\0'; //terminate string
    while (n-- >0)
    pstr[n]=c;  //fill rest of string
    return pstr;
}

上面为数组函数

#include<iostream>
using namespace std;

struct travel_time
{
    int hours;
    int mins;
};

const int Mins_per_hr=60;//进位变量
//注意前面引用函数的时候,一定要记得添加分号
travel_time sum(travel_time t1, travel_time t2); //两个结构进行求和函数
void show_time(travel_time t);

int main()
{
    //定义了两个结构的小时和分钟
    travel_time day1 = {5,45};
    travel_time day2 = {4,55};
    //先进行相加,然后再显示
    travel_time trip = sum(day1,day2);
    cout<<"Two-day total: ";
    show_time(trip);
    
    travel_time day3 = {4,32};
    cout<<"Three-day total: ";
    travel_time trip1 = sum(trip,day3);
    show_time(trip1);
    
    travel_time day4 = {1,32};
    cout<<"Four-day total: ";
    show_time(sum(trip1,day4));
    
    return 0;
}

travel_time sum(travel_time t1,travel_time t2)
{
    travel_time total;//首先定义了total是一个结构体
    total.mins = (t1.mins + t2.mins) % Mins_per_hr;
    total.hours = (t1.hours + t2.hours)+(t1.mins + t2.mins) / Mins_per_hr;
    return total;
}   

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


上面是结构体函数

#include<iostream>
#include<cmath>

using namespace std;
struct polar
{
    double distance;
    double angle;
};

struct rect
{
    double x;
    double y;
};

//prototypes
polar rect_to_polar(rect xypos);
void show_polar(polar dapos);

int main()
{
    rect rplace;
    polar pplace;
    
    while(cin>>rplace.x>>rplace.y)
    {
        cout<<"Enter the x and y values: "<<rplace.x<<" "<<rplace.y<<endl;
        pplace = rect_to_polar(rplace);//转换为极坐标
        show_polar(pplace);
        cout<<"Next two numbers (q to quit): ";
    }
    cout<<"Done.\n";
    return 0;
}

polar rect_to_polar(rect xypos)
{
    polar answer;//构建了一个answer的直坐标函数
    
    answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
    answer.angle = atan2(xypos.y,xypos.x);
    return answer;
}

void show_polar(polar dapos)
{
    const double Red_to_deg=57.29577951;//约等于PI/180;
    cout<<"distance="<<dapos.distance;
    cout<<",angle="<<dapos.angle*Red_to_deg;
    cout<<" degrees\n";    
}

 

#include<iostream>
#include<cmath>

using namespace std;
struct polar
{
    double distance;
    double angle;
};

struct rect
{
    double x;
    double y;
};

//prototypes
/*polar rect_to_polar(rect xypos);
void show_polar(polar dapos);*/
void rect_to_polar(const rect * pxy, polar * pda);
void show_polar(const polar * pda);


int main()
{
    rect rplace;
    polar pplace;
    
    while(cin>>rplace.x>>rplace.y)
    {
        cout<<"Enter the x and y values: "<<rplace.x<<" "<<rplace.y<<endl;
        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->x * pxy->x + pxy->y * pxy->y);
    pda->angle = atan2(pxy->y,pxy->x);
}

void show_polar(const polar * pda)
{
    const double Red_to_deg=57.29577951;//约等于PI/180;
    cout<<"distance="<<pda->distance;
    cout<<",angle="<<pda->angle*Red_to_deg;
    cout<<" degrees\n";    
}

上面也是函数结构示例

#include<iostream>
using namespace std;

double betsy(int);
double pam(int);

void estimate(int lines,double (*pf)(int));

int main()
{
    int code;
    while(cin>>code){
    cout<<"How many lines of code do you need? "<<code<<endl;
    cout<<"Here's Betsy's estimate:\n";
    estimate(code,betsy);
    cout<<"Here's Pam's estimate:\n";
    estimate(code,pam);
    }
    return 0;
}

double betsy(int lns)
{
    return 0.05*lns;
}

double pam(int lns)
{
    return 0.03*lns+0.0004*lns*lns;
}

void estimate(int lines,double (*pf)(int))
{
    cout<<lines<<" lines will take ";
    cout<<(*pf)(lines)<<" hour(s)\n";
    
}

 函数指针示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值