65 719
Assignments Done
99,2%
Successfully Done
In October 2018

Answer to Question #18453 in C++ for diganto007

Question #18453
Write a C++ program that simply add numbers (using stacks), the program reads in floating point numbers, push them onto a stack, add them together whenever a plus sign, "+", is entered, and print results
Expert's answer
/*Write a C++ program that simply add numbers (using stacks), the program reads in floating point numbers,
push them onto a stack, add them together whenever a plus sign, "+", is entered, and print results*/
# include <iostream>

using namespace std;

class stck{
int mas[20];
int pos;
public:
void init();
void push(int a);//push an element into stack
int get();//get an element out
int retPos();
};
void stck::init(){
pos=0;
for(int i=0;i<20;i++)
mas[i]=0;
}
int stck::retPos(){
return pos;
}
void stck::push(int a){
if (pos==20) cout<<"stack is overfulled!";
else {
mas[pos]=a;
pos++;}
}
int stck::get(){
if (pos==0) cout<<"stack is empty!";
else {
int ret=mas[pos-1];
pos--;
return ret;}
}
int main(){
stck a;
& a.init();
int ch(1);
cout<<"enter array\n";
cin>>ch;

& while (ch!=0){
&
& a.push(ch);
& cin>>ch;
& }
int pos = a.retPos();//amount of elements
int sum(0);//sum
for(int i=0;i<pos;i++){
& sum+=a.get();
}
cout<<"The sum is "<<sum<<"\n";

system("PAUSE");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions