C++ 实验四
来源:互联网 发布:ws2811编程 编辑:程序博客网 时间:2024/06/06 14:11
【实验目的】
1、掌握函数的定义和说明方法;
2、掌握函数调用时的传值调用、传址调用和引用调用方法;
3、掌握函数重载的方法;
4、学习使用指向字符串的指针变量;
1、掌握函数的定义和说明方法;
2、掌握函数调用时的传值调用、传址调用和引用调用方法;
3、掌握函数重载的方法;
4、学习使用指向字符串的指针变量;
5、掌握作用域的种类和范围;
函数的返回
- 返回非引用类型
当函数返回非引用类型时,其返回值可以是局部对象,也可以是求解表达式的结果:编译器在调用函数的地会将函数返回值复制给临时对象(temporary),故可以返回局部对象
- 返回引用类型(引用是某块内存的别名;指针指向一块内存,它的内容是所指内存的地址)
当函数返回引用类型时,没有复制返回值,而是返回对象本身
!千万不要返回局部对象或变量的引用和指针
引用返回左值:这样的函数可用于任何要求使用左值的地方
#include <iostream>#include <string>using namespace std;string &get_val(string&s){return s;}/*string &get_val(string s){return s;}*///errorint main(){string s="hello";cout<<s<<endl;get_val(s)="hello world";//此处可以给函数返回值赋值cout<<s<<endl;return 0; }
1.
#include <iostream>#include <cmath>using namespace std;const double PI=3.1415926;void conver(double r,double angle,double *x,double *y){*x=r*cos(angle);*y=r*sin(angle);cout<<"指针调用 ";}void conver(double r,double angle,double &x,double &y){x=r*cos(angle);y=r*sin(angle);cout<<"引用调用 ";}int main(){double r,angle;cout<<"输入半径:";cin>>r;cout<<"输入相角:";cin>>angle;angle=angle/180*PI;double x,y;double &xx=x;double &yy=y;conver(r,angle,xx,yy);cout<<"x:"<<x<<" y:"<<y<<endl;x=0;y=0;conver(r,angle,&x,&y);cout<<"x:"<<x<<" y:"<<y<<endl;return 0;}2.
#include <iostream>using namespace std;struct complexNum{double real,imaginary;};double product(double a,double b){return a*b;}complexNum product(complexNum a,complexNum b){complexNum result;result.real=a.real*b.real-a.imaginary*b.imaginary;result.imaginary=a.imaginary*b.real+a.real*b.imaginary;return result;}int main(){double x,y;complexNum c1,c2;cout<<"输入一对浮点数: ";cin>>x>>y;cout<<"输入一对复数(即输入:实部1 虚部1 实部2 虚部2): ";cin>>c1.real>>c1.imaginary>>c2.real>>c2.imaginary;cout<<"浮点数相乘结果: "<<product(x,y)<<endl;complexNum c3=product(c1,c2);cout<<"复数相乘结果:"<<c3.real<<"+"<<c3.imaginary<<"j"<<endl;return 0;}
3.
#include <iostream>#include <cmath>const double PI=3.1415926;using namespace std;struct Date{int year,month,day;};double compute_emotional_index(int days){return sin(2*PI/28*days);}double compute_physiological_index(int days){return sin(2*PI/23*days);}double compute_mentality_index(int days){return sin(2*PI/33*days);}bool isLeap(int year){if(year%4==0 && year%100!=0 || year%400==0)return true;elsereturn false;}int computeday(Date birth,Date current){const int DAYS_LEAP=366;const int DAYS_NONLEAP=365;int mDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int days=0,i;if(isLeap(birth.year)){mDays[2]=29;}days+=mDays[birth.month]-birth.day;for(i=birth.month+1;i<13;i++){days+=mDays[i];}for(i=birth.year+1;i<current.year;i++){days+=(isLeap(i)?DAYS_LEAP:DAYS_NONLEAP);}if(isLeap(current.year))mDays[2]=29;elsemDays[2]=28;for(i=1;i<current.month;i++){days+=mDays[i];}days+=current.day;return days;}int main(){Date birth,current;int days;cout<<"Input Birthday (e.g.1994 1 1): ";cin>>birth.year>>birth.month>>birth.day;cout<<"Input your day (e.g.2013 1 1): ";cin>>current.year>>current.month>>current.day;days=computeday(birth,current);cout<<current.year<<"年"<<current.month<<"月"<<current.day<<"日的生物节律指数: "<<compute_emotional_index(days)+compute_mentality_index(days)+compute_physiological_index(days)<<endl;return 0; }
- C语言程序设计实验手册--实验四
- μC/OS-II实验:实验四 消息队列
- 程序设计C 实验四 题目四 求组合数(0082)
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 实验四
- 【特别推荐】几款极好的 JavaScript 下拉列表插件
- Oracle 删除重复数据只留一条
- SQL SERVER – Get Database Backup History for a Single Database
- 写给大家的编程书(4)
- Python 语言简介
- C++ 实验四
- Cocos2d-X Python 2.7.5 新建项目
- android 拍照图片旋转问题
- DDNS配置实例(DHCP+DNS=DDNS)
- configure/make/make install简要说明
- pat basic level 1016
- 如何把一个java工程打成一个jar包
- mac 添加adb的运行环境
- jquery中的事件处理详细介绍