This is my program which is used to input a Tree from keyboard. For example:
Code: Select all
if the tree is:
1
/
2
/ \
3 4
Then I can input :
(1 (2 (3 () () ) (4 () () ) ) () )
Code: Select all
#include <stdio.h>
int main(){
char c;
int left=0,right=0;
int v;
while(left>right || left==0){
c=getchar();
while(c!='(' && c!=')')
c=getchar();
if(c=='('){
left++;
putchar(c);
if(scanf("%d",&v))
printf("%d",v);
}
else{
right++;
putchar(c);
}
}
return 0;
}
Code: Select all
#include <iostream.h>
int main(){
char c;
int left=0,right=0;
int v;
while(left>right || left==0){
cin.get(c);
while(c!='(' && c!=')')
cin.get(c);
if(c=='('){
left++;
cout<<c;
if(cin>>v)
cout<<v;
}
else{
right++;
cout<<c;
}
}
return 0;
}