Saturday, January 26, 2013

Write a C++ program to MAke Simple calculator


Exercise 1)
Write a program and call it calc.cpp which is the basic calculator and receives three values from input via keyboard.
 The first value as an operator (Op1) should be a char type and one of (+, -, *, /, s) characters with the following meanings:
o ‘+’ for addition (num1 + num2)
o ‘-’ for subtraction (num1 - num2)
o ‘*’ for multiplication (num1 * num2)
o ‘/’ for division (num1 / num2)
o ‘s’ for swap
 Program should receive another two operands (Num1, Num2) which could be float or integer.
 The program should apply the first given operator (Op1) into the operands (Num1, Num2) and prints the relevant results with related messages in the screen.
 Swap operator exchanges the content (swap) of two variables, for this task you are not allowed to use any further variables (You should use just two variables to swap).

#include<iostream>
#include<cmath>

using namespace std;

int main()
{
//-------defining variables and initializing them-------------    
    double num1,num2;
    char operation,redo;
//--------Printing my name on screen----------------    
    cout<<"Welcome to the calculater program v.1.0 written by Your Name"<<endl;
    cout<<"***************************************************************"<<endl;
    cout<<endl<<endl<<endl;
//--here do loop is used so that the program can be used more then one time
//without exiting the run screen---------------------------    
    do
    {
 //----receiving the variables from input--------------         
    cout<<" Please enter an operation which you like to calculate (+,-,*,/,s)";
    cout<<"[s stands for swap]:";
    cin>>operation ;
    cout<<endl<<endl;
     cout<<" Please enter two numbers to apply your requested operation(";
    cout<<operation<<"):"<<endl<<"1st num:";
    cin>>num1;
    cout<<"2nd num:" ;
    cin>>num2;
    cout<<endl;
 //---used switch function so thet the operater can be decided------------  
    switch (operation)
    {
//------calculating the requested equation for inputs------------- 
//-------at the same time printing the results on screen-----------          
     case'+':            
             cout<<"The addition of two numbers ("<<num1<<","<<num2<<"):";
             cout<<num1+num2<<endl;
             break; 
     case'-':
             cout<<"The substraction of two numbers ("<<num1<<","<<num2<<"):";
             cout<<num1-num2<<endl;
             break;
      case'*':
             cout<<"The multiplication of two numbers ("<<num1<<","<<num2<<"):";
             cout<<num1*num2<<endl;
             break;
      case'/':
             cout<<"The division of two numbers ("<<num1<<","<<num2<<"):";
             if(num2==0)
             {
             cout<<"not valid"<<endl;
             }
             cout<<(num1/num2)<<endl;
             break;
      case's':
             cout<<"The swap of two numbers ("<<num1<<","<<num2<<"):";
             swap(num1,num2);
             cout<<"1stnumber="<<num1<<"and 2nd number="<<num2<<endl<<endl;                    
           break;
      default:
              cout<<"unknown command"<<endl;
      
           }
 //----now once again the program will ask the user if want to continue or not          
           cout<<"enter y or Y to continue:";
           cin>>redo;
           cout<<endl<<endl;
           }
           while(redo=='y'||redo=='Y');
           
   system("pause");
    return 0;
    
    }



C++ Calculator Program - Source Code





---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------



---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------




C++ Calculator Program
C++ Beginner Calculator Code 
C++ calculator - C++ - Source Code
Simple C++ Calculator - C++ Tutorials
Simple Calculator in C++ - Beginners
homework - Simple calculator program in C/C++ 
How to program a calcuator in C++
Problem with Calculator Program - C++ 
Write a C++ program that mimics a calculator
Calculator in C++ Language 
My Calculator

Build and Run Sample Code Using Log4Cpp from Source Code on Ubuntu

This Post will show How , to Build Log4cpp Source code and the Compile and run the Sample code as shown in this Simple Example Using Ubuntu . So Lets begin with Downloading the Log4cpp.

The download Source code is available on Sourceforge on this Download Link . So Download the Source and Extract it in your Home directory or wherever you want to extract  according to your choice . I have extracted the Log4cpp in my Home directory .

Now Follow the steps below.
  1.   $ cd log4cpp 
  2.  $ ./configure
  3.  $ make
  4.  $ sudo make check
  5.  $ sudo make install    
I think Up to this step there should not be any problem to built and install log4cpp source. 
Once the above steps are successfully executed then we are ready to Compile and run Sample Example . 
Make a C++ file called log4cpp.cpp . and copy and paste the Sample example as shown below .
// log4cpp.cpp

#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"

int main(int argc, char** argv) {
 log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
 appender1->setLayout(new log4cpp::BasicLayout());

 log4cpp::Appender *appender2 = new log4cpp::FileAppender("default", "program.log");
 appender2->setLayout(new log4cpp::BasicLayout());

 log4cpp::Category& root = log4cpp::Category::getRoot();
 root.setPriority(log4cpp::Priority::WARN);
 root.addAppender(appender1);

 log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
 sub1.addAppender(appender2);

 // use of functions for logging messages
 root.error("root error");
 root.info("root info");
 sub1.error("sub1 error");
 sub1.warn("sub1 warn");

 // printf-style for logging variables
 root.warn("%d + %d == %s ?", 1, 1, "two");

 // use of streams for logging messages
 root << log4cpp::Priority::ERROR << "Streamed root error";
 root << log4cpp::Priority::INFO << "Streamed root info";
 sub1 << log4cpp::Priority::ERROR << "Streamed sub1 error";
 sub1 << log4cpp::Priority::WARN << "Streamed sub1 warn";

 // or this way:
 root.errorStream() << "Another streamed error";

 return 0;
}
Now its time to compile the above code . SO compile the code with following command -
 $ g++ -gstabs+ -w -Iheaders log4cpp.cpp -o log4cpp_exe -llog4cpp -lpthread 
And the program will compile successfully but at the time or execution I found of example
  • $ ./log4cpp_exe
The Error will come like below 
  • ERROR : ./log4cpp_inited: error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file or directory
If the above error comes then what you need to do is to set you environment variable like shown in the following step .
  • export LD_LIBRARY_PATH=/usr/local/lib
To set the above environment variable follow the following steps
  • $ gedit ~/.bashrc
Then when the gedit editor for bashrc file will co to the bottom and paste

export LD_LIBRARY_PATH=/usr/local/lib

Then save the bash file and on terminal execute the following command 
  • $ source ~/.bashrc
Now your environment variable is set permanently.

Now once again Run the log4cpp_exe 
  • $ ./log4cpp_exe
and This time the program will run and show the following output.
1352973121 ERROR  : root error
1352973121 ERROR sub1 : sub1 error
1352973121 WARN sub1 : sub1 warn
1352973121 WARN  : 1 + 1 == two ?
1352973121 ERROR  : Streamed root error
1352973121 ERROR sub1 : Streamed sub1 error
1352973121 WARN sub1 : Streamed sub1 warn
1352973121 ERROR  : Another streamed error
If you see the above output on console that means your program run successfully

That's it
Build and Run Sample Code for Log4Cpp C++ library from Source Code on Ubuntu


---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
Newest 'log4cpp' Questions
c++ - getting started with log4cpp in ubuntu
C++ programming: A useful log4cpp example
log4cpp
Searches related to Log4Cpp
log4cpp tutorial
log4cxx
log4cpp category
log4cpp vs log4cxx
log4cpp appender
log4cpp documentation
log4cpp configuration file
log4cpp properties file example

Friday, January 18, 2013

How to compile C program in ubuntu using gcc

To compile your program using gcc compiler you need to download build essential library
so write on terminal

$sudo apt-get install build-essential

make a file called test.c

write a test C program


  • #include<stdio.h>
  • #include<stdlib.h>
  • int main()
  • {
  • printf("\nHello World\n\n");
  • return(0);
  • }

Save the program and go to the directory in which you have made your test.c


cd test.c

COMPILE 

gcc -Wall -W -Werror test.c -o test

RUN

./test

now your program is running.

ubuntu command to install zlib library


Downloading


wget http://www.zlib.net/zlib-1.2.3.tar.gz


Extracting files


tar -xvzf zlib-1.2.3.tar.gz

cd zlib-1.2.3

./configure --prefix=/usr/local/zlib

make

sudo make install

Ubuntu command to Install PCRE Library

# apt-get update
# apt-get install libpcre3 libpcre3-dev

Sunday, January 13, 2013

Extract file/files from a zip file using Java code


THIS IS A SIMPLE CODE TO EXTRACT FILES FROM A ZIP FILE ........



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
  public static void main(String[] argsthrows Exception{
    String destinationname = "d:\\";
    byte[] buf = new byte[1024];
    ZipInputStream zipinputstream = null;
    ZipEntry zipentry;
    zipinputstream = new ZipInputStream(new FileInputStream("fileName"));
    zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
      String entryName = zipentry.getName();
      FileOutputStream fileoutputstream;
      File newFile = new File(entryName);
      String directory = newFile.getParent();

      if (directory == null) {
        if (newFile.isDirectory())
          break;
      }
      fileoutputstream = new FileOutputStream(destinationname + entryName);
      int n;
      while ((n = zipinputstream.read(buf, 01024)) > -1){
        fileoutputstream.write(buf, 0, n);
      }
      fileoutputstream.close();
      zipinputstream.closeEntry();
      zipentry = zipinputstream.getNextEntry();
    }
    zipinputstream.close();
  }
}

Write Zip file using Java code


THIS IS A SIMPLE CODE TO WRITE A ZIP FILE ........


import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
  public static void main(String[] argsthrows Exception {
    FileOutputStream fos = new FileOutputStream("C:/MyZip.zip");
    ZipOutputStream zos = new ZipOutputStream(fos);
    ZipEntry ze = new ZipEntry("C:/file1.txt");
    zos.putNextEntry(ze);
    zos.closeEntry();
    ze = new ZipEntry("C:/file2.txt");
    zos.putNextEntry(ze);
    zos.closeEntry();
    zos.close();
  }
}

Create a zip file using Java


THIS IS A SIMPLE CODE TO cREATE A ZIP FILE ........


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
  public static void main(String[] argsthrows Exception {
    FileInputStream inStream = new FileInputStream("test.txt");
    ZipOutputStream outStream = new ZipOutputStream(new 
      FileOutputStream("compressed.zip"));

    outStream.putNextEntry(new ZipEntry("test.txt"));

    byte[] buffer = new byte[1024];
    int bytesRead;

    while ((bytesRead = inStream.read(buffer)) 0) {
      outStream.write(buffer, 0, bytesRead);
    }

    outStream.closeEntry();
    outStream.close();
    inStream.close();
  }

}

Read zip file using Java

This is a simple code to Read a zip file ........





import 
java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {

  public static void main(String[] argsthrows Exception {
    FileInputStream fis = new FileInputStream("C:/MyZip.zip");
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) {
      System.out.println(ze.getName());
      zis.closeEntry();
    }

    zis.close();

  }
}

Sunday, December 23, 2012

Use JDBC ODBC bridge to read from Excel

import java.sql.Connection;
import java.sql.DriverManager;

public class Main {
  public static void main(String[] argvthrows Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String myDB = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=c:/data.xls;"
        "DriverID=22;READONLY=false";
    Connection con = DriverManager.getConnection(myDB, """");
  }
}
IT Certification Category (English)640x480

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com


Top Online Courses From ProgrammingKnowledge

Python Course http://bit.ly/2vsuMaS
Java Coursehttp://bit.ly/2GEfQMf
Bash Coursehttp://bit.ly/2DBVF0C
Linux Coursehttp://bit.ly/2IXuil0
C Course http://bit.ly/2GQCiD1
C++ Coursehttp://bit.ly/2V4oEVJ
PHP Coursehttp://bit.ly/2XP71WH
Android Coursehttp://bit.ly/2UHih5H
C# Coursehttp://bit.ly/2Vr7HEl
JavaFx Coursehttp://bit.ly/2XMvZWA
NodeJs Coursehttp://bit.ly/2GPg7gA
Jenkins Course http://bit.ly/2Wd4l4W
Scala Coursehttp://bit.ly/2PysyA4
Bootstrap Coursehttp://bit.ly/2DFQ2yC
MongoDB Coursehttp://bit.ly/2LaCJfP
QT C++ GUI Coursehttp://bit.ly/2vwqHSZ