资源描述
第2章 • C++程序设计基础
第2章 C++程序设计基础
【例2-1】 简单C++程序。
//examplech201.cpp
#include <iostream.h> //编译预处理命令
void main() //main函数
{
char name[30]; //定义字符数组
cout<<"Welcome to C++!"; //输出问候语
cout<<"please input your name:";
cin>>name; //从键盘输入名字
cout<<"Hello,"<<name<<"!"<<endl;
cout<<"You Are Welcome!"<<endl;
}
【例2-2】 转义字符的使用。
//examplech202.cpp
#include<iostream.h>
void main()
{
cout<<"This is C++ program"<<endl;
cout<<"This\tis\tC++\tprogram"<<endl;
cout<<"This\nis\nC++\nprogram"<<endl;
}
【例2-3】 变量的定义与使用。
//examplech203.cpp
main()
{
int a,b,c,x;
unsigned int u;
float u1;
double u2;
a=12;
b=-18;
u1=51.8;
u2=58.8;
c=28;u=b+c;
x=a+b;
u2=u1+u2;
cout<<”x=”<<x<<endl;
cout<<”u=”<<u<<endl;
cout<<”u2=”<<u2<<endl;
}
【例2-4】 字符型变量的应用。
//examplech204.cpp
include<iostream.h>
void main()
{
int a1,a2;
char c1,c2,c3,c4;
a1=97;
a2=65;
c1=a1;
c2=a2;
c3='B';
c4=66;
cout<<"a1="<<a1<<endl;
cout<<"a2="<<a2<<endl;
cout<<"C1="<<c1<<endl;
cout<<"C2="<<c2<<endl;
cout<<"C3="<<c3<<endl;
cout<<"C4="<<c4<<endl;
}
【例2-5】 通过指针与引用等方式访问变量。
//examplech205.cpp
#include<iostream.h>
void main()
{
double n=1.8;
double&n1=n;
n1=1.18;
cout<<"n="<<n<<","<<"n1="<<n1<<endl;
double &n11=n1; //引用的传递效应
n1=1.118;
cout<<"n="<<n<<","<<"n1="<<n1//通过变量名及其引用分别访问
<<","<<"n11="<<n11<<endl;
double* ptr=&n;
double* &p=ptr;
*p=1.218;
cout<<"n="<<n<<","<<"n1="<<n1
<<","<<"n11="<<n11<<","<<"*ptr="<<*ptr
<<","<<"*p="<<*p<<endl; //通过指针访问
cout<<"&n="<<&n<<endl;
cout<<"*&n="<<*&n<<endl; //通过地址访问
}
【例2-6】 自增自减运算符的应用。
//examplech206.cpp
#include<iostream.h>
void main()
{
int x,y;
x=17;
y=17;
cout<<"x++="<<x++<<endl;
cout<<"x="<<x<<endl;
cout<<"x--="<<x--<<endl;
cout<<"++y="<<++y<<endl;
cout<<"--y="<<--y<<endl;
}
【例2-7】 自增自减运算符的应用。
//examplech207.cpp
#include<iostream.h>
void main()
{
double x;
int n;
x=158.158;
n=int(x);
cout<<"n="<<n<<endl;
cout<<"x="<<x<<endl;
}
【例2-8】 简单if语句的使用:判断某一年份是否闰年(经典的选择结构程序)。
//examplech208.cpp
#include <iostream.h>
void main(void)
{
int year;
cout<<"请输入年份: ";
cin>>year;
if ((year%4==0&&year%100!=0)||(year%400==0))
cout<<year<<"年是闰年!"<<endl;
else
cout<<year<<"年不是闰年!"<<endl;
}
【例2-9】 条件运算符实现双分支选择结构举例。
//examplech209.cpp
#include <iostream.h>
void main()
{
char a;
cout<<"Please Input a Char: ";
cin>>a;
a=(a>='a'&&a<='z')?(a-32):a;
cout<<a<<endl;
}
【例2-10】 用多分支语句实现对学生成绩的评价。
//examplech210.cpp
#include<iostream.h>
void main()
{
int x;
cout<<"请输入分数:";
cin>>x;
if(x>100||x<0)
cout<<"数据错误!"<<endl;
else if(x==100)
cout<<"满分,很优秀!"<<endl;
else if(100>x&&x>=90)
cout<<"成绩优秀!"<<endl;
else if(90>x&&x>=80)
cout<<"成绩良好!"<<endl;
else if(80>x&&x>=70)
cout<<"成绩中等!"<<endl;
else if(70>x&&x>=60)
cout<<"成绩及格!"<<endl;
else
cout<<"不及格,继续努力!"<<endl;
}
【例2-11】 采用嵌套if语句实现符号函数的求值。
//examplech211.cpp
#include<iostream.h>
void main()
{
int x,y;
cout<<"Please Input X:";
cin>>x;
if(x>=0)
if(x>0) y=1;
else y=0;
else y=-1;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
【例2-12】 用switch语句实现对学生成绩的分级。
//examplech212.cpp
#include<iostream.h>
void main()
{
int x;
int n;
cout<<"请输入分数:";
cin>>x;
if(x>100||x<0)
cout<<"数据错误!"<<endl;
n=x/10;
switch(n)
{
case 10:
cout<<"满分,很优秀!"<<endl;break;
case 9:
cout<<"成绩优秀!"<<endl;break;
case 8:
cout<<"成绩良好!"<<endl;break;
case 7:
cout<<"成绩中等!"<<endl;break;
case 6:
cout<<"成绩及格!"<<endl;break;
default:
cout<<"不及格,继续努力!"<<endl;
}
}
【例2-13】 用while循环语句实现求任意数以内的自然数之和。
//examplech213.cpp
#include<iostream.h>
void main()
{
int i,n,sum;
i=1;
sum=0;
cout<<"Input the n:"<<endl;
cin>>n;
while(i<=n)
{
sum+=i;
i++;
}
cout<<"sum="<<sum<<endl;
}
【例2-14】 用while循环语句实现求任意数以内的自然数之和。
//examplech214.cpp
#include<iostream.h>
void main()
{
int i,n,sum;
i=1;
sum=0;
cout<<"Input the n:"<<endl;
cin>>n;
do
{
sum+=i;
i++;
}
while(i<=n);
cout<<"sum="<<sum<<endl;
}
【例2-15】 用for循环语句解决Fibonacci数列,这是一个古老而又经典的数学问题,Fibonacci数列具有以下特征:
a1=1
a2=1
an=an-1+an-2 n>=3
求该数列的前30项。
//examplech215.cpp
#include<iostream.h>
#include<iomanip.h>
void main()
{
int i,a1,a2;
a1=1;
a2=1;
for(i=1;i<=15;i++)
{
cout<<setw(10)<<a1;
cout<<setw(10)<<a2;
if(i%2==0)cout<<endl;
a1=a1+a2;
a2=a1+a2;
}
}
【例2-16】 循环的终止与退出的应用。
//examplech216.cpp
#include<iostream.h>
void main()
{
int n1,n2;
n2=0;
for (n1=1;;n1++)
{
if(n1%578!=0)
continue;
n2++;
if(n1>3000)break;
cout<<n1<<endl;
}
}
【例2-17】 结构体变量的应用。
//examplech217.cpp
#include<iostream.h>
#include<iomanip.h>
struct student
{
char id[10];
char name[20];
char sex;
int age;
char department[30];
float score;
};
void main()
{
student student1={"05030100","WangWei",'M',20,"Computer",95},
student2={"05030101","LiMing",'M',21,"Physics",98};
float average;
cout<<student1.id<<" "<<student1.name<<" "<<student1.sex<<" "
<<student1.age<<" "<<student1.department<<" "
<<student1.score<<endl;
cout<<student2.id<<" "<<student2.name<<" "<<student2.sex<<" "
<<student2.age<<" "<<student2.department<<" "
<<student2.score<<endl;
average=(student1.score+student2.score)/2;
cout<<"average="<<average<<endl;
cout<<"sizeofstudent1="<<sizeof(student1)<<endl;
}
【例2-18】 联合体的应用。
//examplech218.cpp
#include<iostream.h>
#include<iomanip.h>
struct example
{
char id[10];
char name[20];
char sex;
int age;
union
{
float salary;
float score;
};
}teacher,student;
void main()
{
example teacher={"000788","WangWei",'M',30,5000};
example student={"000518","ZhangYu",'F',20,95};
cout<<teacher.id<<" "<<teacher.name<<" "<<teacher.sex
<<" "<<teacher.age<<" "<<teacher.salary<<endl;
cout<<student.id<<" "<<student.name<<" "<<student.sex
<<" "<<student.age<<" "<<student.score<<endl;
cout<<"sizeofstudent="<<sizeof(student)<<endl;
cout<<"sizeofteacher="<<sizeof(teacher)<<endl;
}
【例2-19】 枚举类型的应用:一口袋内有五个不同颜色的球,颜色分别是red、yellow、blue、white、black五种颜色,每次从袋内取出3个球,然后放回袋中,每次能取出三种不同颜色的球的情况总共有多少种?
//examplech219.cpp
#include<iostream.h>
#include<iomanip.h>
void main()
{
enum color{red,yellow,blue,white,black};
enum color ball;
int n,loop,i,j,k;
n=0; //总次数赋初值0
for(i=red;i<=black;i++)
for(j=red;j<=black;j++)
if(i!=j) //前两个球颜色不同
{
for(k=red;k<=black;k++)
if((k!=i)&&(k!=j)) //第三个球颜色不同于前两个
{
n=n+1; //总次数增1
cout<<setw(5);
cout<<n;
for(loop=1;loop<=3;loop++)
{
switch(loop)
{
case 1: ball=(enum color)i; break;
case 2: ball=(enum color)j; break;
case 3: ball=(enum color)k; break;
default: break;
}
switch(ball)
{
case red:cout<<setw(10)<<"red";
break;
case yellow:cout<<setw(10)<<"yellow";
break;
case blue:cout<<setw(10)<<"blue";
break;
case white:cout<<setw(10)<<"white";
break;
case black:cout<<setw(10)<<"black";
break;
default: break;
}
}
cout<<endl;
}
}
cout<<"total="<<n<<endl;
}
【例2-20】 typedef的应用。
//examplech220.cpp
#include<iostream.h>
typedef struct
{
int month;
int day;
int year;
}date;
void main()
{
date birthday={12,18,2004};
cout<<”Your Birthday is:”;
cout<<birthday.month<<”-”<<birthday.day<<” ”-
<<birthday.year<<”-”<<endl;
}
10
展开阅读全文