//Future advancements-> add save as file, in text or in a database option (for the sequences)
// add more series to this
//Programmer - Aditya Gupta
#include<iostream>
#include<stdlib.h>
#include<math.h>
//#include<fstream> //reqd when save support is added to this!
using namespace std;
struct Node{
int data;
Node* next;
};
void inline title(void){
system("cls");
cout<<"\tNAMASTE!...\n\nProgram-AP,GP Generator\nPrograammer - MR. ADITYA GUPTA (TEChY 3)\n\n";
}
class List{ //NOTE: I HAVE REMOVED ALL COMMENTS, REFER TO THE LINKED LIST TEMPLATE FOR DETAILED COMMENTS
//here only generator specific comments are there, and that too which are not in template
Node *top,*rear;
public:
void Push(int dat);
int isempty();
int search(int dat);
int noofelements();
void Pop(); //CAUTION- will decrease number of terms by 1 each time it runs, and pushing the last element again will be same as initial
void tPop(); //removes from start (head), just in case the user wants the first term to be changed, but then the whole AP will have to be changed
//(if he wants another number to be the first term)
//but in case he wants the 2nd one to become the first, just use Push() to add any number at last
//deleting in between a series doesnt actually make sense!!
void disp();
List(){ top=rear=NULL;
}
~List(){ //Q. WILL DESTRUCTOR REQUIRE DELETE STATEMENTS? (LIKE DELETE TOP; ETC. ?)
}
}AP,GP;
int main()
{
int a=0,cd=0,r=0,n,nterm=0,ch;
rechose:
title();
cout<<"List:\n1. AP\n2. GP\nWhat do you want to generate? (select corresponding numbers) :"; cin>>ch;
switch(ch){
case 1: cout<<"Type the first element :"; cin>>a; cout<<"\nType the common difference: "; cin>>cd;
cout<<"\nNumber of terms you want (as much as you want)...:"; cin>>n;
for(int i=1;i<=n;i++){ AP.Push(a+(i-1)*cd);
}
cout<<endl; AP.disp(); break;
default: cout<<"\nWrong choice, Chose again..."; goto rechose; break; //exit hi karna hoga to user khud band kar sakta hai!
case 2: cout<<"Type the first element :"; cin>>a; cout<<"\nType the common ratio(a2/a1): "; cin>>r;
cout<<"\nNumber of terms you want (as much as you want)...:"; cin>>n;
for(int i=1;i<=n;i++){ GP.Push(a*pow(r,(i-1)));
}
cout<<endl; GP.disp(); break;
}
return 0;
}
void List::Push(int dat){
Node *ptr=new Node;
ptr->next=rear;
ptr->data=dat;
rear->next=ptr;
}
void List::Pop(){
Node *temp=top; Node *temp2=new Node; //used temp2 since it will change only in the loop, 'before' temp changes, the loop stops at last element, so temp2 will still be the 2nd last element
while(temp->next!=rear) //loop stops when temp is representing last element, and temp2 representing the 2nd last
{
temp2=temp;
temp=temp->next;
}
rear=temp2;
temp2->next=rear;
temp->next=NULL;
delete temp; delete temp2; //last element deleted
//BUG NOTE- If a number is unnecesasarily being deleted, then it maybe due to deleting *temp2
}
void List::tPop(){
Node *temp=new Node;
temp=top;
top=temp->next;
temp->next=NULL;
delete temp;
}
int List::isempty(){ //can directly use noofelements() bcz in case of empty loop will not run, hence no time difference
if(top=rear) return 1;
else return 0;
}
int List::noofelements(){
Node *temp=new Node; temp=top; int count=0;
while(temp!=rear){ count++; temp=temp->next;
}; ++count; //compensating for not counting last element
delete temp;
return count;
}
int List::search(int dat){
Node *temp=top; int ret=0,count=0;
while(temp!=rear){ count++; if(temp->data==dat) ret=count; temp=temp->next;};
if(temp->data==dat) ret=count+1; //compensating for last element, since the loop cant check for that
//CAUTION- DONT DELETE TEMP, IT NOW REPRESENTS A NODE
return ret;
}
void List::disp(){
Node *temp=top; cout<<endl;
while(temp!=rear){ cout<<temp->data<<" ";
}; cout<<temp->data; //compensating for last element, since the loop cant check for that
delete temp;
}
// add more series to this
//Programmer - Aditya Gupta
#include<iostream>
#include<stdlib.h>
#include<math.h>
//#include<fstream> //reqd when save support is added to this!
using namespace std;
struct Node{
int data;
Node* next;
};
void inline title(void){
system("cls");
cout<<"\tNAMASTE!...\n\nProgram-AP,GP Generator\nPrograammer - MR. ADITYA GUPTA (TEChY 3)\n\n";
}
class List{ //NOTE: I HAVE REMOVED ALL COMMENTS, REFER TO THE LINKED LIST TEMPLATE FOR DETAILED COMMENTS
//here only generator specific comments are there, and that too which are not in template
Node *top,*rear;
public:
void Push(int dat);
int isempty();
int search(int dat);
int noofelements();
void Pop(); //CAUTION- will decrease number of terms by 1 each time it runs, and pushing the last element again will be same as initial
void tPop(); //removes from start (head), just in case the user wants the first term to be changed, but then the whole AP will have to be changed
//(if he wants another number to be the first term)
//but in case he wants the 2nd one to become the first, just use Push() to add any number at last
//deleting in between a series doesnt actually make sense!!
void disp();
List(){ top=rear=NULL;
}
~List(){ //Q. WILL DESTRUCTOR REQUIRE DELETE STATEMENTS? (LIKE DELETE TOP; ETC. ?)
}
}AP,GP;
int main()
{
int a=0,cd=0,r=0,n,nterm=0,ch;
rechose:
title();
cout<<"List:\n1. AP\n2. GP\nWhat do you want to generate? (select corresponding numbers) :"; cin>>ch;
switch(ch){
case 1: cout<<"Type the first element :"; cin>>a; cout<<"\nType the common difference: "; cin>>cd;
cout<<"\nNumber of terms you want (as much as you want)...:"; cin>>n;
for(int i=1;i<=n;i++){ AP.Push(a+(i-1)*cd);
}
cout<<endl; AP.disp(); break;
default: cout<<"\nWrong choice, Chose again..."; goto rechose; break; //exit hi karna hoga to user khud band kar sakta hai!
case 2: cout<<"Type the first element :"; cin>>a; cout<<"\nType the common ratio(a2/a1): "; cin>>r;
cout<<"\nNumber of terms you want (as much as you want)...:"; cin>>n;
for(int i=1;i<=n;i++){ GP.Push(a*pow(r,(i-1)));
}
cout<<endl; GP.disp(); break;
}
return 0;
}
void List::Push(int dat){
Node *ptr=new Node;
ptr->next=rear;
ptr->data=dat;
rear->next=ptr;
}
void List::Pop(){
Node *temp=top; Node *temp2=new Node; //used temp2 since it will change only in the loop, 'before' temp changes, the loop stops at last element, so temp2 will still be the 2nd last element
while(temp->next!=rear) //loop stops when temp is representing last element, and temp2 representing the 2nd last
{
temp2=temp;
temp=temp->next;
}
rear=temp2;
temp2->next=rear;
temp->next=NULL;
delete temp; delete temp2; //last element deleted
//BUG NOTE- If a number is unnecesasarily being deleted, then it maybe due to deleting *temp2
}
void List::tPop(){
Node *temp=new Node;
temp=top;
top=temp->next;
temp->next=NULL;
delete temp;
}
int List::isempty(){ //can directly use noofelements() bcz in case of empty loop will not run, hence no time difference
if(top=rear) return 1;
else return 0;
}
int List::noofelements(){
Node *temp=new Node; temp=top; int count=0;
while(temp!=rear){ count++; temp=temp->next;
}; ++count; //compensating for not counting last element
delete temp;
return count;
}
int List::search(int dat){
Node *temp=top; int ret=0,count=0;
while(temp!=rear){ count++; if(temp->data==dat) ret=count; temp=temp->next;};
if(temp->data==dat) ret=count+1; //compensating for last element, since the loop cant check for that
//CAUTION- DONT DELETE TEMP, IT NOW REPRESENTS A NODE
return ret;
}
void List::disp(){
Node *temp=top; cout<<endl;
while(temp!=rear){ cout<<temp->data<<" ";
}; cout<<temp->data; //compensating for last element, since the loop cant check for that
delete temp;
}
Comments
Post a Comment
You are free to leave any kind of suggestion, or improvement. It doesn't require you to sign in either.