C++实验报告-1
实验题目1
【旱冰场围栏造价】有一个圆形的旱冰场地,围栏用帆布围成,每平方米造价M元,输入旱冰场半径和围栏高度,用程序计算出其造价。类的声明已给出,请完善成员函数的定义和主函数。
程序源代码
#include<iostream>
#include<cstring>
using namespace std;
const int M = 20;
const int N = 35;
const double p = 3.14;
class Circle
{
private:double radius;
public:void setRadius(double a);int area();int circumference();
};
void Circle::setRadius(double a)
{radius = a;
}
int Circle::area()
{double s;s = p * radius * radius;return s;
}
int Circle::circumference()
{double l;l = 2 * p * radius;return l;
}
int main()
{double r;cout << "请输入旱冰场的半径: ";cin >> r;Circle c;c.setRadius(r);cout << "旱冰场的造价为 " << M * c.area() + N * c.circumference() << " 元。" << endl;return 0;
}
实验题目2
【明天在哪里】输入今天的日期(年月日),计算明天的日期。要求进行输入日期合法性验证。
程序源代码
#include <iostream>
#include<cmath>
using namespace std;
class Time
{
public:void set_time();void show_time();void add_day();int add_month();int judge_month();void add_year();
private:bool is_time(int, int, int); int year;int month;int day;
};
void Time::add_year()
{year = year + 1;
}
int Time::add_month()
{month = month + 1;if (month > 12){month = 1;add_year();}return month;
}
int Time::judge_month()
{switch (month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:{if (day > 31){day = 1;add_month();}}break;case 2:{if (year % 4 == 0 && day > 29){day = 1;add_month();}else if (year % 4 != 0 && day > 28){day = 1;add_month();}}break;default:{if (day > 30){day = 1;add_month();}}}return day;
}
void Time::add_day()
{day = day + 1;judge_month();
}
void Time::set_time()
{char c1, c2;cout << "请输入时间(格式 年:月:日)";while (1){cin >> year >> c1 >> month >> c2 >> day;if (c1 != ':' || c2 != ':')cout << "格式不正确,请重新输入" << endl;else if (!is_time(year, month, day))cout << "时间非法,请重新输入" << endl;elsebreak;}
}
void Time::show_time()
{cout << year << ":" << month << ":" << day << endl;
}
bool Time::is_time(int y, int m, int d)
{switch (m){case 1:case 3:case 5:case 7:case 8:case 10:case 12:{if (y < 0 || y>2030 || m < 0 || m>12 || d < 0 || d>31)return false;}break;case 2:{if ((y < 0 || y>2030 || m < 0 || m>12 || d < 0 || d>29) && year % 4 == 0)return false;else if ((y < 0 || y>2030 || m < 0 || m>12 || d < 0 || d>28) && year % 4 != 0)return false;}break;default:if (y < 0 || y>2030 || m < 0 || m>12 || d < 0 || d>30)return false;}return true;
}
int main()
{Time t1;t1.set_time();t1.add_day();t1.show_time();return 0;
}
实验题目3
【图书】编写一个Book类,包含name(书名)、writer(著者)、public_name(出版社)、price(价格)、number(数量)、NO(书号)等数据成员。定义成员函数setBook,用来为书籍的数据成员赋值,定义成员函数borrow和restore,分别办理借出和还回(借、还书时,暂时只完成数量的增1减1,这里的书仍是一种书的概念,随着课程进展,我们继续做真正的图书管理系统,每本书都能追踪到是谁何时借了,何时还了)业务,定义成员函数print用于输出—有关书的信息,定义set_NO与get_NO来实现修改类对象的书号与获得类对象的书号。自己写main函数,用来测试你编写的类。
程序源代码san
#include<iostream>
#include<cstring>
using namespace std;
const int b1 = 1, b2 = 2;
int main();
int i,n = 10;
class Book
{
private:string name;string writer;string publicname;int price;int number;int no;
public:void setbook(string, string, string, int);int borrow();int restore();void print();void set_no(Book& t, Book& k);bool get_no(int);
};
void Book::setbook(string a, string b, string c, int p)
{name = a;writer = b;publicname = c;price = p;
}
void Book::set_no(Book& t, Book& k)
{cin >> number;if (number == b1){t.print();}if (number == b2){k.print();}
}
void Book::print()
{cout << "本书书名为:" << name << "\n";cout << "本书作者为:" << writer << "\n";cout << "本书出版社为:" << publicname << "\n";cout << "本书价格为:" << price << "元" << endl;
}
bool Book::get_no(int m)
{number = m;if (number == b1)return true;if (number == b2)return false;
}
int Book::borrow()
{int e,m;while (1){cout << "请输入借阅的数量:" << endl;while (1){cin >> m;if (m <= n)break;elsecout << "库存不足,请重新输入:" << endl;}n = n - m;cout << "借阅成功,库存剩余量为" << n << "本"<<"\n";cout << "是否继续借阅,是请输入1,否请输入2" << endl;cin >> e;if (e == 2)break;}cout << "继续查看图书或退出系统,前者请输入1,后者请输入2" << endl;cin >> i;if (i == 1)return main();
}
int Book::restore()
{int f;cout << "输入要还书的数量:" << endl;cin >> f;n = n + f;cout << "还书成功," << "该书的现有库存为:" << n << "本" << "\n";cout << "继续查看图书或退出系统,前者请输入1,后者请输入2" << endl;cin >> i;if (i == 1)return main();elseexit(0);
}int main()
{Book k1, k2, t;int d,q ,r, i, o;k1.setbook("平凡的世界", "路遥", "人民出版社", 25);k2.setbook("活着", "余华", "文化出版社", 20);cout << "请输入要查看的书号(1或2)" << endl;t.set_no(k1,k2);while (true){cout << "是否更改书号,是请输入1,否请输入2" << endl;cin >> i;if (i == 1){cout << "输入要更改的书号:" << endl;t.set_no(k1, k2);}else break;}cout << "借阅该图书请输入1,还书请输入2" << endl;cin >> q;if (q == 1){t.borrow();}else if (q == 2){t.restore();}return 0;
}