// 类graph的实现 #include "graph.h" #include <iostream> using namespace std; // 带参数的构造函数的实现 Graph::Graph(char ch, int n): symbol(ch), size(n) { } // 成员函数draw()的实现 // 功能:绘制size行,显示字符为symbol的指定图形样式 void Graph::draw() { int i,j,m,k; for(i=1;i<=5;i++){ for(j=1;j<=size-i;j++){ cout<<ends; } for(k=1;k<=2*i-1;k++){ cout<<symbol; } cout<<endl; } // 补足代码 // ... }
#ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 private: char symbol; int size; }; #endif
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1(‘*‘,5); graph1.draw(); system("pause"); system("cls"); Graph graph2(‘$‘,7); graph2.draw(); return 0; }
#ifndef FRACTION_H #define FRACTION_H class Fraction { public: Fraction(int top0=0,int bottom0=1); void polish(); void add(Fraction a,Fraction b); void minus(Fraction a,Fraction b); void multiply(Fraction a,Fraction b); void division(Fraction a,Fraction b); void compare(Fraction a,Fraction b); void show(); private: int top;//分子 int bottom;//分母 }; #endif
#include"Fraction.h" #include<iostream> #include<cmath> using namespace std; Fraction::Fraction(int top0,int bottom0):top(top0),bottom(bottom0){ } //讨论分母 void Fraction::show() { //分母为零 if(bottom==0) cout<<"分母为零,无意义"<<endl; else { if(bottom==1) cout<<" "<<top<<endl; else { cout<<top<<"/"<<bottom<<endl; } } } //最大公约数 int gongyue(int a,int b) { int t; if(a<b) { t=a; a=b; b=t; } while(t=a%b) { a=b; b=t; } return b; } //最小公倍数 int gongbei(int a,int b) { int t=gongyue(a,b); return a*b/t; } void Fraction::polish() { if(top>0&&bottom<0) { top=0-top; bottom=0-bottom; } else if(top<0&&bottom<0) { top=0-top; bottom=0-bottom; } else if(bottom==0) { cout<<"无意义"<<endl; } } //加法 void Fraction::add(Fraction a,Fraction b) { bottom=gongbei(a.bottom,b.bottom); top=(bottom/a.bottom)*a.top+(bottom/b.bottom)*b.top; polish(); show(); } //减法 void Fraction::minus(Fraction a,Fraction b) { bottom=gongbei(a.bottom,b.bottom); top=(bottom/a.bottom)*a.top-(bottom/b.bottom)*b.top; polish(); show(); } //乘法 void Fraction::multiply(Fraction a,Fraction b) { top=a.top*b.top; bottom=a.bottom*b.bottom; polish(); show(); } //除法 void Fraction::division(Fraction a,Fraction b) { top=a.top*b.bottom; bottom=a.bottom*b.top; polish(); show(); } //比较 void Fraction::compare(Fraction a,Fraction b) { double m,n; m=a.top/a.bottom; n=b.top/b.bottom; if(m>n) { cout<<"较大的数是"<<a.top<<"/"<<a.bottom; cout<<"较小的数是"<<b.top<<"/"<<b.bottom; } else if(m<n) { cout<<"较大的数是"<<b.top<<"/"<<b.bottom; cout<<"较小的数是"<<a.top<<"/"<<a.bottom; } else if(m=n) { cout<<"两个数相等"<<endl; } }
#include<iostream> #include"Fraction.h" using namespace std; int main() { //输入初始值 Fraction a; cout<<"a:"<<endl; a.show(); Fraction b(3,4); cout<<"b:"<<endl; b.show(); Fraction c(5); cout<<"c:"<<endl; c.show(); Fraction d; cout<<"+:"<<endl; d.add(a,b); cout<<"-:"<<endl; d.minus(a,c); cout<<"*:"<<endl; d.multiply(b,c); cout<<"/:"<<endl; d.division(c,b); cout<<"?:"<<endl; d.compare(b,c); return 0; }
1.一开始在第二个程序中,就符号前应该空几个空格并不是很清楚,可能也是因为没有梳理算法,没有思考完整就直接开始写代码,思路并不是很清晰只是凭借感觉在写,在乱改,
后来老师指出这个问题后重新想好并写好。
2.第二个程序在自己写的过程中,出现了许许多多的错误,从单词拼错这种小错误,到程序逻辑上错误这种严重的错误都有,就连一开始在创建项目时也出现一些错误,在自己反复查找
和舍友的提点下完成了该程序。
原文:https://www.cnblogs.com/agsjg/p/10747398.html