class Graph{ public: Graph(char x,int n); void draw(); private: char sign; int line; };
#include"Graph.h" #include<iostream> using namespace std; Graph::Graph(char x,int n):sign(x),line(n){} void Graph::draw() { int i,j,k,n; for(i=1;i<line+1;i++) { for(j=0;j<line-i;j++) cout<<" "; for(k=0;k<2*i-1;k++) cout<<sign; for(n=0;n<line-i;n++) cout<<" "; cout<<endl; } }
#include"Graph.h" #include<iostream> using namespace std; int main() { Graph graph1('*',5); Graph graph2('$',7); graph1.draw(); graph2.draw(); return 0; }
for循环一开始用的i=0;。。。。结果少了一行 后来跟着程序走了一遍 发现错误
#include<iostream> using namespace std; class Fraction{ public: Fraction(int t=0,int b=1):top(t),bottom(b){}; void output() { cout<<top<<"/"<<bottom<<endl; } void input(int t,int B) { top=t; bottom=B; } void add(const Fraction &f); void subtract(const Fraction &f); void multiply(const Fraction &f); void divide(const Fraction &f); void compare (const Fraction &f); private: int top; int bottom; };
#include"Fraction.h" #include <iostream> using namespace std; void Fraction::add(const Fraction &f1) { bottom=bottom*f1.bottom; top=top*f1.bottom+f1.top*bottom; } void Fraction::subtract(const Fraction &f1) { bottom=bottom*f1.bottom; top=top*f1.bottom-f1.top*bottom; } void Fraction::multiply(const Fraction &f1) { bottom=bottom*f1.bottom; top*=f1.top; } void Fraction::divide(const Fraction &f1) { bottom*=f1.top; top*=f1.bottom; } void Fraction::compare (const Fraction &f1) { double x; double y; x=double(top) /double (bottom) ; y=double (f1.top)/double (f1.bottom); if(x>y) cout<<top<<"/"<<bottom<<">"<<f1.top<<"/"<<f1.bottom; else if(x<y) cout<<top<<"/"<<bottom<<"<"<<f1.top<<"/"<<f1.bottom; else cout<<top<<"/"<<bottom<<"="<<f1.top<<"/"<<f1.bottom; }
#include"Fraction.h" #include<iostream> using namespace std; int main () { int t,y; cin>>t>>y; Fraction a; Fraction b(3,4); Fraction c(5); Fraction d; cout<<"input a fraction named d ="<<endl; d.input(t,y); d.output(); cout<<"a="; a.output(); cout<<"b="; b.output(); cout<<"b+c="; b.add(c); b.output(); cout<<"b-c="; b.subtract(c); b.output(); cout<<"b*c="; b.multiply(c); b.output(); cout<<"b/c="; b.output(); b.divide(c); b.output(); cout<<"compare b and c"<<endl; b.compare(c); return 0; }
一开始compare设了两个参数 f1 f2 本想输出的时候直接调用output可以减少代码的量 后来发现 对于第一个的输出f1.output 出了问题 改了很久 放弃了 老老实实瞧出来了
compare由于粗心 把f2.bottom 写成了bottom 找了好久好久 以为是if的问题 老脸泪两行