简体   繁体   English

如何划分两个变量,直到在c ++中的循环内得到1的商

[英]how to divide two variables till get a quotient of 1 inside a loop in c++

I made a program which divides two variables suppose a & b, reads from a text file, but this on a loop coz I wanted to divide these variables 'till it got a quotient of 1, this only applicable for a number that is divisible, and if not divisible it would indicate/prints "Invalid". 我创建了一个程序,它将两个变量分开,假设a&b,从文本文件中读取,但这在一个循环上,因为我想将这些变量分开,直到它得到1的商,这只适用于可被整除的数字,如果不可分,则表示/打印“无效”。 So far this all Ive got for I am already stuck. 到目前为止,这一切都得到了我已经卡住了。 Hope someone could give me an advice how to do this. 希望有人能给我一个如何做到这一点的建议。

inside my textfile mytxt.txt. 在我的文本文件mytxt.txt中。 It has one space between two numbers every lines. 每行两个数字之间有一个空格。

27 3
40 4
1000 5
625 5

heres what ive got so far in my code 继我在我的代码中到目前为止所得到的内容

  #include <iostream>
  #include <fstream>
  int main() {

   std::ifstream file("mytxt.txt");
    if ( file.eof()) return 0;
     int a, b, cnt=0;

      while (!file.eof()) {
            file>>a>>b;
          loop: 
                 std::cout<<a << " ";
                 a/=b;
          if(a%b == cnt) 
                 goto loop;
                 std::cout<<"\n";
                    }
          file.close();
          system("pause");
}

output of this is 输出是

27 9 3
40
1000 200 40
625 125 25 5
Press any key to continue . . . 

but it should be like this 但它应该是这样的

27 9 3 1
Invalid
Invalid
625 125 25 5 1

it should have 3/3 to have 1 in the last. 它应该有3/3,最后有1。 What should I have in my code to do this and insert a condition that if its not divisible, it prints "Invalid". 我应该在我的代码中执行此操作并插入一个条件,如果它不可分,则打印“无效”。 thanks in advance. 提前致谢。 pls excuse my grammar. 请原谅我的语法。

You are printing a before you have divided it... And you don't identify when it's "invalid". 你在分割它之前打印a ......并且你没有确定它何时“无效”。 If you want to print only "Invalid", I would think that you need to store the values you got as divisors, and then print them only when the value gets to 1. 如果你只想打印“无效”,我认为你需要存储你得到的值作为除数,然后只有当值达到1时才打印它们。

And you should use while(file>>a>>b) instead of while(!file.eof()) , as you will run the loop once more than required if you use the latter. 你应该使用while(file>>a>>b)而不是while(!file.eof()) ,因为如果你使用后者,你将运行一次以上的循环。

The problem is that you divide a and THEN you check if it is still divisible...so, for example, you print "3", then a := 3/3=1 (which is not divisible by 3) and now your loop finishes without having printed "1"... 问题是你划分了一个然后你检查它是否仍然是可分的...所以,例如,你打印“3”,然后a:= 3/3 = 1(不能被3整除)现在你的循环完成而没有打印“1”...

However, I'd suggest you to use another 'while' loop instead 'goto'. 但是,我建议你使用另一个'while'循环而不是'goto'。

From your description: 从您的描述:

divide these variables 'till it got a quotient of 1 将这些变量分开,直到得到1的商

That means looping until a == 1 , not while a%b == 0 . 这意味着循环直到a == 1 ,而a%b == 0

if not divisible it would indicate/prints "Invalid" 如果不可分,则表示/打印“无效”

So check for divisibility with 所以检查可分性

if (a%b != 0) {
    // it's invalid
}

I would recommend using looping constructs like for and while , rather than hand-crafting your own spaghetti with goto , and also indenting the code to match its structure, to make the code easier for a human to follow. 我建议使用像forwhile这样的循环结构,而不是用goto手工制作你自己的意大利面,还要缩进代码以匹配它的结构,以使代码更易于人类遵循。

#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file("Input.txt");
    if (file.eof()) return 0;
    int a, b, c, cnt=0;
    while (!file.eof())
    {
        file>>a>>b;
        c = a;
        while(a > 1)
        {
            if (a % b) break;
            a /= b;
        }
        if (a == 1)
        {
            while(c >= 1)
            {
                std::cout << c << " ";
                c /= b;
            }
        }
        else
        {
            std::cout << "Invalid";
        }
        std::cout << std::endl;
    }
    file.close();
}

We use the first while loop to find if it is possible to reach 1. If it is not possible we print Invalid or we iterate till we get 1 and print all the intermediate results. 我们使用第一个while循环来查找是否可以达到1.如果不可能我们打印Invalid或我们迭代直到得到1并打印所有中间结果。

Output 产量

27 9 3 1 
Invalid
Invalid
625 125 25 5 1 

I think this code would do what you want (look at the notes below it for details): 我认为这段代码可以满足你的需求(详见下面的注释):

#include <iostream>
#include <fstream>
#include <math.h>

int main() {
    std::ifstream file("mytxt.txt");
    if (file.eof())
        return 0;

    int a, b;
    double tmp;

    while ((file >> a >> b)) {
        if( a == 0 || b == 0 )
            continue;                    

        tmp = log(a) / log(b);
        if (tmp != floor(tmp))
            std::cout << "Invalid";
        else {
            do {
                std::cout << a << " ";
            }while((a/=b) != 1);
            std::cout << "1";
        }
        std::cout << "\n";
    }
    file.close();
    system("pause");
}

First : if you want to check for invalid using the division loop, then you would have to save all values and then check if it reached 1 print them all, otherwise print Invalid. 第一 :如果你想使用除法循环检查无效,那么你必须保存所有值,然后检查它是否达到1打印全部,否则打印无效。

Second : ' First ' is considered a BAD approach, you could use log function instead, check if the logarithm of the a to the base b gives you a non-algebraic integer number then print Invalid. 第二个 :' 第一个 '被认为是一个坏的方法,你可以使用log函数,检查a到基数b的对数是否给你一个非代数整数然后打印无效。 Otherwise go to your division loop and print your number sequence, note that printing 1 is an already-known info that could be omitted. 否则,请转到您的分区循环并打印您的数字序列,请注意,打印1是已知的信息,可以省略。

Third : It worth to add a check on the reading, for example if your file has a new line at its end, you won't exit the loop. 第三 :值得添加对读数的检查,例如,如果你的文件在其末尾有一个新行,你将不会退出循环。 So, it is recommended that you put an IO check on the reading stream if(!(file >> a >> b)) ; 因此, if(!(file >> a >> b)) ,建议您对读取流进行IO检查; you even could make it the main loop check. 你甚至可以把它作为主循环检查。 Also, it is worth to add logical checks on a and b to avoid special values like 0 . 此外,值得在a和b上添加逻辑检查以避免像0这样的特殊值。

Fourth : Generally, using goto statement is considered harmful except in few cases. 第四 :一般来说,除少数情况外,使用goto语句被认为是有害的。 So, it is better to avoid it as you can. 所以,最好尽可能避免它。 Here are nice articles and discussions about it: 这里有很好的文章和讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM