程序设计基础实验报告

更新时间:2023-12-26 03:22:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

程序设计基础

1

实验一

(3)<2> 读入三个整数a、b、c,交换它们中的数,使a存放b的值,b存放c的值,c存放a的值。

#include void main() { int a,b,c,temp; cout<<\ cin>>a>>b>>c; temp=a; a=b; b=c; c=temp; cout<

(3)<3> 对任意输入的四位整数,分别求出其各位数字,并按从后到前的顺序依次输出。例如,输入为1234时,输出结果为4,3,2,1。 #include void main() { int a,b,c,d,n;

2

}

cout<<\cin>>n;

if(n>999&&n<10000){ a=n/1000; b=(n00)/100; c=(n0)/10; d=n; cout<

实验二

(2)<1> 求解下面函数的值。

e x<0,y<0 z= ln(x+y) 1≤x+y〈10 log10 其它情况 #include #include void main() { float x,y,z; cout<<\

3

|x+y|+1x+y

}

cin>>x>>y; if(x<0&&y<0){ z=exp(x+y); }

if(x+y>=1&&x+y<10){ z=log(x+y);

}else z=log10(fabs(x+y)+1); cout<

<2> 编程求解下列各计算式: 1) S=

?i=1+2+3+…+100

i?1100#include

void main() { int i,n,s; s=0; for(i=1;i<=100;i++){ s=s+i; } cout<

4

2) S=

?i!=1!+2!+…+7!

i?17#include

void main() { int i,t,s; s=0; t=1; for(i=1;i<=7;i++){ t=t*i; s=s+t; } cout<

5

4)

2n?1X3X5X7n+1XY=X-+-+…+(-1)+…的值,精确到10-6。

(2n?1)!3!5!7!#include

#include using namespace std;

float f(int x,int n) {

int s=1;

for(int i=1;i<=n;i++) s*=i;

return pow(x,n)/s; }

int main() {

float x,y=0;

cout<<\ cin>>x;

for(int i=1;i+=2;){ y+=pow(-1,i+1)*f(x,i); if(f(x,i)<1e-6) break; }

cout<

6

return 0; }

<3> 打印下面图形。

1 1 3 1 1 3 5 3 1 1 3 5 7 5 3 1 1 3 5 7 9 7 5 3 1 … … … … … … … 1 3 … … 21 … … … 3 1 #include #include #include

int main() { for(int i=1;i<=11;i++){ for(int j=1;j<=11-i;j++)

cout<<\ \ for(j=1;j<=2*i-1;j++) cout<

7

}

<4> 编程产生出1到10以内的所有数对并输出,其中i>j。 #include

void main() { int i,j; for(i=1;i<=10;i++){ for(j=1;j

8

<5> 编程求出10000以内的所有符合如下条件的数:其高位数字小于低位数字。如12,238,3578等。但21,548不符合条件。 #include void main() { int i;

for(i=10;i<100;i++){ if(i/10

9

实验三

<1> 编程产生下列数组,并输出。 2) (1 3 6 10 15 21 28 36 45 55) #include

void main() { int i,a[10]; for(i=0;i<10;i++){ a[i]=(i+1)*(i+2)/2; } for(i=0;i<10;i++) cout<

10

⑵二维数组

4) A B C D E F B C E H L Q C D F I M R D E G J N S E F H K O T #include

void main() { int i,j; char a[5][6]; for(i=0,j=0;j<6;j++) a[i][j]='A'+j; for(i=1;i<5;i++) for(j=0;j<6;j++){ a[i][j]='A'+i+j*(j+1)/2; } for(i=0;i<5;i++){ for(j=0;j<6;j++) cout<

11

<4> 随机输入一组数组元素值,利用题<2>使一个数组有序。然后随机输入一个数,用折半查找法在数组中查找,如在数组中,则输出元素在数组中的位置;如不在,则输出提示 #include using namespace std;

const int n=10; int main() { int i,k,temp,a[n]; for(k=0;k>a[k]; for(k=0;k<10;k++){ for(i=0;i<10;i++){ if(a[i]>a[i+1]){ temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } } for(i=0;i<10;i++) cout<

12

int num; cin>>num;

while(low<=high){ mid=(low+high)/2; if(num==a[mid]) break; else if(num

if(num==a[mid]) cout<<\所查找的数在第\个位置\ else

cout<<\不存在这个数\

return 0; }

<2> 判断一个二维数组是否有“鞍点”,即该位置上的元素在该行上最大,在该列上最小。如有,输出其行列号和值;若无,给出提示。(数组元素的值用scanf输入) #include #include

const int row=3; const int col=3; void main()

13

{ }

int i=0,j=0,d; int b,c,count=0; int a[row][col];

cout<<\输入数组\for(i=0;i

for(j=0;j

for(i=0;i

for(j=0;j

if(a[b][c]<=a[i][j]){ b=i; c=j; } }

for(d=0;d

if(a[d][c]

if(d==row) {

cout<<\ count++; }

}

if(count==0) cout<<\没有鞍点\

14

实验四

(2)编写程序实现下列问题的求解。

<1> 求方程ax2+bx+c=0的根,用三个函数分别求b2-4ac大于0、等于0和小于0时的根,并输出结果。从主函数输入a、b、c的值。 #include #include

void f1(float x,float y,float z) { float x1,x2; x1=((-y)+sqrt(y*y-4*x*z))/(2*x); x2=((-y)-sqrt(y*y-4*x*z))/(2*x); cout<<\ cout<<\}

void f2(float x,float y) { float x1,x2; x1=x2=(-y)/(2*x); cout<<\}

void f3(float) {

15

cout<<\无实数根\}

void main() { float a,b,c; cin>>a>>b>>c; if(b*b>4*a*c)f1(a,b,c); if(b*b==4*a*c)f2(a,b); if(b*b<4*a*c)f3(a); }

<3> 编写一个将十进制整数转换为十六进制字符串的函数。 #include using namespace std;

void main() { char c[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; char r[100],i=0; int n; cout<<\输入一个十进制数\ cin>>n; while(n>0) { r[i]=n; i++;

16

n/=16; }

cout<<\转化成16进制后\for(int j=i-1;j>=0;j--) cout<

实验五

〈1〉 输入三个整数,按由小到大的顺序输出,然后将程序改为:输入三个字符串,按由

小到大的顺序输出。

#include void main() { int i,j,temp,a[3]; for(i=0;i<3;i++) cin>>a[i]; for(i=0;i<3;i++) for(j=0;j<3;j++){ if(a[j]>a[j+1]){ temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } for(i=0;i<3;i++) cout<

17

}

〈2〉 将一个3*3的矩阵转置,用一个函数实现。在主函数中用scanf输入以下矩阵元素:

{2,4,6,8,10,12,14,16,18}。将数组名作为函数参数。函数调用后在主函数中输出已转置的矩阵。

#include using namespace std; void s(int a[]) { printf(\转置后\\n\ printf(\}

int main() { int a[9]; printf(\输入9个整数\\n\ scanf(\],&a[8]); printf(\转置前\\n\

printf(\ s(a); return 0; }

18

〈3〉 自己写一个strcmp函数,实现两个字符串的比较。两个字符串s1,s2由main函数输

入,strcmp函数的返回值也在main函数中输出。

#include using namespace std;

int strcmp(char a[], char b[]) { }

int main() { }

int i, j;

char a[20], b[20];

cout << \cin >> a >> b;

cout << strcmp(a, b) << endl;

return 0; int i, j;

for (i = 0; (a[i] != '\\0') || (b[i] != '\\0'); i++) { } return j;

if (a[i]>b[i]) { j = 1; break; } else if (a[i] = b[i]) j = 0; else { j = -1; break; }

19

〈4〉 动态链表的每个结点包括一个整数和一个后继指针。分别编写过程完成如下操作: (1) 从键盘输入数据建立链表,并按数据的输入顺序建立链表。

(2) 依次打印其中各结点的数据。 #include #include struct list {

int value; struct list *next; };

void main() {

int n,i=0,m;

struct list *p,*q,*head,*tmp; //第一行输入数据个数n; printf(\请输入链表的长度:\scanf(\if(n<1)return;

//第二行依次输入n个整数; printf(\请输入节点的值:\

p=(struct list*)malloc(sizeof(struct list)); head=p;

20

{ }

Time one, two, three;

cout<<\ one.get_time();

cout<<\ two.get_time();

three.add_time(one,two);

cout<<\ three.display_time();

2. 定义一个圆类(Circle),属性为半径(radius)、圆周长和面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。要求定义构造函数(以半径为参数,缺省值为0,周长和面积在构造函数中生成)和拷贝构造函数。

#include using namespace std; #define pi 3.1415926 class circle{

double r, l, s;

circle(const circle&a); circle() {

r = 0;

cout << \cin >> r;

cout << \cout << \

public:

26

};

}

cout << \

circle::circle(const circle& a){ }

int main() { }

circle a; circle b(a); return 0; r = a.r;

实验八

(1) 定义一个类CDate用于记录日期(包括年、月、日),其中提供成员函数从标准输入

读入日期具体值、将类对象的日期属性输出到标准输出以及当前对象表示的日期为星期几等函数。时间输入、输出方式应该尽量与人们熟悉的方式相近。

#include class data {

private:

int year; int month; int day; public:

void get(int a,int b,int c)

27

{

year=a;month=b;day=c; }

void print(); void pxq(); };

void data::print() {

cout << \这一天是:\年\月\日\}

void data::pxq() {

int y ,c,m,d,a,b; a=year/1000;

b=(year-a*1000)/100; c=a*10+b;

y=year-a*1000-b*100; d=day;

if(month==1) m=13;

else if(month==2) m=14; else m=month;

int choice=y+y/4+c/4-2*c+(26*m+26)/10+d-1; choice=choice%7; switch(choice) {

case 0:

cout << \这一天是星期日\ break; case 1:

cout <<\这一天是星期一\ break; case 2:

cout <<\这一天是星期二\ break; case 3:

cout<<\这一天是星期三\ break; case 4:

cout <<\这一天是星期四\ break; case 5:

cout<<\这一天是星期五\

28

break; case 6:

cout <<\这一天是星期六\ } }

int main() {

data d1; int a,b,c;

cout << \输入年,月,日:\ cin >>a>>b>>c; d1.get(a,b,c); d1.print(); d1.pxq(); return 0; }

(2) 首先,定义一个学生类CStudent,数据成员是:学生的姓名和学号,成员函数是输

出学生姓名和学号。然后,定义一个学生类的派生类,描述学生成绩的CScore,添加数据成员数学、语文、化学,添加获取数学、语文和化学成绩的成员函数。最后,主函数中,创建2个CScore类对象,输出总分最大同学的姓名、学号和总成绩。

#include #include

29

using namespace std; class cstudent{ private:

int num; string name;

public: };

class cscore:public cstudent{

double math, chinese, chem; void display_1(){ }

void get_value_2(){ cout << \

cout << \

public:

double a;

void get_value_1(){ cout << \

chinese >> chem; } };

int main() {

double sum_score(){ a = math + chinese + chem; return(a); }

30

cscore a1, a2;

a1.get_value_1(); a1.get_value_2(); a2.get_value_1(); a2.get_value_2();

if (a1.sum_score() > a2.sum_score()) { a1.display_1(); cout << \else { a2.display_1(); cout << \return 0;

}

小型公司人员信息管理

某小型公司,主要有四类人员:经理、技术人员、销售经理和推销员。要求存储这些人员的姓名、编号、级别、当月薪水,计算月薪总额并显示全部信息。 人员编号基数为1000,每输入一个人员的信息,编号顺序加1。

31

程序要对所有人员有提升级别的功能。为简单起见,所有人员的初始级别均为1级,然后进行升级,经理升为4级,技术人员和销售经理升为3级,推销员仍为1级。

月薪计算办法是:经理拿固定月薪8000元;技术人员按每小时100元领取月薪;推销员的月薪按该推销员当月销售额的4%提成;销售经理既拿固定月薪也领取销售提成,固定月薪为5000元,销售提成为所管辖部门当月销售总额的5‰。

根据上述需求,设计一个基类employee,然后派生出technician(技术人员)类、manager(经理)类和salesman(推销员)类。由于销售经理(salesmanager)既是经理又是销售人员,兼具两类人员的特点,因此同时继承manager和salesman两个类。 在基类中,除了定义构造函数和析构函数以外,还应统一定义对各类人员信息都应有的操作,这样可以规范各派生类的基本行为。但是各类人员的月薪计算方法不同,不能在基类employee中统一定义计算方法。各类人员信息的显示内容也不同,同样不能在基类中统一定义显示方法。因此,在employee类中用纯虚函数的方式定义了计算月薪函数pay()和显示信息函数displayStatus(),然后在派生类中再根据各自的同名函数实现具体的功能。 由于salesmanager的两个基类又有公共基类employee,为避免二义性,这里将employee类设计为虚基类。 #include\#include\class employee {

protected:

char name[20]; int grade; float pay; public:

int NO; employee(){}

employee(char NAME[],int no) {

strcpy(name,NAME); NO=no; grade=1; }

void display() {

cout<<\姓名为\编号为\级别为\ } };

class technician:public employee {

private:

float time; public:

technician(){}

technician(char NAME[],int no,float TIME):employee(NAME,no)

32

{

time=TIME; pay=time*100; grade=3; }

void getT(){cout<<\月薪为\};

class manager:virtual public employee {

public:

manager(){}

manager(char NAME[],int no):employee(NAME,no) {

pay=8000; grade=4; }

void getM(){cout<<\月薪为\};

class salesman:virtual public employee {

public:

float sale; salesman(){}

salesman(char NAME[],int no,float SALE=0):employee(NAME,no) {

sale=SALE;

pay=sale*5/100; }

void getS(){cout<<\月薪为\};

class salesmanager:public manager,public salesman {

public:

salesmanager(){}

salesmanager(char NAME[],int no):manager(NAME,no),salesman(NAME,no) {

strcpy(name,NAME); NO=no; grade=3; } };

void main() {

technician TT[1000];

33

salesman SS[1000]; manager MM[1000];

salesmanager SSMM[1000]; int A=0,B=0;

int te=0,sa=0,ma=0,sm=0; int n=1000; float sum=0; while(A!=3) {

cout<<\输入\输出\退出\ cin>>A;

if(A==1) {

cout<<\技术员\经理\销售员\销售经理\

cin>>B; if(B==1) {

char NAME1[20]; float TIME;

cout<<\请输入姓名\ cin>>NAME1;

cout<<\请输入工作时间\ cin>>TIME;

technician T(NAME1,n++,TIME); TT[te++]=T; }

if(B==2) {

char NAME2[20];

cout<<\请输入姓名\ cin>>NAME2;

manager M(NAME2,n++); MM[ma++]=M; }

if(B==3) {

char NAME3[20]; float SALE;

cout<<\请输入姓名\ cin>>NAME3;

cout<<\请输入销售额\ cin>>SALE;

34

sum+=SALE;

salesman S(NAME3,n++,SALE); SS[sa++]=S; }

if(B==4) {

char NAME4[20];

cout<<\请输入姓名\ cin>>NAME4;

salesmanager SM(NAME4,n++); SSMM[sm++]=SM; } }

if(A==2) {

for(int N=1000;N<=n;N++) {

for(int N1=0;N1<=te;N1++) {

if(TT[N1].NO==N) {

TT[N1].display(); TT[N1].getT(); } }

for(int N2=0;N2<=ma;N2++) {

if(MM[N2].NO==N) {

MM[N2].display(); MM[N2].getM(); } }

for(int N3=0;N3<=sa;N3++) {

if(SS[N3].NO==N) {

SS[N3].display(); SS[N3].getS(); } }

for(int N4=0;N4<=sm;N4++) {

if(SSMM[N4].NO==N)

35

}

} }

}

}

{

SSMM[N4].display();

cout<<\月薪为\}

36

本文来源:https://www.bwwdw.com/article/cm9x.html

Top