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
1
Expert's answer
2012-11-13T10:34:23-0500
/*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";
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments
Leave a comment