使用switch语句的奇怪输出

时间:2016-05-14 15:25:46

标签: c++ class object switch-statement

我刚用一个类创建了一个程序,它允许你简单地用有理数来计算。因此,对于unkerstanding,我的代码是: (不要问为什么它的关系而不是理性我只是愚蠢的命名事物)

main.cpp中:

function rep(b1) {
    var s1 = "000000000000";
    var s2 = "111111111111";
    if (b1 >=1 && b1 <= 12) {
        return s2.slice(0, b1) + s1.slice(b1);
    } else {
        return "no value";
    }
}

console.log(rep(6)); // 111111000000
console.log(rep(8)); // 111111110000

Relational.h:

#include <iostream>
#include <stdlib.h>
#include "Relational.h"

using namespace std;

char Relational::lastOperation = 'z';

void main() {

Relational a(4, 5), b(3, 17), c;

c.inverse(a); c.outLast();
c.add(a, b); c.outLast();
c.sub(a, b); c.outLast();
c.mult(a, b); c.outLast();
c.div(a, b); c.outLast();

system("pause");
}

relational.cpp:

class Relational
{
private:
int numerator, denominator;

public:
Relational(int n, int d);
Relational(int n) :numerator(n), denominator(0) {}
Relational() : numerator(0), denominator(0) {}

void setNumerator(int n) { numerator = n; }
void setDenominator(int d) {
    if (d<0)
    {
        numerator = -numerator;
        denominator = -d;
    }
    else denominator = d;
}
int getNumerator() const { return numerator; }
int getDenominator() const { return denominator; }
void out() const { std::cout << "\tNumerator" << numerator << "\n\tDenominator " << denominator << std::endl; }
void outLast();
int inverse(Relational a);
int add(Relational a, Relational b);
int sub(Relational a, Relational b);
int mult(Relational a, Relational b);
int div(Relational a, Relational b);

private:

void shorten();

public:

static int anzahl;
static char lastOperation;

};

所以我遇到的问题是switch语句。 当我运行代码时,它应该如下所示:

#include <iostream> #include <stdlib.h> #include "Relational.h" using namespace std; Relational::Relational(int n, int d) { setNumerator(n); setDenominator(d); } int Relational::inverse(Relational a) { if (a.numerator != 0) { numerator = a.numerator; denominator = a.denominator; lastOperation = 'k'; return 1; } else return 0; } int Relational::add(Relational a, Relational b) { numerator = a.numerator*b.denominator + b.numerator*a.denominator; denominator = a.denominator*b.denominator; shorten(); lastOperation = 'a'; return 1; } int Relational::sub(Relational a, Relational b) { numerator = a.numerator*b.denominator - b.numerator*a.denominator; denominator = a.denominator*b.denominator; shorten(); lastOperation = 's'; return 1; } int Relational::mult(Relational a, Relational b) { numerator = a.numerator*b.numerator; denominator = a.denominator*b.denominator; shorten(); lastOperation = 'm'; return 1; } int Relational::div(Relational a, Relational b) { if (b.numerator != 0) { numerator = a.numerator*b.denominator; denominator = a.denominator*b.numerator; if (denominator<0) { numerator = -numerator; denominator = -denominator; } shorten(); lastOperation = 'd'; return 1; } else return 0; } void Relational::shorten() { int x, y, rest; x = numerator; y = denominator; if (x<0) x = -x; while (y>0) { rest = x%y; x = y; y = rest; } numerator = numerator / x; denominator = denominator / x; } void Relational::outLast() { switch (lastOperation) { case 'k': std::cout << "Inverse:" << "\n\tNumerator: " << numerator << "\n\tDenominator: " << denominator << std::endl; case 'a': std::cout << "Sum:" << "\n\tNumerator: " << numerator << "\n\tDenominator: " << denominator << std::endl; case 's': std::cout << "Difference:" << "\n\tNumerator: " << numerator << "\n\tDenominator: " << denominator << std::endl; case 'm': std::cout << "Product:" << "\n\tNumerator: " << numerator << "\n\tDenominator: " << denominator << std::endl; case 'd': std::cout << "Quotient:" << "\n\tNumerator: " << numerator << "\n\tDenominator: " << denominator << std::endl; default: out(); } }
相反它看起来像这样:

Inverse: Nominator: 5 Denominator: 4 Sum: Nominator: 83 Denominator: 85 Difference: Nominator: 53 Denominator: 85 Product: Nominator: 12 Denominator: 85 Quotient: Nominator: 68 Denominator: 15

我绝对不知道为什么它会给我这个输出 - 我非常感谢你的帮助:)。

0 个答案:

没有答案