概述
//根据半径计算圆的周长和面积
#include <iostream.h>
const float PI=3.1416; //声明常量(只读变量)PI为3.1416
float fCir_L(float); //声明自定义函数fCir_L()的原型
float fCir_S(float); //声明自定义函数fCir_S()的原型
//以下是main()函数
main()
{
float r,l,s; //声明3个变量
cout<<"r="; //显示字符串
cin>>r; //键盘输入
l=fCir_L(r); //计算圆的周长,赋值给变量l
s=fCir_S(r); //计算圆的面积,赋值给变量s
cout<<"l="<<l; //显示计算结果
cout<<"ns="<<s;
}
//定义计算圆的周长的函数fCir_L()
float fCir_L(float x)
{
float z=-1.0; //声明局部变量
if (x>=0.0) //如果参数大于0,则计算圆的周长
z=2*PI*x;
return(z); //返回函数值
}
//定义计算圆的面积的函数fCir_S()
float fCir_S(float x)
{
float z=-1.0; //声明局部变量
if (x>=0.0) //如果参数大于0,则计算圆的面积
z=PI*x*x;
return(z); //返回函数值
}
/* Program: P1-2.CPP
Written by: Hap
Date written: 02:11:10
*/
#include <iostream.h>
void main(void)
{
double s1,s2,s3;
s1=1.5; /* 对变量s1赋值*/
cout<<"s1="<<s1<<endl;
/* 对变量s2赋值*/ s2=2.5;
cout<<"s2="<<s2<<endl;
s3= /* 对变量s3赋值*/ 3.5;
cout<<"s3="<<s3<<endl;
cout<<"s1+s2+s3="<<s1+s2+s3<<endl; //计算并显示
//计算并显示 cout<<"s1+s2+s3="<<s1+s2+s3<<endl;
}
#include <iostream.h>
main()
{
double r=1.0;
cout<<"r="<<r<<endl;
double l;
l=2*3.1416*r; //计算圆的周长,赋值给变量l
cout<<"l="<<l<<endl; //显示圆的周长
double s=3.1416*r*r; //计算圆的面积,赋值给变量s
cout<<"s="<<s<<endl; //显示圆的面积
cout<<"r="; //显示提示输入的信息
cin>>r; //键盘输入
l=2*3.1416*r; //计算圆的周长,赋值给变量l
cout<<"l="<<l<<endl; //显示圆的周长
s=3.1416*r*r;
cout<<"s="<<s<<endl; //显示圆的面积
}
#include <iostream.h> //包含iostream.h头文件
void main()
{
//输出字符常量、变量和字符串
char c1='A';
cout<<'W';
cout<<c1<<endl;
cout<<"This is a test."<<endl;
cout<<"------------------"<<endl;
//输出整型常量、变量和表达式
int n=100;
cout<<10;
cout<<n;
cout<<2*n<<endl; //输出整型表达式
cout<<"------------------"<<endl;
//输出浮点型常量、变量和表达式
double pi=3.1415926,r=10.0,s=pi*r*r;
cout<<pi<<endl;
cout<<r;
cout<<s;
cout<<2*r*pi<<endl; //输出浮点型表达式
cout<<"------------------"<<endl;
//一个cout可以输出多项数据
cout<<'W'<<" "<<c1<<endl;
cout<<"This is a test."<<endl;
cout<<"pi="<<pi<<" r="<<r<<" s="<<s<<endl;
}
#include <iostream.h> //包含iostream.h头文件
main()
{
//输入输出字符
char c;
cin>>c;
cout<<"c="<<c<<endl;
//输入输出整型数据
int n;
cin>>n;
cout<<"n="<<n<<endl;
//输入输出浮点型数据
double x;
cin>>x;
cout<<"x="<<x<<endl;
//输入提示
cout<<"n=";
cin>>n;
cout<<"n="<<n<<endl;
//多项输入
cout<<"c n x"<<endl;
cin>>c>>n>>x;
cout<<"c="<<c<<" n="<<n<<" x="<<x<<endl;
}
#include <iostream.h> //包含iostream.h头文件
main()
{
//声明整型变量
int a,b;
//从键盘上为整型变量赋值
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
//整型数的算术运算
cout<<a<<"+"<<b<<"="<<a+b<<endl;
cout<<a<<"-"<<b<<"="<<a-b<<endl;
cout<<a<<"*"<<b<<"="<<a*b<<endl;
cout<<a<<"/"<<b<<"="<<a/b<<endl;
cout<<a<<"%"<<b<<"="<<a%b<<endl;
//测试溢出
short n=32767,m; //n取short类型的最大值
cout<<"n="<<n<<endl;
m=n+1; //引起溢出
cout<<"n+1="<<m<<endl;
}
#include <iostream.h> //包含iostream.h头文件
main()
{
//声明变量,并初始化
int a=010,b=10,c=0X10;
//以十进制形式显示数据
cout<<"DEC:";
cout<<" a="<<a;
cout<<" b="<<b;
cout<<" c="<<c<<endl;
//以八进制形式显示数据
cout<<"OCT:";
cout<<oct; //指定八进制输出
cout<<" a="<<a;
cout<<" b="<<b;
cout<<" c="<<c<<endl;
//以十六进制形式显示数据
cout<<"HEX:";
cout<<hex; //指定十六进制输出
cout<<" a="<<a;
cout<<" b="<<b;
cout<<" c="<<c<<endl;
//八、十和十六进制数混合运算并输出
cout<<"a+b+c=";
cout<<dec; //恢复十进制输出
cout<<a+b+c<<endl;
//测试八、十和十六进制输入
cout<<"DEC:a="; cin>>a;
cout<<"OCT:b="; cin>>b;
cout<<"HEX:a="; cin>>c;
cout<<"DEC:"<<dec<<endl; //指定十进制输出
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
#include <iostream.h> //包含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;
}
#include <iostream.h> //包含iostream.h头文件
main()
{
//字符类型变量的声明
char c1='A';
char c2;
//字符数据的运算及输出
c2=c1+32;
cout<<"c1="<<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;
}
#include <iostream.h> //包含iostream.h头文件
main()
{
char c1='a',TAB='t';
//阵铃一声
cout<<c1<<endl;
//使用水平制表符
cout<<1<<TAB<<2<<TAB<<3<<TAB<<4<<endl;
//使用双引号
cout<<"He said "Thank you"."<<endl;
//使用回车换行
cout<<"abcn"<<"def"<<'n';
}
#include <iostream.h> //包含iostream.h头文件
main()
{
//声明bool变量,并初始化
bool flag1=false,flag2=true;
//输出布尔常量和变量
cout<<"false:"<<false<<endl;
cout<<"true: "<<true<<endl;
cout<<"flag1="<<flag1<<endl;
cout<<"flag2="<<flag2<<endl;
//布尔变量的赋值和输出
int x=1;
flag1=x>0; //存放关系运算结果
cout<<"flag1="<<flag1<<endl;
flag2=flag1; //bool类型变量相互赋值
cout<<"flag2="<<flag2<<endl;
//布尔变量超界处理
flag1=100;
cout<<"flag1="<<flag1<<endl;
flag2=-100;
cout<<"flag2="<<flag2<<endl;
}
#include <iostream.h>
const double PI=3.1416; //声明常量(const变量)PI为3.1416
main()
{
//声明3个变量
double r,l,s;
//输入圆的半径
cout<<"r=";
cin>>r;
//计算圆的周长
l=2*PI*r;
cout<<"l="<<l<<endl;
//计算圆的面积
s=PI*r*r;
cout<<"s="<<s<<endl;
}
#include<iostream.h>
main()
{
//定义枚举类型,并指定其枚举元素的值
enum color {
RED=3,
YELLOW=6,
BLUE=9
};
//声明枚举变量a和b,并为枚举变量a赋初值
enum color a=RED;
color b; //合法,与C语言不同
// 输出枚举常量
cout<<"RED="<<RED<<endl;
cout<<"YELLOW="<<YELLOW<<endl;
cout<<"BLUE="<<BLUE<<endl;
//枚举变量的赋值和输出
b=a;
a=BLUE;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
//a=100; 错误!
//a=6 也错误!
//枚举变量的关系运算
b=BLUE; // 枚举变量的赋值运算
cout<<"a<b="<<(a<b)<<endl;
}
#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;
}
#include <iostream.h>
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
}
#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;
}
#include <iostream.h>
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
}
#include <iostream.h>
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;
}
#include <iostream.h>
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;
}
#include <iostream.h>
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;
}
#include <iostream.h>
main()
{
//声明变量语句中使用顺序运算
int x, y;
//计算中使用顺序运算
x=50;
y=(x=x-5, x/5);
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
#include <iostream.h>
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;
}
#include <iostream.h>
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;
}
#include <iostream.h>
main()
{
int x,y;
cout<<"x=";
cin>>x;
if (x<=0) { //满足条件执行
y=2*x;
cout<<"y="<<y; //输出结果
}
else { //不满足条件执行
y=x*x;
cout<<"y="<<y; //输出结果
}
}
#include <iostream.h>
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;
}
#include <iostream.h>
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;
}
#include <iostream.h>
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;
}
#include <iostream.h>
main()
{
int a,b,Max;
//输入数据
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
//找出较大值
Max=a>b?a:b;
cout<<"Max="<<Max<<endl;
}
#include <iostream.h>
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;
}
#include <iostream.h>
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; //当不符合上述情况时执行本子句
}
}
#include<iostream.h>
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; //访问重名的全局变量
}
#include<iostream.h>
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;
}
#include<iostream.h>
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;
}
#include<iostream.h>
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;
}
#include<iostream.h>
main()
{
//计算和打印打印乘法九九表
for (int i=1;i<=9;i++) {
cout<<i;
for (int j=1;j<=9;j++)
cout<<'t'<<i<<"*"<<j<<"="<<i*j;
cout<<endl;
}
}
#include<iostream.h>
main()
{
int x,sum=0;
//定义标号L1
L1: cout<<"x=";
cin>>x;
if (x==-1)
goto L2; //无条件转移语句,转到L2语句处
else
sum+=x;
goto L1; //无条件转移语句,转到L1语句处
//定义标号L2
L2: cout<<"sum="<<sum<<endl;
}
#include<iostream.h>
main()
{
//累加键盘输入的数据
double x,sum=0.0;
while(1) {
cout<<"x=";
cin>>x;
if (x<=0) break;
sum+=x;
}
cout<<"sum="<<sum<<endl;
}
#include<iostream.h>
main()
{
int i;
for (i=1;i<=20;i++)
{
if (i%3==0) //能被 3 整除的整数,返回进行下次循环
continue;
cout<<i<<" ";
}
cout<<endl;
}
#include<iostream.h>
main()
{
//声明数组和变量
int a[5],i,sum;
double avg;
//从键盘上循环为数组赋值
for (i=0;i<5;i++) {
cout<<"a["<<i<<"]=";
cin>>a[i];
}
//直接显示数组元素
cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<endl;
//利用for循环显示数组各元素的值
for (i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
//计算数组元素之和,并显示计算结果
sum=a[0]+a[1]+a[2]+a[3]+a[4];
cout<<"sum="<<sum<<endl;
//利用循环计算数组的累加和
for (sum=0,i=0;i<5;i++)
sum+=a[i];
//显示累加和及平均值
cout<<"sum="<<sum<<endl;
avg=sum/5.0;
cout<<"avg="<<avg<<endl;
}
#include<iostream.h>
main()
{
int i,max,index,a[5];
//从键盘上为数组赋值
for (i=0;i<=4;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
// 利用循环遍历数组,找出最大值的元素及其下标
max=a[0];
for (i=0;i<=4;i++)
{
if (max<a[i])
{
max=a[i];
index=i;
}
}
cout<<"nMax="<<max<<" index="<<index;
}
#include<iostream.h>
#define size 5
main()
{
//声明变量
int i,j;
float t,a[size];
//从键盘上为数组赋值
for (i=0;i<size;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
//对数组按从小到大顺序排序
for (i=0;i<size-1;i++)
for (j=i+1;j<size;j++)
if (a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
//显示排序结果
for (i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
//输入要查找的数据
int value;
int found; //找到为1,否则为0
int low,high,mid;
for (i=1;i<=3;i++) {
cout<<"value=";
cin>>value;
//二分法查找数组a
found=0;
low=0;
high=size-1;
while(low<=high)
{
mid=(high+low)/2;
if (a[mid]==value)
{
found=1;
break;
}
if (a[mid]<value)
low=mid+1;
else
high=mid-1;
}
if (found)
cout<<"The valu found at:a["<<mid<<"]="<<a[mid]<<endl;
else
cout<<"The "<<value<<" is not found!"<<endl;
}
}
#include<iostream.h>
main()
{
//声明变量
int i,j;
float t,a[5];
//从键盘上为数组赋值
for (i=0;i<=4;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
//对数组按从大到小顺序排序
for (i=0;i<=3;i++)
for (j=i+1;j<=4;j++)
if (a[i]<=a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
//显示排序结果
for (i=0;i<=4;i++)
cout<<a[i]<<" ";
}
#include<iostream.h>
main()
{
//声明二维数组及变量
int a[2][3],i,j;
//从键盘上为数组a赋值
for (i=0;i<2;i++)
for (j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
//显示数组a
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
//找出该数组的最大元素及其下标
int h,l,Max=a[0][0];
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
if (Max<a[i][j]) {
Max=a[i][j];
h=i;
l=j;
}
}
}
cout<<"Max:"<<"a["<<h<<"]["<<l<<"]="<<a[h][l]<<endl;
}
#include<iostream.h>
main()
{
//声明字符数组和变量
char str[6];
int i;
//从键盘上输入字符串
cout<<"str=";
cin>>str;
cout<<str<<endl;
//按数组和下标变量两种方式显示字符数组
cout<<str<<endl;
for (i=0;i<6;i++)
cout<<str[i];
cout<<endl;
//字符串反向输出
for (i=5;i>=0;i--)
cout<<str[i];
cout<<endl;
//将字符数组变成大写字母后输出
for (i=0;i<=5;i++)
str[i]-=32; //小写字母转换成大写字母
cout<<str<<endl; //显示字符串
}
#include<iostream.h>
main()
{
//声明变量和指针变量
int a,b,c,*ip;
//指针变量ip指向变量a
a=100;
ip=&a; //使指针变量 ip 指向变量a
cout<<"a="<<a<<endl;
cout<<"*ip="<<*ip<<endl;
cout<<"ip="<<ip<<endl;
//指针变量ip指向变量b
ip=&b; //使指针变量 ip 指向变量b
b=200;
cout<<"b="<<b<<endl;
cout<<"*ip="<<*ip<<endl;
cout<<"ip="<<ip<<endl;
//指针变量ip指向变量c
ip=&c; //使指针变量 ip 指向变量b
*ip=a+b;
cout<<"c="<<c<<endl;
cout<<"*ip="<<*ip<<endl;
cout<<"ip="<<ip<<endl;
}
#include<iostream.h>
main()
{
//声明数组、变量和指针变量
int a[2][3],i,j;
int* ip;
//从键盘上为数组a赋值
for (i=0;i<2;i++) //为数组a赋值
for (j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
//利用下标变量显示数组a
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
//利用指针变量显示数组a
ip=&a[0][0];
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cout<<ip<<" ";
cout<<*ip<<endl;
ip++;
}
}
}
#include<iostream.h>
main()
{
//声明数组、变量和指针变量
int a[]={1,2,3,4,5,6};
int *ip1,*ip2;
//测试指针的赋值运算
ip1=a;
ip2=ip1;
cout<<"*ip1="<<(*ip1)<<endl;
cout<<"*ip2="<<(*ip2)<<endl;
//测试指针的自增自减运算和组合运算
ip1++;
ip2+=4;
cout<<"*ip1="<<(*ip1)<<endl;
cout<<"*ip2="<<(*ip2)<<endl;
//测试指针变量之间的关系运算
int n=ip2>ip1;
cout<<"ip2>ip1="<<n<<endl;
cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl;
//指针变量之间的减法
n=ip2-ip1;
cout<<"ip2-ip1="<<n<<endl;
}
#include<iostream.h>
main()
{
//声明字符型数组和指针变量
char str[10];
char *strip=str;
//输入输出
cout<<"str=";
cin>>str; //用字符数组输入字符串
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
cout<<"strip=";
cin>>strip; //用字符指针变量输入字符串
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
//利用指针变量改变其指向字符串的内容
*(strip+2)='l';
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
//动态为字符型指针变量分配内存
strip=new char(100);
cout<<"strip=";
cin>>strip; //用字符指针变量输入字符串
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
}
#include<iostream.h>
main()
{
// 声明用于存放运动员号码的数组
int h[]={1001,1002,1003,1004};
// 声明用于存放运动员成绩的数组
float x[]={12.3,13.1,11.9,12.1};
//声明用于存放运动姓名的字符型指针数组
char *p[]={"Wang hua","Zhang jian","Li wei","Hua ming"};
//i,j,it是用做循环控制变量和临时变量
int i,j,it;
//ft 用做暂存变量
float ft;
//pt为字符型指针变量用做暂存指针变量
char *pt;
//用选择法对数组x进行排序,并相应调整数组h和p中的数据
for (i=0;i<=3;i++)
for (j=i+1;j<=3;j++)
if (x[i]>=x[j]) {
ft=x[i],x[i]=x[j],x[j]=ft;
it=h[i],h[i]=h[j],h[j]=it;
pt=p[i],p[i]=p[j],p[j]=pt;
}
//以下打印排序结果
for (i=0;i<=3;i++)
cout<<h[i]<<" ,"<<p[i]<<" ,"<<x[i]<<endl;
}
#include<iostream.h>
main()
{
//声明指针数组
char *colors[]={"Red","Blue","Yellow","Green"};
//指向指针的指针变量
char **pt;
//通过指向指针的变量访问其指向的内容
pt=colors;
for (int i=0;i<=3;i++) {
cout<<"pt="<<pt<<endl;
cout<<"*pt="<<*pt<<endl;
cout<<"**pt="<<**pt<<endl;
pt++;
}
}
#include<iostream.h>
main()
{
//定义结构类型
struct books
{
char title[20];
char author[15];
int pages;
float price;
} ;
//声明结构变量
struct books Zbk={"VC++ ","Zhang",295,35.5};
books Wbk;
//对结构变量的输出
cout<<"Zbk:"<<endl;
cout<<Zbk.title <<endl;
cout<<Zbk.author<<endl;
cout<<Zbk.pages<<endl;
cout<<Zbk.price<<endl;
cout<<"--------------------"<<endl;
//对结构成员的运算
Zbk.pages+=10;
Zbk.price+=0.5;
cout<<"Zbk.pages="<<Zbk.pages<<endl;
cout<<"Zbk.price="<<Zbk.price<<endl;
cout<<"--------------------"<<endl;
//对结构变量的输入输出
cout<<"Wbk.title =";
cin>>Wbk.title;
cout<<"Wbk.author=";
cin>>Wbk.author;
cout<<"Wbk.pages=";
cin>>Wbk.pages;
cout<<"Wbk.price=";
cin>>Wbk.price;
cout<<"Wbk:"<<endl;
cout<<Wbk.title <<endl;
cout<<Wbk.author<<endl;
cout<<Wbk.pages<<endl;
cout<<Wbk.price<<endl;
cout<<"--------------------"<<endl;
//结构变量之间的相互赋值
books temp;
temp=Wbk;
cout<<"temp:"<<endl;
cout<<temp.title<<endl;
cout<<temp.author<<endl;
cout<<temp.pages<<endl;
cout<<temp.price<<endl;
}
#include<iostream.h>
main()
{
int i;
//定义结构类型
struct student {
int num;
char name[10];
float maths;
float physics;
float chemistry;
double total;
};
//声明结构数组st
student st[3];
//从键盘上为结构数组输入值
cout<<" num name maths physics chemistry "<<endl;
for (i=0;i<3;i++)
{
cout<<i+1<<" ";
cin>>st[i].num;
cin>>st[i].name;
cin>>st[i].maths;
cin>>st[i].physics;
cin>>st[i].chemistry;
}
//计算每个学生的总成绩
for (i=0;i<3;i++)
st[i].total=st[i].maths+st[i].physics+st[i].chemistry;
//输出结构数组各元素的值
for (i=0;i<3;i++)
{
cout<<"st["<<i<<"]: ";
cout<<st[i].num<<'t';
cout<<st[i].name<<'t';
cout<<st[i].maths<<'t';
cout<<st[i].physics<<'t';
cout<<st[i].chemistry<<'t';
cout<<st[i].total<<endl;
}
}
#include<iostream.h>
main()
{
//定义结构类型
struct human {
char name[10];
int sex;
int age;
};
//声明结构变量和结构指针变量,并初始化
struct human x={"WangPing",1,30},*p=NULL;
//结构指针变量指向对象
p=&x;
//显示结构变量的值
cout<<"x.name="<<x.name<<endl;
cout<<"x.sex="<<x.sex<<endl;
cout<<"x.age="<<x.age<<endl;
//利用结构指针显示结构对象中的数据
cout<<"(*p).name="<<(*p).name<<endl;
cout<<"(*p).sex="<<(*p).sex<<endl;
cout<<"(*p).age="<<(*p).age<<endl;
cout<<"p->name="<<p->name<<endl;
cout<<"p->sex="<<p->sex<<endl;
cout<<"p->age="<<p->age<<endl;
//通过结构指针为结构对象输入数据
cout<<"name:";
cin>>(*p).name;
cout<<"sex:";
cin>>(*p).sex;
cout<<"age:";
cin>>(*p).age;
//显示结构变量的值
cout<<"x.name="<<x.name<<endl;
cout<<"x.sex="<<x.sex<<endl;
cout<<"x.age="<<x.age<<endl;
}
include<iostream.h>
main()
{
//定义结构类型
struct human {
char name[10];
int sex;
int age;
};
//声明结构变量和结构指针,并初始化
struct human x={"WangPing",1,30},*p=&x;
//利用结构指针显示结构中的数据
cout<<"(*p).name="<<(*p).name<<endl;
cout<<"(*p).sex="<<(*p).sex<<endl;
cout<<"(*p).age="<<(*p).age<<endl;
cout<<"-------------------------"<<endl;
//利用new运算符为p分配内存
p=new human;
//从键盘上为p指向的结构对象赋值
cout<<"p->name=";
cin>>p->name;
cout<<"p->sex=";
cin>>p->sex;
cout<<"p->age=";
cin>>p->age;
cout<<"-------------------------"<<endl;
//显示p所指结构对象的值
cout<<"p->name="<<p->name<<endl;
cout<<"p->sex="<<p->sex<<endl;
cout<<"p->age="<<p->age<<endl;
cout<<"-------------------------"<<endl;
//显示结构变量的值
cout<<"x.name="<<x.name<<endl;
cout<<"x.sex="<<x.sex<<endl;
cout<<"x.age="<<x.age<<endl;
//释放p指向的内存
delete p;
}
#include<iostream.h>
main()
{
//定义结构类型
struct human {
char name[10];
int sex;
int age;
};
//声明结构数组和结构指针变量,并初始化
human x[]={{"WeiPing",1,30},{"LiHua",1,25},{"LiuMin",0,23}},*p=NULL;
//用下标变量的输出结构数组的元素
for (int i=0;i<3;i++)
{
cout<<x[i].name<<'t';
cout<<x[i].sex<<'t';
cout<<x[i].age<<endl;
}
cout<<"----------------"<<endl;
//用结构指针输出结构数组的元素
for (p=x;p<=&x[2];p++)
{
cout<<p->name<<'t';
cout<<p->sex<<'t';
cout<<p->age<<endl;
}
}
#include<iostream.h>
main()
{
//定义一个包含指针成员的结构类型
struct test {
char *str;
int *ip;
} x;
//使用结构变量x中的整型指针ip
x.ip=new int; //分配1个单元
*(x.ip)=100;
cout<<"x.ip:"<<x.ip<<'t'<<*(x.ip)<<endl;
cout<<"---------------"<<endl;
delete x.ip;
x.ip=new int[5]; //分配5个单元
for(int i=0;i<5;i++)
*(x.ip+i)=100+i;
cout<<"x.ip:"<<endl;
for(i=0;i<5;i++)
cout<<x.ip+i<<'t'<<(*(x.ip+i))<<endl;
delete x.ip;
cout<<"---------------"<<endl;
//使用结构变量x中的字符型指针str
x.str=new char('A'); //分配1个单元
cout<<"x.str:"<<(*x.str)<<endl;
cout<<"---------------"<<endl;
delete x.str;
x.str=new char[5]; //分配多个单元
*x.str='G';
*(x.str+1)='o';
*(x.str+2)='o';
*(x.str+3)='d';
*(x.str+4)='