Sunday, 3 February 2013


Smart Calculator:

/**
* author Akhilesh Dhar Dubey
**/

#include<stdio.h>
#include<conio.h>
#include<STDLIB.H>
#include<STRING.H>
main(){
int operand1,operand2,i,j,count,calc,flag,ans,more_opr;
char x[20],opr,y[10],z[10];
do{
operand1=0;
operand2=0;
i=0;
j=0;
count=0;
calc=0;
flag=1;
ans=0;
fflush(stdin);
// clrscr();
printf("Enter:\n");
scanf("%[^\n]s",&x);
fflush(stdin);
// more_opr=0;

for(i=0;x[i]!='\0';){

for(j=0;x[i] =='0' || x[i] =='1' || x[i] =='2' || x[i] =='3' || x[i] =='4' || x[i] =='5' || x[i] =='6' || x[i] =='7' || x[i] =='8' || x[i] =='9';i++,j++){
strcpy((y+j),(x+i));//y[j]=x[i];
}
// strcpy(y,x); //we can write this also bcoz x contain value before any opr accur.
//printf("\t\t\t %d \t\t\t\n",atoi(y));
operand1=atoi(y);
printf("operand1 = %d\t",operand1);

if(x[i] =='+'){
opr=x[i];
i++;
more_opr++;
}
else if(x[i] =='-'){
opr=x[i];
i++;
more_opr++;
}
else if(x[i] =='*'){
opr=x[i];
i++;
more_opr++;
}
else if(x[i] =='/'){
opr=x[i];
i++;
more_opr++;
}

/* if(more_opr >=2){
printf("wrong operation performed...");
break;
}
*/
for(j=0;x[i] =='0' || x[i] =='1' || x[i] =='2' || x[i] =='3' || x[i] =='4' || x[i] =='5' || x[i] =='6' || x[i] =='7' || x[i] =='8' || x[i] =='9';i++,j++){
strcpy((z+j),(x+i));
}
//printf("\t\t\t %d \t\t\t\n",atoi(z));
operand2=atoi(z);
printf("operand2 = %d\t",operand2);

if(opr =='+'){
if(flag){
calc=operand1+operand2;
flag=0;
}
else
calc+=operand2;
printf("opr = %c\n",opr);
}
else if(opr =='-'){
if(flag){
calc=operand1-operand2;
flag=0;
}
else
calc-=operand2;
printf("opr = %c\n",opr);
}
else if(opr =='*'){
if(flag){
calc=operand1*operand2;
flag=0;
}
else
calc*=operand2;
printf("opr = %c\n",opr);
}
else if(opr =='/'){
if(flag){
calc=operand1/operand2;
flag=0;
}
else
calc/=operand2;
printf("opr = %c\n",opr);
}
}
printf("\ncalc = %d",calc);

printf("\n\nDo you want to calculate more:(1/0)");
scanf("%d",&ans);
}while(ans==1);
}

Moving Eyes:

import java.awt.*;
import java.applet.*;
/*
<applet code="MovingFace" width=300 height=50>
</applet>
 */

public class MovingFace extends Applet implements Runnable {

    Thread t = null;
    int state, i = 0;
    int X = 0, Y = 0;
    int UserX = 102, UserY = 102;
    boolean stopFlag;
// Set colors and initialize thread.
    public void init() {
        setBackground(Color.gray);
        setForeground(Color.red);
    }
// Start thread
    public void start() {
        X = Integer.parseInt(getParameter("Xcord"));
        Y = Integer.parseInt(getParameter("Ycord"));
        t = new Thread(this, "eyeThread");
        stopFlag = false;
        t.start();
    }
// Entry point for the thread that runs the banner.
    public void run() {
        for (;;) {
            try {
                for (i = 0; i < 20; i += 2) {
                    repaint();
                    Thread.sleep(200);
                }
                for (; i > 0; i -= 2) {
                    repaint();
                    Thread.sleep(200);
                }

                if (stopFlag) {
                    break;
                }
            } catch (InterruptedException e) {
            }
        }
    }
// Pause the banner.
    public void stop() {
        stopFlag = false;
        t = null;
    }
// Display the banner.
    public void paint(Graphics g) {

        g.setColor(Color.orange);
        g.fillOval(X + 0, Y + 0, 100, 100); //face

        g.setColor(Color.black);
        g.fillOval(X + 15 + i, Y + 30, 10, 10); //moving eyes
        g.fillOval(X + 55 + i, Y + 30, 10, 10); //moving eyes

        g.drawOval(X + 15, Y + 20, 30, 20); //eye left
        g.drawOval(X + 55, Y + 20, 30, 20); //eye right

        g.setColor(Color.blue);
        int xpoints[] = {X + 40, X + 60, X + 50}; // for nose
        int ypoints[] = {Y + 65, Y + 65, Y + 40};
        int num = 3;
        g.fillPolygon(xpoints, ypoints, num); // end nose

        g.setColor(Color.red);
        g.fillOval(X + 32, Y + 75, 35, 8); //mouth

        int xpoints1[] = {1, UserX - 1, UserX - 1, 1};
        int ypoints1[] = {1, 1, UserY - 1, UserY - 1};
        int num1 = 4;
        g.drawPolygon(xpoints1, ypoints1, num1);

        int xpoints2[] = {2, UserX - 2, UserX - 2, 2};
        int ypoints2[] = {2, 2, UserY - 2, UserY - 2};
        int num2 = 4;
        g.drawPolygon(xpoints2, ypoints2, num2);
    }
}

JAVA fall:

import java.awt.*;
import java.applet.*;
/*
<applet code="JavaFall" width=300 height=50>
</applet>
 */

public class JavaFall extends Applet implements Runnable {

    Thread t = null;
    String msg1, msg2, msg3, msg4;
    char ch;
    int UserX = 100, UserY = 250;
    int i = 0, j = 0, k = 0, k1 = 0, k2 = 0, k3 = 0, k4 = 0, count = 0;
    boolean stopFlag;
// Set colors and initialize thread.
    public void init() {
        msg1 = "J";
        msg2 = "A";
        msg3 = "V";
        msg4 = "A";
        setBackground(Color.gray);
        setForeground(Color.red);
        setFont(new Font("serif", Font.BOLD, 30));
    }
// Start thread
    public void start() {
        t = new Thread(this, "eyeThread");
        stopFlag = false;
        t.start();
    }
// Entry point for the thread that runs the banner.
    public void run() {
        /* for( ; ; )
        { */
        try {
            for (i = 0; i < 4; i++) {
                j += 50;
                for (; k < 100; k += 10) {
                    Thread.sleep(2000);
                    repaint();
                    count++;

                    if (stopFlag) {
                        break;
                    }

                    if (count == 1) {
                        k1 = 100;
                    }
                    if (count == 2) {
                        k2 = 100;
                    }
                    if (count == 3) {
                        k3 = 100;
                    }
                    if (count == 4) {
                        k4 = 100;
                    }
                /* repaint();
                Thread.sleep(200);
                k1=0;k2=0;k3=0;k4=0;
                 */
                }

            }
        } catch (InterruptedException e) {
        }
    /* }*/
    }
// Pause the banner.
    public void stop() {
        stopFlag = true;
        t = null;
    }
// Display the banner.
    public void paint(Graphics g) {
        g.drawString(msg1, k1 + (-60), 50);
        g.drawString(msg2, k2 + (-60), 100);
        g.drawString(msg3, k3 + (-60), 150);
        g.drawString(msg4, k4 + (-60), 200);
        int xpoints1[] = {1, UserX - 1, UserX - 1, 1};
        int ypoints1[] = {1, 1, UserY - 1, UserY - 1};
        int num1 = 4;
        g.drawPolygon(xpoints1, ypoints1, num1);

        int xpoints2[] = {2, UserX - 2, UserX - 2, 2};
        int ypoints2[] = {2, 2, UserY - 2, UserY - 2};
        int num2 = 4;
        g.drawPolygon(xpoints2, ypoints2, num2);
    }
}

Dictionary:

import java.io.*;
import java.sql.*;

/**
 *
 * @author Akhilesh
 */
public class NewDictionary {

    public static void main(String args[]) throws SQLException, ClassNotFoundException, IOException {

        Connection con = prepareConnection();
        Statement st = con.createStatement();
        String query = "select * from [Sheet1$]";
        ResultSet rs = st.executeQuery(query);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter Your Word Here:");
        String i = br.readLine();

        int count = 0;
        System.out.println("Word\tMeaning:\n");
        while (rs.next()) {
            if (i.equals(rs.getString("word"))) {
                System.out.println(i + "\t" + rs.getString("meaning") + "\t\n");
                count = 1;
            }
        }
        if (count == 0) {
            System.out.println("\nWord Does NOT matched in Dictionary\n");
        }
    }//main
    public static Connection prepareConnection() throws SQLException, ClassNotFoundException {

        String driverClassName = "sun.jdbc.odbc.JdbcOdbcDriver";
        String url = "jdbc:odbc:mydsn";
        Class.forName(driverClassName);
        return DriverManager.getConnection(url);
    }//prepareConnection
}//class

Copy One DB to Another DB:-

package com;

import java.io.*;
import java.sql.*;

/**
 *
 * @author Akhilesh
 */
public class DBCopy {

    public static void main(String args[]) throws SQLException, ClassNotFoundException, IOException {

//excel connection
        Connection conDb1 = prepareConnectionDB1();
        Statement stmtDb1 = conDb1.createStatement();
        String queryDb1 = "select * from [Sheet1$]";
        ResultSet rsDb1 = stmtDb1.executeQuery(queryDb1);

// mysql connection
        Connection conDb2 = prepareConnectionDB2();
        Statement stmtDb2 = conDb2.createStatement();
//String queryDb2a = "Create table data(word varchar(45),meaning varchar(300))";
//stmtDb2.executeUpdate(queryDb2a);
        ResultSet rsDb2 = null;

        System.out.println("table created");

        String queryDb2b = "select * from data";
        rsDb2 = stmtDb2.executeQuery(queryDb2b);

        System.out.println("Words\t\tMeanings");

        while (rsDb2.next()) {
            System.out.println(rsDb2.getString("word") + "\t" + rsDb2.getString("meaning"));
        }

        rsDb1.close();
        conDb1.close();
        rsDb2.close();
        conDb2.close();

    }//main

    public static Connection prepareConnectionDB1() throws SQLException, ClassNotFoundException {

        String driverClassName = "sun.jdbc.odbc.JdbcOdbcDriver";
        String url = "jdbc:odbc:mydsn";
        Class.forName(driverClassName);
        return DriverManager.getConnection(url);
    }//prepareConnectionDB1

    public static Connection prepareConnectionDB2() throws SQLException, ClassNotFoundException {

        String driverClassName = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/dictionary";
        String userName = "root";
        String password = "root";
        Class.forName(driverClassName);
        return DriverManager.getConnection(url, userName, password);
    }//prepareConnectionDB2
}//class

Dangling Pointer:

#include<iostream.h>
#include<conio.h>
int main()
{
char *dp = NULL;
{
char c='A';
dp = &c;
} 
/* c falls out of scope */ 
//cout<<"result="<<c;
/* dp is now a dangling pointer */
cout<<"result="<<dp;
getch();
}

Stringizing Operater and Token-passing Operater:

#include<stdio.h>
#include<conio.h>
#define c(i) printf("x"#i"=%d",x##i)
main()
{
int x3=5;
clrscr();
c(3);
getch();
}

// # stringizing operater.
// ## token-passing operater.