最近发表了一篇名为《【实验报告模板】C++实验报告》的范文,好的范文应该跟大家分享,为了方便大家的阅读。
C++实验报告实验1-1 过程化编程
【实验目的】
理解、掌握过程化编程程序设计思想。
【实验内容】
1.程序填空,练习类、对象、继承的定义和实现方法。
2.根据程序运行结果,补充完整程序。
【实验要求】
我们在进行英语阅读的时候,会发现一个有趣的现象:有些字串是左右对称的,如madam。我们把这种字串称为symmetry text 即“对称文”。
现在有若干航字串,每一行可以由数字、标点符号、空格符以及英文字符(包括大小写)组成。要你帮忙编程判断是否是对称文,否则,就不能最大限度地发现有趣现象了。
输入说明
每个字串为一行,每行结束以回车符为标志,可能有上百上千行业说不定。当字串为“000000”时,输入结束。
英文字符不区分大小写,即Madam亦为对称文。不要忘了“{<([”与“])>}”也是互为对称的。
输出说明
如果是对称文,则输出“Symmetry”,否则输出“Not symmetry”。每个结论占一行。
图 1 symmetry.in
图 2 symmetry.out
【程序代码】 #include<iostream>
#include<string>
using namespace std;
bool isMatch(string s);
int main() {
}
bool isMatch(string s) {
{
} else if ((s[i] >= "A"&&s[i] <= "Z") &&
(s[len - i - 1] >= "a"&&s[len - i - 1] <= if (s[i]!= (s[len - i - 1] - ("a"
- "A"))) { return false;
} if (s[i]!= (s[len - i - 1] + ("a"
- "A"))) { return false;
int len = s.length();
for (int i = 0;
i<len / 2;
i++) { if ((s[i] >= "a"&&s[i] <= "z") &&
(s[len - i - 1] >= "A"&&s[len - i - 1] <= "Z")) string s;
while (1) { } return 0;
cin >>
s;
if (s.compare("000000") == 0) { } if (isMatch(s)) { } else { } cout <<
"Not symmetry"
<<
endl;
cout <<
"Symmetry"
<<
endl;
peak;
"z")) {
} } } }
else if (((s[i] == "{"&&s[len - i - 1] == "}"))) { } else if (((s[i] == "<"&&s[len - i - 1] == ">"))) { } else if (((s[i] == "["&&s[len - i - 1] == "]"))) { } else if (((s[i] == "("&&s[len - i - 1] == ")"))) { } else { } if (s[i]!= s[len - i - 1]) { } return false;
continue;
continue;
continue;
continue;
return true;
【运行结果】
图 3 实验一运行结果
实验1-2 面向对象编程技术(1)
最全面的范文参考写作网站【实验目的】
理解面向对象的的程序设计思想。
【实验内容】
定义一个时间类Time,能提供和设置由时、分、秒组成的时间,并编出应用程序,要求包括定义时间对象,设置时间,输出该对象提供的时间。并请将类定义作为界面,用多文件结构实现之。
【程序代码】 //Time.h
#include<iostream>
class Time {
public:
};
//Time.cpp
#include "Time.h"
#include<iostream>
void Time::inputT()
{
begin:
int a,b,c;
std::cout<<
"Input time(H:M:S)\n";
std::cin >>
a >>
b >>
c;
if (c >
60 || c <
0) { } else std::cout <<
"Wrong time!Please set again!\n";
goto begin;
int h;
int m;
int s;
void inputT();
void changeT();
void outputT();
}
{ } if (b >
60 || b <
0) { } else { } if (a >
24 || a <
0) { } else if (a == 24) { } else { } h = a;
m = b;
s = c;
if (b!=0 || c!=0) { } else { } h = a;
m = b;
s = c;
std::cout <<
"Wrong time!Please set again!\n";
goto begin;
s
td::cout <<"Wrong time!Please set again!\n";
goto begin;
std::cout <<
"Wrong time!Please set again!\n"
;
goto begin;
void Time::changeT() {
char p;
std::cout <<
"Do you wanna change time?(Y/N): \n";
std::cin >>
p;
if (p == "n"
|| p == "N") { } std::cout <<
"Thank you for your using!\n"
;
下页C++实验报告
{ begin1: int a,b,c;
std::cout <<
"Input time(H:M:S)\n";
std::cin >>
a >>
b >>
c;
if (c >
60 || c <
0) { } else { } if (b >
60 || b <
0) { } else { } if (a >
24 || a <
0) { } else if (a == 24) { } else { } h = a;
m = b;
s = c;
if (b!= 0 || c!= 0) { } else { } h = a;
m = b;
s = c;
std::cout <<
"Wrong time!Please set again!\n";
goto begin1;
std::cout <<
"Wrong time!Please set again!\n"
;
goto begin1;
std::cout <<
"Wrong time!Please set again!\n";
goto begin1;
std::cout <<
"Wrong time!Please set again!\n";
goto begin1;
}
void Time::outputT()
//testmain.cpp
#include
"Time.h"
#include<iostream>
void main(void)
{
} Time time1;
time1.inputT();
time1.outputT();
time1.changeT();
time1.outputT();
{ } std::cout <<
"Output time(H:M:S)\n"
<<
h <<
":"
<<
m <<
":"
<<
s;
【运行结果】
图 4 实验二运行结果
实验3 面向对象程序设计(2)
【实验要求】
改写程序f0815.cpp,使之含有构造函数,拷贝构造函数和析构函数。并对主函数和矩阵向量的乘法也进行改写。对于第91和92行,合并成“multiply(ve,ma).display();”使之不会产生内存泄漏。
【实验程序】
//实验三
//改写f0815.cpp
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class Vector
{
};
Vector::Vector(int s)
{
}
Vector::Vector(const Vector&
s)
{
} int i;
sz = s.sz;
v = new int[sz];
for (i = 0;
i<sz;
i++) { } v[i] = s.v[i];
sz = s;
if (s <= 0) { } v = new int[s];
cerr <<
"bad Vector size.\n";
exit(1);
int size() { return sz;
} Vector(int);
Vector(const Vector&
s);
int&
operator[](int);
void display();
~Vector();
int* v;//指向一个数组,表示向量 int sz;
public:
Vector::~Vector()
{
}
int&
Vector::operator[](int i)//引用返回的目的是返回值可以做左值 {
}
void Vector::display()
{
}
class Matrix
{
};
Matrix::Matrix(int i,int j)
{
}
Matrix::Matrix(const Matrix&
s)
{
int i,j;
szl = s.szl;
szl = i;
szr = j;
if (i <= 0 || j <= 0) { } m = new int[i*j];
cerr <<
"bad Matrix size.\n";
exit(1);
int* m;
int szl,szr;
Matrix(int,int);
Matrix(const Matrix&
m);
~Matrix();
int sizeL() { return szl;
} int sizeR() { return szr;
} int&
elem(int,int);
int i;
for (i = 0;
i<sz;
++i) cout <<
v[i] <<
"
";
cout <<
"\n";
if (i<0 || i >= sz) { } return v[i];
cerr <<
"Vector index out of rang.\n";
exit(1);
delete[] v;
public:
} szr = s.szr;
m = new int[szl*szr];
for (i = 0;
i<szl;
i++) { } for (j = 0;
j<szr;
j++) { } m[i*szr + j] = s.m[i*szr + j];
Matrix::~Matrix()
{
}
int&
Matrix::elem(int i,int j)//引用返回值的目的是可以做左值 {
}
Vector multiply(Matrix&
m,Vector&
v)
{//矩阵乘向量
}
void main()
{ int i,j;
Matrix me(m);
Vector va(v);
if (m.sizeR()!= v.size()) { } Vector r(m.sizeL());//创建一个存放结果的空向量 for (i = 0;
i<me.sizeL();
i++) { } return r;
r[i] = 0;
for (j = 0;
j<me.sizeR();
j++) { } r[i] += me.elem(i,j)*va[j];
cerr <<
"bad multiply Matrix with vector.\n";
exit(1);
if (i<0 || szl <= i || j<0 || szr <= j) { } return m[i*szr + j];
cerr <<
"Matrix index out of range.\n";
exit(1);
delete[] m;
上页 下页C++实验报告
} int i,j;
ifstream in("in.txt");
//文件名 int x,y;
in >>
x >>
y;
Matrix ma(x,y);
for (i = 0;
i<x;
++i) for (j = 0;
j<y;
++j) in >>
ma.elem(i,j);
in >>
x;
Vector ve(x);
for (i = 0;
i<x;
++i) { } Matrix me(ma);
Vector va(ve);
multiply(ma,ve).display();
in >>
ve[i];
【实验结果】
图 5 实验三运行结果
实验4 面向对象程序设计(3)
【实验要求】
请在程序f0904.cpp中的日期类的基础上,实现一个可以进行加天数操作获得另一个日期,以及进行日期减日期操作获得相隔天数的日期类,并进行应用程序设计:
创建2015.8.21和2008.8.8两个日期,并计算中间相隔的天数,前者加上300天会是什么日子呢?范文TOP100【实验程序】 一、头文件部分
#pragma once
//class Date with year-month-day version
#include<iostream>
#include<string>
using namespace std;
class Date {
};
二、函数定义 int year,month,day;
Date(int n = 1) { } int ymd2i()const;
void i2ymd(int n);
static const int tians[];
Date(const string&
s);
Date(int y,int m,int d):year(y),month(m),day(d){ } Date operator+ (int n)const { } Date&
operator+=(int n) { } Date&
operator++() { } int operator-(Date&
d)const { } bool isLeapYear()const { } friend ostream&
operator<<(ostream&
o,const Date&
d);
return!(year % 4) &&
(year % 100) ||!(year % 400);
return ymd2i() - d.ymd2i();
return *this += 1;
i2ymd(ymd2i() + n);
return *this;
return Date(ymd2i() + n);
i2ymd(n);
public:
//Date.cpp
//Class Date with year-month-day Version
#include"Date.h"
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
const int Date::tians[] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
const int Y400 = 146097;//number of days of 400 years
const int Y100 = 36524;//number of days of 100 years
const int Y4 = 1461;//number of days of 4 years
Date::Date(const string&
s) {
(转载于:www.fwwang.cn 在点 网)}
void Date::i2ymd(int absDay) {
}
int Date::ymd2i()const {
int yearDay = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
return yearDay + tians[month - 1] + (isLeapYear() &&
month >
2) + day;
year = absDay / Y400 * 400;
int y = absDay%Y400;//被400年除得的天数 if (y == Y400 - 1) { } year += y / Y100 * 100;
y %= Y100;
year += y / Y4 * 4;
y%=Y4;
if (y == Y4 - 1) { } year += y / 365;
y %= 365;
if (y == 0) { } year++;
bool leap = isLeapYear();
for (month = 1;
month<12 &&
y>tians[month] + (month >= 2 &&
leap);
month++);
day = y - tians[month - 1];
month = 12,day = 31;
return;
month = 12,day = 30;
return;
month = 12,day = 30;
return;
year = atoi(s.substr(0,4).c_str());
month = atoi(s.substr(5,2).c_str());
day = atoi(s.substr(8,2).c_str());
}
ostream&
operator<<(ostream&
o,const Date&
d) {
return o <<
setfill("0") <<
setw(4) <<
d.year <<
"-"
<<
setw(2) <<
d.month <<
"-"
<<
}
三、testmain
//testmain.cpp
//using Date class
#include"Date.h"
#include<iostream>
using namespace std;
int main() {
Date d1(2005,8,21);
Date d2(2008,8,8);
cout<<
"2005.8.21与2008.8,8中间相隔的天数是:"<<
d2 - d1 <<
"\n";
cout <<"2005.8.21加上300天是:"<<
d1 + 300 <<
"\n";
} setw(2) <<
d.day <<
setfill("
");
【程序结果】
实验5 面向对象程序设计(4)
【实验要求】
在上题Date类的基础上,继承一个WDate类,它包含了星期几信息,因而,显示日期的成员要做修改,应同时显示星期几。另外,还要增加获得星期几的成员。想一想,类中数据成员置年、月、日好呢,还是绝对天数好呢?
进而进行应用程序设计:
创建2005.8.21和2008.8.8两个日期,分别显示这两个日期。
【实验程序】
一、头文件Date.h
#pragma once
//Date.h
//class Date with year-month-day version
#include<iostream>
#include<string>
using namespace std;
class Date {
};
int year,month,day;
static const int tians[];
Date(int n = 1) { } int ymd2i( )const;
void i2ymd(int n);
Date(const string&
s );
Date(int y,int m,int d) :year(y),month(m),day(d) {} Date operator+(int n)const { } Date&
operator+=(int n) { } Date&
operator++() { } int operator-(Date&
d)const { } bool isLeapYear()const { } friend ostream&
operator<<(ostream&
o,const Date&
d);
return!(year % 4) &&
(year % 100) ||!(year % 400);
return ymd2i() - d.ymd2i();
return *this += 1;
i2ymd(ymd2i() + n);
return *this;
return Date(ymd2i() + n);
i2ymd(n);
protected: public:
上页 下页 余下全文C++实验报告
二、头文件WDate.h
#pragma once
//WDate.h
#include"Date.h"
#include<iostream>
#include<string>
using namespace std;
class WDate :public Date {
protected:
WDate operator+(int n) const { return Date::operator+(n);
} WDate&
operator+=(int n) { Date::operator+=(n);
return *this;
} WDate&
operator++() {return *this += 1;} int getWeekDay() { return ymd2i() % 7;
}//0:Sunday,1:Monday,etc..int operator-(WDate&
wd)const { return ymd2i() - wd.ymd2i();
} friend ostream&
operator<<(ostream&
o,const WDate&
wd);
};
WDate(int n = 1) :Date(n) {} WDate(const Date&
d) :Date(d) {} WDate(const string&
s) :Date(s) {} WDate(int y,int m,int d) :Date(y,m,d) {} public:
三、Date.cpp
//Date.cpp
//Class Date with year-month-day Version
#include"Date.h"
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
const int Date::tians[] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
const int Y400 = 146097;//number of days of 400 years
const int Y100 = 36524;//number of days of 100 years
const int Y4 = 1461;//number of days of 4 years
Date::Date(const string&
s) {
}
void Date::i2ymd(int absDay) {
year = absDay / Y400 * 400;
int y = absDay%Y400;//被400年除得的天数 if (y == Y400 - 1) { month = 12,day = 30;
year = atoi(s.substr(0,4).c_str());
month = atoi(s.substr(5,2).c_str());
day = atoi(s.substr(8,2).c_str());
} } return;
year += y / Y100 * 100;
y %= Y100;
year += y / Y4 * 4;
y %= Y4;
if (y == Y4 - 1) { } year += y / 365;
y %= 365;
if (y == 0) { } year++;
bool leap = isLeapYear();
for (month = 1;
month<12 &&
y>tians[month] + (month >= 2 &&
leap);
month++);
day = y - tians[month - 1];
month = 12,day = 31;
return;
month = 12,day = 30;
return;
int Date::ymd2i()const {
}
ostream&
operator<<(ostream&
o,const Date&
d) {
return o <<
setfill("0") <<
setw(4) <<
d.year <<
"-"
<<
setw(2) <<
d.month <<
"-"
<<
}
setw(2) <<
d.day <<
setfill("
");
int yearDay = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
return yearDay + tians[month - 1] + (isLeapYear() &&
month >
2) + day;
四、WDate.cpp
//WDate.cpp
//Class WDate with year-month-day Version
#include"WDate.h"
#include<iostream>
using namespace std;
ostream&
operator<<(ostream&
o,const WDate&
wd) {
switch(wd.ymd2i() % 7) { case 0: return o <<
"Sun."
<<
Date(wd);
case 1: return o <<
"Mon."
<<
Date(wd);
case 2: return o <<
"Tue."
<<
Date(wd);
case 3: return o <<
"Wed."
<<
Date(wd);
case 4: return o <<
"Thr."
<<
Date(wd);
case 5: return o <<
"Fri."
<<
Date(wd);
case 6: return o <<
"Sat."
<<
Date(wd);
}
}
五、testmain.cpp //testmain.cpp
//using WDate class
#include "WDate.h"
#include <iostream>
using namespace std;
int main() {
WDate d1(2005,8,21);
WDate d2(2008,8,8);
cout <<
d1 <<
"\n";
cout <<
d2 <<
"\n";
}
【实验结果】
上页以上就是《【实验报告模板】C++实验报告》的范文全部内容,涉及到实验、对称、程序、时间、对象、定义、输出、结果等方面,希望对大家有用。