}
intcheck1(inti,intn)//与前面的数不相同
{
intj;
for(j=1;j<=n-1;j++)
{
if(a[j]==i)
return0;
}
return1;
}
intcheck2(intn)//素数检查
{
intt=sqrt(n);
intj;
for(j=2;j<=t;j++)
{
if(n%j==0)
return0;
}
return1;
}
intcheck3(inti,intn)//检查相邻数字之和是否为素数
{
if(n<10)
returncheck2(i+a[n-1]);
else
{
intt=check2(i+a[n-1])+check2(i+a[1]);
if(t==2)
return1;
else
return0;
}
}
voidfind(intn)
{
inti;
for(i=2;i<=10;i++)
{
if(check1(i,n)==1&&check3(i,n)==1)
{
a[n]=i;
if(n==10)
output();
else
{
find(n+1);
a[n]=0;
}
}
}
}
intmain()
{
inti;
for(i=1;i<=10;i++)
{
a[i]=0;
}
a[1]=1;
find
(2);
return0;
}
第三章:
一、简答题
1、以变量为例,阐述数据类型的意义。
答:
1、不同类型数据,操作不同。
如intx=1.5,操作的结果是x的值为1,
floatx=1.5,操作结果是x的值1.5。
2、不同类型数据决定了操作符。
如果操作数的类型与运算符的要求不相符,则作用在操作数上的操作将可能被视为非法操作。
类型对操作数的安全提供了保障。
2、什么是变量三要素?
名字、类型、值、地址
二、填空
1、C
3、B
9编辑并运行程序,学习每个运算符的语义
同书本P68
第四章:
二、填空
1、D3、D4、A10、C12、A23、D
三、分析题
1、分析下面程序的运行结果
#include
usingnamespacestd;
voidmain()
{
for(inti=1;i<=9;i++)
{
for(intj=1;j<=10-i;j++)
cout<<"";
for(intj=1;j<=i;j++)
cout<
cout<}
}
运行有问题
修改后
#include
usingnamespacestd;
voidmain()
{
inti,j;
for(i=1;i<=9;i++)//1
{//22
for(j=1;j<=10-i;j++)//333
cout<<"";//4444
for(j=1;j<=i;j++)//55555
cout<
cout<}//88888888
}//输出结果为:
999999999
四、编程题
1、求解二元一次方程ax^2+bx+c=0的根。
注意,应考虑各种可能性及程序的健壮性。
#include"iostream"
#include"math.h"
usingnamespacestd;
voidmain()
{
inta=0,b=0,c=0;
cout<<"Pleaseentera=";
cin>>a;
cout<<"Pleaseenterb=";
cin>>b;
cout<<"Pleaseenterc=";
cin>>c;
if(a==0)
{
doublex0;
x0=(-c)/b*1.0;有问题?
?
cout<<"此时为一元一次方程有唯一解"<return;
}
doubletemp=b*b-4.0*a*c;
if(temp<0)
{
cout<<"二元一次方程无解"<return;
}
else
{
doublex1=0,x2=0;
temp=sqrt(temp);//求temp的平方的根,然后在赋值给temp
x1=-(b+temp)/(2.0*a);
x2=(-b+temp)/(2.0*a);
cout<<"二元一次方程的解为x1="<cout<<"二元一次方程的解为x2="<}
}
5、编写输出如图所示的图案
*
**
********
********
********
**
*
#include
usingnamespacestd;
#defineM7
#defineN9
intmain()
{
inta[M][N]={
{0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,1,1,0},
{1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,1,1,0},
{0,0,0,0,0,0,1,0,0}
};
for(inti=0;i<7;i++)
{
for(intj=0;j<9;j++)
{
if(a[i][j]==0)
cout<<"";
else
cout<<"*";
}
cout<}
return0;
}
第五章:
一、问答题
1、什么是函数重载?
如何进行函数重载?
答:
函数重载指同一函数名可以对应多个函数的实现。
eg:
函数名add(),功能是求和即两个操作的和。
其中,一个函数实现两个整型数的和,另一个函数实现两个浮点型数的和,再一个函数实现两个复数的和。
每种函数实现对应一个函数体,函数名字相同,但函数的参数类型不同。
这就是函数的重载。
函数重载在类和对象的应用中尤为重要。
函数重载要求编译器能唯一确定调用一个函数时应执行哪个函数代码,即采取哪个函数实现。
确定函数的实现时,要从函数参数的个数和类型上来区分。
这就是说,进行函数重载时,要求同命名的函数的参数个数或参数类型不同;否则,无法实现重载。
举例:
#include
intadd(int,int);
doubleadd(double,double);
voidmain()
{
cout<cout<}
intadd(intx,inty)
{
returnx+y;
}
doubleadd(doublea,doubleb)
{
returna+b;
}
4、下列两个函数是否为重载?
voidfunc(inta,intb);
intfunc(inta,intb);
答:
不是重载。
因为它们参数个数和类型都相同,只有返回值不同,返回值不足以区分两个函数。
7、什么是函数的类型和值?
是否所有函数都有返回值?
是否所有函数都有类型?
函数类型是指函数返回值的数据类型,函数的返回值可以是指针、值或者引用。
并不是所有的函数都有返回值,有的函数可以没有返回值,函数无返回值,要根据函数实现的功能来决定。
如:
函数add()
intadd(inta,intb)
{
returna+b;
}
用来实现两个整数的求和,那么就需要返回值来返回求和结果,而print()只向屏幕上输出一些信息,就不需返回值,所以返回类型为void。
voidprint(char*str)
{
cout<}
所有的函数都有返回类型,如果没有显示指出函数的返回值类型,默认返回类型为int型
二、选择题
2、B8、B
三、分析题
4、#include
voidswap_values(inta,intb)
{
inttemp;
temp=a;a=b;b=temp;
}
voidmain(void)
{
intbig=2002,small=0;
cout<<"valuesbeforefunction"<
swap_values(big,small);
cout<<"valuesafterfunction"<
}
分析:
输出结果:
Valuesbeforefunction2002and0
Valuesafterfunction2002and0
在函数调用中,可以通过值传递方式在函数间传递数据。
但是,这种传递无法改变实参的值。
只是因为在进入被调用函数时,变量进行了复制。
被调用函数的任何更改和实参没有任何联系。
所以,在主程序中打印它的局部变量时,变量值没有任何改变。
四、编程题
2、编写一个函数,在一个字符串数组中查找"Hello",如果找回返回指向“Hello”开始出的指针;否则抛出一个异常“NotFound”.
#include
#include
#include
char*findstring(char*a,char*dest)
{
inti,m,n;
m=strlen(dest);
n=strlen(a)-m;
try//用于抛出错误
{
for(i=0;i<=n;i++)
{
intk=i;//用于记录当前i的位置,便于追溯
for(intj=0;j<=m-1;j++)
{
if(a[k]==dest[j])
k++;//如果当前字符相等进行下一个字符比较
else
break;
if(j==m-1)
return&a[i];
}
}
throw"Notfound!
";
}
catch(char*str)
{cout<<"thereisaexceptionis:
"<}
voidmain(void)
{
charstr1[]="Thisisatestchararray.Helloishere.";
chardest[]="Hello";
char*result=NULL;
result=findstring(str1,dest);
cout<<"Thestringis:
"<}
3、编写一个程序其中包括3个重载函数Display()用于显示数值。
这些函数根据实参类型,将数值显示在屏幕上。
第一个函数输出一个double值,前面用字符串“Adouble:
”引导;第二个数值输出一个int值,前面用“Aint:
”引导;第三个函数输出一个char字符,前面用“Achar:
”引导。
在主函数中,分别用double,float,int,char和short型变量去调用Display()函数。
#include
voidDisplay(intidata)
{printf("Aint:
%d\n",idata);}
voidDisplay(doubleddata)
{printf("Adouble:
%16f\n",ddata);}
voidDisplay(charcdata)
{printf("Achar:
%c\n",cdata);}
voidmain(void)
{
inta=3;
doubleb=3.33;
floatc=1.2;
chard='a';
shorte=34;
Display(a);
Display(b);
Display(c);
Display(d);
Display(e);
}
第六章:
一、简答题
1、什么是枚举类型?
对枚举类型变量的值有何要求?
答:
枚举类型是由用户枚举的常量值所构成的类型。
enum-week{sun,mon,thus,thurs,fri,sat}。
2、字符数组和字符串之间有何不同?
答:
单个字符用‘’表示,字符串用“”表示,字符数据类型不同。
4、什么是数据结构?
与程序的关系如何?
答:
数据结构是人们按照逻辑结构或在物理上存储的数据排列形式。
二、选择题
1、D2、A4、A5、B
三、分析题
1、分析下列程序运行结果。
#include
usingnamespacestd;
enumDocter{Barry,Brank,Catherine,Frank}doc[4];
voidmain()
{
doc[0]=Barry;
doc[1]=Brank;
doc[2]=Catherine;
doc[3]=Frank;
cout<<"doc[0]="<cout<<"doc[1]="<cout<<"doc[2]="<cout<<"doc[3]="<}
分析结果:
doc[0]=barry
doc[1]=brank
doc[2]=catherine
doc[3]=frank
2、分析下面程序运行结果。
#include
voidmain()
{intb[][4]={{1,2,3},{4,6},{6}};
cout<
cout<
cout<
}
结果分析:
1230
4600
6000
四、编程题
1、定义表示教师或学生的结构类型person,该类型的数据对象中储存一个人的姓名、年龄和地址数据外,对于学生还需要存储器总学分和所学专业,对于教师要储存其工资和职称。
设计一个函数,用于输入某个人员的数据,同时设计另外一个函数用于输出这个人员的数据。
使用这个类型和这两个函数设计一个管理学生和教师数据的程序,在程序中可以使用一个数组来存储这些数据。
示例程序:
#include
#include
#include
#include
#include
structperson{
charcName[10];
intiAge;
intbIsTeacher;
charcAddress[50];
floatfScore;
charcProfession[50];
floatfWage;
charcGrade[20];};
intInputData(personarray[])
{
inti=0,persons=0;
while
(1)
{
cout<<"Inputname(typequittoexitinput):
";
cin>>array[i].cName;
if(strcmp(array[i].cName,"quit")==0)break;//退出条件
cout<<"Iputage:
";
cin>>array[i].iAge;
cout<<"Iputaddress:
";
cin>>array[i].cAddress;
cout<<"Isateacher?
(1isYes,0isNo:
)";
cin>>array[i].bIsTeacher;
if(array[i].bIsTeacher)//如果是老师,则输入工资和职称
{
cout<<"Inputwage:
";
cin>>array[i].fWage;
cout<<"IputGrade:
";
cin>>array[i].cGrade;}//如果是学生,输入学分和专业
else
{
cout<<"Inputscore:
";
cin>>array[i].fScore;
cout<<"Inputprofession:
";
cin>>array[i].cProfession;
}
i++;
persons++;
}
returnpersons;
}
voidPrintData(personarray[],inti)//打印第i个人的情况
{
cout<<"Nameis:
"<