Created
October 24, 2014 17:44
-
-
Save masious/1357533f29c99f9c9731 to your computer and use it in GitHub Desktop.
q3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Node.h" | |
#include<iostream> | |
using namespace std; | |
Node::Node(int elem){ | |
data = elem; | |
isDataSet = true; | |
next = nullptr; | |
} | |
Node::Node(void){ | |
isDataSet = false; | |
next = nullptr; | |
} | |
Node::~Node(void){} | |
void Node::add(int elem){ | |
if(isDataSet) | |
get()->next = new Node(elem); | |
else{ | |
data = elem; | |
isDataSet = true; | |
} | |
} | |
void Node::add(Node * elem){ | |
if(isDataSet) | |
get()->next = elem; | |
else{ | |
data = elem->data; | |
isDataSet = true; | |
} | |
} | |
Node * Node::get(int i){ | |
Node * p = this; | |
int ii; | |
for(ii = 0 ; ii <= i && p->next ; ii++) | |
p = p->next; | |
if( ii != i ) | |
return 0; | |
return p; | |
} | |
Node * Node::get(){ | |
Node * p = this; | |
while(p->next) | |
p = p->next; | |
return p; | |
} | |
void Node::print(){ | |
if(!isDataSet){ | |
cout<<"list is empty"; | |
return ; | |
} | |
if(next) | |
next->print(); | |
if(data) | |
cout<<data; | |
else | |
cout<<"000"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class Node | |
{ | |
private: | |
bool isDataSet; | |
public: | |
int data; | |
Node * next; | |
// constructor | |
Node(int); | |
Node(void); | |
// adding elements to the end of the link list | |
void add(int); | |
void add(Node*); | |
// getting the i-th element | |
Node * get(int); | |
//getting the last element | |
Node * get(); | |
// printing the link list | |
void print(); | |
// destructor | |
~Node(void); | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<sstream> | |
#include "Node.h" | |
using namespace std; | |
Node nodify(string str){ | |
Node res; | |
for(int i=str.size()-3;i>-3;i-=3) | |
res.add(stoi(str.substr(max(0,i),i<0?i+3:3))); | |
return res; | |
} | |
Node sum(Node a,Node b){ | |
Node res,*p,*q; | |
p = &a; | |
q = &b; | |
bool hasCarry=false; | |
for( int i = 0 ; p || q ; p = p->next , q = q->next ){ | |
if(!p) | |
p = new Node(0); | |
if(!q) | |
q = new Node(0); | |
int s = p->data + q->data + hasCarry; | |
if(s>999){ | |
hasCarry = true; | |
s -= 1000; | |
} else | |
hasCarry = false; | |
res.add(s); | |
} | |
if(hasCarry) | |
res.add(1); | |
return res; | |
} | |
int main(){ | |
Node aFirst,bFirst,res; | |
string a,b; | |
cin>>a>>b; | |
aFirst = nodify(a); | |
bFirst = nodify(b); | |
res = sum(aFirst,bFirst); | |
res.print(); | |
cout<<endl; | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment