函数: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;
}