您的位置:首页 > 编程语言 > C语言/C++

C++基础实例(1)

2016-05-02 01:38 417 查看

简单面积计算函数

注:源码头部的#if 0与尾部的#endif为注释全部

/*计算圆的面积与周长*/
#include<iostream>
using namespace std;
#define PI 3.14159
double area(double x);//面积
double zc(double x);//周长

int main()
{
double ar=0.0;
double cz=0.0;
double r;
cout<<"please input the r:";
cin>>r;

ar=area(r);
cz=zc(r);

cout<<"area is:"<<ar<<endl;
cout<<"zc is:"<<cz<<endl;

return 0;
}

//定义面积函数
double area(double x)
{
double are=0.0;
if(x<=0)
{
cout<<"error,please input again..."<<endl;
}
else
{
are=PI*x*x;
}
return are;
}

//周长函数
double zc(double x)
{
double len=0.0;
if(x<=0)
{
cout<<"error,please input again..."<<endl;
}
else
{
len=2*PI*x;
}
return len;
}

基本计算

/*基本计算*/
#include<iostream>
using namespace std;

int main()
{
int i,j;
cout<<"input:";
cin>>i>>j;
cout<<i+j<<endl;
cout<<i-j<<endl;
cout<<i*j<<endl;
cout<<i/j<<endl;
/*测试溢出*/
short n=32767,m;//short最大值
cout<<"n="<<n<<endl;
m=n+1;
cout<<"m="<<m<<endl;
return 0;
}


进制相关

/*进制相关*/
#include<iostream>
using namespace std;

int main()
{
int a=010,b=10,c=0x10;

//十进制
cout<<"DEC:"<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
cout<<endl;

//八进制
cout<<"OCT:"<<endl;
cout<<oct;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
cout<<endl;

//十六进制
cout<<"HEX:"<<endl;
cout<<hex;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;

//混合运算输出
cout<<"a+b+c=";
cout<<dec;
cout<<a+b+c<<endl;

return 0;
}


数据类型

/*double/float`*/
#include <iostream>  //包含iostream.h头文件
#include<iomanip.h>   // iomanip.h头文件包含setprecision()的定义
main()
{
//float型变量的声明、输入、计算和输出
float fx,fy;
cout<<"fx=";
cin>>fx;
cout<<"fy=";
cin>>fy;
cout<<fx<<"+"<<fy<<"="<<fx+fy<<endl;
cout<<fx<<"-"<<fy<<"="<<fx-fy<<endl;
cout<<fx<<"*"<<fy<<"="<<fx*fy<<endl;
cout<<fx<<"/"<<fy<<"="<<fx/fy<<endl<<endl;
//cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl;  Error!

//double型变量的声明、输入、计算和输出
float dx,dy;
cout<<"dx=";
cin>>dx;
cout<<"dy=";
cin>>dy;
cout<<dx<<"+"<<dy<<"="<<dx+dy<<endl;
cout<<dx<<"-"<<dy<<"="<<dx-dy<<endl;
cout<<dx<<"*"<<dy<<"="<<dx*dy<<endl;
cout<<dx<<"/"<<dy<<"="<<dx/dy<<endl<<endl;
//cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl;  Error!

//测试float和double类型数据的有效位
fx=10.0;fy=6.0;
float fz=fx/fy;
dx=10.0;dy=6.0;
double dz=dx/dy;
cout<<"fz=";
cout<<setprecision(20)<<fx<<"/"<<fy<<"="<<fz<<endl;
cout<<"dz=";
cout<<setprecision(20)<<dx<<"/"<<dy<<"="<<dz<<endl<<endl;;

//float型溢出
float x=3.5e14;
cout<<"x="<<x<<endl;
cout<<"x*x="<<x*x<<endl;
cout<<"x*x*x="<<x*x*x<<endl;
}


字符

/*char*/
#include<iostream>
using namespace std;

int main()
{
//字符变量声明
char c1='A';
char c2;

//字符数据运算及输出
c2=c1+32;
cout<<"c="<<c1<<endl;
cout<<"c2="<<c2<<endl;

//字符及ASCII输出
cout<<c1<<":"<<int(c1)<<endl;
cout<<c2<<":"<<int(c2)<<endl;
cout<<'&'<<int('&')<<endl;

//输入字符
cout<<"c1 c2"<<endl;
cin>>c1>>c2;
cout<<"c1="<<c1<<" c2="<<c2<<endl;

return 0;

}


制表符等

#if 0
#include<iostream>
using namespace std;

int main()
{
char c1='\a',TAB='\t';
//阵铃一声
cout<<c1<<endl;

//水平制表
cout<<1<<TAB<<2<<TAB<<3<<TAB<<4<<endl;

//使用双引号
cout<<"swxc\"tx\"."<<endl;

//使用回车换行
cout<<"xctx\n"<<"0707\n";

return 0;
}
#endif


bool

#if 0
/*bool(0,1,false,true)*/
#include<iostream>
using namespace std;

int main()
{
//声明bool变量并初始化
bool xc1=false,xc2=true;

//输出bool变量及常量
cout<<"false:"<<false<<endl;
cout<<"true:"<<true<<endl;
cout<<"xc1:"<<xc1<<endl;
cout<<"xc2:"<<xc2<<endl;

//bool变量的赋值与输出
int a=3;
xc1=a>0;
cout<<"xc1="<<xc1<<endl;
xc2=xc1;
cout<<"xc2:"<<xc2<<endl;

//bool超界处理
xc1=100;
xc2=-200;
cout<<"xc1:"<<xc1<<endl;
cout<<"xc2:"<<xc2<<endl;

return 0;
}
#endif


const

#if 0
/*const*/
#include<iostream>
using namespace std;
const double sw=2.56;
int main()
{
double a=1.5l,sum;
sum=a+sw;
cout<<"sum is:"<<sum<<endl;
return 0;
}
#endif


enum

#if 0
/*enum*/
#include<iostream>
using namespace std;

int main()
{
//定义枚举类型,并为枚举元素赋值
enum num{
one=1,
two=2,
three=3
};
//声明枚举变量并赋值
enum num a=two;
num b;//在C++中是允许的(struct也可以)

//输出枚举常量
cout<<"one:"<<one<<endl;
cout<<"two:"<<two<<endl;
cout<<"three:"<<three<<endl;

//枚举变量的赋值和输出
b=a;
a=one;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;

//关系运算
b=three;
cout<<"a<b="<<(a<b)<<endl;

return 0;

}
#endif


赋值误差

#if 0
#include <iostream.h>
const double PI=3.1416;     //声明常量(const变量)PI为3.1416
main()
{
//声明3个变量
double r=3,l,s;

//计算圆的周长
l=2*PI*r;
cout<<"l="<<l<<endl;

//计算圆的面积
s=PI*r*r;
cout<<"s="<<s<<endl;

//验证赋值误差
int il,is;
il=l;
is=s;
cout<<"il="<<il<<endl;
cout<<"is="<<is<<endl;
}
#endif


if

#if 0
/*输入判断奇偶数*/
#include <iostream>
using namespace std;
main()
{
int n;
cout<<"n=";
cin>>n;
if (n>=0 && n<=100 &&n%2==0)
cout<<"n="<<n<<endl;
else
cout<<"The "<<n<<" is out of range!"<<endl;
}
#endif


自增自减

#if 0
/*递增与递减*/
#include <iostream.h>
using namespace std;
main()
{
//变量声明
char c;
double x,y;

//测试自增
cout<<"++E and E++ :"<<endl;
c='B';
cout<<"c="<<++c<<endl;   //输出c=C
c='B';
cout<<"c="<<c++<<endl;   //输出c=B
x=1.5;
y=5+ ++x;               //加号后的空格不能少
cout<<"y="<<y<<endl;    //输出y=7.5
x=1.5;
y=5+x++;
cout<<"y="<<y<<endl;    //输出y=6.5
cout<<"--------------------"<<endl;

//测试自减
cout<<"--E and E-- :"<<endl;
c='B';
cout<<"c="<<--c<<endl;   //输出c=A
c='B';
cout<<"c="<<c--<<endl;   //输出c=B
x=1.5;
y=5+--x;
cout<<"y="<<y<<endl;    //输出y=5.5
x=1.5;
y=5+x--;
cout<<"y="<<y<<endl;    //输出y=6.5
}
#endif


关系表达式

#if 0
/*关系表达式*/
#include <iostream.h>
main()
{
int a=3, b=2;

//输出关系表达式
cout<<(a<b)<<endl;
cout<<(a<b)<<(a>b)<<(a>=b)<<(a==b)<<(a!=b)<<endl;

bool flag=2*a<b+10;
cout<<"flag="<<flag;
}
#endif


逻辑、关系运算

#if 0
/*逻辑运算与关系运算*/
#include <iostream.h>
using namespace std;
main()
{
float a=3.5,b=2.1,c=0;
cout<<"a="<<a<<"  b="<<b<<"  c="<<c<<endl;

//与运算
cout<<"a&&b="<<(a&&b)<<endl;//输出1
cout<<"a&&c="<<(a&&c)<<endl;//输出0

//或运算
cout<<"a||b="<<(a||b)<<endl;//输出1
cout<<"a||c="<<(a||c)<<endl;//输出1

//非运算
cout<<"!a="<<!a<<endl<<"!c="<<!c<<endl;//输出0  1

//关系运算和逻辑运算
bool flag=a>=0 && a<=5;  //变量a在[0,5]区间内
cout<<"a=>0 && a<=5="<<flag<<endl;//输出1

//算术运算、关系运算和逻辑运算
cout<<"a+5>2*b+2||a<b+3="<<(a+5>2*b+2||a<b+3)<<endl;//输出1
}
#endif


移位

#if 0
/*逻辑运算*/
#include <iostream>
using namespace std;
main()
{
//按位与运算
cout<<"24&12="<<(24&12)<<endl;
//按位异或运算
cout<<"24^12="<<(24^12)<<endl;
//按位或运算
cout<<"24|12="<<(24|12)<<endl;
//按位取反运算
cout<<"~24="<<(~24)<<endl;

//左移位运算
cout<<"5<<3="<<(5<<3)<<endl;
cout<<"-5<<3="<<(-5<<3)<<endl;

//右移位运算
cout<<"5>>3="<<(5>>3)<<endl;
cout<<"-5>>3="<<(-5>>3)<<endl;
}
#endif


表达式语句

#if 0
/*表达式语句*/
#include <iostream>
using namespace std;
main()
{
int a=1,b=1,c=3;
//显示a,b,c的值
cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl;

//计算显示(1) b+=a+2*c%5; 的结果
b+=a+2*c%5;    	//相当于表达式语句 b=b+(a+2*c%5);
cout<<"(1) b="<<b<<endl;

//计算显示(2) a<<=c-2*b; 的结果
a=1,b=1,c=3;
a<<=c-2*b;     	// 相当于表达式语句 a=a<<(c-2*b);
cout<<"(2) a="<<a<<endl;

//计算显示(3) a*=b=c=3;的结果
a=1,b=1,c=3;
a*=b=c=3;      	//相当于语句组 c=3;b=c;a=a*b;
cout<<"(3) a="<<a<<"  b="<<b<<"  c="<<c<<endl;

//计算显示(4) a+=b+=c;的结果
a=1,b=1,c=3;
a+=b+=c;       	//相当于语句组 b=b+c; a=a+b;
cout<<"(4) a="<<a<<"  b="<<b<<"  c="<<c<<endl;

//计算显示(5) a-=b=++c+2;的结果
a=1,b=1,c=3;
a-=b=++c+2;      	//相当于语句组 ++c;b=b+c+2;a=a-b;
cout<<"(5) a="<<a<<"  b="<<b<<"  c="<<c<<endl;
}
#endif


sizeof

#if 0
/*sizeof*/
#include <iostream>
using namespace std;
main()
{
//用 sizeof 计算各类种常量的字节长度
cout<<"sizeof('$')="<<sizeof('$')<<endl;
cout<<"sizeof(1)="<<sizeof(1)<<endl;
cout<<"sizeof(1.5)="<<sizeof(1.5)<<endl;
cout<<"sizeof(\"Good!\")="<<sizeof("Good!")<<endl;

//用sizeof 计算各类型变量的字节长度
int i=100;
char c='A';
float x=3.1416;
double p=0.1;
cout<<"sizeof(i)="<<sizeof(i)<<endl;
cout<<"sizeof(c)="<<sizeof(c)<<endl;
cout<<"sizeof(x)="<<sizeof(x)<<endl;
cout<<"sizeof(p)="<<sizeof(p)<<endl;

//用sizeof 计算表达式的字节长度
cout<<"sizeof(x+1.732)="<<sizeof(x+1.732)<<endl;

//用 sizeof 计算各类型的字节长度
cout<<"sizeof(char)="<<sizeof(char)<<endl;
cout<<"sizeof(int)="<<sizeof(int)<<endl;
cout<<"sizeof(float)="<<sizeof(float)<<endl;
cout<<"sizeof(double)="<<sizeof(double)<<endl;

//用sizeof 计算数组的字节长度
char str[]="This is a test.";
int a[10];
double xy[10];
cout<<"sizeof(str)="<<sizeof(str)<<endl;
cout<<"sizeof(a)="<<sizeof(a)<<endl;
cout<<"sizeof(xy)="<<sizeof(xy)<<endl;

//用sizeof 计算自定义类型的长度
struct st {
short num;
float math_grade;
float Chinese_grade;
float sum_grade;
};
st student1;
cout<<"sizeof(st)="<<sizeof(st)<<endl;
cout<<"sizeof(student1)="<<sizeof(student1)<<endl;
}
#endif

顺序运算

#if 0
/*顺序运算*/
#include <iostream>
using namespace std;
main()
{
//声明变量语句中使用顺序运算
int x, y;

//计算中使用顺序运算
x=50;
y=(x=x-5, x/5);
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
#endif


类型转换

#if 0
/*类型转换*/
#include <iostream>
using namespace std;
main()
{
//测试表达式类型的转换
int n=100,m;
double x=3.791,y;
cout<<"n*x="<<n*x<<endl;

//赋值类型转换
m=x;
y=n;
cout<<"m="<<m<<endl;
cout<<"y="<<y<<endl;

//强制类型转换
cout<<"int(x)="<<int(x)<<endl;
cout<<"(int)x="<<(int)x<<endl;
cout<<"int(1.732+x)="<<int(1.732+x)<<endl;
cout<<"(int)1.732+x="<<(int)1.723+x<<endl;
cout<<"double(100)="<<double(100)<<endl;
}
#endif


if(1)

#if 0
/*if*/
#include <iostream>
using namespace std;
main()
{
float a,b,s;

cout<<"a b"<<endl;
cin>>a>>b;	   //利用cin从键盘上为变量 a,b 赋值
s=a;
if (a<b) {
s=b;         //if语句中只有这一个语句,可省略花括号
}
s=s*s;          //变量s中保存a,b中较大的一个数的平方
cout<<"s="<<s;
return 0;
}
#endif


if-else

#if 0
/*if-else*/
#include <iostream>
using namespace std;
main()
{
int x,y;
cout<<"x=";
cin>>x;
if (x<=0) {            //满足条件执行
y=2*x;
cout<<"y="<<y<<endl;;     //输出结果
}
else  {              //不满足条件执行
y=x*x;
cout<<"y="<<y<<endl;    //输出结果
}
}
#endif


if嵌套

#if 0
/*if-else内层*/
#include <iostream>
using namespace std;
main()
{
int a,b,c;
int smallest;
cout<<"a  b  c"<<endl;
cin>>a>>b>>c;
if (a<=b)    //外层条件语句
{
if (a<=c)    //内层条件语句
smallest=a;
else
smallest=c;
}
else
{
if (b<=c)    //内层条件语句
smallest=b;
else
smallest=c;
}
cout<<"Smallest="<<smallest<<endl;
}
#endif

#if 0
#include <iostream>
using namespace std;
main()
{
int score;

//从键盘上输入分数
cout<<"score=";
cin>>score;

//用带else if的条件语句判断处理
if (score<0 || score>100)
{
cout<<"The score is out of range!"<<endl;
}
else if (score>=90)
cout<<"Your grade is a A."<<endl;
else if (score>=80)
cout<<"Your grade is a B."<<endl;
else if (score>=70)
cout<<"Your grade is a C."<<endl;
else if (score>=60)
cout<<"Your grade is a D."<<endl;
else
cout<<"Your grade is a E."<<endl;
}
#endif

#if 0
/*100以内偶数*/
#include<iostream>
using namespace std;
main()
{
for(int i=0;i<100;i++)
{
if(i%2==0)
{
cout<<i<<"\t";
}
else
continue;
}
return 0;
}
#endif


二元运算

#if 0
#include <iostream>
using namespace std;
main()
{
int a,b,Max;
//输入数据
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;

//找出较大值
Max=a>b?a:b;
cout<<"Max="<<Max<<endl;
return 0;
}
#endif


判断

#if 0
/*除法*/
#include <iostream>
using namespace std;
main()
{
int a,b;
//输入数据
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;

//除法判断
if (b!=0 && a%b==0) {
cout<<b<<" divides "<<a<<endl;
cout<<"a/b="<<a/b<<endl;
}
else
cout<<b<<" does not divide "<<a<<endl;
}
#endif


switch

#if 0
/*switch*/
#include <iostream>
using namespace std;
main()
{
//x,y 为操作数,c为运算符
int x,y,z;
char c1;
cin>>x>>c1>>y;   //c1

//多路选择语句选择不同表达式计算语句
switch(c1) {
case '+':cout<<x<<"+"<<y<<"="<<x+y<<endl;
break;
case '-':cout<<x<<"-"<<y<<"="<<x-y<<endl;
break;
case '*':cout<<x<<"*"<<y<<"="<<x*y<<endl;
break;
case '/':cout<<x<<"/"<<y<<"="<<x/y<<endl;
break;
case '%':cout<<x<<"%"<<y<<"="<<x%y<<endl;
break;
default :cout<<"Wrong !"<<endl; //当不符合上述情况时执行本子句
}
}
#endif


全局与局部变量

#if 0
/*全局变量与局部变量*/
#include<iostream>
using namespace std;
float x=365.5;  //声明全局变量
main() {
int x=1,y=2;
double w=x+y;
{
double x=1.414,y=1.732,z=3.14;
cout<<"inner:x="<<x<<endl;
cout<<"inner:y="<<y<<endl;
cout<<"inner:z="<<z<<endl;
cout<<"outer:w="<<w<<endl;
cout<<"::x="<<::x<<endl;    //访问重名的全局变量
}
cout<<"outer:x="<<x<<endl;
cout<<"outer:y="<<y<<endl;
cout<<"outer:w="<<w<<endl;

//cout<<"inner:z="<<z<<endl;无效
cout<<"::x="<<::x<<endl;    //访问重名的全局变量
}
#endif


for

#if 0
/*for*/
#include<iostream>
using namespace std;
main() {
//显示1,2,3...10
for(int i=1;i<=10;i++)
cout<<i<<" ";
cout<<endl;

//显示10,9,8...1
for(int j=10;j>=1;j--)
cout<<j<<" ";
cout<<endl;

//显示1,3,5...9
for(int k=1;k<=10;k=k+2)
cout<<k<<" ";
cout<<endl;

//显示ABC...Z
for(char c='A';c<='Z';c++)
cout<<c;
cout<<endl;

//显示0,0.1,0.2...1.0
for(float x=0;x<=1.0;x=x+0.1)
cout<<x<<" ";
cout<<endl;

//显示0,0.1,0.2...1.0
for(float x1=0;x1<=1.0+0.1/2;x1=x1+0.1)
cout<<x1<<" ";
cout<<endl;

//计算s=1+2+3...+100
int s=0;
for(int n=1;n<=100;n++)
s=s+n;
cout<<"s="<<s<<endl;
}
#endif


while

#if 0
/*while*/
#include<iostream>
using namespace std;
main()
{
//计算s=1+2+3...+100
int s=0,n=1;
while(n<=100) {
s=s+n;
n++;
}
cout<<"s="<<s<<endl;

//累加键盘输入的数据
double x,sum=0.0;
cout<<"x=";
cin>>x;
while(x!=0) {
sum+=x;
cout<<"x=";
cin>>x;
}
cout<<"sum="<<sum<<endl;
}
#endif


do-while

#if 0
/*do-while*/
#include<iostream>
using namespace std;
main()
{
//计算s=1+2+3...+100
int s=0,n=0;
do  {
n++;
s+=n;
}while(n<100);
cout<<"s="<<s<<endl;

//累加键盘输入的数据
double x,sum=0.0;
do {
cout<<"x=";
cin>>x;
sum+=x;
} while(x!=0);
cout<<"sum="<<sum<<endl;
}
#endif

#if 0
/*9*9*/
#include<iostream>
using namespace std;

int main()
{
for(int i=1;i<=9;i++)
{
cout<<i;
for(int j=i;j<=9;j++)
{
cout<<"\t"<<i<<"*"<<j<<"="<<i*j<<endl;
}
}
}
#endif


goto

#if 0
/*goto*/
#include<iostream>
using namespace std;

int main()
{
int x,sum=0;
L1:cout<<"x=";//定义标记L1
cin>>x;
if(x==-2)
goto L2;//转到L2
else
sum+=x;
cout<<"sum="<<sum<<endl;
goto L1;//转到L1

L2:cout<<"error!"<<"sum="<<sum<<endl;
goto L1;

return 0;
}
#endif


break

#if 0
/*break(跳出)*/
#include<iostream>
using namespace std;
main()
{
//累加键盘输入的数据
double x,sum=0.0;
while(1) {
cout<<"x=";
cin>>x;
if (x<=0) break;
sum+=x;
}
cout<<"sum="<<sum<<endl;
}
#endif


continue

#if 0
#include<iostream>
using namespace std;
main()
{
//累加键盘输入的数据
double x,sum=0.0;
while(1) {
cout<<"x=";
cin>>x;
if (x<=0) continue;
sum+=x;
}
cout<<"sum="<<sum<<endl;
}
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: