c++第三次作业
---恢复内容开始---
Part2 基于已有信息,补足并扩充程序。 在graph文件夹里提供有三个文件:
graph.h (类Graph的声明)
graph.cpp (类Graph的实现)
main.cpp (类Graph的测试: 定义Graph类对象,调用绘图接口绘制图形
graph.h、graph.cpp、main.cpp
1 #ifndef GRAPH_H 2 #define GRAPH_H 3 4 class Graph{ 5 public: 6 Graph(char ch,int n); 7 void draw(); 8 private: 9 char symbol; 10 int size; 11 }; 12 13 #endif
1 #include"graph.h" 2 #include<iostream> 3 using namespace std; 4 Graph::Graph(char ch,int n):symbol(ch),size(n){ 5 } 6 void Graph::draw(){ 7 for(int i=1;i<=size;i++){ 8 for(int j=1;j<=size-i;j++) 9 cout<<" "; 10 for(int j=1;j<=2*i-1;j++) 11 cout<<symbol; 12 //for(int j=1;j<=size-i;j++) 13 //cout<<" "; 14 cout<<endl; 15 } 16 }
1 #include <iostream> 2 #include "graph.h" 3 using namespace std; 4 int main() { 5 Graph graph1('*',5); 6 graph1.draw(); 7 system("pause"); 8 Graph graph2('$',7); 9 graph2.draw(); 10 return 0; 11 }
结果:

Part3 基于需求描述设计、定义并实现分数类Fraction,并编写代码完成测试。 具体要求如下: 设计一个分数类
Fraction描述分数(两个整数的比值)
fraction.h,fraction.cpp,main.cpp
#ifndef FRACTION_H #define FRACTION_H class Fraction{ public: Fraction(int x=0,int y=1):top(x),bottom(y){} Fraction(const Fraction &p):top(p.top),bottom(p.bottom){} void jia(Fraction &a,Fraction &b); void jian(Fraction &a,Fraction &b); void cheng(Fraction &a,Fraction &b); void chu(Fraction &a,Fraction &b); void bijiao(Fraction &a,Fraction &b); private: int top; int bottom; }; #endif
#include"fraction.h" #include<iostream> #include<cmath> using namespace std; void hua(int,int); void yue(int,int); void hua1(int,int); void yue(int a,int b){ if(a==0) cout<<"0"<<endl; else if(a==b) cout<<"1"<<endl; else{ for(int i=2;i<=fabs(a);i++) if(a%i==0&&b%i==0){ a/=i;b/=i; } if(b<0) hua(-a,-b); else hua1(a,b); //cout<<"("<<a<<"/"<<b<<")"<<endl; cout<<endl; } } void hua1(int a,int b){ if(a==0) cout<<"0"; else if(b==1) cout<<a; else if(a%b==0&&a>0) cout<<"1"; else if(a%b==0&&a<0) cout<<"-1"; else cout<<"("<<a<<"/"<<b<<")"; } void hua(int a,int b){ if(b==1) cout<<a; else cout<<"("<<a<<"/"<<b<<")"; } void Fraction::jia(Fraction &a,Fraction &b){ hua(a.top,a.bottom); cout<<"+"; hua(b.top,b.bottom); cout<<"="; int x=a.top*b.bottom+a.bottom*b.top,y=a.bottom*b.bottom; yue(x,y); } void Fraction::jian(Fraction &a,Fraction &b){ hua(a.top,a.bottom); cout<<"-"; hua(b.top,b.bottom); cout<<"="; int x=a.top*b.bottom-a.bottom*b.top,y=a.bottom*b.bottom; yue(x,y); } void Fraction::cheng(Fraction &a,Fraction &b){ hua(a.top,a.bottom); cout<<"*"; hua(b.top,b.bottom); cout<<"="; int x=a.top*b.top,y=a.bottom*b.bottom; yue(x,y); } void Fraction::chu(Fraction &a,Fraction &b){ if(b.top==0) cout<<"无 意义"<<endl; else{ hua(a.top,a.bottom); cout<<"/"; hua(b.top,b.bottom); cout<<"="; int x=a.top*b.bottom,y=a.bottom*b.top; yue(x,y); } } void Fraction::bijiao(Fraction &a,Fraction &b){ int x=a.top*b.bottom-a.bottom*b.top; hua(a.top,a.bottom); if(x>0) cout<<">"; else if(x==0) cout<<"="; else cout<<"<"; hua(b.top,b.bottom); cout<<endl; }
#include"fraction.h" #include<iostream> #include<stdlib.h> #include<stdio.h> using namespace std; void qu(int *n1,int *n2){ scanf("%d/%d",n1,n2); if(*n2==0) { cout<<"分 母不可为0!请重新输入:"<<endl; qu(n1,n2); } } void zidinyi(){ int n1=0,n2=1,m1=0,m2=1; cout<<"请 输入第一个数:";qu(&n1,&n2); cout<<"请 输入第二个数:";qu(&m1,&m2); Fraction a(n1,n2); Fraction b(m1,m2); Fraction t; cout<<"加 法:";t.jia(a,b); cout<<"减 法:";t.jian(a,b); cout<<"乘 法:";t.cheng(a,b); cout<<"除 法:";t.chu(a,b); cout<<"比 较:";t.bijiao(a,b); } void shili1(){ Fraction a(1,2); Fraction b(3); Fraction t; cout<<"事 例:a(1,2),b(3):"<<endl; cout<<"加 法:";t.jia(a,b); cout<<"减 法:";t.jian(a,b); cout<<"乘 法:";t.cheng(a,b); cout<<"除 法:";t.chu(a,b); cout<<"比 较:";t.bijiao(a,b); } void shili2(){ Fraction a(4,5); Fraction b=6; Fraction t; cout<<"事 例:a(3,4),b=5:"<<endl; cout<<"加 法:";t.jia(a,b); cout<<"减 法:";t.jian(a,b); cout<<"乘 法:";t.cheng(a,b); cout<<"除 法:";t.chu(a,b); cout<<"比 较:";t.bijiao(a,b); } int main(){ int n,k=0; cout<<"1为演示,2为自定义运算,3为退出系统"<<endl; while(k==0){ cout<<"请 输入指令:";cin>>n; switch(n){ case 1:{ int t,b=2,a=1; t=(rand()%(b-a+1))+ a; if(t==1) shili1(); else shili2(); break; } case 2:zidinyi();break; case 3:cout<<"谢 谢使用!"<<endl;k=1;break; default:cout<<"无 效指令,请重新输入!"<<endl; } cout<<endl; } }
结果:

可测试类型太多,不一一列举了。
感想:
1、莽代码的时候,Never Mind $ome Loose。
2、文字说明有没有必要这么写?
3、oj大整数除法,求大佬帮忙看看还有什么问题?
(除数为0时不知道该输出什么,暂定为0)
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 using namespace std; 5 void zsz(int a[],char a1[],int n);//转为数组 6 int gwc(int a[],int n1,int b[],int n2,int k);//做高位除 7 int jian(int a[],int b[],int na,int nb);//做减法 8 9 int jian(int a[],int b[],int na){ 10 for(int i=0;i<na;i++){ 11 int t=a[i]-b[i]; 12 if(t<0){ 13 a[i]=t+10; 14 a[i+1]--; 15 } 16 else a[i]=t; 17 } 18 if(a[na]==-1) return 0; 19 else return 1; 20 } 21 int gwc(int a[],int na,int b[],int N,int k){ 22 int n=N+k; 23 int c[100]={0}; 24 for(int i=0;i<n;i++) 25 c[i]=a[na-n+i]; 26 //for(int i=0;i<n;i++) 27 // cout<<c[i]; 28 //cout<<endl; 29 int s=0,z=1; 30 while(z==1){ 31 z=jian(c,b,n); 32 if(z==1) s++; 33 else{ 34 for(int i=0;i<n;i++){ 35 int t=c[i]+b[i]; 36 if(t>=10){ 37 c[i]=t-10; 38 c[i+1]++; 39 } 40 else c[i]=t; 41 } 42 for(int i=0;i<n;i++) 43 a[na-n+i]=c[i]; 44 } 45 } 46 return s; 47 } 48 void zsz(int a[],char a1[],int n){ 49 for(int i=0;i<n;i++) 50 a[i]=a1[n-i-1]-'0'; 51 } 52 int main(){ 53 int n; 54 scanf("%d\n",&n); 55 for(int j=0;j<n;j++){ 56 char stra[101]={"\0"},strb[101]={"\0"}; 57 int a[100]={0},b[100]={0},c[100]={0}; 58 gets(stra);gets(strb); 59 int na=strlen(stra),nb=strlen(strb); 60 zsz(a,stra,na);zsz(b,strb,nb); 61 for(;a[na-1]==0&&na;na--){} 62 for(;b[nb-1]==0&&nb;nb--){} 63 if(na>=nb&&na&&nb){ 64 int na1=na,na2=na,k=0; 65 while(na2>=nb){ 66 //cout<<na2-nb<<endl; 67 c[na2-nb]=gwc(a,na1,b,nb,k); 68 //cout<<c[na2-nb]<<endl; 69 if (c[na2-nb]!=0) { 70 na1=na1-k-1;k=0; 71 for(;a[na1]!=0;na1++){k++;} 72 } 73 else k+=1; 74 na2--; 75 } 76 int cn; 77 for(cn=na-nb;c[cn]==0;cn--){} 78 for(int i=cn;i>=0;i--) 79 cout<<c[i]; 80 cout<<endl; 81 } 82 else cout<<"0"<<endl; 83 } 84 return 0; 85 }
---恢复内容结束---

浙公网安备 33010602011771号