上次说到基本的创建栈,以及将两者结合:
#include<iostream>
#include<stdlib.h>
#include<map>
using namespace std;
//map的定义
map<int,char> map32f; //使用map结构创建32进制搜索表
map<int, string>::iterator iter;
void loading_map() { //当然是打表万岁啦
map32f.insert(pair<int,char>(0,'0'));
map32f.insert(pair<int,char>(1,'1'));
map32f.insert(pair<int,char>(2,'2'));
map32f.insert(pair<int,char>(3,'3'));
map32f.insert(pair<int,char>(4,'4'));
map32f.insert(pair<int,char>(5,'5'));
map32f.insert(pair<int,char>(6,'6'));
map32f.insert(pair<int,char>(7,'7'));
map32f.insert(pair<int,char>(8,'8'));
map32f.insert(pair<int,char>(9,'9'));
//十进制内
map32f.insert(pair<int,char>(10,'A'));
map32f.insert(pair<int,char>(11,'B'));
map32f.insert(pair<int,char>(12,'C'));
map32f.insert(pair<int,char>(13,'D'));
map32f.insert(pair<int,char>(14,'E'));
map32f.insert(pair<int,char>(15,'F'));
//十六进制区
map32f.insert(pair<int,char>(16,'G'));
map32f.insert(pair<int,char>(17,'H'));
map32f.insert(pair<int,char>(18,'J'));
map32f.insert(pair<int,char>(19,'K'));
map32f.insert(pair<int,char>(20,'L'));
map32f.insert(pair<int,char>(21,'M'));
map32f.insert(pair<int,char>(22,'N'));
map32f.insert(pair<int,char>(23,'P'));
map32f.insert(pair<int,char>(24,'Q'));
map32f.insert(pair<int,char>(25,'R'));
map32f.insert(pair<int,char>(26,'T'));
map32f.insert(pair<int,char>(27,'U'));
map32f.insert(pair<int,char>(28,'V'));
map32f.insert(pair<int,char>(29,'W'));
map32f.insert(pair<int,char>(30,'X'));
map32f.insert(pair<int,char>(31,'Y'));
//三十二进制区
}
typedef struct node {
char data; //用字符型表示进制数更好表示高进制的
struct node *next;
} Node;
typedef struct stack {
Node *top;
int count;
} Link_Stack;
//创建栈
Link_Stack *Creat_stack() {
Link_Stack *p;
p= new Link_Stack;
p->count=0;
p->top=NULL;
return p;
}
//入栈 push
Link_Stack *Push_stack(Link_Stack *p,char elem) {
if(p==NULL)
return NULL;
Node *temp;
temp = new Node;
temp->data=elem;
temp->next=p->top;
p->top=temp;
p->count++;
return p;
}
//出栈 pop
Link_Stack *Pop_stack(Link_Stack *p) {
Node *temp;
temp = p->top;
if(p->top==NULL) {
cout<<"Error:The stack is empty."<<endl;
return p;
} else {
p->top=p->top->next;
delete temp;
p->count--;
return p;
}
}
//遍历栈:输出栈中所有元素
int show_stack(Link_Stack *p) {
Node *temp;
temp=p->top;
if(p->top==NULL) {
cout<<"Error: The stack is empty."<<endl;
return 0;
}
while(temp!=NULL) {
cout<<temp->data;
temp=temp->next;
}
cout<<endl;
return 0;
}
int main() {
Link_Stack *p;
p=Creat_stack();
loading_map();
int orign,n,i=0; //orign原数,n需要转换的进制数,i循环控制
cin>>orign>>n;
while(orign>0) {
Push_stack(p,map32f[orign%n]) ;
orign=orign/n;
i++;
}
show_stack(p);
//好像没有用到pop出栈操作//
return 0;
}
/*
32进制表
0-9,
A-10/B-11/C-12/D-13/E-14/F-15
G-16/H-17/J-18/K-19/L-20/M-21
N-22/P-23/Q-24/R-25/T-26/U-27
V-28/W-29/X-30/Y-31
注:
① 26个字母中去除【I、O、S、Z】
② 32进制可作为日期的表示
*/
/*
16进制表
0~9,
A-10/B-11/C-12/D-13/E-14/F-15
*/
/*
再高位就需要调用z~Z的区别了
并且加上@#*之类的符号表示
再在高位一些 就需要特殊定义了
*/注:<bits/stdc++.h>万能头文件,其意思是包含所有C/C++需要得头文件,后面我们为了显得标准所以手动使用了<iostream><stdlib.h><map>三个头文件,其中<map>是map的STL库调用必备的头文件,没有他就无法用map打表,其余均为常用的头文件。

十进制的1000转换为32进制结果为Y8,一切正常!
到此,这个基本上就结束了,但是STL强大之处还没有说完,我们现在使用的栈,也被STL库包含进去了,所以,我们这个代码又可以精简为此:
代码5:
#include<bits/stdc++.h>
using namespace std;
map<int,char> map32f; //使用map结构创建32进制搜索表
map<int,char>::iterator iter;
stack<char> s; //创建一个栈
void loading_map() { //当然是打表万岁啦
map32f.insert(pair<int,char>(0,'0'));
map32f.insert(pair<int,char>(1,'1'));
map32f.insert(pair<int,char>(2,'2'));
map32f.insert(pair<int,char>(3,'3'));
map32f.insert(pair<int,char>(4,'4'));
map32f.insert(pair<int,char>(5,'5'));
map32f.insert(pair<int,char>(6,'6'));
map32f.insert(pair<int,char>(7,'7'));
map32f.insert(pair<int,char>(8,'8'));
map32f.insert(pair<int,char>(9,'9'));
//十进制内
map32f.insert(pair<int,char>(10,'A'));
map32f.insert(pair<int,char>(11,'B'));
map32f.insert(pair<int,char>(12,'C'));
map32f.insert(pair<int,char>(13,'D'));
map32f.insert(pair<int,char>(14,'E'));
map32f.insert(pair<int,char>(15,'F'));
//十六进制区
map32f.insert(pair<int,char>(16,'G'));
map32f.insert(pair<int,char>(17,'H'));
map32f.insert(pair<int,char>(18,'J'));
map32f.insert(pair<int,char>(19,'K'));
map32f.insert(pair<int,char>(20,'L'));
map32f.insert(pair<int,char>(21,'M'));
map32f.insert(pair<int,char>(22,'N'));
map32f.insert(pair<int,char>(23,'P'));
map32f.insert(pair<int,char>(24,'Q'));
map32f.insert(pair<int,char>(25,'R'));
map32f.insert(pair<int,char>(26,'T'));
map32f.insert(pair<int,char>(27,'U'));
map32f.insert(pair<int,char>(28,'V'));
map32f.insert(pair<int,char>(29,'W'));
map32f.insert(pair<int,char>(30,'X'));
map32f.insert(pair<int,char>(31,'Y'));
//三十二进制区
}
int main() {
loading_map();
int orign,n,i=0; //orign原数,n需要转换的进制数,i循环控制
cin>>orign>>n;
while(orign>0) {
s.push(map32f[orign%n]);
orign=orign/n;
i++;
}
while(!s.empty()){
cout<<s.top();
s.pop();
}
return 0;
}直接利用STL实现功能,其中利用pop出栈的方式逐个检索栈中元素,发现之前没有调用的pop出栈又出奇的用上了。
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复