实验1:C++循环程序设计
1、
1、求a+aa+aaa+aaaa+… …+aa…a(n个),其中a为1~9之间的整数。例如:当a = 1, n = 3时,求1+11+111之和;当a = 5, n = 6时,求5+55+555+5555+55555+5555555之和。
#include<iostream> using namespace std; int main() { int i,n,a,b; float sum; cin>>a>>n; sum=0; b=a; for(i=0;i<n;i++) { sum=sum+a; a=a*10+b; } cout<<sum<<endl; return 0; }
2、
2、由0到4五个数字,组成五位数,每个数字用一次,但十位和百位不能为3(当然万位不能为0),输出所有可能的五位数。
#include<iostream> using namespace std; int main() { //a,b,c,d,e分别是个位 十位 百位 千位 万位 //e = 1 2 3 4 //d = 0 1 2 3 4 //c = 0 1 2 4 //b = 0 1 2 4 //a = 0 1 2 3 4 int num = 0; int a[5] = {0,1,2,3,4};//个位 int b[4] = {0,1,2,4};//十位 int c[4] = {0,1,2,4};//百位 int d[5] = {0,1,2,3,4};//千位 int e[4] = {1,2,3,4};//万位 for(int e1 = 0;e1 < 4;e1++) { for(int d1 = 0;d1< 5;d1++) { for(int c1 = 0;c1 < 4;c1++) { for(int b1 = 0;b1 < 4;b1++) { for(int a1 = 0;a1 < 5;a1++) { if(a[a1]!=b[b1] && a[a1]!=c[c1] && a[a1]!=d[d1] && a[a1]!=e[e1] && b[b1]!=c[c1] && b[b1]!=d[d1] && b[b1]!=e[e1] && c[c1]!=d[d1] && c[c1]!=e[e1] && d[d1]!=e[e1])//每一位不能重复 { num = e[e1]*10000+d[d1]*1000+c[c1]*100+b[b1]*10+a[a1]; cout<<num<<" "; } } } } } } return 0; }
心得
第一次体验C++开心
C++与C不同之处总结:
①最前面的头文件相似但有不同之处;
②输入cin>>;输出cout;这里的<<,>>本来是向前向后移动,但现在运算符重载,作为输入输出流的一部分;
③主函数为int型,记得return 0;
④大部分可以参照C,没有那么难,但也要沉进去。
实验2:数值数据输入与输出
编写一个程序,首先输入一个任意字符串,当输入1时,去掉该字符串最前面的“”,输入2时,去掉字符串中间的“”,输入3时,去掉最右端的“”,输入4时,去掉该字符串中的全部“”,输入5时,程序退出。(要求:5个子功能全部采用子函数来实现,字符串要求使用指针来进行操作)
#include <iostream> #include <string.h> using namespace std; void fuc1(char *a); void fuc2(char *a); void fuc3(char *a); void fuc4(char *a); int main() { int choice,count=0; char *a; char b[100]; a=b; cout<<"请输入待处理字符串:"; cin>>b; do{ cout<<"*************************"<<endl; cout<<"* 1、去掉前面的* *"<<endl; cout<<"* 2、去掉中间的* *"<<endl; cout<<"* 3、去掉后面的* *"<<endl; cout<<"* 4、去掉全部* *"<<endl; cout<<"* 5、退出 *"<<endl; cout<<"**************************"<<endl; cout<<endl; if(count==0) cout<<"请输入你的选择(1-5):"; else cout<<"请再次输入你的选择(1-5):"; cin>>choice; cout<<endl; switch (choice) { case 1: fuc1(b); break; case 2: fuc2(b); break; case 3: fuc3(b); break; case 4: fuc4(b); break; default: break; } count++; }while(choice!=5); cout<<"程序退出中。。。。。。。。。。"<<endl; return 0; } /* 1、去掉前面的* */ void fuc1(char *a) { int i,flag=0,j=0; char c[100]; for(i=0;i<strlen(a);i++) { if(a[i]!='*') flag=1; if(flag==1) { c[j]=a[i]; j++; } } cout<<"结果字符串为:"; for(i=0;i<j;i++) { cout<<c[i]; } cout<<endl; cout<<endl; } /* 2、去掉中间的* */ void fuc2(char *a) { int i,count=0,begin=0,end=0; char c[100]; for(i=0;i<strlen(a);i++) { if(a[i]!='*') { begin=i; break; } } for(i=strlen(a)-1;i>=0;i--) { if(a[i]!='*') { end=i; break; } } for(i=0;i<strlen(a);i++) { if(a[i]=='*'&&i>begin&&i<end) continue; c[count]=a[i]; count++; } cout<<"结果字符串为:"; for(i=0;i<count;i++) { cout<<c[i]; } cout<<endl; cout<<endl; } /* 3、去掉后面的* */ void fuc3(char *a) { int i,j=0,flag=0,count=0; char c[100]; char d[100]; for(i=strlen(a)-1;i>=0;i--) { c[j]=a[i]; j++; } for(i=0;i<strlen(a);i++) { if(c[i]!='*') flag=1; if(flag==0) continue; d[count]=c[i]; count++; } cout<<"结果字符串为:"; for(i=count-1;i>=0;i--) { cout<<d[i]; } cout<<endl; cout<<endl; } /* 4、去掉全面* */ void fuc4(char *a) { int i,j=0; char c[100]; for(i=0;i<strlen(a);i++) { if(a[i]!='*') { c[j]=a[i]; j++; } } cout<<"结果字符串为:"; for(i=0;i<j;i++) { cout<<c[i]; } cout<<endl; cout<<endl; }
心得
本实验难点:循环语句
这题是在请教同学之后思考写出来的,过程有点艰辛。
对于超出凭看就能有思路的题目,可以先想清白每一个循环的内在逻辑,画好逻辑图或者用伪代码先弄一下。
对于自己思考很久还弄不出来的题,及时请教老师同学,不遗留问题。根据点拨自己再次思考,一般情况下就没有其他问题了。但是,对于这类题目,要注意定期回顾,隔段时间再看,观察自己是否已经掌握了这类题的解决方案,有思路,能写代码,无报错,如果有报错,及时总结,对比发现是新报错还是之前出现,如果出现过,以后遇到类似代码部分要更谨慎。
实验3:类与对象的定义与使用
定义一个学生成绩类Score,描述学生成绩的私有数据成员为学号(No)、姓名(Name[8])、数学(Math)、物理(Phi)、数据结构(Data)、平均分(ave)。定义能输入学生成绩的公有成员函数Write(),能计算学生平均分的公有成员函数Average(),能显示学生成绩的公有成员函数Display()。在主函数中用Score类定义学生成绩对象数组s[3]。用Write()输入学生成绩,用Average()计算每个学生的平均分,最后用Display()显示每个学生的成绩。
实验数据:
No Name Math Phi Data Ave
1001 Zhou 80 70 60
1002 Chen 90 80 85
1003 Wang 70 75 89
//*************** //** 1.cpp ** //*************** #include<iostream> #include "1.h" #include<string.h> using namespace std; #include<iomanip> void Score::Write(int a,char b[],double c,double d,double e) { No=a; strcpy(Name,b); Math=c; Phi=d; Data=e; } void Score::Average() { ave=(Math+Phi+Data)/3; //平均值的计算 } void Score::Display() { cout<<setw(8)<<No<<setw(6)<<Name<<setw(6)<<Math<<setw(6)<<Phi<<setw(6)<<Data<<setw(6)<<ave<<endl; } //*************** //** 2.cpp ** //*************** #include<iostream> #include "1.h" #include<string.h> using namespace std; #include<iomanip> void main() { Score s[3]; s[0].Write(1001,"Zhou",80,70,60); s[1].Write(1002,"Chen",90,80,85); s[2].Write(1003,"Wang",70,75,89); s[0].Average(); s[1].Average(); s[2].Average(); cout<<setw(8)<<"No"<<setw(6)<<"Name"<<setw(6)<<"Math"<<setw(6)<<"Phi"<<setw(6)<<"Data"<<setw(6)<<"Ave"<<endl; s[0].Display(); s[1].Display(); s[2].Display(); } //*************** //** 1.h ** //*************** #include<iostream> #include<string.h> using namespace std; class Score{ private: int No; //学号 char Name[8]; //姓名 double Math; //数学 double Phi; //物理 double Data; //数据结构 double ave; //平均分 public: void Write(int a,char b[],double c,double d,double e); //输入学生成绩 void Average(); //计算学生平均分 void Display(); //显示每个学生的成绩};
心得
初步掌握类与对象的定义与使用方法,构造函数、拷贝构造函数的定义与使用方法,构函数的定义与使用方法。理解构造函数与析构函数的调用过程。
所遇问题及改进:
①关于带参构造函数的char初始化
void Score::Write(int a,char b[],double c,double d,double e) { No=a; strcpy(Name,b);
注意b[]和stcpy;
如果报错:不能将第二个参数从char型变量参数 转化成
静态(const)char指针变量、意思是说你那个地方本来应该用char指针的,但你给的是个char型变量.你把你的参数写成&c(假设你那里用的变量名是c)。
考虑【】
②” ” s[0].Write(1001,“Zhou”,80,70,60);
③setw记得头文件#include
cout<<setw(8)<<“No”<<setw(6)<<“Name”<<setw(6)<<“Math”<<setw(6)<<“Phi”<<setw(6)<<“Data”<<setw(6)<<“Ave”<<endl;
④注意.h文件头文件不能互引自己本身 如果报错:#include nesting level is 362 deep; possible
infinite recursion 就是因为这个原因
实验4:类的组合定义与访问
建立一个存放素数的类Prime,具体要求如下。
①私有数据成员。
int a[100]:存放指定范围内的所有素数。
int n1,n2:存放指定范围的下限和上限
int num:存放素数的个数。
②公有成员函数
Prime(int m1,int m2):构造函数,用参数m1、m2初始化n1、n2,同时初始化num。
void primef():求指定范围内的所有素数,把它们依次存放在数组a中。并将求出的素数个数赋给num。
void show():显示求出的素数的个数及所有的素数,每行输出5个素数。
③在主函数中定义一个Prime类的对象p(100,200),通过p调用成员函数完成求素数及输出素数的工作。
//*************** //** 1.cpp ** //*************** #include<iostream> #include<cmath> #include "1.h" using namespace std; void Prime::primef() { int k; for(int i=n1;i<=n2;i++) { k=0; for(int j=2;j<sqrt(i);j++) if(i%j==0) { k=1; break; } if(k==0) a[num++]=i; } } void Prime::show() { for(int i=0;i<num;i++) { if(i%5==0) { cout<<endl; } cout<<a[i]<<' '; } cout<<endl; } //*************** //** 2.cpp ** //*************** #include<iostream> #include "1.h" using namespace std; void main() { Prime p(100,200); p.primef(); p.show(); } //*************** //** 1.h ** //*************** class Prime{ public: Prime(int m1,int m2):n1(m1),n2(m2),num(0) {} //构造函数,用参数m1、m2初始化n1、n2,同时初始化num void primef(); //求指定范围内的所有素数,把它们依次存放在数组a中。并将求出的素数个数赋给num void show(); //显示求出的素数的个数及所有的素数,每行输出5个素数 private: int a[100]; //存放指定范围内的所有素数 int n1,n2; //存放指定范围的下限和上限 int num; //存放素数的个数 };
理解无参构造函数与带参构造函数的区别,掌握类的组合的定义,掌握对象成员的初始化方法。
实验5:静态成员函数设计
编写一个程序,输入N个学生数据,包括学号、姓名、C++成绩, 要求输出这些学生的数据、平均分与成绩等级。 提示:
1)设计一个学生类Stud,除了包括id(学号)、name(姓名)和C(成绩)数据成员外, 还有两个静态成员数据sum和num,分别存放总分和人数,
2)另有两个普通成员函数setdata()和print(),分别用于给数据成员赋值和输出数据成员的值, 另有一个静态成员函数avg(),它用于计算平均分,并输出平均分。
3)同时设计一个友元函数,输出成绩对应的等级: 大于等于90:优;8090:良;7079:中;60~69:及格;小于60:不及格。
4)在main()函数中定义了一个对象数组(不少于5个学生,各数据值自己确定),用于存储输入的学生数据,设计一个完整的程序。
//*************** //** 1.cpp ** //*************** #include<iostream> #include<iomanip> #include "1.h" using namespace std; void Stud::setdata() { cout<<"Please input Student id,name,C according to the order:"; cin>>id>>name>>C; } void Stud::print() { cout<<setw(10)<<name<<setw(6)<<id<<setw(8)<<C<<setw(8)<<level<<endl; } void Stud::total() { sum+=C; num++; } float Stud::avg() { return sum/num; } void Level(Stud &s) { if(s.C>=90) strcpy(s.level,"优"); else if(s.C>=80) strcpy(s.level,"良"); else if(s.C>=70) strcpy(s.level,"中"); else if(s.C>=60) strcpy(s.level,"及格"); else strcpy(s.level,"不及格"); } //*************** //** 2.cpp ** //*************** #include<iostream> #include "1.h" #include<iomanip> #include<string.h> using namespace std; int Stud::sum=0; int Stud::num=0; int main() { int i; Stud *p=new Stud[6]; //定义了一个对象数组,用于存储输入的学生数据 for(i=0;i<6;i++) { (p+i)->setdata(); (p+i)->total(); } cout<<setw(10)<<"姓名"<<setw(6)<<"学号"<<setw(8)<<"C++成绩"<<setw(8)<<"等级"<<endl; for(i=0;i<6;i++) { Level(*(p+i)); (p+i)->print(); } cout<<"Average C score: "<<Stud::avg()<<endl; return 0; } //*************** //** 1.h ** //*************** class Stud{ public: void setdata(); //给数据成员赋值 void print(); //输出数据成员的值 void total(); static float avg(); //计算平均分,并输出平均分 friend void Level(Stud &); //输出成绩对应的等级 private: int id; //学号 char name[20]; //姓名 float C; //C++成绩 char level[10]; //等级 static int sum; //总分 static int num; //人数 };
理解静态成员的概念,掌握静态函数的定义与调用方法;对象数组、对象指针及其使用方法;
例:(友元函数)friend void Level(Stud &); //输出成绩对应的等级
实验6:运算符重载
下面是一个整型数组类intArray的声明,请给出该类所有数据成员的类外定义。要求:
(1)能通过对“>>”、“<<”的重载实现数组元素的直接输入及其输出。输入时,第一次输入的值为数组长度,后面连续输入若干个数作为数组内容。输出时,首先提示该数组元素个数,然后依次输出该数组各元素;
(2)重载“=”、“+”、“-” 运算符使之能对两个数组类对象进行直接赋值、加减运算。
(3)写出主函数对该类进行测试。要求:
1)先构造包含10个数组元素的数组对象a,再调用流提取运算符重载函数实现a的元素初始化(元素值分别为1—10),输出a数组;
2)并利用a来构造数组b,输出数组b,并调用get函数获取b中下标为奇数的各元素值并输出,然后调用set函数将上述元素值乘以2,输出b数组;
3)构造一个包含5个元素的数组c,其值全为1,输出该数组,再调用ReSize函数将其大小重置为10,调用“=”重载函数将a的值复制给c,输出数组c;
4)分别将b+c,b-c的值送给数组d和数组e,并输出结果。
class intArray {public: intArray(int size);//构造函数 intArray(const intArray &x);//复制构造函数 ~intArray();//析构函数 bool Set(int i, int elem);//设置第i个数组元素的值,设置成功返回true,失败返回false bool Get(int i, int &elem); //获取第i个数组元素的值,获取成功返回true,失败返回false int Length( ) const;//获取数组的长度 void ReSize ( int size ); //重置数组 intArray &operator=(const intArray &other); //赋值运算符“=”重载函数 intArray &operator+(const intArray &other); //加运算符“=”重载函数 intArray &operator-(const intArray &other) ; //减运算符“=”重载函数 friend istream & operator>>(istream &, intArray &); //数组的整体输入 friend ostream & operator<<(ostream &, intArray &); //数组的整体输出 private: int *element; //指向动态数组的指针 int arraysize; //数组的当前长度 }; //*************** //** 1.cpp ** //*************** #include <iostream.h> #include "1.h" int intArray::Length() const { return arraysize; } intArray intArray::operator + (const intArray &other) { //数组元素对应相加 intArray s=other; int i; for(i=0;i<other.arraysize;i++) { s.element[i]=element[i]+other.element[i]; } return s; } intArray intArray::operator - (const intArray &other) { //数组元素对应相减 intArray s=other; int i; for(i=0;i<other.arraysize;i++) { s.element[i]=element[i]-other.element[i]; } return s; } intArray intArray::operator = (const intArray &other) { //相似的进行赋值 element=other.element; arraysize=other.arraysize; return *this; } void intArray::ReSize (int size) { delete[] element; element=new int[size]; //重置指针大小 arraysize=size; } bool intArray::Set(int i, int elem) { if(i<arraysize) { element[i]=element[i]*elem; return true; } else { return false; } } bool intArray::Get(int i, int &elem) { if(i<arraysize) { elem=element[i]; return true; } else { return false; } } intArray::intArray(int size) //构造函数 { arraysize=size; element=new int[size]; } intArray::intArray(const intArray &x) //拷贝构造函数 { arraysize=x.arraysize; element=x.element; } intArray::intArray() //不带参的构造函数 { arraysize=0; element=new int[0]; } //*************** //** 2.cpp ** //*************** #include <iostream.h> #include "1.h" ostream & operator<<(ostream &output, intArray &s) { //重载流插入运算符 int i; output<<"数组的长度是"<<s.arraysize<<" "<<"数组:"; for(i=0;i<s.arraysize;i++) { output<<s.element[i]<<" "; } output<<endl; return output; } istream & operator>>(istream &input, intArray &s) { //重载流提取运算符 int i=0,n; cout<<"第一次输入的值为数组长度,后面连续输入若干个数作为数组内容"<<endl; input>>n; intArray temp(n); s=temp; for(i=0;i<s.arraysize;i++) { input>>s.element[i]; } return input; } int main() { int i,temp; intArray a; cin>>a; cout<<a; intArray b=a; cout<<b; cout<<"Get函数的结果:"; for(i=0;i<10;i++) { if(i%2!=0&&b.Get(i,temp)==1) { cout<<temp<<" "; } } for(i=0;i<10;i++) { if(i%2!=0) { b.Set(i,2); } } cout<<b; intArray c; cin>>c; cout<<c; c.ReSize(10); c=a; cout<<c; intArray d=b+c; cout<<d; intArray e=b-c; cout<<e; return 0; } //*************** //** 1.h ** //*************** #include <iostream.h> class intArray{ public: intArray(); intArray(int size); //构造函数 intArray(const intArray &x); //复制构造函数 friend istream & operator>>(istream &, intArray &); //数组的整体输入 friend ostream & operator<<(ostream &, intArray &); //数组的整体输出 bool Set(int i, int elem); //设置第i个数组元素的值,设置成功返回true,失败返回false bool Get(int i, int &elem); //获取第i个数组元素的值,获取成功返回true,失败返回false void ReSize(int size); //重置数组 intArray operator = (const intArray &other); //重载赋值运算符“=” intArray operator + (const intArray &other); //重载加运算符“+” intArray operator - (const intArray &other); //重载减运算符“=” int Length() const; //获取数组的长度 private: int *element; //指向动态数组的指针 int arraysize; //数组的当前长度 };
很有些难度,修改比写的时间要长
实验7:虚函数的使用
编程完成如下要求
(1)假设已有一个CShape图形类,并满足如下要求:1)CShape类有一个纯虚函数CountArea,能够计算图形的表面积;2)CShape类有一个纯虚函数CountVolume,能够计算图形的体积;
(2)由CShape类派生立方体CCube类,该类满足如下要求:1)CCube类有一个带参数的构造函数,其参数分别对应于立方体的长、宽、高;2)用一个成员函数来实现对立方体长、宽、高的设置;3)重载CShape类的CountArea和CountVolume,分别完成立方体的表面积和体积的计算。
(3)由CShape类派生球体CSphere类,该类满足如下要求:1)CSphere类有一个带参数的构造函数,其参数对应于球体的半径;2)用一个成员函数来实现对球体半径的设置;3)重载CShape类的CountArea和CountVolume,分别完成球体的表面积和体积的计算。
(4)实现一个main函数,在main函数中至少完成如下工作:1)实例化一个CCube类的对象a_cube和CSphere类的对象b_sphere;2)定义一个CShape类的指针p;3)将a_cube的长、宽和高分别设置为4、5和6;将p指向a_cube,通过p将a_cube的表面积和体积打印到屏幕上;4)将b_sphere的半径设置为7;将p指向b_sphere,通过p将b_sphere的表面积和体积打印到屏幕上。
//*************** //** 1.cpp ** //*************** #include <iostream> #include "1.h" #include "2.h" #include "3.h" using namespace std; void CSphere::CountArea() //计算表面积 { cout<<"area: "<<4*3.14159*radius*radius<<endl; } void CSphere::CountVolume() //计算体积 { cout<<"volume: "<<4/3*3.14159*radius*radius*radius<<endl; } void CSphere::Getsphere(double r) { radius=r; } void CCube::Getcube(double X,double Y,double Z) { x=X; y=Y; z=Z; } void CCube::CountArea() //计算表面积 { cout<<"area: "<<2*(x*y+x*z+y*z)<<endl; } void CCube::CountVolume() //计算体积 { cout<<"volume: "<<x*y*z<<endl; } //*************** //** 2.cpp ** //*************** #include <iostream> #include "1.h" #include "2.h" #include "3.h" using namespace std; int main() { CCube a_cube; CSphere b_sphere; CShape *p; //定义的Shape类指针 a_cube.Getcube(4,5,6); p=&a_cube; p->CountArea(); p->CountVolume(); b_sphere.Getsphere(7); p=&b_sphere; p->CountArea(); p->CountVolume(); return 0; } //*************** //** 1.h ** //*************** class CShape{ public: virtual void CountArea()=0; //纯虚函数声明 virtual void CountVolume()=0; }; //*************** //** 2.h ** //*************** class CCube: public CShape{ public: CCube(double X=0,double Y=0,double Z=0):x(X),y(Y),z(Z){} //带默认参数的使用初始化列表的构造函数 void Getcube(double X=0,double Y=0,double Z=0); //计算体积 virtual void CountArea(); virtual void CountVolume(); protected: double x; double y; double z; }; //*************** //** 3.h ** //*************** class CSphere: public CShape{ public: CSphere(double r=0):radius(r){} //带默认参数的使用初始化列表的构造函数 void Getsphere(double r); virtual void CountArea(); virtual void CountVolume(); protected: double radius; };
多态性,纯虚函数的使用,抽象类