计算机程序设计复习大纲Word下载.docx
- 文档编号:472343
- 上传时间:2023-04-29
- 格式:DOCX
- 页数:37
- 大小:24.43KB
计算机程序设计复习大纲Word下载.docx
《计算机程序设计复习大纲Word下载.docx》由会员分享,可在线阅读,更多相关《计算机程序设计复习大纲Word下载.docx(37页珍藏版)》请在冰点文库上搜索。
Pleaseinputtwonumbers:
"
;
cin>
a>
b;
c=a+b;
cout<
a<
+"
<
b<
="
c<
endl;
}
二、基本数据类型、变量、运算符、表达式
1.基本数据类型
short16位整数
int32位整数
float32位浮点数
double64位浮点数
char8位字符
bool布尔
2.变量定义
intx;
intx=3;
doubled=3.41;
charc='
a'
boolb=true;
boolb=false;
3.转义字符
'
\n'
换行,'
\r'
回车,'
\t'
制表,'
\\'
反斜线,'
\'
'
单引号,'
\"
双引号,
,'
\0'
字符串结尾符
4.算术运算符
+、-、*、/、%、++、--
5.关系运算符
>
、>
=、==、<
=、<
、!
=
6.逻辑运算符
!
、&
&
、||
7.数据类型修饰符
signed、unsigned
signedx;
unsignedintx;
unsignedx;
8.数据类型定义
typedef
typedeffloatreal;
9.常量修饰符
const
constdoubledelta=0.00007;
10.二进制位运算符
&
、|、^、~、<
11.典型例子
例2-4温度转换:
输入一个华氏温度,计算并输出对应的摄氏温度值。
//Example2-4:
温度转换
doublec,f;
请输入一个华氏温度:
f;
c=5.0/9.0*(f-32);
对应于华氏温度"
f<
的摄氏温度为"
c<
三、控制结构
1.顺序结构
2.单分支结构
if
y=-1;
if(x>
0)
y=1;
if-else
}else
if-elseif-else
}elseif(y<
}else
y=0;
4.多分支结构
switch-case-default
switch(x)
case0:
//...
break;
case1:
default:
5.while循环结构
while(条件)
//代码
6.do-while循环结构
do
}while(条件);
7.for循环结构
for(初值;
条件;
步长)
8.break、continue结构
9.异常处理
try
test;
if(test!
=0)
throwtest;
//抛出异常
else
throw"
itisazero"
}catch(inti)
{cout<
Exceptoccurred:
i<
catch(constchar*s)
s<
...
}catch(...)
}catch(Exceptione)
10.典型例子
例3-1编程实现分段函数
//Example3-1:
分段函数
intmain()
{
doublex,y;
Pleaseinputx="
x;
if(x<
y=x+1;
cout<
x="
x<
y=x+1="
y<
elseif(x<
1)//0≤x<
1
y=1;
y="
else//1≤x
y=x*x*x;
y=x*x*x="
例3-3计算自然对数的底数e,当通项<
10的-7时停止计算。
//Example3-3:
计算常数e的值
doublee=1.0;
doubleu=1.0;
intn=1;
while(u>
=1.0E-7)
u=u/n;
e=e+u;
n=n+1;
e="
e<
(n="
n<
)"
四、数组与字符串
1.一维数组
定义格式:
inta[10];
inta[10]={1,2,3,4,5,6,7,8,9,10};
inta[]={1,2,3,4,5,6,7,8,9,10};
inta[10]={0};
使用格式:
a[i]=9;
a[i];
2.二维数组
inta[2][5];
inta[2][5]={{1,2,3,4,5},{6,7,8,9,10}};
inta[2][5]={1,2,3,4,5,6,7,8,9,10};
inta[][5]={1,2,3,4,5,6,7,8,9,10};
inta[2][10]={0};
a[i][j]=9;
a[i][j];
3.字符串
chara[10];
chara[10]={'
'
b'
c'
d'
e'
f'
g'
h'
i'
j'
};
chara[]={'
chara[10]="
abcdefghij"
chara[]="
\0"
a[i]='
z'
strcpy(a,"
xyz"
);
a;
4.典型例子
例4-1给一维数组输入7个整数,找出该数组中的最大数。
//Example4-1:
求数组中的最大元素
inta[7];
Pleaseinputanarraywithsevenelements:
//输入每个数组元素的值
for(inti=0;
i<
7;
i++)
intbig=a[0];
for(intj=0;
j<
j++)//遍历整个数组
if(a[j]>
big)
big=a[j];
max="
big<
return0;
例4-6编写一个用于对整型数组进行排序的程序,排序方法使用简单的交换排序法。
//Example4-6:
交换排序
constintCOUNT=16;
intlist[]=
{
503,87,512,61,908,170,897,275,
653,426,154,509,612,677,765,703
};
for(inti=0;
COUNT;
for(intj=COUNT-1;
j>
i;
j--)
if(list[j-1]>
list[j])
{
inttmp=list[j-1];
list[j-1]=list[j];
list[j]=tmp;
}
cout<
Theresultis:
//输出排序后的数组
for(intk=0;
k<
16;
k++)
cout<
list[k]<
return0;
例4-9字符串连接。
//Example4-9:
连接两个字符串
chardestination[81]="
abcdefghijklmnopqrstuvwxyz"
charsource[]="
ABCDEFGHIJKLMNOPQRSTUVWXYZ"
inti=strlen(destination);
intj=0;
while(source[j]!
destination[i++]=source[j++];
destination[i]=0;
Theresultis:
destination<
五、指针、地址与引用
1.地址与引用
int&
y=x;
y=5;
x=5;
2.地址与指针
int*p=&
int*p=newint();
int*p=newint(3);
int*p=newint[10];
*p=5;
p++;
p[3]=6;
3.指针的运算
+、-、++、--、*、[]
4.举例:
(1)int*ptr;
//指针所指向的类型是int
(2)char*ptr;
//指针所指向的的类型是char
(3)int**ptr;
//指针所指向的的类型是int*
(4)int(*ptr)();
//指针所指向的的类型是intf()
(5)int(*ptr)[3];
//指针所指向的的类型是int()[3]
(6)int*(*ptr)[4];
//指针所指向的的类型是int*()[4]
5.典型例子
例6-6编写一个字符串比较函数,仅比较两个字符串的前面若干个字符,且在比较时不区分大小写字母。
//Example6-6:
不区分大小写字母的部分字符串比较
intmystrnicmp(char*str1,char*str2,intn)
while(toupper(*str1)==toupper(*str2)&
*str1!
=0&
*str2!
n>
str1++;
str2++;
n--;
returntoupper(*str1)-toupper(*str2);
例6-8使用指针编写一个用于对整型序列进行排序的函数,排序方法使用简单选择排序法。
//Example6-8:
简单选择排序
//函数selectsort():
voidselectsort(int*list,intcount)
for(inti=0;
count-1;
intk=i;
for(intj=i+1;
count;
j++)
if(*(list+j)<
*(list+k))k=j;
if(k!
=i)
inttmp=*(list+i);
*(list+i)=*(list+k);
*(list+k)=tmp;
}
intarray[6]={2,7,2,2,3,1};
selectsort(array,6);
6;
i++)cout<
array[i]<
六、函数、数组与函数、引用与函数、指针与函数、函数指针
1.函数
定义格式:
intadd1(inta,intb){
returna+b;
intadd2(&
inta,int&
b){
intadd3(int*a,int*b){
return*a+*b;
intadd4(intcount,inta[]){
intc=0;
count;
i++){
c=c+a;
}
returnc;
调用格式:
inta=3;
intb=5;
intc=add1(a,b);
c=add2(a,b);
c=add3(&
a,&
b);
iny(*ptr)(int,int)=add1;
c=ptr(a,b);
intd[]={1,2,3,4};
c=add4(4,d);
2.典型例子
例5-5编写一个用于对整型数组进行排序的函数,排序方法使用例4-6的简单交换排序法。
//Example5-5:
交换排序
//函数bubble_up():
冒泡法排序
voidbubble_up(intlist[],intcount)
i=i+1)
for(intj=count-1;
j=j-1)
if(list[j-1]>
{
inttmp=list[j-1];
list[j-1]=list[j];
list[j]=tmp;
}
//测试冒泡法排序的主程序
inti;
intarray[16]=
503,87,512,61,908,170,897,275,
653,426,154,509,612,677,765,703
};
原数组是:
for(i=0;
bubble_up(array,5);
//函数调用
对数组前5项进行排序后的结果是:
bubble_up(array,16);
对整个数组排序后的结果是:
例7-1采用递归算法求n!
//Example7-1:
用递归方法求n!
//函数fac():
求阶乘的递归函数
intfac(intn)
if(n<
0)//不能求负数的阶乘
return?
1;
elseif(n==0)//0的阶乘为1
return1;
else
returnn*fac(n-1);
//n!
为(n-1)!
乘以n
例7-11编写一个用于在字符串中查找某字符的函数。
//Example7-11:
函数strchr():
在字符串中查找指定字符
char*strchr(char*string,intc)
while(*string!
=c&
*string!
string++;
if(*string==c)//查找成功
returnstring;
else//string==0(字符串结束符),字符串中没有c
returnNULL;
七、结构体、枚举体、共同体
1.结构体
structDate{intyear;
intmonth;
intday};
Datedate;
date.year=2012;
2.枚举体
enumColor{RED,GREEN,BLUE};
Colorcolor;
color=RED;
3.共同体
unionPosition{intgrand;
chartitle[20]};
Positionpos;
pos.grand=3;
strcpy(pos.title,"
Engineer"
4.典型例子
例8-7词频统计:
输入一系列英文单词,单词之间用空格隔开,用“xyz”表示结束输入,统计输入过哪些单词以及各单词出现的次数,统计时区分大小写字母,最后按单词的字典顺序输出单词和出现次数的对照表。
//Example8-7:
词频统计
//词条类型
structWordList
charword[50];
intfreq;
//词典排序函数
voidSort(WordListlist[],intcount)
if(strcmp(list[j-1].word,list[j].word)>
WordListtmp;
tmp=list[j-1];
list[j-1]=list[j];
list[j]=tmp;
//主函数:
进行词频统计
WordListlist[5000];
inti,num=0;
chartemp[50];
请输入一系列英语单词,以xyz表示输入结束"
temp;
while(strcmp(temp,"
)!
=0)
for(i=0;
num;
i++)//扫描当前词典
if(strcmp(list[i].word,temp)==0)//若词典中存在该词条,词频加1
list[i].freq++;
break;
if(i>
=num)//若词典中无该词条,添加该词
strcpy(list[i].word,temp);
list[i].freq=1;
num++;
cin>
//继续输入单词
Sort(list,num);
//对词典进行排序
//输出词典
词频统计结果如下:
list[i].word<
\t"
list[i].freq<
八、类、继承与对象
1.类:
classA:
基类{
权限修饰符:
成员变量定义
构造函数定义
virtual虚函数定义
virtual纯虚函数定义=0;
一般成员函数定义
析构函数定义
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 程序设计 复习 大纲