Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int MAX = 1000;
- typedef int info_t;
- struct magacin{
- info_t M[MAX];
- int idx;
- void Init(){
- idx = -1;
- }
- void stackUnderFlow(){
- cout<<"Emptystack"<<endl;
- exit(0);
- }
- void stackOverFlow(){
- cout<<"FullStack"<<endl;
- exit(0);
- }
- bool isEmpty(){
- if(idx == -1){
- return true;
- }else return false;
- }
- bool isFull(){
- if(idx == MAX - 1){
- return true;
- }else return false;
- }
- int size(){
- return idx + 1;
- }
- info_t pop(){
- if(isEmpty()){
- stackUnderFlow();
- }
- int pom = M[idx];
- idx--;
- return pom;
- }
- info_t peek(){
- if(isEmpty()){
- stackUnderFlow();
- }
- return M[idx];
- }
- void push(info_t x){
- if(isFull()){
- stackOverFlow();
- }else{
- idx++;
- M[idx] = x;
- }
- }
- };
- void funk(magacin &m){
- magacin prevrten,kopija,rezultantna;
- prevrten.Init();
- rezultantna.Init();
- kopija.Init();
- while(!m.isEmpty()){
- int cifra = m.pop();
- prevrten.push(cifra);
- kopija.push(cifra);
- }
- while(!prevrten.isEmpty()){
- m.push(prevrten.pop());
- }
- while(!m.isEmpty()){
- int pom = m.pop();
- int pos = kopija.pop();
- int zbir = pom + pos;
- rezultantna.push(zbir > 9 ? 9 : zbir);
- }
- // nema potreba da proveruvame !kopija.isEmpty(), bidejki samo pecatime od rezultantna
- // treba da imame samo while(!rezultantna.isEmpty())
- while(!rezultantna.isEmpty() and !kopija.isEmpty()){
- cout<<rezultantna.pop();
- }
- }
- int main() {
- magacin s;
- int no, element, c;
- s.Init();
- cout << "Vnesete koj broj da se proveri:";
- cin >> no;
- while(no != 0){
- element = no%10;
- no /= 10;
- s.push(element);
- }
- funk(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement