binary addition
来源:互联网 发布:怎样才能做淘宝客推广 编辑:程序博客网 时间:2024/06/05 13:23
/*============================================================================ Name : Exercise.cpp Author : Haier Version : 1.01 Copyright : Copyright (c) 2014 Description : binary addition in C++, Ansi-style, Compile by Eclipse ============================================================================*/#include <iostream>#include <stack>#include <stdio.h>using namespace std;stack<int> read(){ stack<int> MyStack; int Number,Bit,i; cout<<"Please input the Bit: "; cin>>Number; cout<<"Please input the Number of each Bit: "; for(i=0; i<Number; i++) { cin>>Bit; MyStack.push(Bit); } return MyStack;}void display(stack<int> &MyStack){ while(!MyStack.empty()) { cout<<" "<<MyStack.top(); MyStack.pop(); }}stack<int> add(stack<int> &stack1,stack<int> &stack2){ int PlusNumber=0,Addend=0; int Sum=0,Carry=0; stack<int> Mystack; while(!stack1.empty() || !stack2.empty()) { if(!stack1.empty()) { PlusNumber=stack1.top(); stack1.pop(); } if(!stack2.empty()) { Addend=stack2.top(); stack2.pop(); } Sum =(PlusNumber+Addend+Carry)%2; Carry=(PlusNumber+Addend+Carry)/2; Mystack.push(Sum); } if(Carry==1) { Mystack.push(Carry); } return Mystack;}int main(){stack<int> s1,s2,s3;int ch;cout<<"\n\t\t\t***MENU***\n";cout<<"\n1........Read first number" <<"\n2........Read second number" <<"\n3........Display addtion of two numbers" <<"\n4........Exit";do{cout<<"\nEnter your choice..: ";cin>>ch;switch(ch){case 1: fflush(stdin);s1=read();break;case 2: fflush(stdin);s2=read();break;case 3:cout<<"\nThe result of addition is :";s3=add(s1,s2);display(s3);break;}}while(ch!=4);return 0;}
0 0
- binary addition
- 【codewarns-8kyu】- Arguments to Binary addition
- Binary search with addition and subtraction
- Martian Addition
- ZOJ_1205_Martian Addition
- Strange Addition
- Simple Addition
- zoj1205Martian Addition
- Learning Addition
- NSArray+Addition
- NSDate+Addition
- NSDictionary+Addition
- NSString+Addition
- UIImage+Addition
- Range Addition
- Range Addition
- Range Addition
- 14.7.4 Addition operator
- 哈希表对字符串的高效处理3:实现一个听高级的字符匹配算法
- 学生通讯录 课程设计
- Deep clone object by serialization
- 职工综合信息管理系统
- 【Java编码准则】の #11不要使用Object.equals()来比较密钥值
- binary addition
- PAT1005. Spell It Right
- struts2 配备精要之defaultStack与paramsPrepareParamsStack
- 《UNIX环境高级编程》下的开发环境
- LINQ语句结合Lambda表达式使用
- Reverse Integer--反转整数
- cJSON学习笔记 续集
- 先写想法--基于安卓的所有软件特定功能的快捷键引用
- POJ 3140 Contestants Division(简单的树形dp)