函数+共用体+结构体+宏+枚举

函数:int型函数需要返还一个值 在后期使用中使得有值为该值

两种引用形式:

A.在main函数之前

float k(a,b,c)

{float v;

v=a*b*c;

return(v);}

在main函数中直接使用

v=k(a,b,c)

B.被调用

在main前面加 int k

void 不需要返还数值

int Fibonacci(int n)  
{  
     if( n == 1 || n == 2) 
          return 1;  
    else  
          return Fibonacci(n-1)+Fibonacci(n-2); 
}  
  

#define SUM(A,B)   (a+b)

在后期使用中使得有值为该值

结构体和共用体

struct student

{int sid;

char name【30】;

}stu1;

cin>>stu1.sid;

#include <iostream.h>
struct students
{
 int sid;
char name[30];
int age;
char dept[50];
};
float avg(struct students stu[],int n)
{float av= 0;

for(int i=0;i<n;i++)
av=av+stu[i].age;
av= av/n;
return av;
}
void main()
{
struct students stu[3]={{1001,"张三",19,"计算机"},{1001,"李四",20,"计算机"}, {1001,"王一",19,"计算机"}};

cout<<avg(stu,3)<<endl;
}

#include <iostream.h>
struct complex
{
double r;
double i;
};
struct complex input()
{
struct complex c;
cin>>c.r>>c.i;
return c;
}
void pr(struct complex c)
{
 cout<<c.r<<"+"<<c.i<<"i"<<endl;
}
void main()
{
struct complex myc;
myc=input();
pr(myc);
}

共用体

union data

{int i;

char ch;

}

nuino data u1{65}

#include <iostream>
using namespace std;
union data
{
int ch[11];
int i;
};
void main()
{
union data u1;
cin>>u1.i;
int j=0,a,k;
k=u1.i;
while(k/2!=0)
{u1.ch[j]=k%2;
j++;
k=k/2;
}
u1.ch[j]=k;
for(a=j;a>=0;a--)
cout<<u1.ch[a]<<" ";
cout<<endl;
}

枚举

enum week {1,2,3}

如果没有赋值 那么第一个就是0

#include <iostream.h>
void main()
{
	enum week{SAT=1,SUN,HON,TUE,wEND,THUR,FRI};
	enum week w1;
     w1=TUE;
    cout<<w1;
}

typedef int INT

新名字

#include <iostream.h>

typedef struct DATE
{
int y;
int m;
int d;
} Date;
int dayofyear(Date day)
{int sumday= 0;
int arm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
for(int i=0;i<day.m;i++)
sumday+=arm[i];
sumday+=day.d;
if((day.y%4==0&&day.y%100!=0)||day.y%400==0)
sumday++;
return sumday;}
void main()
{
Date d1;
cin>>d1.y>>d1.m>>d1.d;
cout<<dayofyear(d1)<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值