C++实验四

实验四 函数和作用域

【实验目的】

1、掌握函数的定义和说明方法;

2、掌握函数调用时的传值调用、传址调用和引用调用方法;

3、掌握函数重载的方法;

4、学习使用指向字符串的指针变量;

5、掌握作用域的种类和范围;

【实验内容】

1、编写一个极坐标转换为直角坐标的程序,具体要求如下:

•在main函数中读入极坐标半径和相角;

•使用函数conver实现极坐标和直角坐标之间的转换;

•分别用传址调用和引用调用的方式调用conver函数;

2、编写一个程序,实现浮点类型数据和复数类型数据的相乘;具体要求如下:

•用结构体定义复数类型数据;

•编写浮点类型数据乘法运算函数product;

•重载函数product实现复数类型数据相乘;

•在mian函数中通过键盘输入一对浮点数和一对复数,分别对这两对数据进行相乘计算,并输出结果;

3、编写程序计算一个人的生物节律指数,它是生理、智力和情绪指数之和。情绪周期 是一个正弦曲线,其振幅为1,周期为28天,在给定的一天内,一个人的情绪指数是 , 是他的年龄(以天计算)类似地,生理和智力周期也是正弦曲线,振幅为1,周期分别是23天和33天。

•编写输入年月日计算天输的函数computeday;

•编写计算生理指数的函数compute_physiological_index;

•编写计算情绪指数的函数compute_emotional_index;

•编写计算智力指数的函数compute_mentality_index;

•在主程序中输入年月日,计算生物节指数并输出;

 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 void conver(double *p,double *q){
 5  double m,n;
 6     m=(*p)*cos(*q);
 7  n=(*p)*sin(*q);
 8  cout<<m<<"     "<<n<<endl;
 9 }
10 void conver(double &p,double &q){
11  double m,n;
12     m=p*cos(q);
13  n=p*sin(q);
14     cout<<m<<"     "<<n<<endl;
15 }
16 int main(){
17    double l,o;
18    cout<<"输入极坐标半径和相角:"<<endl;
19    cin>>l>>o;
20    cout<<"传址调用得出的指教坐标"<<endl;
21    conver(&l,&o);
22    cout<<"引用调用得出的指教坐标"<<endl;
23    conver(l,o);
24    return 0;
25 }
 1 #include<iostream>
 2 using namespace std;
 3 struct  complex
 4 {
 5  double real;
 6  double image;
 7 }Plural1,Plural2; 
 8 void product(double m,double n){
 9   cout<<m*n<<endl;
10 }
11 void product(double m,double n, double a,double b){
12  double real0,image0;
13     real0=m*a-n*b;
14  image0=m*b+n*a;
15  cout<<real0<<"  "<<image0<<endl;
16 }
17 int main(){
18    double f1,f2;
19    cout<<"输入一对浮点数:"<<endl;
20    cin>>f1>>f2;
21    cout<<"输入一对复数:"<<endl;
22    cin>>Plural1.real>>Plural1.image>>Plural2.real>>Plural2.image;
23    cout<<"输出浮点数相乘结果:"<<endl;
24    product(f1,f2);
25    cout<<"输出复数相乘结果:"<<endl;
26    product(Plural1.real,Plural1.image,Plural2.real,Plural2.image);
27    return 0;
28 }
 1 #include<iostream>
 2 #include<math.h>
 3 using namespace std;
 4 const double PI=3.1415926;
 5 struct dates{
 6    int year,month,day;
 7 }birth,now;
 8 int computeday(dates b,dates n){
 9     int age=0;
10     int monthday[13]={31,28,31,30,31,30,31,31,30,31,30,31};
11     if((b.year%4==0&&b.year%100!=0||b.year%400==0)&&(b.month<=2)){ monthday[1]=29;}
12  for(int i=b.year+1;i<n.year;i++){
13        if(i%4==0&&i%100!=0||i%400==0)
14     age+=366;
15     else age+=365;}
16     for(int j=b.month;j<12;j++){
17         age+=monthday[j];
18  }
19     for(j=0;j<n.month-1;j++){
20         age+=monthday[j];
21  }
22  age+=monthday[b.month-1]-b.day;
23  age+=n.day;
24     return age;
25 }
26 double compute_physiological_index(int a){
27     return sin(2*PI/23*a);
28 }
29 double compute_emotional_index(int a){
30     return sin(2*PI/28*a);
31 }
32 double compute_mentality_index(int a){
33     return sin(2*PI/33*a);
34 }
35 int main(){
36   int age;
37   double phy,emo,men;
38   cout<<"输入出生年月日:";
39   cin>>birth.year>>birth.month>>birth.day;
40   cout<<"输入现在的年月日:";
41   cin>>now.year>>now.month>>now.day;
42   age=computeday(birth,now);
43   phy=compute_physiological_index(age);
44   emo=compute_emotional_index(age);
45   men=compute_mentality_index(age);
46   cout<<"输出生物节指数:"<<phy+emo+men<<endl;
47   return 0;
48 }

 

posted @ 2013-12-12 23:29  a梦想去柬埔寨  阅读(870)  评论(0编辑  收藏  举报