Wednesday, June 22, 2016

File Management

code : how to read data from a file and write output in anther file


///Bis-millah-hir-rahmanir-rahim.
//arafat,sylhet engineering college,Bangladesh.

///****Love coding, love solving problems. --Coder shall be coder.******


#include<bits/stdc++.h>
using namespace std;

#define pi acos(-1)
#define nl endl
#define sp " "
#define msa 200010
#define pf printf
#define sc scanf
#define pb push_back
#define pob pop_back

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;


int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);

    FILE *f,*f2; ///tow pointer for read and write
    int a,b,c;
    f = fopen("file.txt","r"); ///you must create a file(any name, here file.txt)
    f2 = fopen("out.txt", "w"); /// create another file or it will create itself (just rename the file here)
    fscanf(f,"%d %d",&a,&b); ///fscanf is a function for reading data from file
    c=a+b;
    fprintf(f2,"%d\n",c); /// fprintf is a function for writing data to a file
    fclose(f); /// close the input file
    fclose(f2); /// close the output file



    return 0;
}

Wednesday, June 15, 2016

Different uses of string class plus

string name;
getline (cin, name);
// read a whole line into the string name(works like ase gets() )

//swaping
string s1,s2;
cin>>s1>>s2;
swap(s1,s2);

//we could convert the first character of a string to upper case with the following code

string s= "hello";
s[0] = toupper(s[0]);

//we could use that method to control a loop that allows us to convert all the characters to upper case

for (string::size_type i = 0; i < str.length(); i++)
{
    str[i] = toupper (str[i]);
}

// for lowercase we can use tolower() function


to find posintion of a character or substring

string s;
cin>>s;
int p= s.find('a'); ///to find first occurence of a character or substring
int q= s.rfind('a'); ///to find last occurence of a character or substring
cout<<p;

Different Uses of String class

C++ Standard Library: The string Class

The string class is part of the C++ standard library. A string represents a sequence of characters.
To use the string class, #include the header file:
    #include <string>

Constructors:

  • string ()
    - creates an empty string ("")
  • string ( other_string )
    - creates a string identical to other_string
  • string ( other_string, position, count )
    - creates a string that contains count characters from other_string, starting at position. If count is missing (only the first two arguments are given), all the characters from other_string, starting at position and going to the end of other_string, are included in the new string.
  • string ( count, character )
    - create a string containing character repeated count times
Examples:
 string s1;                //   s1 = ""                

 string s2( "abcdef" );    //   s2 = "abcdef"          

 string s3( s2 );          //   s3 = "abcdef"          

 string s4( s2, 1 );       //   s4 = "bcdef"           

 string s5( s2, 3, 2 );    //   s5 = "de"              

 string s6( 10, '-' );     //   s6 = "----------"      

      
The string class also has a destructor that takes care of freeing the memory storing the characters when the object is destroyed.

Constant Member Functions:

These functions do not modify the string.
  • const char * data ()
    - returns a C-style null-terminated string of characters representing the contents of the string
  • unsigned int length ()
    - returns the length of the string
  • unsigned int size ()
    - returns the length of the string (i.e., same as the length function)
  • bool empty ()
    - returns true if the string is empty, false otherwise

Operators Defined for string:

  • Assign =
    string s1;
    string s2;
    ...
    s1 = s2; // the contents of s2 is copied to s1
  • Append +=
    string s1( "abc" );
    string s2( "def" );
    ...
    s1 += s2; // s1 = "abcdef" now
  • Indexing []
    string s( "def" );
    char c = s[2]; // c = 'f' now
    s[0] = s[1]; // s = "eef" now
  • Concatenate +
    string s1( "abc" );
    string s2( "def" );
    string s3;
    ...
    s3 = s1 + s2; // s3 = "abcdef" now
  • Equality ==
    string s1( "abc" );
    string s2( "def" );
    string s3( "abc" );
    ...
    bool flag1 = ( s1 == s2 ); // flag1 = false now
    bool flag2 = ( s1 == s3 ); // flag2 = true now
  • Inequality !=
    - the inverse of equality
  • Comparison <, >, <=, >=
    - performs case-insensitive comparison
    string s1 = "abc";
    string s2 = "ABC";
    string s3 = "abcdef";
    ...
    bool flag1 = ( s1 < s2 ); // flag1 = false now
    bool flag2 = ( s2 < s3 ); // flag2 = true now

Member Functions:

  • void swap ( other_string )
    - swaps the contents of this string with the contents of other_string.
    string s1( "abc" );
    string s2( "def" );
    s1.swap( s2 ); // s1 = "def", s2 = "abc" now 

  • string & append ( other_string )
    - appends other_string to this string, and returns a reference to the result string.
  • string & insert ( position, other_string )
    - inserts other_string into this string at the given position, and returns a reference to the result string.
  • string & erase ( position, count )
    - removes count characters from this string, starting with the character at the given position. If count is ommitted (only one argument is given), the characters up to the end of the string are removed. If both position and count are omitted (no arguments are given), the string is cleared (it becomes the empty string). A reference to the result string is returned.
  • unsigned int find ( other_string, position )
    - finds other_string inside this string and returns its position. If position is given, the search starts there in this string, otherwise it starts at the beginning of this string.
  • string substr ( position, count )
    - returns the substring starting at position and of length count from this string

Friday, June 10, 2016

solution for n! in java using BigInteger Class

import java.util.Scanner;
import java.math.BigInteger;
class Main
{
// standard Java class name in UVa OJ
    public static void main(String[] args)
    {
        int n;
        Scanner sc= new Scanner(system.in);
        n=sc.nextInt;
        BigInteger fac = BigInteger.ONE;
        for (int i = 2; i <=n; i++)
            fac = fac.multiply(BigInteger.valueOf(i));
// it is in the library!
        System.out.println(fac);
    }
}

Monday, June 6, 2016

integer to string and string to integer

integer to string:
int a = 10;
stringstream ss;
ss << a;
string str = ss.str();

string to integer:
stringstream convert(p1);
convert>>v;
stringstream conver(p2);
conver>>v2;
cout<<v+v2<<nl;

Speedup Android Studio

Go to Help ->Edit Custom VM Options, and chnge this 4 setting. After that close your Android Studio. This settings are for 8gb of ram pc...