求大神帮我改改c++程序,总是停止运行!

问题描述

求大神帮我改改c++程序,总是停止运行!

#include
using namespace std;
class Fraction
{
private:
int num,den;
void normalize();
int gcf(int a,int b);
int lcm(int a,int b);
public:
Fraction() {set(0,1);cout<<"construct"<<endl;}
Fraction(int n,int d){set(n,d);cout<<"construct"<<endl;}
Fraction(Fraction &src);
void set(int n,int d)
{ num=n;den=d;normalize();}
int get_num() {return num;}
int get_den() {return den;}
Fraction add(Fraction other);
Fraction mult(Fraction other);
};
Fraction::Fraction(Fraction &src){
cout<<"Now calling copy constructor."<<endl;
num=src.num;
den=src.den;
}

void Fraction::normalize(){
if(den==0||num==0){
num=0;
den=0;
}
if(den<0){
num*=-1;
den*=-1;
}
int n=gcf(num,den);
num=num/n;
den=den/n;
}
int Fraction::gcf(int a,int b){
if(b==0)
return abs(a);
else
return gcf(b,a%b);
}
int Fraction::lcm(int a,int b){
int n=gcf(a,b);
return a/n*b;
}
Fraction Fraction::add(Fraction other){
Fraction fract;
int lcd=lcm(den,other.den);
int quot1=lcd/den;
int quot2=lcd/other.den;
fract.set(num*quot1+other.num*quot2,lcd);
return fract;
}
Fraction Fraction::mult(Fraction other)
{
Fraction fract;
fract.set(num*other.num,den*other.den);
return fract;
}

int main()
{
Fraction f1(3,4);
Fraction f2(f1);
Fraction f3=f1.add(f2);
cout<<"The value is";
cout<<f3.get_num()<<"/";
cout<<f3.get_den()<<endl;
system("PAUSE");
return 0;
}

解决方案

void Fraction::normalize(){
if(den==0||num==0){
num=0; //这里把分子分母设置成了0,下面出现除0错误
den=0;
}
if(den<0){
num*=-1;
den*=-1;
}
int n=gcf(num,den);
num=num/n; //这里出现除0错误
den=den/n;
}

解决方案二:

都已经判断了分子或分母为0,了你不用else,后面还要参加运算,不是肯定会出错的么?

#include <iostream>
using namespace std;
class Fraction
{
private:
    int num, den;
    void normalize();
    int gcf(int a, int b);
    int lcm(int a, int b);
public:
    Fraction() { set(0, 1); cout << "construct" << endl; }
    Fraction(int n, int d){ set(n, d); cout << "construct" << endl; }
    Fraction(Fraction &src);
    void set(int n, int d)
    {
        num = n; den = d; normalize();
    }
    int get_num() { return num; }
    int get_den() { return den; }
    Fraction add(Fraction other);
    Fraction mult(Fraction other);
};
Fraction::Fraction(Fraction &src){
    cout << "Now calling copy constructor." << endl;
    num = src.num;
    den = src.den;
}
void Fraction::normalize(){
    if (den == 0 || num == 0){
        num = 0;
        den = 0;
    }
    else //都已经判断了分子或分母为0,了你不用else,后面还要参加运算,不是肯定会出错的么?
    {
        if (den < 0){
            num *= -1;
            den *= -1;
        }
        int n = gcf(num, den);
        num = num / n;
        den = den / n;
    }
}
int Fraction::gcf(int a, int b){
    if (b == 0)
        return abs(a);
    else
        return gcf(b, a%b);
}
int Fraction::lcm(int a, int b){
    int n = gcf(a, b);
    return a / n*b;
}
Fraction Fraction::add(Fraction other){
    Fraction fract;
    int lcd = lcm(den, other.den);
    int quot1 = lcd / den;
    int quot2 = lcd / other.den;
    fract.set(num*quot1 + other.num*quot2, lcd);
    return fract;
}
Fraction Fraction::mult(Fraction other)
{
    Fraction fract;
    fract.set(num*other.num, den*other.den);
    return fract;
}
int main()
{
    Fraction f1(3, 4);
    Fraction f2(f1);
    Fraction f3 = f1.add(f2);
    cout << "The value is";
    cout << f3.get_num() << "/";
    cout << f3.get_den() << endl;
    system("PAUSE");
    return 0;
}
时间: 2024-08-01 03:54:19

求大神帮我改改c++程序,总是停止运行!的相关文章

编程语言学习-求大神帮我看看这程序哪里有问题 运行的时候有时候会出现相同的票数

问题描述 求大神帮我看看这程序哪里有问题 运行的时候有时候会出现相同的票数 package com.homework; public class BuyTickets1 implements Runnable { static int tickets = 30; private String name; public BuyTickets1(String name) { this.name = name; } public void run() { for (int i = 1; i <= 35

求大神帮做一个小程序~~~

问题描述 要求:1.以往账单保存下来2.输出电表上上个月数据和本月数据,算出结果----------------求大神~~~~· 解决方案 解决方案二:需求一点也不明确,怎么帮你弄.解决方案三:就是这个,随便用什么做,我伯伯用,一度多少钱也要,做成个窗体,谢谢大神了解决方案四:大神呢?!!!!!!解决方案五:以往账单就这种说明--只能说增删改查解决方案六:标准CRUD请用gridview大法解决方案七:要保留以往账单数据的话,只能手动录入每月的账单数据了.然后就是各种textbox的取值增删改查

字符串处理-求大神帮解决如下程序,最基本的C语言字符串类型,不用编太难(如下为问题要求,测试用例,输出用例)

问题描述 求大神帮解决如下程序,最基本的C语言字符串类型,不用编太难(如下为问题要求,测试用例,输出用例) Background Given an m by n grid of letters and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid.

c++-求大神帮我把这机构化的C++程序改成C++面向对象的程序,有酬谢(单项选择题标准化考试系统)

问题描述 求大神帮我把这机构化的C++程序改成C++面向对象的程序,有酬谢(单项选择题标准化考试系统) #include<stdio.h>#include<stdlib.h>//应用动态存储分配函数//#include<time.h># define LEN sizeof(struct question)struct question{ char ask[200];//选择题题目// char answer[4][80];//选择题选项每个答案的长度// int rig

总是不出结果-求大神帮我看看 这c++程序问题出在哪里

问题描述 求大神帮我看看 这c++程序问题出在哪里 // BaseShape.h文件#include using namespace std; class BaseShape { public: BaseShape() {}; virtual ~BaseShape() {}; virtual void DrawShape() = 0; }; //*********************************//ShapeFactory.h文件 #include #include #inclu

求大神帮我解释几句程序,图书管理系统读者部分的。

问题描述 求大神帮我解释几句程序,图书管理系统读者部分的. int Reader::select_Library(Chaxun & se)//图书馆在册书查询{ MYSQL_RES * point; int bb[7]={14145141244}; string aa=""select book_name as '书名'ISBNwriter as '作者'publish as '出版社'publish_time as '出版日期' price as '价格'maxbook-nu

求大神帮小女子指导一下程序,遥控按键按下 1602 无法显示按键值

问题描述 求大神帮小女子指导一下程序,遥控按键按下 1602 无法显示按键值 /*----------------------------------------------- 名称:遥控器红外解码数液晶显示 ------------------------------------------------*/ #include #include #define uchar unsigned char #define uint unsigned int uchar i,a,b,c,d; uint

VS2012无法附加进程,求大神帮解决

问题描述 VS2012无法附加进程,求大神帮解决 解决方案 直接在vs里调试你的asp.net程序

新生 求大神帮帮忙!

问题描述 新生 求大神帮帮忙! 解决方案 求大神帮帮忙 解决方案二: 先看看你的数据库启动了没 解决方案三: 试试: 打开'程序'-'所有程序'-'Microsoft SQL Server 2012 '-'配置工具'-'SQL Server 配置管理器',在弹出的窗体中,找到'SQL Server 2012 网络配置',把'MSSQLSERVER的协议'下的"Named Pipes"和"TCP/IP"启动,然后重新启动Microsoft SQL Server 201