C++大作业报告

时间:2024.4.13

C++大作业报告

姓名

总学号

班级

题目:2—1

内容:用三种循环语句完成求100以内的质数

设计思路:1既不是质数也不是合数,所以直接从2考虑。找出来这

些数字就是要保证这个数只能让1和其本身整除,所以让

这个数先除以2,然后慢慢整除其小于除以2后的数,然

后输出这些数。

程序代码:

while 循环

#include<iostream>

using namespace std;

int main()

{

int i=2;

} int j,n,m; while (i<101) { } return 0; m=1;n=i/2;j=2; while (j<=n) { } if(m) cout<<i<<endl; if(i%j==0) { } j++; m=0; break; i++;

Do while 循环

#include<iostream> using namespace std;

void main() {

int i=2; int j,n,m; do { } m=1;n=i/2;j=2; do { } while(i<101); if(i%j==0) { } j++; while(j<=n) if(m) cout<<i<<endl; m=0; break; i++

} return 0;

For 循环

#include<iostream> using namespace std; void main() {

int i,j,n,m; for (i=2;i<101;i++) { } m=1;n=i/2; for (j=2;j<=n;j++) { } if (m) cout<<i<<endl; if (i%j==0) { } m=0; break;

}

运return 0; 行结果

C大作业报告

结论: 不管for还是while还是do while,他们的循环体都是一样的,

所以只要编出来一个就等于全编出来了,而且程序要设计尽量简单。

题目:2—2

内容:输入一个有符号的十进制数,转换成机内二进制数输出(要求

用位操作运算)。

设计思路:利用位运算将二进制的每一位取出存入数组,然后按要求

输出。

程序代码:#include <iostream>

using namespace std;

void main()

{

char a; int t[8]; int i; cout<<"请输入一个数y:\n"; cin>>a; for(i=0;i<8;i++) { } for(i=7;i>=0;i--) cout<<t[i]; t[i]=a&0x01; a=a>>1; cout<<endl;

} system("pause");

结果

C大作业报告

:

结论:只有掌握位运算规则,才能编出来程序

内容: 书上P144,4-10

设计一个用于人事管理的“人员”类 .由于考虑到通用性,这

里只抽象出所有人员都具有的属性:编号,性别,出生日期,身份证号.(“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。)

设计思路:通过构造函数,实现人员的录入和输出。

程序代码:

#include<iostream>

using namespace std;

class date

{

private:

int year;

int month;

int day;

public:

date(int a=0,int b=0,int c=0){year=a;month=b;day=c;}

inline void setyear(int y)

{ year=y; }

void setmonth(int m)

{

}

void setday(int d)

{

}

void showdate()

{ day=d; month=m;

}

};

cout<<year<<" "<<month<<" "<<day<<" "<<endl;

class people

{

private:

char number[100];

char id[100];

char sex[2];

date birthday; public:

people();

people(people&p); ~people(){};

void setnumber(char* a) {

}

void setid(char*);

void setsex(char* c) { strcpy(number,a);

} strcpy(sex,c);

void setbirthday(date d) {

}

char *getnumber() {

}

char *getsex() {

}

char *getid() {

}

date getbirthday() {

} return birthday; return id; return sex; return number; birthday=d;

};

date d;

char m;

people::people():birthday(d) {

}

void people::setid (char*ids) {

strcpy(id,ids);

}

int main()

{

date birthday;

cout<<"录入信息"<<endl;

people p1;

//people*p[4]={&p1,&p2,&p3,&p4}; cout<<"输入员工的出生日期"<<endl; cout<<"年";

int a;

cin>>a;

birthday.setyear (a); cout<<"月";

int b;

cin>>b;

birthday.setmonth (b); cout<<"日";

int c;

cin>>c;

birthday.setday (c);

cout<<"输入编号"<<endl; char numberstr[20]; cin>>numberstr;

p1.setnumber (numberstr); cout<<"输入身份证号"<<endl; char idstr[20];

cin>>idstr;

p1.setid (idstr);

cout<<"输入性别"<<endl; char sexstr[30];

cin>>sexstr;

p1.setsex (sexstr);

cout<<"输出信息"<<endl;

cout<<"员工的出生日期";

birthday.showdate ();

cout<<"编号为 "<<p1.getnumber() <<" 身份证号为 "<<p1.getid() <<"性别为 "<<p1.getsex() ;

return 0;

}

运 行结果

C大作业报告

结论: 要充分理解函数的概念,只有在理解的情况下才能编出程序。

但是不能实现多个成员的录入和输出。

内容: 书上P144,4-11

定义并实现一个矩形类,有长、宽两个属性,由成员函数计算矩形的

面积。

设计思路:通过设计类,实现矩形的计算函数

程序代码:#include<iostream>

using namespace std;

class Rectangle

{

public:

Rectangle(); float area(); void show();

private:

}; float a;float b;

Rectangle::Rectangle()

{

do{

cout<<"please input two numbers :"<<endl; cin>>a>>b;

}

float Rectangle::area()

{

}

void Rectangle::show()

{

}

int main()

{

Rectangle c;

c.show();

return 0;

}

cout<<"a="<<a<<",b="<<b<<",area="<<area()<<endl; return a*b; }while(a<= 0 || b<= 0);

运行结果:

结论: 要理解类的含义,理解每个定义的作用!

内容:书上P186 5-7

定义一个Cat类,拥有静态数据成员numOfCats,记录cat的个体数目,静态成员函数getNumOfCats(),读取numOfCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法!

设计思路:定义一个cat类,通过构造函数,并且声明静态数据成员。 代码:

#include <iostream>

#include <string>

using namespace std;

class Cat

{

public:

Cat(){++numOfCats;}

Cat(const Cat& cat){++numOfCats;}

virtual ~Cat(){--numOfCats;}

static int getNumOfCats(){return numOfCats;} private:

static int numOfCats;

};

int Cat::numOfCats=0;

int main()

{

Cat a;

Cat b;

cout<<"numOfCats:"<<Cat::getNumOfCats()<<endl; Cat c(a);

Cat* p=new Cat();

cout<<"numOfCats:"<<Cat::getNumOfCats()<<endl;

delete p;

cout<<"numOfCats:"<<Cat::getNumOfCats()<<endl;

return 0;

} 运行结果:

结论:这部分与类密切联系,所以要掌握类,并且理解静态数据成员的使用.

内容:书上P186 5-14

定义Boat类和Car两个类,二者都有weight属性,定义二者的友元

函数getTotalWeight(),计算二者重量之和!

设计思路:定义两个类,使其为友元函数,在其基础上进行所需的运算。

代码:

#include<iostream>

using namespace std;

class Car;

class Boat

{

private:

int Boatweight;

public:

Boat()

{

Boatweight=450;

}

friend int totalWeight(Boat &,Car &); };

class Car

{

private:

int Carweight;

public:

Car( )

{

Carweight=450;

}

friend int totalWeight(Boat &,Car &);

};

int totalWeight(Boat &x,Car &y)

{

return x.Boatweight+y.Carweight;

}

int main()

{

Boat a;

Car b;

cout<<"这两者的总重量为"<<totalWeight(a,b)<<endl;

return 0;

}

运行结果

C大作业报告

:

结论:在理解类的情况下,可以用友元函数。在编写程序时,可以有效的减少程序的冗长。

内容:书上P248

已知有一个数组名叫oneArray,用一条语句求出其元素的个数。

设计思路:利用sizeof函数

代码:

#include <iostream>

using namespace std;

int main()

{

int array[] = {8, 4, 3, 4, 5,7,9,10};

int i = sizeof(array)/sizeof(int);

cout << i << endl;

return 0;

}

结果:

结论:掌握基本函数的含义和用法

内容:书上P249 6-20

实现一个名为SimpleCircle的简单圆类。其数据成员int*itsRadius

为一个指向其半径的指针,存放其半径值。设计数据成员的各种操作,给出这个类的完整实现并测试这个类。

设计思路:利用类与友元函数

代码:

#include <iostream>

using namespace std;

class SimpleCircle

{

public:

SimpleCircle();

SimpleCircle(int);

SimpleCircle(const SimpleCircle &);

~SimpleCircle() {}

void SetRadius(int);

int GetRadius()const;

private:

int *itsRadius;

};

SimpleCircle::SimpleCircle()

{

itsRadius = new int(5);

}

SimpleCircle::SimpleCircle(int radius)

{

itsRadius = new int(radius);

}

SimpleCircle::SimpleCircle(const SimpleCircle & rhs)

{

int val = rhs.GetRadius();

itsRadius = new int(val);

}

int SimpleCircle::GetRadius() const

{

return *itsRadius;

}

int main()

{

SimpleCircle CircleOne, CircleTwo(9);

cout << "CircleOne: " << CircleOne.GetRadius() << endl;

cout << "CircleTwo: " << CircleTwo.GetRadius() << endl;

return 0;

}

结果:

结论:要充分理解类的含义与用法,理解友元的用法 五

内容:书上P250 6-22

编写函数void reverse(string &s),用递归算法是字符串倒置。

算法设计:利用递归算法设计倒叙

代码:

#include<iostream>

#include<string>

using namespace std;

void reverse (char *s)

{

} char t; static int i=0; t = *(s+strlen(s)-i-1); *(s+strlen(s)-i-1) = *(s+i); *(s+i) = t; i++; if(strlen(s)/2==i); else reverse (s);

void main()

{

} char s[100]; cout<<"请输入字符串:"<<endl; cin>>s; reverse(s); cout<<"倒序后的字符串:"<<endl; cout<<s<<endl;

结果:

C大作业报告

结论:要熟练掌握递归方法

内容:书上P250 6-27

定义一个Employee类,其中包括姓名、地址、城市和邮编等属性,

包括setname(),display(),等函数。Display()使用cout语句显示姓名、地址、城市和邮编等属性,函数setname()改变对象的姓名属性,实现并测试这个类。

算法设计:利用类来设计这个程序

代码:

#include <iostream>

using namespace std;

class Employee {

private:

char *name,*address,*city,*postCode; public:

Employee(char *_name,char *_address,char *_postCode)

{

name = _name;

address = _address;

city = _city;

postCode = _postCode;

}

void setName(char *_name){

name = _name;

}

void display()

{

cout<<"name : "<<name<<endl; cout<<"address : "<<address<<endl; *_city,char

cout<<"city : "<<city<<endl;

cout<<"postcode : "<<postCode<<endl; }

};

int main(int argc,char *argv[])

{

Employee *e =new Employee("张三","天堂","宇宙","000000");

e->display();

e->setName("李四");

e->display();

} 结果:

结论:熟练掌握类的方法,用类来表示需要表示的东西。 六

题目:定义一个基类sharp,在此基础上派生出rectangle和circle,两

者都有getarea()函数计算面积。 程序代码:

#include <iostream>

#include <cmath>

#define pi 3.14

using namespace std;

class shape

{

public:

virtual float area()=0; };

class circle:public shape {

public:

circle(float r1)

{

r=r1;

}

private:

float r;

float area()

{

return (float)pi*r*r; }

};

class rectangle:public shape {

public:

rectangle(float w1,float h1) {

width=w1;height=h1; }

private:

float width,height; float area()

{

return width*height; }

};

class square : public rectangle

{

public:

square(float len):rectangle(len,len){}; ~square(){};

float area(float len)

{

return len * len;

};

};

int main()

{

shape* s[2];

int m,a,b;

cout<<"输入圆半径:"<<endl; cin>>m;

s[0]=new circle(m);

cout<<s[0]->area()<<endl;

cout<<"输入长和宽:"<<endl; cin>>a>>b;

s[1]=new rectangle(a,b);

cout<<s[1]->area()<<endl;

s[ 2 ] = new square( a );

cout << s[2]->area() << endl;

return 0;

}:

运行结果:

结论:在派生类的基础上在派生,相当于把派生看作是基类!

题目:定义一个Document类,有数据成员name,从document派生出book类,增加数据pagecount。

程序代码:

#include<iostream>

#include<string>

using namespace std;

class Document

{

};

public: Document(string Name) { } void display() / { } private: string name; cout<<"name="<<name<<endl; name=Name;

class Book:public Document {

public:

Book(string nam,int page): Document(nam) { } void show() //显示Book类数据的函数 { pageCount=page;

};

} cout<<"pageCount="<<pageCount<<endl; private: int pageCount; //该类有数据成员pageCount

int main()

{

Book a("李四",50);

} a.display(); //显示数据name a.show(); //显示数据pageCount return 0;

运行结果:

结论:掌握类的含义,在类的基础上派生,直接输出的!可以转换为输入输出!

题目:7-10

一、内容:定义一个object类,有数据成员weight及相应的操作函

数,由此派生出box类,增加数据成员height和width及相应的操作函数,声明一个box类对象,观察构造函数和析构函数的调用顺序。

二、设计思路:利用类的派生思想

三、程序代码:

#include<iostream>

using namespace std;

class Object

{

public:

Object() { } void set_weight(int neww=0,int newn=0) { } void total_weight() weight=neww; num=newn; cout<<"call the Object's constructor"<<endl;

{ } int getw(){return weight;} ~Object() { } cout<<"call the Object's desconstructor"<<endl; cout<<"the total weight is"<<" "<<weight*num<<endl;

private:

};

class Box:public Object

{

public:

Box() { int weight,num;

cout<<"call the Box's constructor"<<endl;

} void set_digital(int h=0,int w=0,int l=0) { } height=h;width=w;length=l;

void showBox() { cout<<"Box's height:"<<height<<endl; cout<<"Box's width:"<<width<<endl; cout<<"Box's length:"<<length<<endl;

} ~Box() { } cout<<"call the Box's desconstructor"<<endl; cout<<"box's weight"<<getw()<<endl;

private:

};

void main()

{

} Box a; a.set_weight(4,5); a.total_weight(); a.set_digital(1,2,3); a.showBox(); int height,width,length;

四、运行结果:

题目:7—11

一、内容:定义一个基类baseclass,从它派生出类derivedclass。

Baseclass有成员函数fn1,fn2,derivedclass也有成员函数fn1,fn2。在主函数中声明一个derivedclass的对象,分别用derivedclass的对象以及baseclass和derivedclass的指针来调用fn1,fn2,观察运行结果。

程序代码:

#include<iostream>

using namespace std;

class BaseClass

{

public:

void fun1() { } void fun2() cout<<"1"<<endl;

}; { } cout<<"2"<<endl;

class DerivedClass:public BaseClass {

public:

};

void main()

{

DerivedClass a; cout<<"通过DerivedClass的对象调用函数"<<endl; a.BaseClass::fun1(); void fun1() { } void fun2() { } cout<<"4"<<endl; cout<<"3"<<endl;

a.BaseClass::fun2();

a.fun1();

a.fun2();

BaseClass &b=a;

cout<<"通过BaseClass的指针调用函数"<<endl; b.fun1();

b.fun2();

DerivedClass &t=a;

cout<<"通过DerivedClass的指针调用函数"<<endl; t.BaseClass::fun1();

t.BaseClass::fun2();

t.fun1();

t.fun2();

} 运行结果:

一、内容:

使用I/O流以文本方式建立一个文件test1.txt,写入字符“已成功写入文件!”用其他字处理程序(例如windows的记事本程序Nodepad)打开,看看是否正确写入。

使用I/O流以文本方式打开11-3题建立的文本text1.txt读出其内容并显示出来,看看是否正确。

二、设计思路:利用输出流对象ofstream和输入流对象ifstream实现对文件的操作。

代码:

#include<iostream>

#include <fstream>

#include <string>

using namespace std;

void main()

{

ofstream ofile("test1.txt");

string s="已成功写入文件";

cout<<s<<" "<<s.length();

ofile.write((char*)&s,s.length());

ofile.close();

ifstream tfile("test1.txt");

cout<<"\ndispaly file\n";

string s2;

if(tfile)

{

tfile.read((char*)&s2,s.length());

cout << s2<<endl;

}

else

{

cout << "ERROR: Cannot open file 'payroll'." << endl; }

} tfile.close();

结果:

题目11-6

一、内容:定义一个dog类,包涵体重和年龄两个成员函数,声明一个实例dog1,体重为5,年龄为10,使用I/O流把dog1的状态写入磁盘文件,再声明另一个dog2,通过读文件把dog1的状态赋给dog2。使用二进制方式操作文件,看看结果有何不同;再看看磁盘文件的

ASCII码有何不同。

二、设计思路:

三、程序代码:

#include<iostream>

#include <fstream>

using namespace std;

class dog

{

public:

dog(int weight, long days):itsWeight(weight),

itsNumberDaysAlive(days){}

~dog(){}

int GetWeight()const { return itsWeight; }

void SetWeight(int weight) { itsWeight = weight; }

long GetDaysAlive()const { return itsNumberDaysAlive; } void SetDaysAlive(long days) { itsNumberDaysAlive = days; } private:

int itsWeight;

long itsNumberDaysAlive;

};

int main() // returns 1 on error

{

char fileName[80];

cout << "Please enter the file name: ";

cin >> fileName;

ofstream fout(fileName);

// ofstream fout(fileName,ios::binary);

if (!fout)

{

cout << "Unable to open " << fileName << " for writing.\n"; return(1);

}

dog Dog1(5,10);

fout.write((char*) &Dog1,sizeof Dog1);

fout.close();

ifstream fin(fileName);

// ifstream fin(fileName,ios::binary);

if (!fin)

{

cout << "Unable to open " << fileName << " for reading.\n"; return(1);

}

dog Dog2(2,2);

cout << "Dog2 weight: " << Dog2.GetWeight() << endl;

cout << "Dog2 days: " << Dog2.GetDaysAlive() << endl;

fin.read((char*) &Dog2, sizeof Dog2);

cout << "Dog2 weight: " << Dog2.GetWeight() << endl;

cout << "Dog2 days: " << Dog2.GetDaysAlive() << endl;

fin.close();

return 0;

}

四、运行结果:

C大作业报告

内容: 分别用函数重载,函数的缺省参数及模板函数完成两个或三

个(n个)数相加。

设计思路:定义不同的函数让两个数相加,然后让其根据数据类型相

加。 设计相加的函数,然后用主函数调用函数,运行结

果。

程序代码:

函数重载:

#include<iostream>

using namespace std; int sum(int m,int n)

{

}

double sum(double m,double n) {

}

int main()

{

int m,n; cout<<"Enter two numbers:"; cin>>m>>n; cout<<"Their sum:"<<sum(m,n)<<endl; double x,y; cout<<"Enter two other numbers:"; cin>>x>>y; cout<<"Their sum:"<<sum(x,y)<<endl; return 0; return m+n; return m+n;

}

缺省参数:

#include<iostream>

using namespace std;

int Sum(int a,int b=5,int c=2) {

return a+b+c;

}

int main()

{

}

函数模板:

#include<iostream>

using namespace std;

int Sum(int a,int b/*=4*/,int c/*=8*/) {

return a+b+c; const x=8,y=1,z=9; cout<<Sum(x,y,z)<<endl; cout<<Sum(x,y)<<endl; cout<<Sum(x)<<endl; return 0;

}

template<class T> T sum(T x,T y) {

}

int main() {

}

int a=4,b=9; double c=3.4,d=2.6; cout<<"Their sum:"<<sum(a,b)<<endl; cout<<"Their sum:"<<sum(c,d)<<endl; return 0; return x+y; 运行结果: 结果一:

C大作业报告

结果二:

C大作业报告

结果三:

C大作业报告

结论:个人觉得使用函数重载比较好,因为其易明白, 容易操作。 十

内容:用类完成一个时钟,要求做到①使用构造函数 ②使用析构函

数 ③十二时制,二十四时制 ④显示时间 ⑤输入走过的时间,在输出走过之后显示的时间

设计思路:设置时钟,构造函数,通过数字大小判定是否是AM还

是PM,看分,秒是否大于60,注意加一。

代码:

#include<iostream>

using namespace std;

class Clock

{

public:

Clock(int newH,int newM,int newS); Clock(Clock &a); void setTime(int newH,int newM,int newS); void showTime(); void run(int newH,int newM,int newS,int k); private:

};

Clock::Clock(int newH,int newM,int newS) {

}

void Clock::setTime(int newH,int newM,int newS) {

}

inline void Clock::showTime() hour=newH; minute=newM; second=newS; hour=newH; minute=newM; second=newS; int hour,minute,second;

{

}

void Clock::run(int newH,int newM,int newS,int k)

{

newS=newS+k; if(newS>60) { second=newS-60; } else second=newS; if(newM>60) { minute=newM-60; } newH=newH+1; newM=newM+1; cout<<"PM"<<hour-12<<":"<<minute<<":"<<second<<endl; else if((hour>=0)||(hour<12)) cout<<"AM"<<hour<<":"<<minute<<":"<<second<<endl; if((hour>=12)&&(hour<=23))

}

else minute=newM;

int main()

{ int h,m,s,k;

cout<<"please enter time:"<<endl; cout<<"h"<<" ""m"<<" ""s"<<endl; cin>>h>>m>>s; cout<<"k"<<endl; cin>>k; Clock a(0,0,0); a.showTime(); a.setTime(h,m,s);

}

a.showTime(); a.run(h,m,s,k); a.showTime(); return 0;

运行结果:

结论:时钟函数需要自己掌握构造,调用函数等,要认真,不放弃,就会编出自己的程序

十一

内容: 求解一元二次方程

设计思路:通过类来设计程序。

程序代码:

#include<iostream>

#include<math.h>

using namespace std;

class funtion

{

public:

void make();

void display();

void answer();

private:

float a,b,c;

float x1,x2;

float r,i,f;

};

void funtion::make ()

{

cout<<"输入 a b c 的值"<<endl; cin>>a;

cin>>b;

cin>>c;

}

void funtion::display () {

f=b*b-4*a*c;

if(f>0)

{

x1=-b/(2*a)+sqrt(f)/(2*a);

x2=-b/(2*a)-sqrt(f)/(2*a);

}

else if(f==0)

{

x1=-b/(2*a);

x2=-b/(2*a);

}

else

{

r=-b/(2*a);

i=sqrt(-f)/(2*a);

}

}

void funtion::answer()

{

if(f>0)

cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl; else if(f==0)

cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl; else

cout<<"x1="<<r<<'+'<<i<<"i"<<"

"<<"x2="<<r<<'+'<<i<<"i"<<endl;

}

int main()

{

funtion c;

c.make ();

c.display ();

c.answer ();

return 0;

} 运行结果:

结论:类需要灵活使用,明白面对对象和面对过程的区别! 十二

内容:书上P222修改

算法设计:通过书上的描述,加上一些自己的理解 代码:

#include<iostream>

#include<cassert>

#include<stdlib.h> using namespace std; class Point {

public:

Point():x(0),y(0) { } Point(int x,int y):x(x),y(y) { } ~Point(){cout<<"Destrucor called."<<endl;} int getX() const{return x;} int getY() const{return y;} void move(int newX,int newY) { } x=newX; y=newY; cout<<"Constructor called."<<endl; cout<<"Default Constructor called."<<endl;

private:

}; int x,y;

class ArrayOfPoints {

public:

//构造类数组 ArrayOfPoints(int size):size(size) { } //插入元素到类数组 void Insert(int index)//插入到第index的前面 { int i; Point * newpoint; Point * newarray; size=size+1; newpoint=new Point; newarray=new Point[size]; for(i=0;i<index-1;i++) { newarray[i]=points[i]; points=new Point[size];

} } newarray[i]=*newpoint; for(i++;i<size;i++) { } points=newarray; newarray[i]=points[i-1]; //删除类数组 ~ArrayOfPoints() { } //类数组元素访问 Point element(int index) { } //类数组元素修改 void change(int index) assert(index>=0 && index<size); return points[index]; cout<<"Deleting…-"<<endl; delete [] points;

{

int x,y;

assert(index>=0&&index<size);

cout<<"enter X:";

cin>>x;

cout<<"enter Y:";

cin>>y;

points[index].move(x,y);

}

//类数组元素遍历

void Print()

{

int s=size;

while(s)

{

cout<<size-s+1<<".:数"<<"X:"<<points[size-s].getX()<<" Y:"<<points[size-s].getY()<<endl; s--;

}

}

private:

Point * points; 数

}; int size;

int main() {

int size; cout<<"please enter the number of points :"; cin>>size; ArrayOfPoints a(size); cout<<"enter the number of the point you want to change:"; cin>>size; a.change(size-1); cout<<"X:"; a.element(size).getX(); cout<<endl<<"Y:"; a.element(size).getY(); cout<<endl<<"输入你想要插入的位置:"; cin>>size; a.Insert(size); a.change(size-1); a.Print(); system("pause"); return 0;

} 结果:

结论:熟练掌握数组,指针等运算。要多练习! 十三

题目:分数运算

代码:

#include<iostream>

using namespace std;

int A,B;

class complex{

public:

complex(int m=0,int n=1):fenzi(m),fenmu(n){} complex operator+(const complex &c2) const; complex operator-(const complex &c2) const; complex operator*(const complex &c2) const; complex operator/(const complex &c2) const; void display() const;

private:

int fenzi;

int fenmu;

};

complex complex::operator+(const complex &c2) const{ if(fenmu>=c2.fenmu)

{if(!fenmu%c2.fenmu)

B=fenmu;

else

B=fenmu*c2.fenmu;

}

if(fenmu<c2.fenmu)

{if(!c2.fenmu%fenmu)

B=c2.fenmu;

else

B=fenmu*c2.fenmu;

}

A=fenzi*(B/fenmu)+c2.fenzi*(B/c2.fenmu);

for(int i=2;A>=i;i++)

if(A%i==0 && B%i==0)

{

A/=i;

B/=i;

i--;

}

return complex(A,B);

}

complex complex::operator-(const complex &c2) const {

if(fenmu>=c2.fenmu)

{if(!fenmu%c2.fenmu)

B=fenmu;

else

B=fenmu*c2.fenmu;

}

if(fenmu<c2.fenmu)

{if(!c2.fenmu%fenmu)

B=c2.fenmu;

else

B=fenmu*c2.fenmu;

}

A=fenzi*(B/fenmu)-c2.fenzi*(B/c2.fenmu);

for(int i=2;A>=i;i++)

if(A%i==0 && B%i==0)

{

A/=i;

B/=i;

i--;

}

return complex(A,B);

}

complex complex::operator*(const complex &c2) const{

A=fenzi*(c2.fenzi);

B=fenmu*(c2.fenmu);

for(int i=2;A>=i;i++)

if(A%i==0 && B%i==0)

{

A/=i;

B/=i;

i--;

}

return complex(A,B);

}

complex complex::operator/(const complex &c2) const{

A=fenzi*(c2.fenmu);

B=fenmu*(c2.fenzi);

for(int i=2;A>=i;i++)

if(A%i==0 && B%i==0)

{

A/=i;

B/=i;

i--;

}

return complex(A,B);

}

void complex::display() const{

cout<<" "<<fenzi<<"/"<<fenmu<<" "<<endl;

}

int main() {

int a,b,c,d;

char n;

cout<<"请输入你想进行运算的数的分子和分母"<<endl; cin>>a>>b>>c>>d;

cout<<a<<"/"<<b<<" "<<c<<"/"<<d<<endl; complex c1(a,b),c2(c,d),c3;

cout<<"请输入你想进行的运算"<<endl;

cin>>n;

switch(n)

{

case'+':c3=c1+c2;

c3.display();

break;

case'-':c3=c1-c2;

c3.display();

break;

case'*':c3=c1*c2;

c3.display();

break;

case'/':c3=c1/c2;

c3.display();

break;

}

return 0;

} 结果:

结论:充分理解类的含义,使用全局变量,让程序更有可读性。

十四

题目:定义一个基类sharp,在此基础上派生出rectangle和circle,两者都有getarea()函数计算面积。

程序代码:

#include <iostream>

#include <cmath>

#define pi 3.14

using namespace std;

class shape

{

public:

virtual float area()=0; };

class circle:public shape {

public:

circle(float r1) {

r=r1;

}

private:

float r;

float area() {

return (float)pi*r*r; }

};

class rectangle:public shape

{

public:

rectangle(float w1,float h1) {

width=w1;height=h1;

}

private:

float width,height;

float area()

{

return width*height;

}

};

class square : public rectangle {

public:

square(float len):rectangle(len,len){}; ~square(){};

float area(float len)

{

return len * len; };

};

int main()

{

shape* s[2];

int m,a,b;

cout<<"输入圆半径:"<<endl; cin>>m;

s[0]=new circle(m);

cout<<s[0]->area()<<endl; cout<<"输入长和宽:"<<endl; cin>>a>>b;

s[1]=new rectangle(a,b); cout<<s[1]->area()<<endl; s[ 2 ] = new square( a ); cout << s[2]->area() << endl; return 0;

}:

运行结果:

结论:在派生类的基础上在派生,相当于把派生看作是基类!

题目:定义一个Document类,有数据成员name,从document派生出book类,增加数据pagecount。

程序代码:

#include<iostream>

#include<string>

using namespace std;

class Document

{

public: Document(string Name) { } name=Name;

};

void display() / { } private: string name; cout<<"name="<<name<<endl;

class Book:public Document {

public:

}; Book(string nam,int page): Document(nam) { } void show() //显示Book类数据的函数 { } private: int pageCount; //该类有数据成员pageCount cout<<"pageCount="<<pageCount<<endl; pageCount=page;

int main()

{

Book a("李四",50);

} a.display(); //显示数据name a.show(); //显示数据pageCount return 0;

运行结果:

结论:掌握类的含义,在类的基础上派生,直接输出的!可以转换为输入输出!

十五

一、内容:对书上355页数组类进行改进使其能够记住数组有效元素

个数。

二、设计思路:在数组类模板Array的声明和实现都在文件Array.h中。在文件中声明定义一个记录数组元素个数的函数。

程序代码:

#ifndef ARRAY_H

#define ARRAY_H #include<cassert>

//数组类模板定义 template<class T> class Array {

private: T* list; int size; int length; public: Array(int sz=50); Array(const Array<T>&a); ~Array(); Array<T>&operator=(const Array<T>&rhs); T&operator[](int i); const T &operator [] (int i) const; operator T*(); operator const T*() const; int getSize()const; void resize(int sz);

}; int getlength()const; void put(T ele ) { } void show() { } int i; for(i=0;i<length;i++) { } cout << setw(5) <<list[i]; if ((i+1) % 10 == 0) //每输出10个数换行一次 cout << endl; list[length]=ele; length++;

//构造函数

template <class T>

Array<T>::Array(int sz) {

assert(sz >= 0) ;

size = sz; // 将元素个数赋值给变量size

list = new T[size]; //动态分配size个T类型的元素空间

}

// 析构函数

template <class T>

Array<T>::~Array()

{ delete [] list;

}

// 拷贝构造函数

template <class T>

Array<T>::Array(const Array<T>& a)

{

//从对象X取得数组大小,并赋值给当前对象的成员 size = a.size;

//为对象申请内存并进行出错检查

list = new T[size]; // 动态分配size个T类型的元素空间

for(int i=0;i<size;i++)

}

template <class T> list[i]=a.list[i]; length=0;

Array<T>& Array<T>::operator= (const Array<T>& rhs) {

int n = rhs.size; // 取rhs的数组大小

if (size != n)

{

delete [] list; // 删除数组原有内存

list = new T[n];

size = n;

}

// 从rhs向本对象复制元素

for(int i=0;i<size;i++)

list[i]=a.list[i]; // 重新分配n个元素的内存

return *this; // 返回当前对象的引用

}

template <class T>

T& Array<T>::operator[] (int n)

{

assert(n>=0 && n<size) ; // 检查下标是否越界 return list[n];

}

template<class T>

const T& Array<T>::operator[] (int n) const {

assert(n>=0 && n<size) ; // 检查下标是否越界 return list[n]; // 返回下标为n的数组元素 }

template <class T>

Array<T>::operator T* ()

{

return list; // 返回当前对象中私有数组的首地址 }

template <class T>

Array<T>::operator const T* () const

{

return list; // 返回当前对象中私有数组的首地址 }

//取当前数组的大小

template <class T>

int Array<T>::getSize() const

{

return size;

}

template <class T>

int Array<T>::getlength() const

{

}

// 将数组大小修改为sz

template <class T>

void Array<T>::resize(int sz)

{

assert (sz >= 0); // 检查是否sz<= 0

if (sz == size)

return;

T* newList = new T[sz]; // 申请新的数组内存

int n = (sz <= size) ? sz : size; // 将sz与size中较小的一个赋值给n

// 将原有数组中前n个元素复制到新数组中 return length;

for(int i=0;i<n;i++)

newList[i]=list[i];

delete[] list;

list = newList;

size = sz; // 更新size

}

#endif // ARRAY_CLASS

#include <Array>

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

Array<int> A(10);

int n;

int primecount = 0, i, j;

cout << "Enter a value >= 2 as upper limit for prime numbers: ";

cin >> n;

A.put (2);

for(i = 3; i < n; i++)

{

if (A.getlength () == A.getSize() )

A.resize(A.getlength () + 10);

if (i % 2 == 0)

continue;

j = 3;

while (j <= i/2 && i % j != 0) j += 2;

if (j > i/2)

A.put (i);

}

A.show ();

cout << endl;

return 0;

}运行结果:

C大作业报告

更多相关推荐:
大作业报告格式

供配电技术课程大作业报告书题目指导教师姓名学号日期机电工和系20xx20xx学年第2学期报告书格式要求一报告前置部分一摘要内容包括研究目的方法结果结论300字400字四部分二格式要求1中文摘要摘要黑体三号居中摘...

大作业报告格式

面向对象技术课程大作业设计报告书题目超市管理系统指导教师宋涛姓名学号日期20xx1122管理科学与工程学院20xx20xx学年第1学期一需求分析随着小型超市规模的发展不断扩大商品数量急剧增加商品的各种信息量也成...

作业报告

关于仙林大学城饮料消费行为调查分析报告调查背景仙林大学城现在拥有的院校主要有南京大学南京师范大学南京财经大学南京邮电大学南京中医药大学南京信息职业技术学院南京理工大学紫金学院应天学院南京森林警察学院南京体育学院...

大作业报告格式

数据库原理大作业作业题目小组成员指导教师提交时间一需求分析功能分析功能结构图提取数据过程以及结果二概念结构设计根据需求分析得到实体属性以及实体间联系用文字描述清楚最后得到ER图三逻辑结构设计根据ER图转换相应的...

毕业大作业实践报告

中国石油大学华东现代远程教育毕业大作业实践报告题目工商企业安全管理实践报告学习中心奥鹏学习中心年级专业网络09秋工商企业管理学生姓名学号实践单位南通腾宇混凝土有限公司实践起止时间20xx年4月20xx年7月中国...

大作业报告封面

下大作业报告学院名称东方学院专业计算机科学与技术题目学期13142班级学号姓名报告成绩答辩成绩教师姓名李秉璋20xx年6月程序设计基础1作业性质大作业程是学生在学习完程序设计基础下后为提高学生编写程序的能力要求...

毕业大作业(实践报告)

中国石油大学华东现代远程教育毕业大作业实践报告题目工商企业管理专业实践报告学习中心青岛校区学习中心年级专业工商企业管理学生姓名学号实践单位实践起止时间中国石油大学华东远程与继续教育学院完成时间年月日0中国石油大...

大作业报告

基于数字图像处理的答题卡识别顾金华南京信息工程大学信息与控制学院自动化系学号20xx13360291主要功能本设计包括图片的读取答题卡角度旋转矫正答题卡坐标位置矫正答题卡灰度值读取四个主要功能该系统具有显示答题...

数据结构大作业实验报告20xx302521 刘恋

数据结构实验报告班级10011006姓名刘恋学号20xx302521Email1020xx3169qqcom日期20xx年12月30日实验题目构建一个字典树并实现相关的查找任务实验目的设计存储结构完成字典树的构...

JSP大作业(报告模板)

课程名称实验项目姓名专业学号验报告WEB应用开发技术计算机信息管理20xx年月日实北京工业大学通州分校实验项目名称一实验目的1熟练掌握JSP运行环境的配置方法灵活运用JSP语法根据自己定义的题目通过JSP编程的...

大作业实验报告

西北工业大学软件技术专用软件技术大作业技术报告1西北工业大学软件技术专用2西北工业大学软件技术专用3西北工业大学软件技术专用4西北工业大学软件技术专用5西北工业大学软件技术专用6西北工业大学软件技术专用7西北工...

Java大作业报告

课程Java程序设计与应用开发题目登录系统与钟表实现班级XXXXXXXXXX学号XXXXXXXX姓名XXXX日期XXXXX1一大作业目的用面向对象语言JAVA完成登录系统和钟表的分析设计和实施通过课程设计掌握面...

大作业报告(32篇)