慎铭 2021-04-21 08:08 采纳率: 100%
浏览 1487
已采纳

VS2019:引发了异常: 写入访问权限冲突。 **_Str** 是 0x1110112。

//Rational.h
#pragma once
#include<iostream>

class Rational
{
public:
	friend std::istream &operator>>(std::istream &input, Rational &rational);
	void friend addition(const Rational &rational1, const Rational &rational2);

	Rational(int numerator = 1, int denominator = 2);
	
	void setNumerator(int numerator);
	void setDenominator(int denominator);

	int getNumerator()const;
	int getDenominator()const;

	void printFloat();
	void printFraction();
private:
	struct
	{
		int numerator;
		int denominator;
	}number;
	inline void simplify();
};

 

 

//Rational.cpp
#include "Rational.h"
#include<stdexcept>
#include<cstdlib>
#include<cassert>

using namespace std;

std::istream& operator>>(std::istream& input, Rational& rational)
{
	char* strPtr = NULL;
	input >> strPtr;
	char** strPtr1_2 = NULL;
	assert(strPtr != NULL);
	rational.number.numerator = strtol(strPtr, strPtr1_2, 10);
	char** strPtr2_2 = strPtr1_2 + 1;
	rational.number.denominator = strtol(*strPtr2_2, NULL, 10);
	return input;
}

void addition(const Rational& rational1, const Rational& rational2)
{
	int numerator1 = rational1.number.numerator * rational2.number.denominator;
	int numerator2 = rational2.number.numerator * rational1.number.denominator;
	int numerator = numerator1 + numerator2;
	int denominator = rational1.number.denominator * rational2.number.denominator;
	Rational rational(numerator, denominator);
	rational.printFraction();
	
}

Rational::Rational(int numerator, int denominator)
{
	number.numerator = numerator;
	setDenominator(denominator);
}

void Rational::setNumerator(int numerator)
{
	number.numerator = numerator;
	simplify();
}

void Rational::setDenominator(int denominator)
{
	if (denominator == 0)
	{
		throw invalid_argument("The denominator can't be '0'");
	}
	else
	{
		number.denominator = denominator;
		simplify();
	}
}

int Rational::getNumerator() const
{
	return number.numerator;
}

int Rational::getDenominator() const
{
	return number.denominator;
}

void Rational::printFloat()
{
	float result = (float)number.numerator / number.denominator;
	cout << result << endl;
}

void Rational::printFraction()
{
	cout << number.numerator << '/' << number.denominator << endl;
}

inline void Rational::simplify()
{
	int a = number.denominator;
	int b = number.numerator;
	int c = a % b;
	while (c != 0)
	{
		a = b;
		b = c;
		c = a % b;
	}
	number.denominator /= b;
	number.numerator /= b;
}

 

 

//main.cpp
#include"Rational.h"

using namespace std;

int main()
{
	Rational rational1(3, 9);
	Rational rational2(1, 2);

	cout << "The numerator of rational1 is " << rational1.getNumerator() << endl;
	cout << "The denominator of rational1 is " << rational1.getDenominator() << endl;
	cout << "Print the rational1 in float form:";
	rational1.printFloat();
	cout << "Print the rational1 in fraction form:";
	rational1.printFraction();

	cout << "\n\n" << endl;

	cout << "The numerator of rational2 is " << rational2.getNumerator() << endl;
	cout << "The denominator of rational2 is " << rational2.getDenominator() << endl;
	cout << "Print the rational2 in float form:";
	rational2.printFloat();
	cout << "Print the rational2 in fraction form:";
	rational2.printFraction();

	cout << "\n\n" << endl;

	cout << "The result of that rational1 add rational2 is:";
	addition(rational1, rational2);

	cout << "\n\n" << endl;

	cin >> rational1;
	cout << "The numerator of rational1 is " << rational1.getNumerator() << endl;
	cout << "The denominator of rational1 is " << rational1.getDenominator() << endl;
	cout << "Print the rational1 in float form:";
	rational1.printFloat();
	cout << "Print the rational1 in fraction form:";
	rational1.printFraction();

	cin >> rational2;
	cout << "The numerator of rational2 is " << rational2.getNumerator() << endl;
	cout << "The denominator of rational2 is " << rational2.getDenominator() << endl;
	cout << "Print the rational2 in float form:";
	rational2.printFloat();
	cout << "Print the rational2 in fraction form:";
	rational2.printFraction();

	cout << "The result of that rational1 add rational2 is:";
	addition(rational1, rational2);
}
  • 写回答

5条回答 默认 最新

  • Eleven 2021-04-21 09:23
    关注

    char* strPtr = NULL;

    input >> strPtr;
    --------------------------
    空指针?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错