1.draw函数算法:设函数为line,则第i行先输出line-1个空格,再输出i*2-1个字符,再输出line-1个空格。
//graph.h部分#pragma onceclass graph{public: graph(char ch, int l);//构造函数 void draw(); //制图private: char sign;int line;};
//graph.cpp部分#include#include"graph.h" //头文件using namespace std;graph::graph(char ch, int l) { //构造函数的实现 sign = ch; line = l;}void graph::draw() { //draw函数的实现 int i, j; for (i = 1; i <= line; i++) { //第i行 for (j = 1; j <= line - i;j++) cout << " "; //每行先打出line(行数)-1个空格 for (j = 1; j <= i * 2 - 1; j++) cout << sign; //再打出i*2-1个字符 for (j = 1; j <= line - 1; j++) cout << " "; //最后再打出line(行数)-1个空格 cout << endl; }}
//main.cpp部分#include#include"graph.h"using namespace std;int main() { char m; int n; while (1) { cout << "请输入图像的字符与尺寸:"; cin >> m >> n; graph g1(m, n); g1.draw(); cout << "请问您是否需要重新设置图像,是(y),否(n):"; //扩展部分,支持重新设置字符和尺寸 char c; cin >> c; if (c == 'n')break; if (c == 'y')continue; } return 0;}
#pragma onceclass Fraction {public: Fraction(); //构造函数 Fraction(int t, int b);//构造函数(函数重载) Fraction(int t); //构造函数(函数重载) void show(); //show函数 void add(Fraction &f1);//分数相加 void sub(Fraction &f1);//分数相减 void mul(Fraction &f1);//分数相乘 void div(Fraction &f1);//分数相除 void compare(Fraction f1, Fraction f2);//分数比较private: int top; int bottom; //top为分子,bottom为分母};
#include#include"Fraction.h"using namespace std;int sim(int t, int j) { //sim函数,功能为找出分子与分母的最大公约数(扩展部分) int a, b, i, m, c, n; a = t; b = j; if (a >= b)m = b; else m = a; for (i = 1; i <= m; i++) { if (a%i == 0 && b%i == 0)c = i; } return c; //返回值为最大公约数c}Fraction::Fraction() { //Fraction函数的实现 top = 0; bottom = 1;}Fraction::Fraction(int t, int b) { //Fraction函数的实现(函数重载) top = t; bottom = b;}Fraction::Fraction(int t) { //Fraction函数的实现(函数重载) top = t; bottom = 1;} void Fraction::add(Fraction &f1) { //分数加法add函数的实现 Fraction f2; f2.top = top * f1.bottom + f1.top*bottom; f2.bottom = bottom * f1.bottom; f2.show();} void Fraction::sub(Fraction &f1) { //分数减法sub函数的实现 Fraction f2; f2.top = top * f1.bottom - f1.top*bottom; f2.bottom = bottom * f1.bottom; f2.show(); } void Fraction::mul(Fraction &f1) { //分数乘法mul函数的实现 Fraction f2; f2.top =top*f1.top; f2.bottom =bottom*f1.bottom; f2.show(); } void Fraction::div(Fraction &f1) { //分数除法div函数的实现 Fraction f2; f2.top =top*f1.bottom; f2.bottom = bottom * f1.top; f2.show(); } void Fraction::show() { //show函数的实现 if (top > 0 && bottom > 0) { int c; c = sim(top, bottom); cout << top / c << "/" << bottom / c < 0) { int c; c = sim(-top, bottom); cout << top / c << "/" << bottom / c << endl;//分子分母同除以最大公约数得到最简形式(扩展) } if (top > 0 && bottom < 0) { int c; c = sim(top, -bottom); cout << -top / c << "/" << -bottom / c << endl;//将分母的负号移动至分子上(扩展) } if (top < 0 && bottom < 0) { int c; c = sim(-top, -bottom); cout << -top / c << "/" << -bottom / c << endl;//分子分母同为负数时去除负号 } if (top == 0) cout << top << "/" << bottom << endl; } void Fraction::compare(Fraction f1, Fraction f2) { //分数比较compare函数的实现 float a = float(f1.top) / float(f1.bottom); float b = float(f2.top) / float(f2.bottom); if (a <= b) { cout << f1.top << "/" << f1.bottom << "<="; f2.show(); cout << endl; } if (a > b) { cout << f1.top << "/" << f1.bottom << ">"; f2.show(); cout << endl; } }
#include#include"Fraction.h"using namespace std;int main() { Fraction f1; Fraction f2(5); Fraction f3(3, 6); Fraction f4(-5, 6); Fraction f5(3, -4); Fraction f6(-5, -6); f1.show(); f2.show(); f3.show(); f4.show(); f5.show(); f6.show();//不同形式的分数显示 f3.sub(f4); f3.add(f4); f5.mul(f6); f5.div(f6); //分数的加减乘除 f1.compare(f2, f4); //分数比较 return 0;}