问题1
某班需要从磁盘文件1.txt中读入若干同学的《面向对象程序设计》成绩求平均值,并把平均值 输出到屏幕上。现在要求设计一个File类完成此项工作。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include<iostream> #include <fstream> #include <string> #include<sstream> using namespace std; class File { private: double filename; public: File() {} File(double s) { filename = s; } double readfile(); }; double File::readfile() { string s; double num[100], k = 0, m = 0; ifstream fin; fin.open("grade.txt", ios::in); getline(fin, s); istringstream string_to_num(s); for (int i = 0; i <= 5; i++) { string_to_num >> num[i]; cout << num[i] << endl; m = m + num[i]; k = k + 1; } fin.close(); cout <<"平均成绩:" << m / k << endl; return 0; } int main() { File abc; abc.readfile();
}
|
问题2
在彩色图像中,每个像素都有红、绿、蓝三个分量,现要求设计一个Pixel类来描述像素: class Pixel { public: Pixel(){ red=green=blue=0;} Pixel(unsigned ,unsigned ,unsigned ); private: unsigned int red,green,blue; }; (1)完成构造函数,实现数据成员初始化; (3分) (2)重载运算符+,完成两个像素点对象的加法运算;(3分) (3)重载运算符= =,完成两个像素点对象的比较运算;(3分) (4)重载运算符>>,完成对像素的数据成员进行输入;(3分) (5)重载运算符<<,完成对像素的数据成员进行输出。(3分)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include<iostream> #include<string> #include<cmath> using namespace std; class Pixel { protected: unsigned int red, green, blue; public: Pixel() { red = 0; green = 0; blue = 0; } Pixel(unsigned int red, unsigned int green, unsigned int blue) :red(red), green(green), blue(blue) {} unsigned int operator + (Pixel& b) { return ((red + b.red) + (green + b.green) + (blue + b.blue)); } int operator == (Pixel& b) { if (red == b.red && green == b.green && blue == b.blue) { return 1; } return 0; } friend istream& operator >> (istream& c, Pixel& b); friend ostream& operator << (ostream& c, Pixel& b); }; istream& operator >> (istream& c, Pixel& b) { c >> b.red >> b.green >> b.blue; return c; } ostream& operator <<(ostream& c, Pixel& b) { c << b.red << "\n" << b.green << "\n" << b.blue; return c; } int main() { int r1, g1, b1; int r2, g2, b2;
Pixel p; cout << "请输入第一个像素点三个分量:" << endl; cin >> r1 >> g1 >> b1; Pixel a1(r1, g1, b1); cout << "请输入第二个像素点三个分量:" << endl; cin >> r2 >> g2 >> b2; Pixel a2(r2, g2, b2);
cout << "加法运算:" << endl; cout << a2 + a1 << endl;
cout << "比较运算" << endl; if (a1 == a2) { cout << "1" << endl; } else { cout << "0" << endl; }
cout << "输入运算a1" << endl; cin >> a1;
cout << "输出运算a1结果" << endl; cout << a1; return 0; }
|
问题3
在实际应用中,经常需要做一些统计工作,例如统计某班男生数量。请设计一个函数模板count,通过它能统计数组中等于某值的元素个数,并返回该值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include<iostream> #include<string> using namespace std; template <class T> void count(T*a,int n) { int counter = 0,num; cout << "请输入你要查询的数值:" << endl; cin >> num; for (int i = 0;i < n;i++) { if (a[i] == num) counter++; } cout <<"你查找的元素个数有" << counter << endl; delete[]a; } int main() { int n, counter; cout << "请输入数组元素个数:" << endl; cin >> n; int* a = new int[n]; cout << "请输入数组元素:" << endl; for (int i = 0;i < n;i++) { cin >> a[i]; } count(a,n); return 0; }
|
问题4
本题要求设计一个用于仿真自动售货机的仿真程序。在机器的初始状态建立后(即待售的物品、价格,以及初始的存货量),用户可以查看机器的当前状态,向机器投币,购买机器内的物品,得到物品和找零,假定自动售货机有3种待售物品。每种物品有四个属性:编号,物品名称,价格,数量。设计两个类(一个物品类Goods和一个机器类Machine)来实现这个仿真程序。 (1)设计物品类Goods,实现物品类数据成员的赋值和获取;(8分); (2)设计Machine类,完成机器状态初始化(4分),显示目前物品状态(4分),购买物品(4分)三个功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include<iostream> #include<string> using namespace std; static string name[100]{}; static int number[100]{}; static int coun[100]{}; static float price[100]{}; static int n; static int nn; class goods { public: void disinfo(); void getinfo() { cout << "你要填充的几个货物信息?" << endl; cin >> n; for (int i = 0;i <n;i++) { cout << "请输入第" << i + 1 << "个货物的编号、名称、价格、数量" << endl; cin >> number[i] >> name[i] >> price[i] >> coun[i]; } } }; void goods::disinfo() { cout << "\t***********************\t" << endl; cout << "\t******自动贩卖机*******\t" << endl; cout << "\t***********************\t" << endl; cout << "序号\t" << "商品名称\t" << "价格\t" << "数量\t" << endl; for (int i = 0;i < n;i++) { cout << number[i] << "\t" << name[i] << "\t\t" << price[i] << "\t" << coun[i] << "\t" << endl; }
} class machine :public goods { public: void cost1(); void cost2(); };
void machine::cost1() { int n1=0; cout << "请输入你要购买的物品序号:" << endl; cin >> nn; cout << "请输入买的货品数量:" << endl; cin >> n1; if (n1 > coun[nn - 1]) { cout << "货品数量不足!" << endl; system("pause"); } else { n1 = int(coun[nn-1]) - n1; cout << "货物已购买,还剩:" << n1 << "件" << endl; } } void machine::cost2() { float p1=0.0; cout << "请输入购买金额:" << endl; cin >> p1; if (p1 < price[nn - 1]) { cout << "输入金额数量不足!不可售卖!" << endl; system("pause"); } else { p1 = -(price[nn - 1] - p1); cout << "购买成功!找零:" << p1 << "元" << endl; } } int main() { goods go; machine m; go.getinfo(); go.disinfo(); m.cost1(); m.cost2(); return 0; }
|