C课程设计求解有理数分式方程_第1页
C课程设计求解有理数分式方程_第2页
C课程设计求解有理数分式方程_第3页
C课程设计求解有理数分式方程_第4页
C课程设计求解有理数分式方程_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、湖南人文科技学院计算机系课程设计说明书 课 程 名 称: C+程序设计 课 程 代 码: 408025 题 目: 求解有理数分式方程 年级/专业/班:09级计算机科学与技术系软件工程专业2班 学 生 姓 名: 谷其飞、王武娟、贺红军、艾准、周玉香 学 号: 09436210、09436211、9436212 09436213、09436214 指 导 教 师: 袁辉勇 开 题 时 间: 2010 年 9 月 16 日完 成 时 间: 2010 年 9 月 26 日 目 录摘 要3一、引言2二、设计目的与任务21、本课程设计的目的22、本课程设计的任务2三、设计方案31、总体设计3、详细设计43

2、、程序清单54、程序调试与体会75、运行结果8四、结论8五、参考文献9摘 要 本课程设计的目的是通过定义分式类,在设计中我们采用多文件编程原理,利用#include命令来实现“文件包含”的操作,使程序更加清晰,明了。程序中我们将类的说明设计成.h头文件,类的实现、主函数分开在不同的.cpp源文件中,这样一来提高了程序代码的重用和编程效率。关键字:多文件编程、类、构造函数、继承、派生。Abstract This course is designed by defining point, line, area. Through the derived and to realize thinheri

3、ted other ways to reuse code for the triangle area. In the design, we adopt documents, include using the principle of programming commands to realize "#" operation documents contain more clearly, procedures, and clear. In the process, we will point, line, area, separated in different. CPP

4、source file, and each class has its header files, thus improving the program code reuse and programming efficiency.Key words:Many documents programming, kind, constructor, inheritance,derived.C+程序设计语言课程设计 求解有理数分式方程一、引言随着人们生活水平的提高,计算机发展异常迅速。如今,计算机已经深入到我们社会的各个领域,计算机的使用也已不再局限于科学计算,它已进入人类社会的各个领域并发挥着越来越重

5、要的作用。通过计算机对各类问题求解已经成为一种高效、快捷的方式。本课程设计就是用类的继承,派生等问题和实现代码重用以及函数的调用等方式来提高编程效率的。二、设计目的与任务1、本课程设计的目的1)通过课程设计更进一步理解C+的基础知识和面向对象的思想。2)训练用系统的观点和软件开发一般规范进行软件开发,并在此过程中培养严谨的科学态度和良好的工作作风。初步掌握软件开发过程的问题分析、系统设计、程序编码、测试等基本方法和技能。3)熟练掌握C+中类及类所具备的功能在程序中的应用,并熟练了解类中函数的调用。2、本课程设计的任务 一、定义分式类,其中包含分子和分母。 二、实现功能:求解一元一次有理数分式方

6、程三、要求: 1、设计菜单实现功能选择2、输入功能:输入有理数分式方程3、计算并输出方程的根,并用最简分式表示4、使用多文件方式设计:1)类的说明设计成.h头文件2)类的实现为一个.cpp文件3)主函数为另一个.cpp文件 三、设计方案1、总体设计1)设计多个头文件:conio.h;math.h;iostream.h等。2)定义类Fraction和FractionEquation ,对成员函数进行定义,并运用拷贝构造函数,析构函数,继承及派生。3)设计多个函数并进行调用实现求解一元一次有理数分式方程、详细设计2.1类接口设计void print();输出函数void printX(); 输出函

7、数,Fraction();无参构造函数Fraction(Fraction &other)拷贝构造函数virtual Fraction();析构函数void Simplify(); 分式简化运算bool IsInteger();判断分式是否表示一个整数(即分母是否为1)bool IsNegative();判断分式是否表示一个负数FractionEquation();无参构造函数FractionEquation(int a,int b,int c,int d,int e,int f);/含6个参数的构造函数virtual FractionEquation();析构函数void DrawWa

8、rn():输入参数函数;void DrawMenu()设计菜单实现功能选择;Fraction Count();计算方程的根函数;void print();输出方程函数;void printAnswer();输出方程的根函数;switch();多分支语句;booll();判断分式;2.2主函数。main() 调用main() 调用DrawMenu(); print();IsNegative();FractionEquation (a,b,c,d,e,f);等等。3、程序清单#include <conio.h>#include <iostream>using namespa

9、ce std;#include <math.h>/分式类,完成诸如(1/3+3/5这样的计算)class Fraction public:void print();/ 输出函数void printX();/ 输出函数,当分式作为参数时输出:/ 1/3应写作(1/3),即要加一个括号/ 1/1应不写/ -1/1应只写一个负号Fraction();/ 无参构造函数,必须保留Fraction(int n,int d);/ 包含两个参数的构造(分子,分母)Fraction(Fraction &other);/ 拷贝构造函数virtual Fraction();/ 析构函数void

10、Simplify();/ 分式简化运算,如将18/(-4)化为(-9)/2Fraction operator +(Fraction& other);/ 分式加法运算Fraction operator -(Fraction& other);/ 分式减法运算Fraction operator *(Fraction &other);/ 分式乘法运算Fraction operator /(Fraction &other);/ 分式除法运算bool IsInteger();/ 判断分式是否表示一个整数(即分母是否为1)bool IsNegative();/ 判断分式是否表

11、示一个负数private:int numerator;/分子int denominator; /分母;Fraction:Fraction()Fraction(0,1);Fraction:Fraction()Fraction:Fraction(int n, int d)numerator=n;denominator=d;Simplify();Fraction Fraction:operator +(Fraction &other)Fraction ret;ret.numerator=(this->numerator*other.denominator+this->denomi

12、nator*other.numerator);ret.denominator=this->denominator*other.denominator;ret.Simplify();return ret;Fraction Fraction:operator *(Fraction &other)Fraction ret;ret.numerator=this->numerator*other.numerator;ret.denominator=this->denominator*other.denominator;ret.Simplify();return ret;Frac

13、tion:Fraction(Fraction &other)this->numerator=other.numerator;this->denominator=other.denominator;void Fraction:Simplify()if(denominator<0)numerator=-numerator;denominator=-denominator;int a=abs(numerator),b=abs(denominator);while(a!=0&&b!=0) a<b?b-=a:a-=b; int k=(a=0) ? b :

14、a);numerator=numerator/k;denominator=denominator/k;Fraction Fraction:operator -(Fraction &other)Fraction ret;ret.numerator=(this->numerator*other.denominator-this->denominator*other.numerator);ret.denominator=this->denominator*other.denominator;ret.Simplify();return ret;Fraction Fractio

15、n:operator /(Fraction &other)Fraction ret;ret.numerator=this->numerator*other.denominator;ret.denominator=this->denominator*other.numerator;ret.Simplify();return ret;void Fraction:print()if(denominator!=1 && denominator!=-1)cout<<numerator<<"/"<<denomina

16、tor;else if(denominator=1)cout<<numerator;elsecout<<-numerator;bool Fraction:IsNegative()if (numerator<0)return true;elsereturn false;bool Fraction:IsInteger()if (denominator=1)return true;elsereturn false;void Fraction:printX()if(IsInteger()if(numerator=1)return;if(numerator=-1)cout&

17、lt;<"-"return;print();elsecout<<"("print();cout<<")"class FractionEquation public:FractionEquation();FractionEquation(int a,int b,int c,int d,int e,int f);/含6个参数的构造函数virtual FractionEquation();Fraction Count();/计算方程的根,结果为“分式”void print();/输出方程 Ax+B=Cvoid

18、printAnswer();/输出方程的根 x=根private:Fraction numA;/参数A的值(分式)Fraction numB;/参数B的值(分式)Fraction numC;/参数C的值(分式);FractionEquation:FractionEquation()FractionEquation:FractionEquation()FractionEquation:FractionEquation(int a, int b, int c, int d, int e, int f):numA(b,a),numB(d,c),numC(f,e)void FractionEquati

19、on:print()numA.printX();if (!(numB.IsNegative()/如果B是正数(即d/c>=0)cout<<"x+"elsecout<<"x"numB.print();cout<<"="numC.print();Fraction FractionEquation:Count()Fraction ret;ret=(numC-numB)/numA;return ret;void FractionEquation:printAnswer()Fraction ans=Co

20、unt();cout<<"x="ans.print();void DrawWarn()cout<<endl<<endl;cout<<"t"<<endl;cout<<"t 请先输入参数! "<<endl;cout<<"t"<<endl;void DrawMenu()system("cls");cout<<endl<<endl;cout<<"t&q

21、uot;<<endl;cout<<"t 请选择操作 "<<endl;cout<<"t"<<endl;cout<<"t 1.输入参数 "<<endl;cout<<"t"<<endl;cout<<"t 2.输出结果 "<<endl;cout<<"t"<<endl;cout<<"t 0.退出 "&

22、lt;<endl;cout<<"t"<<endl;main()int a,b,c,d,e,f;char ch;bool beContinue=true;/表示是否继续,用户选了“退出”,则为false,退出程序bool beInited=false;/表示方程是否被初始化,若没有被初始化,则不允许“输出结果”。while(beContinue)DrawMenu();while(1)ch=getch();if(ch<='3' && ch>='0')break;DrawMenu();swi

23、tch(ch)case '1':system("cls");cout<<endl<<endl<<endl;cout<<"t方程为: (b/a)x+d/c=f/e"<<endl;cout<<"t请依次输入a b c d e f:"<<endl;a=0;while(a=0)cout<<"ta="cin>>a;if(a=0)cout<<"t分母不能为0!请重新输入!"

24、<<endl;cout<<"tb="cin>>b;c=0;while(c=0)cout<<"tc="cin>>c;if(c=0)cout<<"t分母不能为0!请重新输入!"<<endl;cout<<"td="cin>>d;e=0;while(e=0)cout<<"te="cin>>e;if(e=0)cout<<"t分母不能为0!请重新输入!&q

25、uot;<<endl;cout<<"tf="cin>>f;beInited=true;break;case '2':system("cls");if(beInited)FractionEquation fa(a,b,c,d,e,f);cout<<endl<<endl<<endl;cout<<"t方程为:"<<endl<<"t "fa.print();cout<<endl;cout&

26、lt;<"t解得:"<<endl<<"t "fa.printAnswer();cout<<endl;cout<<"t"getch();elseDrawWarn();getch();break;case '0':beContinue=false;cout<<endl<<"t"break;return 1;4、程序调试与体会在程序调式过程最需要考虑到的就是调用顺序的表达,即如何知道程序是先调用什么函数后调用什么。于是我们首先将“xx函数的调用”用cout输出,作为一个调用的标记,这样程序是怎么调用的就一清二楚。但本设计中没有判断表达式的语法错误,固在调试切忌出现语法错误。5、运行结果在程序中依次输入分式(5/2)x+5/8=5/6的分子分母,运行结果如图:图三图四图五四、结论通过本次课程设计,让我对C+这门学科对有了进一步的认识,熟练掌握了类及类模板的应用。以前对该课程的恐惧感都以消失,任何事情没有做不到只有愿不愿意去做。刚开始接到该题,心中确实充满困惑。不过在图书馆和网上找了一些资料看了后,便觉得思路就在眼前了,之前不过就是由于对该课程的恐惧而产生了一定的心里影响而已。之后凭借那闪现在眼前的点点思路,慢慢在琢

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论