//根据半径计算圆的周长和面积
#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); //返回函数值
}
2.
/* 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=2.5; /* 对变量s2赋值*/
cout<<"s2="<<s2<<endl;
s3=3.5; /* 对变量s3赋值*/
cout<<"s3="<<s3<<endl;
cout<<"s1+s2+s3="<<s1+s2+s3<<endl; //计算并显示
cout<<"s1+s2+s3="<<s1+s2+s3<<endl; //计算并显示
}
3.
#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; //显示圆的面积
}
4.
#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;
}
5.
#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;
}
6.
#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;
}
7.
#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;
}
8.
#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;
}
9.
#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;
}
10.
#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<<"abc\n"<<"def"<<'\n';
}
11.
#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;
}
12.
#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;
}
13.
#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;
}
14.
#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;
}
15.
#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
}
16.
#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;
}
17.
#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;
cout<<"a&&c="<<(a&&c)<<endl;
//或运算
cout<<"a||b="<<(a||b)<<endl;
cout<<"a||c="<<(a||c)<<endl;
//非运算
cout<<"!a="<<!a<<endl<<"!c="<<!c<<endl;
//关系运算和逻辑运算
bool flag=a>=0 && a<=5; //变量a在[0,5]区间内
cout<<"a=>0 && a<=5="<<flag<<endl;
//算术运算、关系运算和逻辑运算
cout<<"a+5>2*b+2||a<b+3="<<(a+5>2*b+2||a<b+3)<<endl;
}
18.
#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;
}
19.
#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;
}
20.
#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;
}
21.
#include <iostream.h>
main()
{
//声明变量语句中使用顺序运算
int x, y;
//计算中使用顺序运算
x=50;
y=(x=x-5, x/5);
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
22.
#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;
}
23.
#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;
}
24.
#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; //输出结果
}
}
25.
#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;
}
26.
#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;
}
27.
#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;
}
28.
#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;
}
29.
#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;
}
30.
#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; //当不符合上述情况时执行本子句
}
}
31.
#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; //访问重名的全局变量
}
32.
#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;
}
33.
#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;
}
34.
#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;
}
35.
#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;
}
}
36.
#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;
}
37.
#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;
}
38.
#include<iostream.h>
main()
{
int i;
for (i=1;i<=20;i++)
{
if (i%3==0) //能被 3 整除的整数,返回进行下次循环
continue;
cout<<i<<" ";
}
cout<<endl;
}
39.
##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;
}
40.
#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;
}
41.
#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;
}
}
42.
#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]<<" ";
}
43.
#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;
}
44.
#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; //显示字符串
}
45.
#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;
}
46.
#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++;
}
}
}
47.
#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;
}
48.
#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;
}
49.
#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;
}
50.
#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++;
}
}
51.
#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;
}
52.
#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;
}
}
53.
#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;
}
54.
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;
}
55.
#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;
}
}
56.
#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)='\0';
cout<<"x.str:"<<x.str<<endl;
delete x.str;
cout<<"---------------"<<endl;
//在声明结构变量时初始化
test y={"Very Good!",NULL};
cout<<"y.str:"<<y.str<<endl;
cout<<"y.ip:"<<y.ip<<endl;
}
57.
#include<iostream.h>
main()
{
//定义date结构
struct date
{
int year;
int month;
int day;
};
//定义baby结构
struct baby {
int num;
float weight;
date birthday; // date为结构类型
};
//声明baby结构变量并初始化
baby b1={10001,10,{2002,12,25}};
//下列是baby结构变量b1的引用。
cout<<"b1.num="<<b1.num<<endl;
cout<<"b1.weight="<<b1.weight<<endl;
cout<<"b1.birthday.year="<<b1.birthday.year<<endl;
cout<<"b1.birthday.month="<<b1.birthday.month<<endl;
cout<<"b1.birthday.day="<<b1.birthday.day<<endl;
cout<<"--------------------------"<<endl;
//声明baby结构变量temp,并进行赋值运算
baby temp;
temp=b1;
cout<<"temp.num="<<temp.num<<endl;
cout<<"temp.weight="<<temp.weight<<endl;
cout<<"temp.birthday.year="<<temp.birthday.year<<endl;
cout<<"temp.birthday.month="<<temp.birthday.month<<endl;
cout<<"temp.birthday.day="<<temp.birthday.day<<endl;
}
58.
#include<iostream.h>
main()
{
//定义名为list的递归结构
struct list {
char name[10];
int sex;
int age;
list *next; //成员next为指向其自身结构的指针
};
//使用递归结构变量
list L1={"WeiPing",1,35.5,NULL};
cout<<"L1:"<<endl;
cout<<"name\t"<<L1.name<<endl;
cout<<"sex\t"<<L1.sex<<endl;
cout<<"age\t"<<L1.age<<endl;
cout<<"next\t"<<L1.next<<endl;
}
59.
#include<iostream.h>
main()
{
int i;
//定义名为student的递归结构
struct student {
char name[10];
int math;
int computer;
float sum;
student *next; //next成员是指向自身的结构指针
};
//用student声明3个结构指针变量
struct student *head,*tail,*temp;
//申请第1块数据,并设置各结构指针的初值
temp=new struct student; //申请内存
head=temp; // 头指针
tail=head; // 尾指针
//循环为链表输入数据
cout<<"\tname Math Computer"<<endl;
for (i=1;;i++) {
cout<<i<<"\t";
cin>>temp->name;
if (temp->name[0]!='*')
{
cin>>temp->math>>temp->computer;
temp->sum=temp->math+temp->computer;
temp->next=NULL;
tail=temp; //设置链表尾指针
}
else
{
// 以下是输入结束处理
delete temp;
tail->next=NULL;
break;
}
//为下一个学生申请内存
temp->next=new struct student;
temp=temp->next; // 使处理指针temp指向新内存块
}
//将链表数据从头到尾打印出来
cout<<"--------------------"<<endl;
temp=head;
while (temp!=NULL) {
cout<<temp->name<<","<<temp->math<<",";
cout<<temp->computer<<","<<temp->sum<<endl;
temp=temp->next;
}
}
60.
#include<iostream.h>
main()
{
int i;
//定义名为student的递归结构
struct student {
char name[10];
int math;
int computer;
float sum;
student *forw; //forw成员是前指针
student *next; //next成员是后指针
};
//用student声明3个结构指针变量
struct student *head,*tail,*temp;
//申请第1块数据,并设置各结构指针的初值
temp=new struct student; //申请内存
head=temp; // 头指针
tail=head; // 尾指针
head->forw=NULL;
//循环为链表记录输入数据
cout<<"\tname Math Computer"<<endl;
for (i=1;;i++) {
cout<<i<<"\t";
cin>>temp->name;
if (temp->name[0]!='*')
{
cin>>temp->math>>temp->computer;
temp->sum=temp->math+temp->computer;
temp->next=NULL;
tail=temp; //设置链表尾指针
}
else
{
// 以下是输入结束处理
delete temp;
tail->next=NULL;
break;
}
//为下一个学生申请内存
temp->next=new struct student;
temp->next->forw=temp; //设置前指针
temp=temp->next; //使处理指针temp指向新内存块
}
// 将链表数据从头到尾打印出来
cout<<"head------>tail:"<<endl;
temp=head;
while (temp!=NULL) {
cout<<temp->name<<","<<temp->math<<",";
cout<<temp->computer<<","<<temp->sum<<endl;
temp=temp->next;
}
// 将链表数据从尾到头打印出来
cout<<"tail------>head:"<<endl;
temp=tail;
while (temp!=NULL) {
cout<<temp->name<<","<<temp->math<<",";
cout<<temp->computer<<","<<temp->sum<<endl;
temp=temp->forw;
}
}
61.
#include<iostream.h>
main()
{
int i;
//定义联合类型
union utag {
char c;
int k;
float x;
};
//声明联合变量
union utag u;
// 使用联合变量中的字符型成员
u.c='*';
cout<<"u.c="<<u.c<<endl;
// 使用联合变量中的整型成员
u.k=1000;
cout<<"u.k="<<u.k<<endl;
// 使用联合变量中的浮点型成员
u.x=3.1416;
cout<<"u.x="<<u.x<<endl;
//声明联合变量时初始化
utag u1={'A'};
//同时引用联合变量的各成员
cout<<"u1.c="<<u1.c<<endl;
cout<<"u1.k="<<u1.k<<endl;
cout<<"u1.x="<<u1.x<<endl;
}
62.
#include<iostream.h>
main()
{
//定义结构类型,并为声明的结构变量赋初值
struct s_tag {
short i;
float x;
} sx={100,3.1416};
//定义联合类型,并为声明的联合变量赋初值
union u_tag {
short i;
float x;
} ux={1000};
//输出结构类型和结构变量的有关信息
cout<<"sizeof(struct s_tag)="<<sizeof(struct s_tag)<<endl;
cout<<"sx.i="<<sx.i<<endl;
cout<<"sx.x="<<sx.x<<endl;
cout<<"sizeof(sx)="<<sizeof(sx)<<endl;
cout<<"------------------------------"<<endl;
//输出联合类型和联合变量的有关信息
cout<<"sizeof(union u_tag)="<<sizeof(union u_tag)<<endl;
ux.i=200;
cout<<"ux.i="<<ux.i<<endl; //输出联合变量ux 的i成员
ux.x=123.456;
cout<<"ux.x="<<ux.x<<endl; //输出联合变量ux 的x成员
cout<<"sizeof(ux)="<<sizeof(ux)<<endl;
}
63.
#include<iostream.h>
main()
{
//自定义类型
typedef int ARRAY_INT[50];
int i;
ARRAY_INT a; //用自定义类型声明数组变量a
//以下为数组a赋值,并打印
for (i=0;i<50;i++) {
if (i%10==0) //每10个数换一次行
cout<<endl;
a[i]=i;
cout<<a[i]<<"\t";
}
cout<<endl;
}
64.
#include<iostream.h>
//定义结构类型
struct student
{
int num;
char name[20];
float grade;
};
void main(void)
{
//声明数组
int i,size;
char str[]="This is a string.";
int int_values[] = {51, 23, 2, 44, 45,0,11};
float float_values[] = {15.1, 13.3, 22.2, 10.4, 1.5};
student st_arr[]={101,"WangLin",92,102,"LiPing",85,103,"ZhaoMin",88};
//显示char类型数组元素及其大小
size=sizeof(str) / sizeof(char);
cout<<"Number of elements in str: ";
cout<<size<<endl;
for(i=0;i<size;i++) {
cout<<str[i];
}
cout<<endl;
//显示int类型数组元素及其大小
size=sizeof(int_values) / sizeof(int);
cout<<"Number of elements in int_values: ";
cout<<size<<endl;
for(i=0;i<size;i++) {
cout<<int_values[i]<<" ";
}
cout<<endl;
//显示float类型数组元素及其大小
size=sizeof(float_values) / sizeof(float);
cout<<"Number of elements in float_values: ";
cout<<size<<endl;
for(i=0;i<size;i++) {
cout<<float_values[i]<<" ";
}
cout<<endl;
//显示student类型数组元素及其大小
size=sizeof(st_arr) / sizeof(student);
cout<<"Number of elements in st_arr: ";
cout<<size<<endl;
for(i=0;i<size;i++) {
cout<<st_arr[i].num<<" ";
cout<<st_arr[i].name<<" ";
cout<<st_arr[i].grade<<endl;
}
}
65.
#include<iostream.h>
//add()函数的定义,其有返回值
double add(double x,double y)
{
double z;
z=x+y;
cout<<x<<"+"<<y<<"="<<z<<endl;
return(z);
}
main()
{
double a=0.5,b=1.0;
//以不同参数形式调用函数add()
cout<<"add(1.5,2.5)="<<add(1.5,2.5)<<endl;
cout<<"add(a,b)="<<add(a,b)<<endl;
cout<<"add(2*a,a+b)="<<add(2*a,a+b)<<endl;
cout<<"----------------------"<<endl;
//以表达式方式调用函数add()
double c=2*add(a,b);
cout<<"c="<<c<<endl;
cout<<"----------------------"<<endl;
//以语句式方式调用函数add()
add(2*a,b);
cout<<"----------------------"<<endl;
//用其他类型参数调用函数add()
int n=1,m=2;
cout<<"add("<<n<<","<<m<<")="<<add(n,m)<<endl;
}
66.
#include<iostream.h>
//定义符号函数sgn(),其返回值为int类型
int sgn(double x)
{
if (x>0) return(1); //返回出口1
if (x<0) return(-1); //返回出口2
return(0); //返回出口3
}
//main()函数定义
main()
{
double x;
int i;
for (i=0;i<=2;i++) {
cout<<"x=";
cin>>x;
cout<<"sgn("<<x<<")="<<sgn(x)<<endl;
}
}
67.
#include<iostream.h>
//函数原型语句可以在这里
//定义main()函数
main()
{
//max()函数原型声明语句
float max(float,float);
//变量声明语句
float a,b,Max;
//输入参数并计算
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
Max=max(a,b); //调用max()函数
cout<<"max("<<a<<","<<b<<")="<<Max<<endl;
}
//定义max()函数
float max(float x,float y) //max()返回值类型为浮点型
{
float z;
z=(x>y)?x:y;
return(z);
}
68.
#include<iostream.h>
//定义f()函数
f(int x,int y) //f()的参数以值方式传递
{
++x;
--y;
cout<<"x="<<x<<",y="<<y<<endl;
}
main() {
int a,b;
//设置实际参数的值
a=b=10;
//以变量为参数调用f()函数
f(a,b);
//验证实际参数的值
cout<<"a="<<a<<",b="<<b<<endl;
//以表达式参数形式调用f()函数
f(2*a,a+b);
}
69.
#include<iostream.h>
//定义公共结构类型
struct student {
int num;
char name[10];
float maths;
float physics;
float chemistry;
double total;
};
//定义结构输入函数
input_Rec(struct student *p) //参数为student类型的结构指针变量
{
cin>>p->num;
cin>>p->name;
cin>>p->maths;
cin>>p->physics;
cin>>p->chemistry;
}
//定义结构数据交换函数
swap_Rec(struct student *p1,struct student *p2)
{
struct student x;
//交换两个记录的数据
x=*p1;
*p1=*p2;
*p2=x;
}
//输出结构的值
put_Rec(struct student *p)
{
cout<<p->num<<'\t';
cout<<p->name<<'\t';
cout<<p->maths<<'\t';
cout<<p->physics<<'\t';
cout<<p->chemistry<<'\t';
cout<<p->total<<endl;
}
//定义main()函数
main()
{
int i,j;
// 声明结构指针变量和结构数组
struct student *p1,a[3];
//输入3个学生的数据并计算总成绩
cout<<"num\tname\tmaths\tphysics\tchemistry"<<endl;
for (p1=a;p1<=a+2;p1++) {
input_Rec(p1);
p1->total=p1->maths+p1->physics+p1->chemistry;
}
//对3个学生的数据排序
for (i=0;i<=2;i++)
for (j=i+1;j<=2;j++)
if (a[i].total<a[j].total)
swap_Rec(&a[i],&a[j]); //交换两个结构变量中的数据
cout<<"-------------------"<<endl; //输出一分界线
//输出排序后的结构数组
cout<<"num\tname\tmaths\tphysics\tchemistry\ttotal"<<endl;
for (p1=a;p1<=a+2;p1++)
put_Rec(p1);
}
70.
#include<iostream.h>
//定义结构
struct student {
char name[10];
float grade;
};
//交换student类型的数据
void swap(student &x,student &y) //swap的参数为引用传递方式
{
student temp;
temp=x;
x=y;
y=temp;
}
//返回student类型的引用,求优者
student& max(student &x,student &y) //swap的参数为引用传递方式
{
return (x.grade>y.grade?x:y);
}
//显示student类型的数据
void show(student &x) //show的参数为引用传递方式
{
cout<<x.name<<" "<<x.grade<<endl;
}
void main()
{
student a={"ZhangHua",351.5},b={"WangJun",385};
//显示a和b的数据
cout<<"a:";
show(a);
cout<<"b:";
show(b);
cout<<"------------------"<<endl;
//交换a和b的数据,并显示
swap(a,b);
cout<<"a:";
show(a);
cout<<"b:";
show(b);
cout<<"------------------"<<endl;
//计算和显示成绩高者
student t=max(a,b);
cout<<"Max:";
show(t);
}
71.
#include <iostream.h>
//参数带有默认值的函数
disp(int x=1,int y=1,int z=1)
{
cout<<"参数1: "<<x<<endl;
cout<<"参数2: "<<y<<endl;
cout<<"参数3: "<<z<<endl;
cout<<"------------------"<<endl;
}
//main()函数中测试参数带有默认值的函数disp()
void main()
{
disp();
disp(10);
disp(10,20);
disp(10,20,30);
int a=1,b=2,c=3;
disp(a,b,c);
}
72.
#include <iostream.h>
//计算字符串长度的函数
int str_len(const char *string)
{
//char *temp=string; 编译报错!
//*string='x'; 编译报错!
int i=0;
while (*(string+i)!=NULL)
i++;
return i;
}
//main()函数中测试str_len()
void main()
{
char a[]="ABCDE";
cout<<a<<"\t"<<str_len(a)<<endl;
char *str="Hello!";
cout<<str<<"\t"<<str_len(str)<<endl;
cout<<"This is a test."<<"\t"<<str_len("This is a test.")<<endl;
}
73.
#include<iostream.h>
void disp(void); //这个函数声明语句不能少
//定义main()函数的参数和返回值类型是void类型
void main(void)
{
//调用void类型函数
disp();
}
//以下定义disp()函数
void disp(void) {
cout<<" You are welcome."<<endl;
}
74.
#include<iostream.h>
//函数原型语句
int abs(int x);
long abs(long x);
float abs(float x);
//main()函数的定义
void main(void)
{
//声明变量
int i1=32767,i2=-32767;
long l1=456789,l2=-456789;
float x1=1.1234,x2=-1.1234;
//直接在cout输出中调用函数
cout<<abs(i1)<<","<<abs(i2)<<endl;
cout<<abs(l1)<<","<<abs(l2)<<endl;
cout<<abs(x1)<<","<<abs(x2)<<endl;
}
//定义int型的abs()函数
int abs(int x) {
if (x<0)
return(-x);
else
return(x);
}
//定义long型的abs()函数
long abs(long x) {
if (x<0)
return(-x);
else
return(x);
}
//定义float型 abs函数
float abs(float x) {
if (x<0.0)
return(-x);
else
return(x);
}
75.
#include<iostream.h>
//max()为内联函数
inline int max(int x,int y) //注意inline关键字
{
return x>y?x:y;
}
//定义main()函数
main()
{
int a=3,b=5,c;
c=max(a,b);
cout<<"max("<<a<<","<<b<<")="<<c<<endl;
cout<<"max("<<15<<","<<11<<")="<<max(15,11)<<endl;
}
76.
#include<iostream.h>
main()
{
//函数原型声明
int fact(int x);
int n,sn;
//依次从键盘上输入3个正整型数据计算它们的阶乘
for (int i=1;i<=3;i++)
{
cout<<i<<" n=";
cin>>n;
sn=fact(n);
cout<<n<<"!="<<sn<<endl;
}
}
//以下是采用递归方法定义的fact()函数
int fact(int x)
{
if (x==0) return(1);
return(x*fact(x-1)); //此处又调用了它自身
}
77.
#include<iostream.h>
//带参数的main()函数
int main(int argc,char *argv[])
{
int i;
for(i=0;i<argc;i++)
cout<<i<<":"<<argv[i]<<endl;
return 0;
}
78.
#include<iostream.h>
//用函数原型声明要使用的函数
void show_array1(int*,int);
void show_array2(int a[],int);
void sort(int*,int);
main()
{
//声明数组并初始化
int a[]={2,4,6,1,3,5};
int b[3][3]={{2,4,6},{1,3,5},{0,1,2}};
//显示数组的值
cout<<"show_array1(int*,int):"<<endl;
show_array1(a,6);
show_array1(&b[0][0],3*3);
//用sort1排序并显示
cout<<"sort(int*,int) and show_array1(int*,int): "<<endl;
sort(a,6);
show_array1(a,6);
sort(&b[0][0],3*3);
show_array1(&b[0][0],9);
//显示数组的值
cout<<"show_array2(int a[],int):"<<endl;
show_array2(a,6);
show_array2(&b[0][0],3*3);
}
//显示数组,用指针当参数
void show_array1(int *p,int size) {
for(int i=0;i<size;i++)
cout<<*(p+i)<<" ";
cout<<endl;
}
//显示数组,用数组当参数
void show_array2(int a[],int size) {
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
}
//对数组按从大到小顺序排序
void sort(int *p,int size) {
int t;
for (int i=0;i<size-1;i++)
for (int j=i+1;j<size;j++)
if (*(p+i)<=*(p+j))
{
t=*(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
}
}
79.
#include<iostream.h>
//定义结构
struct student {
char name[10];
float grade;
};
//更改student数据的grade成员,参数形式为引用
void change(student &x,float grade)
{
x.grade=grade;
}
//更改student数据的grade成员,参数形式为指针
void change1(student *p,float grade)
{
p->grade=grade;
}
//更改student类型的数据,普通参数形式
void change2(student x,float grade)
{
x.grade=grade;
}
//显示student类型的数据,参数形式为引用
void show(student &x)
{
cout<<x.name<<" "<<x.grade<<endl;
}
//在main()函数中,测试对结构的处理函数
void main()
{
student a={"ZhangHua",351.5};
//显示a的数据
show(a);
//用change修改分数,并显示
cout<<"change(student &x,float grade):"<<endl;
change(a,360);
show(a);
//用change1修改分数,并显示
cout<<"change1(student *p,float grade):"<<endl;
change1(&a,375);
show(a);
//用change2修改分数,并显示
cout<<"change2(student x,float grade):"<<endl;
change2(a,380.5);
show(a);
}
80.
#include<iostream.h>
//定义函数计算数组的和和平均值
void calculate(int a[],int size,int& sum,float& average)
{
sum=0;
for (int i=0;i<size;i++) {
sum+=a[i];
}
average=sum/size;
}
//定义显示数组的函数
void put_arr(int a[],int size)
{
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
}
main()
{
//声明数组并初始化
int asize,bsize;
int a[]={2,4,6,1,3,5};
int b[]={1,3,5,7,9,11,13,15};
//显示数组的值
asize=sizeof(a)/sizeof(int);
cout<<"put_arr(a,asize):"<<endl;
put_arr(a,asize);
bsize=sizeof(b)/sizeof(int);
cout<<"put_arr(b,bsize):"<<endl;
put_arr(b,bsize);
//计算数组的和和平均值
float a_ave,b_ave;
int a_sum,b_sum;
cout<<"calculate(a,asize,a_sum,a_ave):"<<endl;
calculate(a,asize,a_sum,a_ave);
cout<<"a_sum="<<a_sum;
cout<<" a_ave="<<a_ave<<endl;
cout<<"calculate(b,bsize,b_sum,b_ave):"<<endl;
calculate(b,bsize,b_sum,b_ave);
cout<<"b_sum="<<b_sum;
cout<<" b_ave="<<b_ave<<endl;
}
81.
#include<iostream.h>
//参数为函数指针的函数
int get_result(int a, int b, int (*sub)(int,int))
{
int r;
r=sub(a,b);
return r;
}
//计算最大值
int max(int a, int b)
{
cout<<"In max"<<endl;
return((a > b) ? a: b);
}
//计算最小值
int min(int a, int b)
{
cout<<"In min"<<endl;
return((a < b) ? a: b);
}
//求和
int sum(int a, int b)
{
cout<<"In sum"<<endl;
return(a+b);
}
//测试指向函数的指针
void main(void)
{
int a,b,result;
//测试3次
for (int i=1;i<=3;i++) {
cout<<"Input a and b :";
cin>>a>>b;
cout<<i<<"\tget_result("<<a<<","<<b<<", &max):"<<endl;
result =get_result(a, b, &max);
cout<<"Max of "<<a<<" and "<<b<<" is "<<result<<endl;
result = get_result(a, b, &min);
cout<<"Min of "<<a<<" and "<<b<<" is "<<result<<endl;
result = get_result(a, b, &sum);
cout<<"Sum of "<<a<<" and "<<b<<" is "<<result<<endl;
}
}
82.
#include<iostream.h>
#include<stdio.h>
#define size 3
//定义book结构类型
struct book
{
char title[20];
char author[15];
int pages;
float price;
};
//book结构的输入函数
input_book(book& bk,char *name)
{
cout<<name<<":"<<endl;
cout<<"title:";
cin>>bk.title;
cout<<"author:";
cin>>bk.author;
cout<<"pages:";
cin>>bk.pages;
cout<<"price:";
cin>>bk.price;
}
//book结构的输出函数
output_book(book& bk,char *name)
{
cout<<name<<": ";
cout<<bk.title<<" ";
cout<<bk.author<<" ";
cout<<bk.pages<<" ";
cout<<bk.price<<endl;
}
void main(void)
{
//声明变量和结构数组
int i;
char str[20];
book bk[size];
//输入结构数组
for(i=0;i<size;i++) {
sprintf(str,"bk[%d]",i+1);
input_book(bk[i],str);
}
//显示结构数组
for(i=0;i<size;i++) {
sprintf(str,"bk[%d]",i+1);
output_book(bk[i],str);
}
}
83.
#include<iostream.h>
//声明全局变量并初始化
extern int a[]={1,2,3};
extern float p=3.14;
//在show()函数中使用外部变量
show() {
int i;
cout<<"In show():"<<endl;
cout<<"p="<<p<<endl;
cout<<"a[]: ";
for (i=0;i<=2;i++)
cout<<a[i]<<" ";
cout<<endl;
//cout<<"y="<<y<<endl; 编译出错!
}
//声明外部变量并初始化
int y=5678;
//在main()函数中使用外部变量
main()
{
//声明局部变量
int i,p=100;
//显示重名变量
cout<<"In main():"<<endl;
cout<<"p="<<p<<endl;
//显示全局变量
cout<<"::p="<<::p<<endl;
cout<<"a[]: ";
for (i=0;i<=2;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"y="<<y<<endl; //编译正确!
show(); //调用函数
}
84.
#include <iostream.h>
//使用静态变量的计数器函数
count1()
{
//声明静态变量i,并置初值为0。i在count()中局部可见
static int i=0;
return(++i);
}
//使用局部变量的计数器函数
count2()
{
int i=0;
return(++i);
}
//在main()函数中调用count()函数
main()
{
int i;
//调用count1()10次
cout<<"count1():"<<endl;
for (i=1;i<=12;i++)
cout<<count1()<<" ";
cout<<endl;
//调用count2()10次
cout<<"count2():"<<endl;
for (i=1;i<=12;i++)
cout<<count2()<<" ";
cout<<endl;
}
85.
// p1-851.cpp 为main()函数文件
#include<iostream.h>
main()
{
int i,s=0;
extern int fact(int x);
for (i=2;i<=6;i=i+2)
s+=fact(i);
cout<<"s="<<s<<endl;
}
// p1-852.cpp为计算阶乘函数文件
//定义fact()函数为外部(extern)函数
extern int fact(int x)
{
int i,t=1;
if(x==0) return(1);
for(i=1;i<=x;i++)
t*=i;
return(t);
}
86.
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
main() {
//声明变量
FILE *fp1;
char str[80];
//从键盘上任意输入一个字符串
cout<<"Inupt a string:";
cin.getline(str,80);
//以写入方式打开d.dat文件
if ((fp1=fopen("d.dat","w"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 写"流"文件
fputs(str,fp1);
fputs("\n",fp1);
fclose(fp1); //关闭文件
// 以读方式打开d.dat文件
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 循环从"流"文件读取字符,并显示
char ch;
while ((ch=fgetc(fp1))!=EOF)
cout<<ch;
cout<<endl;
fclose(fp1); //关闭文件
}
87.
#include<iostream.h>
#include <process.h>
#include<stdio.h>
#include<conio.h>
void main(void) {
//变量声明
char ch;
FILE *fp1;
//以写入方式打开d.dat文件
if ((fp1=fopen("d.dat","w"))==NULL) {
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
//循环从键盘上读取字符,写入"流"文件
cout<<"char:"<<endl;
cin>>ch;
while (ch!='*') {
fputc(ch,fp1); //将字符写到fp1指向的"流"文件中
cin>>ch;
}
fclose(fp1); //关闭文件
// 以读方式打开d.dat文件
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 循环从"流"文件读取字符,并显示
while ((ch=fgetc(fp1))!=EOF)
cout<<ch<<" ";
cout<<endl;
fclose(fp1); //关闭文件
}
88.
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
main() {
//声明变量
int i=0;
char p[100]; // 声明输入缓冲区
FILE *fp1; // 声明文件指针变量
//以写入方式打开d.dat文件
if ((fp1=fopen("d.dat","w"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 写文件操作
for (i=1;;i++) { //无条件循环
cout<<i<<" string:";
cin>>p; //从键盘上输入数据
if (stricmp(p,"end")) { //如果输入的字符串为end,则结束循环
fputs(p,fp1); //写入文件操作
fputs("\n",fp1);
}
else
break; //退出循环
}
fclose(fp1); //关闭文件
// 以读方式打开d.dat文件
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 循环从文件读取字符,并显示
while (fgets(p,100,fp1)!=NULL)
cout<<p;
fclose(fp1); //关闭文件
}
89.
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#define MAX 10
main() {
//声明变量
int i,n;
FILE *fp1; // 声明文件指针变量
//以写入方式打开d.dat文件
if ((fp1=fopen("d.dat","w"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 写文件操作
for (i=1;i<=MAX;i++) {
n=rand(); //产生1个整数随机数
putw(n,fp1);
cout<<n<<" ";
}
cout<<endl<<"--------------------"<<endl;
fclose(fp1); //关闭文件
// 以读方式打开d.dat文件
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 循环从"流"文件读取字符,并显示
while ((n=getw(fp1))!=EOF)
cout<<n<<" ";
fclose(fp1); //关闭文件
}
90.
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#define MAX 3
main() {
//定义结构类型
struct student {
int num;
char name[10];
float grade;
};
//声明数组和变量
student st[3];
int i;
FILE *fp1; // 声明文件指针变量
//以写入方式打开d.dat文件
if ((fp1=fopen("d.dat","w"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
//从键盘上读数据,写入文件
cout<<" num name grade"<<endl;
for (i=0;i<MAX;i++) {
cout<<i+1<<" ";
cin>>st[i].num;
cin>>st[i].name;
cin>>st[i].grade;
fprintf(fp1,"%d %s %f\n",st[i].num,st[i].name,st[i].grade);
}
fclose(fp1); //关闭文件
// 以读方式打开d.dat文件
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 循环从"流"文件读取字符,并显示
student t;
while ((fscanf(fp1, "%d %s %f",&t.num,t.name,&t.grade))!=EOF) {
cout<<t.num<<" ";
cout<<t.name<<" ";
cout<<t.grade<<endl;
}
fclose(fp1); //关闭文件
}
91.
#include<iostream.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
FILE *fpd,*fpw; // 声明FILE结构指针变量
unsigned char dw;
int i=0;
//以二进制读方式打开Calc.exe文件
if((fpd=fopen("C:\WINDOWS\Calc.exe", "rb"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 以二进制写方式打开test.exe文件
if((fpw=fopen("test.exe", "wb+"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
// 二进制文件读写操作,每次指定读写1个字节
while(!feof(fpd)) { //使用feof()判断文件尾
fread(&dw, 1, 1, fpd);
fwrite(&dw, 1, 1, fpw);
}
// 关闭文件
fclose(fpd);
fclose(fpw);
//执行Calc.exe和Calc.exe文件
cout<<"1 Run C:\WINDOWS\Calc.exe"<<endl;
system("C:\WINDOWS\Calc.exe");
cout<<"-------------------"<<endl;
cout<<"2 Run test.exe!"<<endl;
system("test.exe");
}
92.
#include<iostream.h>
#include <process.h>
#include<stdio.h>
#include<conio.h>
void main(void) {
//声明变量
int i;
char ch;
FILE *fp1;
//以写入方式打开d.dat文件
if ((fp1=fopen("d.dat","w"))==NULL) {
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
//循环从键盘上读取字符,写入文件
cout<<"char:";
cin>>ch;
while (ch!='*') {
fputc(ch,fp1); //将字符写到fp1指向的"流"文件中
cin>>ch;
}
cout<<"--------------------"<<endl;
fclose(fp1); //关闭文件
//以读方式打开d.dat文件
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
//循环从文件读取字符,并显示
while ((ch=fgetc(fp1))!=EOF)
cout<<ch;
cout<<endl<<"--------------------"<<endl;
//以下按倒序方式读取文件中的字符,并显示
for (i=-1;;i--) {
fseek(fp1,i,2); //设置文件指针,偏移量为i,相对文件尾
if ((ch=fgetc(fp1))!=EOF)
cout<<ch;
else
break;
}
cout<<endl<<"--------------------"<<endl;
//以下读取"流"文件中偶数位置上的字符,并打印
long position;
for (i=0;;i=i+2) {
fseek(fp1,i,0); //设置文件指针,偏移量为i,相对文件头
position=ftell(fp1);
if ((ch=fgetc(fp1))==EOF) //遇到文件尾,则退出,否则打印读取的字符
break;
else {
cout<<position<<" :"<<ch<<endl;
}
}
cout<<endl;
fclose(fp1); //关闭文件
}
93.
#include<iostream.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5
//显示数组的数据
void show_array(double x[],int size) {
for(int i=0;i<size;i++)
cout<<x[i]<<" ";
cout<<endl;
}
//main函数测试数组数据的文件读写
int main(void)
{
//声明变量
FILE *fp; // 声明FILE结构指针变量
int i;
double a[MAX]={1.0,1.2,1.4,1.6,1.8};
//显示数组a的数据
cout<<"a:";
show_array(a,MAX);
//打开d.dat文件
if ((fp=fopen("d.dat","wb+"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
//以单个元素对数组进行文件读操作
for(i=0;i<MAX;i++) {
fwrite(&a[i], sizeof(double), 1, fp);
}
rewind(fp); //恢复读写指针的位置
//以单个元素对数组进行文件读操作
double b[MAX];
for(i=0;i<MAX;i++) {
if (!feof(fp)) //使用feof()判断文件尾
fread(&b[i], sizeof(double), 1, fp);
else
break;
}
cout<<"b:";
show_array(b,MAX);//显示数组b的数据
fclose(fp); // 关闭文件
//打开d1.dat文件
if ((fp=fopen("d1.dat","wb+"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
//将数组当成数据块写入文件
fwrite(&a, sizeof(double), MAX, fp);
rewind(fp); //恢复读写指针的位置
//将数组当成数据块从文件中读取
double c[MAX];
if (!feof(fp)) //使用feof()判断文件尾
fread(&c, sizeof(double),MAX,fp);
cout<<"c:";
show_array(c,MAX); //显示数组c的数据
fclose(fp); // 关闭文件
}
94.
#include<iostream.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX 5
//定义结构类型
struct student {
int num;
char name[20];
float grade;
};
//显示student结构数据
void show_str(student a,char *name) {
cout<<name<<":"<<endl;
cout<<a.num<<" "<<a.name<<" "<<a.grade;
cout<<endl;
}
//main函数测试结构数据的文件读写
int main(void)
{
//声明变量
FILE *fp;
//声明FILE结构指针变量
student st={1001,"ZhangBin",85.5};
//显示st结构数据
show_str(st,"st");
//打开d.dat文件
if ((fp=fopen("d.dat","wb+"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
//用fprintf()函数写结构数据到文件
fprintf(fp,"%d %s %f",st.num,st.name,st.grade);
rewind(fp); //恢复读写指针的位置
//用fscanf()函数读文件中的数据赋值给结构并显示
student temp;
fscanf(fp, "%d %s %f",&temp.num,temp.name,&temp.grade);
show_str(temp,"temp");
cout<<"-----------------------"<<endl;
fclose(fp); // 关闭文件
//将结构数据当成数据块进行读写
if ((fp=fopen("d1.dat","wb+"))==NULL) //打开d1.dat文件
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1); //结束程序执行
}
//声明结构数组并初始化
int i;
student starr[3]={{101,"WangPing",92},{102,"Li",85},{103,"LiuMin",97}};
//显示结构数组
for(i=0;i<3;i++)
show_str(starr[i],"starr");
//将结构数组当成数据块写入文件
fwrite(starr, sizeof(student), 3, fp);
rewind(fp); //恢复读写指针的位置
//按数据块从文件中读取数据赋值给结构数组
student temp_arr[3];
if (!feof(fp)) //使用feof()判断文件尾
fread(temp_arr, sizeof(student),3,fp);
for(i=0;i<3;i++)
show_str(temp_arr[i],"temp_arr");
fclose(fp); // 关闭文件
}
95.
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
int main(void)
{
//声明变量
char ch;
char str[20];
int n;
float x;
//用stdin从键盘上输入数据
fprintf(stdout,"ch str\n");
fscanf(stdin,"%c %s",&ch,str);
fprintf(stdout,"n x \n");
fscanf(stdin,"%d %f",&n,&x);
cout<<"----------------"<<endl;
//输出显示
fprintf(stdout,"ch=%c str=%s",ch,str);
fprintf(stdout,"\nn=%d x=%f",n,x);
cout<<endl;
}
96.
#include <stdio.h>
void main( void )
{
int c;
/* Create an error by writing to standard input. */
putc( 'A', stdin );
if( ferror( stdin ) )
{
perror( "Write error" );
clearerr( stdin );
}
/* See if read causes an error. */
printf( "Will input cause an error? " );
c = getc( stdin );
if( ferror( stdin ) )
{
perror( "Read error" );
clearerr( stdin );
}
}
97.
#include<iostream.h>
#include<math.h> //此预处理指令不可少
const double HD=3.1415926/180;
main() {
cout<<"x\tsin(x)"<<endl;
for (int i=0;i<=180;i=i+30)
cout<<i<<"\t"<<sin(i*HD)<<endl;
}
98.
#include<iostream.h>
//以下是几个简单宏替换预处理指令
#define YES 1
#define PI 3.1415926
#define RAD PI/180
#define MESG "This is a string."
//以下是主程序
main() {
//以下各语句使用了宏替换
cout<<"YES="<<YES<<endl;
if (YES)
cout<<"PI="<<PI<<endl;
cout<<"RAD="<<RAD<<endl;
cout<<MESG<<endl;
}
99.
#include<iostream.h>
//以下为带参数宏替换的预处理指令
#define PRINT(k) cout<<(k)<<endl;
#define MAX(a,b) ((a)>(b) ? (a):(b))
main()
{
int i=3,j=2;
//MAX(a,b)宏替换的使用
cout<<"MAX(10,12)="<<MAX(10,12)<<endl;
cout<<"MAX(i,j)="<<MAX(i,j)<<endl;
cout<<"MAX(2*i,j+3)="<<MAX(2*i,j+3)<<endl;
//PRINT(k)宏替换的使用
PRINT(5);
PRINT(MAX(7,i*j));
}
100.
#include<iostream.h>
#define PI 3.1416
main() {
int i=100;
#if 1
cout<<"i="<<i<<endl;