#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
struct stab
{
char vname[10];
int val;
}s[20];
int i,j,k;
int display(int n)
{
cout<<"\nName\tValue\n";
for(i=0;i<n;i++)
{
cout<<s[i].vname<<"\t"<<s[i].val<<endl;
}
return 0;
}
int insert(int i)
{
if(i>=20)
return 0;
else
{
cout<<"Enter Variable Name :";
//gets(s[i].vname);
lab1: cin>>s[i].vname;
if(s[i].vname[0]>='0'&&s[i].vname[0]<='9')
{
cout<<"Invlaid valible name, Start with an Alphabet\n\t Enter Again: ";
goto lab1;
}
for(j=0;j<i;j++)
{
if(strcmp(s[i].vname,s[j].vname)==0)
{
cout<<"Varialble already exists\n\t Enter new: ";
goto lab1;
}
}
cout<<"Enter Value of "<<s[i].vname<<" =";
cin>>s[i].val;
return i+1;
}
}
int create(int n)
{
for(i=0;i<n;i++)
{
insert(i);
}
display(n);
return 0;
}
int search(char c[],int n)
{
int f=0;
for(i=0;i<n;i++)
{
if(strcmp(s[i].vname,c)==0)
{
f=1;break;
}
}
if(f==0)
return 0;
else
return i+1;
}
int modify(int i)
{
cout<<"Current Variable "<<s[i].vname<<" Value is "<<s[i].val<<endl;
return insert(i);
}
int main()
{
int n,ch;
while(1){
cout<<"Enter \n\t 1 to create\n\t 2 to insert\n\t 3 to Display\n\t 4 to Search\n\t 5 to Modify\n\t 0 to exit\n";
cin>>ch;
switch(ch)
{
case 0: return 0;
case 1:
{
cout<<"Enter No of Variables in table: ";
cin>>n;
create(n);break;
}
case 2:
{
int r=insert(i);
if(r==0)
cout<<"Table Full Can't Insert\n";
else
n=r;cout<<"Inserted Successfully\n";
break;
}
case 3:display(n);break;
case 4:
{
char c[20];
cout<<"Enter variable to be searched: ";
cin>>c;
int r=search(c,n);
if(r==0)
{
cout<<"Variable Not Present in table\n";
}
else
{
cout<<"Variable Found at Location "<<r<<endl;
}
break;
}
case 5:
{
char c[20];
cout<<"Enter the Variable to be Modified: ";
cin>>c;
int r=search(c,n);
if(r==0)
cout<<"Variable Not Present in table\n";
else
{
if(modify(r-1))
cout<<"Variable Modified Succesfully\n";
}
break;
}
}
}
}
#include<stdio.h>
#include<string.h>
using namespace std;
struct stab
{
char vname[10];
int val;
}s[20];
int i,j,k;
int display(int n)
{
cout<<"\nName\tValue\n";
for(i=0;i<n;i++)
{
cout<<s[i].vname<<"\t"<<s[i].val<<endl;
}
return 0;
}
int insert(int i)
{
if(i>=20)
return 0;
else
{
cout<<"Enter Variable Name :";
//gets(s[i].vname);
lab1: cin>>s[i].vname;
if(s[i].vname[0]>='0'&&s[i].vname[0]<='9')
{
cout<<"Invlaid valible name, Start with an Alphabet\n\t Enter Again: ";
goto lab1;
}
for(j=0;j<i;j++)
{
if(strcmp(s[i].vname,s[j].vname)==0)
{
cout<<"Varialble already exists\n\t Enter new: ";
goto lab1;
}
}
cout<<"Enter Value of "<<s[i].vname<<" =";
cin>>s[i].val;
return i+1;
}
}
int create(int n)
{
for(i=0;i<n;i++)
{
insert(i);
}
display(n);
return 0;
}
int search(char c[],int n)
{
int f=0;
for(i=0;i<n;i++)
{
if(strcmp(s[i].vname,c)==0)
{
f=1;break;
}
}
if(f==0)
return 0;
else
return i+1;
}
int modify(int i)
{
cout<<"Current Variable "<<s[i].vname<<" Value is "<<s[i].val<<endl;
return insert(i);
}
int main()
{
int n,ch;
while(1){
cout<<"Enter \n\t 1 to create\n\t 2 to insert\n\t 3 to Display\n\t 4 to Search\n\t 5 to Modify\n\t 0 to exit\n";
cin>>ch;
switch(ch)
{
case 0: return 0;
case 1:
{
cout<<"Enter No of Variables in table: ";
cin>>n;
create(n);break;
}
case 2:
{
int r=insert(i);
if(r==0)
cout<<"Table Full Can't Insert\n";
else
n=r;cout<<"Inserted Successfully\n";
break;
}
case 3:display(n);break;
case 4:
{
char c[20];
cout<<"Enter variable to be searched: ";
cin>>c;
int r=search(c,n);
if(r==0)
{
cout<<"Variable Not Present in table\n";
}
else
{
cout<<"Variable Found at Location "<<r<<endl;
}
break;
}
case 5:
{
char c[20];
cout<<"Enter the Variable to be Modified: ";
cin>>c;
int r=search(c,n);
if(r==0)
cout<<"Variable Not Present in table\n";
else
{
if(modify(r-1))
cout<<"Variable Modified Succesfully\n";
}
break;
}
}
}
}
No comments:
Post a Comment