Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: C++ question :how to add space in string? and how to create array without value?

  1. #11
    Join Date
    Sep 2006
    Beans
    90

    Re: C++ question :how to add space in string? and how to create array without value?

    insert text here.
    Code:
    #include <bitset>
    #include <string>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
    
      bitset<1024> file1;
      bitset<1024> file2; //second text file
      string filename;
      char c;
    
      ifstream is;
    
      cout << "Enter the name data text file( usually data.txt): ";
      cin >> filename;
    
      is.open (filename.c_str());        // open file
    
      for(int i = 0; i != 1024; ++i)
      {
        is >> c;       // get character from file
        if(c == '1')
           file1.set(i);
      }
      
      is.close();
      //second text file
      
      cout << "Enter the name PN text file( usually PN.txt): ";
      cin >> filename;
    
      is.open(filename.c_str());        // open file
    
      for(int i = 0; i != 1024; ++i)
      {
        is >> c;       // get character from file
        if(c == '1')
          file2.set(i);
      }
    
      file1 |= file2;
      
      is.close();           // close file
    
      cout << file1;
    
      return 0;
    }
    You might try this out. It has the disadvantage of being more rigid, you will need to determine how many bits you want to read from each file. But it is easier to deal with the subsequent math and print from. It will also probably have a smaller memory footprint than a series of bool.
    Last edited by Jiraiya_sama; April 22nd, 2009 at 12:08 PM.

  2. #12
    Join Date
    Feb 2009
    Beans
    202

    Re: C++ question :how to add space in string? and how to create array without value?

    Quote Originally Posted by AlexenderReez View Post
    erk..my mistake..careless...ok then still cant multiply those two binary file...please help...see error comment in the code
    Well for one your push_backs need to be within while loops. Right now you push only last c you read. Rest are forgotten along the way.

  3. #13
    Join Date
    Nov 2006
    Beans
    1,134

    Re: C++ question :how to add space in string? and how to create array without value?

    This should multiply n1.txt with n2.txt and write the result to n3.txt. Both input numbers can be any length. Characters other than 0 and 1 will be ignored. It's quick and dirty but seems to work. I didn't bother cutting off leading zeros though.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    void read_number(std::string const &file, std::vector<bool> &num) {
            std::ifstream in(file.c_str());
            num.reserve(100);
            while (in.good()) {
                    int v = in.get();
                    if (v == (int)'0') num.push_back(false);
                    else if (v == (int)'1') num.push_back(true);
            }
    }
    
    void write_number(std::string const &file, std::vector<bool> const &num) {
            std::ofstream out(file.c_str());
            for (std::vector<bool>::const_iterator it = num.begin(); it != num.end(); it++) out << (*it ? '1' : '0');
            out << std::endl;
    }
    
    void multiply(std::vector<bool> const &num1, std::vector<bool> const &num2, std::vector<bool> &num3) {
            size_t s1 = num1.size();
            size_t s2 = num2.size();
            num3.resize(s1 + s2);
            for (size_t i1 = s1 - 1; i1 < s1; i1--) {
                    if (num1[i1]) {
                            size_t i3 = s2 + i1;
                            bool c = false;
                            for (size_t i2 = s2 - 1; i2 < s2; i2--) {
                                    bool o1 = num2[i2];
                                    bool o2 = num3[i3];
                                    num3[i3--] = o1 ^ o2 ^ c;
                                    c = o1 & o2;
                            }
                    }
            }
    }
    
    int main() {
            std::vector<bool> n1, n2, n3;
            read_number("n1.txt", n1);
            read_number("n2.txt", n2);
            multiply(n1, n2, n3);
            write_number("n3.txt", n3);
            return 0;
    }
    Example:
    Quote Originally Posted by n1.txt
    1100
    Quote Originally Posted by n2.txt
    1010
    Quote Originally Posted by n3.txt
    01111000
    Have fun!

  4. #14
    Join Date
    Jan 2007
    Location
    heaven , Malaysia
    Beans
    806

    Re: C++ question :how to add space in string? and how to create array without value?

    Quote Originally Posted by eye208 View Post
    This should multiply n1.txt with n2.txt and write the result to n3.txt. Both input numbers can be any length. Characters other than 0 and 1 will be ignored. It's quick and dirty but seems to work. I didn't bother cutting off leading zeros though.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    void read_number(std::string const &file, std::vector<bool> &num) {
            std::ifstream in(file.c_str());
            num.reserve(100);
            while (in.good()) {
                    int v = in.get();
                    if (v == (int)'0') num.push_back(false);
                    else if (v == (int)'1') num.push_back(true);
            }
    }
    
    void write_number(std::string const &file, std::vector<bool> const &num) {
            std::ofstream out(file.c_str());
            for (std::vector<bool>::const_iterator it = num.begin(); it != num.end(); it++) out << (*it ? '1' : '0');
            out << std::endl;
    }
    
    void multiply(std::vector<bool> const &num1, std::vector<bool> const &num2, std::vector<bool> &num3) {
            size_t s1 = num1.size();
            size_t s2 = num2.size();
            num3.resize(s1 + s2);
            for (size_t i1 = s1 - 1; i1 < s1; i1--) {
                    if (num1[i1]) {
                            size_t i3 = s2 + i1;
                            bool c = false;
                            for (size_t i2 = s2 - 1; i2 < s2; i2--) {
                                    bool o1 = num2[i2];
                                    bool o2 = num3[i3];
                                    num3[i3--] = o1 ^ o2 ^ c;
                                    c = o1 & o2;
                            }
                    }
            }
    }
    
    int main() {
            std::vector<bool> n1, n2, n3;
            read_number("n1.txt", n1);
            read_number("n2.txt", n2);
            multiply(n1, n2, n3);
            write_number("n3.txt", n3);
            return 0;
    }
    Example:



    Have fun!
    lets say

    a.txt = b.txt * c.txt * d.txt * e.txt;

    where e.txt contain data alternating the values of 1 and -1

    what should i modified ?
    もう誰かのためじゃなくて 自分のために笑っていいよ
    ~ Please do mark as resolved thread if your problem is solved,thanks~

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •