实验三
2.
// 类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,k; for(int i=1;i<=size;i++){ for(int j=1;j<=size-i;j++) cout<<" "; for(int 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; }


3.
#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(b,c); cout<<"-:"<<endl; d.min(b,c); cout<<"*:"<<endl; d.mul(b,c); cout<<"/:"<<endl; d.div(b,c); cout<<"?:"<<endl; d.compare(b,c); return 0; }
#include "fraction.h" #include<iostream> using namespace std; void fraction::add(fraction a , fraction b){ fraction c; c.top=a.top*b.bottom+b.top*a.bottom; c.bottom=a.bottom*b.bottom; cout<<c.top<<"/"<<c.bottom<<endl; } void fraction::min(fraction a , fraction b){ fraction c; c.top=a.top*b.bottom-b.top*a.bottom; c.bottom=a.bottom*b.bottom; cout<<c.top<<"/"<<c.bottom<<endl; } void fraction::mul(fraction a , fraction b){ fraction c; c.top=a.top*b.top; c.bottom=a.bottom*b.bottom; cout<<c.top<<"/"<<c.bottom<<endl; } void fraction::div(fraction a , fraction b){ fraction c; c.top=a.top*b.bottom; c.bottom=a.bottom*b.top; cout<<c.top<<"/"<<c.bottom<<endl; } void fraction::compare(fraction a , fraction b){ int c1,c2; c1=a.top*b.bottom; c2=b.top*a.bottom; if (c1<c2) cout<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl; else if (c1>c2) cout<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl; else cout<<a.top<<"/"<<a.bottom<<"="<<b.top<<"/"<<b.bottom<<endl; } void fraction::show(){ cout<<top<<"/"<<bottom<<endl; }
#ifndef FRACTION_H #define FRACTION_H class fraction{ public: fraction(int i = 0 , int j = 1):top(i),bottom(j){} void add(fraction a,fraction b); void min(fraction a,fraction b); void mul(fraction a,fraction b); void div(fraction a,fraction b); void compare(fraction a,fraction b); void show(); private: int top; int bottom; }; #endif

1.编写程序不是很顺利,有很多没有搞懂,问了一下同学,参考了一下他们的程序,还是有一些不会的地方,会改正。
2.程序运行出现了很多错误,发现不出,知识方面欠缺很多,课后会找一些程序参考,发现自己的错误。

浙公网安备 33010602011771号