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";
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!
Comments
Leave a comment