Transpose of A Sparse Matrix
Transpose of A Sparse Matrix
import java.io.*;
class Tsparse
{
public static void main(String args[])throws IOException
{
int m,n,i,j,t=0,k,l;
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
System.out.println("ENTER THE ORDER OF THE ARRAY :");
i=Integer.parseInt(br.readLine());
j=Integer.parseInt(br.readLine());
int a[][]=new int [10][20];
int b[][]=new int [10][20];
System.out.println("ENTER THE SPARSE REPRESENTATION OF THE MATRIX :");
for(k=0;k<i;k++)
{
for(l=0;l<j;l++)
{
a[k][l]=Integer.parseInt(br.readLine());
}
}
m=a[0][0]; n=a[0][1]; t=a[0][2];
b[0][0]=n; b[0][1]=m; b[0][2]=t;
System.out.println(" THE SPARSE REPRESENTATION OF THE MATRIX :");
for(k=0;k<i;k++)
{
for(l=0;l<j;l++)
{
System.out.print(" "+a[k][l]);
}
System.out.println();
}
if(t>0)
{
System.out.println("TRANSPOSE OF MATRIX : ");
int q=1;
for(int col=1;col<=n;col++)
{
for(int p=1;p<=t;p++)
{
if(a[p][1]==col)
{
b[q][0]=a[p][1];
b[q][1]=a[p][0];
b[q][2]=a[p][2];
q=q+1;
}
}
}
for(k=0;k<i;k++)
{
for(l=0;l<j;l++)
{
System.out.print(" "+b[k][l]);
}
System.out.println();
}
}
}
}
POLYNOMIAL ADDITION
import java.io.*;
class Link
{
int coef;
int exp;
Link next;
public Link(int c,int e)
{
coef=c;
exp=e;
}
public void displayLink()
{
System.out.print(coef+"x^"+exp);
}
}
class LinkList
{
public Link first;
public Link last;
public LinkList()
{
first=null;
last=null;
}
public void displaypoly()
{
Link current=first;
while(current!=null)
{
current.displayLink();
current=current.next;
if(current!=null)
System.out.print("+");
}
}
}
System.out.println("CURRENT STACK TOP:-"+a.peek());
break;
case 3:
System.out.println("THE ELEMENTS ARE :");
a.showStack();
break;
default:
System.out.println("WRONG CHOICE!!!!!");
}
System.out.println("DO YOU WANNA CONTINUE ??? (Y/N)");
c=(char)br.read();
}while(c=='y'||c=='Y');
}
}
System.out.print( peekN(j) );
System.out.print(' ');
}
System.out.println("");
}
}
class InToPost
{
private StackX theStack;
private String input;
private String output = "";
public InToPost(String in)
{
input = in;
int stackSize = input.length();
theStack = new StackX(stackSize);
}
public String doTrans()
{
for(int j=0; j<input.length(); j++)
{
char ch = input.charAt(j);
theStack.displayStack("For "+ch+" ");
switch(ch)
{
case '+':
case '-':
gotOper(ch, 1);
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
output = output + ch;
break;
}
}
while( !theStack.isEmpty() )
{
theStack.displayStack("While ");
output = output + theStack.pop();
}
theStack.displayStack("End ");
return output;
}
public void gotOper(char opThis, int prec1)
{
while( !theStack.isEmpty() )
{
char opTop = theStack.pop();
if( opTop == '(' )
{
theStack.push(opTop);
break;
}
else
{
int prec2;
if(opTop=='+' || opTop=='-')
prec2 = 1;
else
prec2 = 2;
if(prec2 < prec1)
{
theStack.push(opTop);
break;
}
else
output = output + opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char ch)
{
while( !theStack.isEmpty() )
{
char chx = theStack.pop();
if( chx == '(' )
break;
else
output = output + chx;
}
}
}
class StackXX
{
private int maxSize;
private int[] stackArray;
private int top;
public StackXX(int size)
{
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
public void push(int j)
{ stackArray[++top] = j; }
public int pop()
{ return stackArray[top--]; }
public int peek()
{ return stackArray[top]; }
public boolean isEmpty()
{ return (top == -1); }
public boolean isFull()
{ return (top == maxSize-1); }
public int size()
{ return top+1; }
public int peekN(int n)
{ return stackArray[n]; }
public void displayStack(String s)
{
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for(int j=0; j<size(); j++)
{
System.out.print( peekN(j) );
System.out.print(' ');
}
System.out.println("");
}
}
class ParsePost
{
private StackXX theStack;
private String input;
public ParsePost(String s)
{ input = s; }
public int doParse()
{
theStack = new StackXX(20);
char ch;
int j;
int num1, num2, interAns;
for(j=0; j<input.length(); j++)
{
ch = input.charAt(j);
theStack.displayStack(""+ch+" ");
if(ch >= '0' && ch <= '9')
theStack.push( (int)(ch-'0') );
else
{
num2 = theStack.pop();
num1 = theStack.pop();
switch(ch)
{
case '+':
interAns = num1 + num2;
break;
case '-':
interAns = num1 - num2;
break;
case '*':
interAns = num1 * num2;
break;
case '/':
interAns = num1 / num2;
break;
default:
interAns = 0;
}
theStack.push(interAns);
}
}
interAns = theStack.pop();
return interAns;
}
}
class InpostApp
{
public static void main(String[] args) throws IOException
{
String input1, output1;
int output2;
while(true)
{
System.out.print("Enter infix: ");
System.out.flush();
input1 = getString();
if( input1.equals("") )
break;
InToPost theTrans = new InToPost(input1);
output1 = theTrans.doTrans();
System.out.println("Postfix is " + output1 + '\n');
ParsePost aParser = new ParsePost(output1);
output2 = aParser.doParse();
System.out.println("Evaluates to " + output2);
if( output1.equals("") )
break;
}
}
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
}