输入一:初始化信息包括每页的字节数,第几页,以及指针所占的字符的个数。
输入二:指针数组的内容。
程序1
#include <stdio.h>
#include<string.h>
#include<ctype.h>
#include<fstream>
#include<iostream>
using namespace std;
typedef unsigned long long ULL;
char temp[10000000];
ULL get_dec(char *ch,ULL &begin_addr,int alpha_num){
int k = 0;
ULL ans = 0;
while(k < alpha_num){
if(ch[begin_addr]==' '){
begin_addr++;
continue;
}else{
printf("%c",ch[begin_addr]);
ULL tem = 0;
if(ch[begin_addr]>='0' && ch[begin_addr]<='9'){
tem = ch[begin_addr] - '0';
}else{
char sc = toupper(ch[begin_addr]);
switch(sc){
case 'A': tem = 10;break;
case 'B': tem = 11;break;
case 'C': tem = 12;break;
case 'D': tem = 13;break;
case 'E': tem = 14;break;
case 'F': tem = 15;break;
}
}
ans = ans * 16 + tem;
k++;
begin_addr++;
}
}
printf("H");
return ans;
}
int main(int argc, const char * argv[]) {
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/initializer.txt","r",stdin);
int page_size;
printf("请输入每页的大小(Byte):");
scanf("%d",&page_size);
int page;
printf("请输入这是第几页:(编号从1开始)");
scanf("%d",&page);
ULL base_addr = page_size * (page - 1);
printf("请输入地址占据几个字符:");
int alpha_num;
scanf("%d",&alpha_num);
alpha_num*=2;
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/array_point.txt","r",stdin);
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/output_all.txt","w",stdout);
scanf("%[^\n]",temp);
ULL len = strlen(temp);
printf("temp.length=%llu\n",len);
ULL i = 0;
while(i < len){
ULL ans = get_dec(temp, i, alpha_num);
ULL addr = base_addr + ans;
printf("对应的十进制数是:%llu, 地址是:%llu\n",ans,addr);
}
return 0;
}
程序2(将db中的内容以十六进制字符串的形式打印到文件中)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<fstream>
#include<stdlib.h>
using namespace std;
int like_dec(int a){
return (a>=0 && a<=9);
}
char get_hex_char(int a){
if(like_dec(a)){
return '0' + a;
}else{
return 'A' + a - 10;
}
}
char * cal(int sou){
int f2,s2;
char f,s;
if(sou<0){
sou+=128;//转换为移码表示
f2 = sou/16 + 8;
}else{
f2 = sou/16;
}
s2 = sou%16;
f=get_hex_char(f2);
s=get_hex_char(s2);
char * res=NULL;
res=(char *)malloc(sizeof(char)*2);
*(res) = f;
*(res+1) = s;
return res;
}
int main(){
ifstream fin("/Users/adog/Desktop/文档/qqq/qqq/test02.db");
ofstream fou("/Users/adog/Desktop/文档/qqq/qqq/output.txt");
char ch;
unsigned long long i,j,k;
j=0;
i=0;
k=0;
while(fin.get(ch)){
if(i==0) {
j++;
printf("行号:%llu,字节号:%llu | ",j-1,k);
fou << "行号:"<<j-1<<",字节号:"<<k <<" | ";
}
char * p_c = cal(ch);
printf("%c%c",p_c[0],p_c[1]);
fou << p_c[0] << p_c[1];
k++;
if(k%4==0) {
printf(" ");
fou << " ";
}
if(i==31){
printf("\n");
fou << endl;
i=0;
}else{
i++;
}
}
fin.close();
fou.close();
return 0;
}
程序3(查找对应指针指向的内容)
#include <stdio.h>
#include<string.h>
#include<ctype.h>
#include<fstream>
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef unsigned long long ULL;
char temp[10000000];
ULL addrs[1024];
ULL addrs_index=0;
int flag[4096];
ULL get_dec(char *ch,ULL &begin_addr,int alpha_num){
int k = 0;
ULL ans = 0;
while(k < alpha_num){
if(ch[begin_addr]==' '){
begin_addr++;
continue;
}else{
printf("%c",ch[begin_addr]);
ULL tem = 0;
if(ch[begin_addr]>='0' && ch[begin_addr]<='9'){
tem = ch[begin_addr] - '0';
}else{
char sc = toupper(ch[begin_addr]);
switch(sc){
case 'A': tem = 10;break;
case 'B': tem = 11;break;
case 'C': tem = 12;break;
case 'D': tem = 13;break;
case 'E': tem = 14;break;
case 'F': tem = 15;break;
}
}
ans = ans * 16 + tem;
k++;
begin_addr++;
}
}
printf("H");
return ans;
}
int like_dec(int a){
return (a>=0 && a<=9);
}
char get_hex_char(int a){
if(like_dec(a)){
return '0' + a;
}else{
return 'A' + a - 10;
}
}
char * cal(int sou){
int f2,s2;
char f,s;
if(sou<0){
sou+=128;//转换为移码表示
f2 = sou/16 + 8;
}else{
f2 = sou/16;
}
s2 = sou%16;
f=get_hex_char(f2);
s=get_hex_char(s2);
char * res=NULL;
res=(char *)malloc(sizeof(char)*2);
*(res) = f;
*(res+1) = s;
return res;
}
int main(int argc, const char * argv[]) {
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/initializer.txt","r",stdin);
int page_size;
printf("请输入每页的大小(Byte):");
scanf("%d",&page_size);
int page;
printf("请输入这是第几页:(编号从1开始)");
scanf("%d",&page);
ULL base_addr = page_size * (page - 1);
printf("请输入地址占据几个字符:");
int alpha_num;
scanf("%d",&alpha_num);
alpha_num*=2;
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/array_point.txt","r",stdin);
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/output_all.txt","w",stdout);
scanf("%[^\n]",temp);
ULL len = strlen(temp);
//printf("temp.length=%llu\n",len);
ULL i = 0;
while(i < len){
ULL ans = get_dec(temp, i, alpha_num);
ULL addr = base_addr + ans;
printf("对应的十进制数是:%llu, 加基址之后:%llu\n",ans,addr);
addrs[addrs_index++] = addr;
}
printf("\n因此,指针数组内容为:\n");
for(int i=0;i<addrs_index;i++) cout<<addrs[i]<<" ";
cout<<endl<<"\nbase_addr:"<<base_addr<<endl;
for(int i=0;i<addrs_index;i++){
flag[addrs[i] - base_addr] = 1;//设定标志
}
ifstream fin("/Users/adog/Desktop/文档/qqq/qqq/test02.db");
char ch;
unsigned long long ti,j,k;
j=0;
ti=0;
k=0;
ULL low_addr = addrs[addrs_index - 1];
while(fin.get(ch)){
if(k >= base_addr + page_size){
fin.close();
return 0;
}
if(k < low_addr){
k++;
continue;
}
if(flag[k - base_addr]==1) {
printf("\n地址:%llu\n",k);
}
char * p_c = cal(ch);
printf("%c%c",p_c[0],p_c[1]);
if(k%4==0){
printf(" ");
}
k++;
}
fin.close();
return 0;
}
十进制转位十六进制
char get_c(ULL d){
if(d>=0 && d <=9){
return d + '0';
}else if(d >= 10 && d <= 15){
return d - 10 + 'A';
}else{
printf("数据范围超出限制");
return 'N';
}
}
char * dec_to_hex(ULL a_in){
ULL a,b,c,d;
char * e = (char *)malloc(sizeof(char) * 4);
int h1,h2,h3;
h1 = 1 << 12;
h2 = 1 << 8;
h3 = 1 << 4;
a = a_in / h1;
a_in = a_in - a * h1;
b = a_in / h2;
a_in = a_in - b * h2;
c = a_in / h3;
a_in = a_in - c * h3;
d = a_in;
*(e) = get_c(a);
*(e+1) = get_c(b);
*(e+2) = get_c(c);
*(e+3) = get_c(d);
return e;
}
程序3改进版
#include <stdio.h>
#include<string.h>
#include<ctype.h>
#include<fstream>
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef unsigned long long ULL;
char temp[10000000];
ULL addrs[1024];
ULL addrs_index=0;
int flag[4096];
ULL get_dec(char *ch,ULL &begin_addr,int alpha_num){
int k = 0;
ULL ans = 0;
while(k < alpha_num){
if(ch[begin_addr]==' '){
begin_addr++;
continue;
}else{
printf("%c",ch[begin_addr]);
ULL tem = 0;
if(ch[begin_addr]>='0' && ch[begin_addr]<='9'){
tem = ch[begin_addr] - '0';
}else{
char sc = toupper(ch[begin_addr]);
switch(sc){
case 'A': tem = 10;break;
case 'B': tem = 11;break;
case 'C': tem = 12;break;
case 'D': tem = 13;break;
case 'E': tem = 14;break;
case 'F': tem = 15;break;
}
}
ans = ans * 16 + tem;
k++;
begin_addr++;
}
}
printf("H");
return ans;
}
int like_dec(int a){
return (a>=0 && a<=9);
}
char get_hex_char(int a){
if(like_dec(a)){
return '0' + a;
}else{
return 'A' + a - 10;
}
}
char * cal(int sou){
int f2,s2;
char f,s;
if(sou<0){
sou+=128;//转换为移码表示
f2 = sou/16 + 8;
}else{
f2 = sou/16;
}
s2 = sou%16;
f=get_hex_char(f2);
s=get_hex_char(s2);
char * res=NULL;
res=(char *)malloc(sizeof(char)*2);
*(res) = f;
*(res+1) = s;
return res;
}
char get_c(ULL d){
if(d>=0 && d <=9){
return d + '0';
}else if(d >= 10 && d <= 15){
return d - 10 + 'A';
}else{
printf("数据范围超出限制");
return 'N';
}
}
char * dec_to_hex(ULL a_in){
ULL a,b,c,d;
char * e = (char *)malloc(sizeof(char) * 4);
int h1,h2,h3;
h1 = 1 << 12;
h2 = 1 << 8;
h3 = 1 << 4;
a = a_in / h1;
a_in = a_in - a * h1;
b = a_in / h2;
a_in = a_in - b * h2;
c = a_in / h3;
a_in = a_in - c * h3;
d = a_in;
*(e) = get_c(a);
*(e+1) = get_c(b);
*(e+2) = get_c(c);
*(e+3) = get_c(d);
return e;
}
int main(int argc, const char * argv[]) {
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/initializer.txt","r",stdin);
int page_size;
printf("请输入每页的大小(Byte):");
scanf("%d",&page_size);
int page;
printf("请输入这是第几页:(编号从1开始)");
scanf("%d",&page);
ULL base_addr = page_size * (page - 1);
printf("请输入地址占据几个字符:");
int alpha_num;
scanf("%d",&alpha_num);
alpha_num*=2;
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/array_point.txt","r",stdin);
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/output_all.txt","w",stdout);
scanf("%[^\n]",temp);
ULL len = strlen(temp);
//printf("temp.length=%llu\n",len);
ULL i = 0;
while(i < len){
ULL ans = get_dec(temp, i, alpha_num);
ULL addr = base_addr + ans;
printf("对应的十进制数是:%llu, 加基址之后:%llu\n",ans,addr);
addrs[addrs_index++] = addr;
}
printf("\n因此,指针数组内容为:\n");
for(int i=0;i<addrs_index;i++) cout<<addrs[i]<<" ";
cout<<endl<<"\nbase_addr:"<<base_addr<<endl;
for(int i=0;i<addrs_index;i++){
flag[addrs[i] - base_addr] = 1;//设定标志
}
ifstream fin("/Users/adog/Desktop/文档/qqq/qqq/test02.db");
char ch;
unsigned long long ti,j,k;
j=0;
ti=0;
k=0;
ULL low_addr = addrs[addrs_index - 1];
while(fin.get(ch)){
if(k >= base_addr + page_size){
fin.close();
return 0;
}
if(k < low_addr){
k++;
continue;
}
ULL compara = k - base_addr;
if(flag[compara]==1) {
char * cc = dec_to_hex(compara);
printf("\n");
printf("%c%c%c%cH ",*(cc),*(cc+1),*(cc+2),*(cc+3));
printf("地址:%llu\n",k);
}
char * p_c = cal(ch);
printf("%c%c",p_c[0],p_c[1]);
if(k%4==0){
printf(" ");
}
k++;
}
fin.close();
return 0;
}
再次改进
#include <stdio.h>
#include<string.h>
#include<ctype.h>
#include<fstream>
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef unsigned long long ULL;
char temp[10000000];
ULL addrs[1024];
ULL addrs_index=0;
int flag[4096];
//-------6月5号添加--------
int flag2 = 1;
char store_two[128];
char chs[30];
char chs2[128];
void moveN(char * begin,int n){//将begin所指向的字符向右移动n个单位
*(begin + n) = *begin;
}
char * getChs(char a){
switch(a){
case 'a': return "1010";break;
case 'A': return "1010";break;
case 'b': return "1011";break;
case 'B': return "1011";break;
case 'c': return "1100";break;
case 'C': return "1100";break;
case 'd': return "1101";break;
case 'D': return "1101";break;
case 'e': return "1110";break;
case 'E': return "1110";break;
case 'f': return "1111";break;
case 'F': return "1111";break;
case '0': return "0000";break;
case '1': return "0001";break;
case '2': return "0010";break;
case '3': return "0011";break;
case '4': return "0100";break;
case '5': return "0101";break;
case '6': return "0110";break;
case '7': return "0111";break;
case '8': return "1000";break;
case '9': return "1001";break;
}
return 0;
}
int dd(char * chs){
// while(1){
//memset(chs,0,sizeof(chs));
memset(chs2,0,sizeof(chs2));
//scanf("%s",chs);
int i=0;
const char * cs;
cs = (char *)malloc(4 * sizeof(char));
while(chs[i]!=0){
cs = getChs(chs[i]);
//printf("%s\n",cs);
i++;
strcat(chs2,cs);
}
//printf("%s\n",chs2);
int p=0;
unsigned long len = strlen(chs2);
for(int i=0,j=len-i-1;i<len;i++,j--){
int n = i/8;
if(n>=1 && i%8!=7){
moveN((chs2+j),n);
p = j+n;
}
}
for(int i=0;i<p;i++){
*(chs2+i) ='0';
}
int ans =0;
int index = 1;
for(int j=len-1;j>p;j--){
if(chs2[j] == '1'){
ans = ans + index;
//printf("j=%d ans=%d\n",j,ans);
}
index = index << 1;
}
//printf("\n%s\n",chs2);
//printf("%d\n",ans);
return ans;
// }
}
//----------------
ULL get_dec(char *ch,ULL &begin_addr,int alpha_num){
int k = 0;
ULL ans = 0;
while(k < alpha_num){
if(ch[begin_addr]==' '){
begin_addr++;
continue;
}else{
printf("%c",ch[begin_addr]);
ULL tem = 0;
if(ch[begin_addr]>='0' && ch[begin_addr]<='9'){
tem = ch[begin_addr] - '0';
}else{
char sc = toupper(ch[begin_addr]);
switch(sc){
case 'A': tem = 10;break;
case 'B': tem = 11;break;
case 'C': tem = 12;break;
case 'D': tem = 13;break;
case 'E': tem = 14;break;
case 'F': tem = 15;break;
}
}
ans = ans * 16 + tem;
k++;
begin_addr++;
}
}
printf("H");
return ans;
}
int like_dec(int a){
return (a>=0 && a<=9);
}
char get_hex_char(int a){
if(like_dec(a)){
return '0' + a;
}else{
return 'A' + a - 10;
}
}
char * cal(int sou){
int f2,s2;
char f,s;
if(sou<0){
sou+=128;//转换为移码表示
f2 = sou/16 + 8;
}else{
f2 = sou/16;
}
s2 = sou%16;
f=get_hex_char(f2);
s=get_hex_char(s2);
char * res=NULL;
res=(char *)malloc(sizeof(char)*2);
*(res) = f;
*(res+1) = s;
return res;
}
char get_c(ULL d){
if(d>=0 && d <=9){
return d + '0';
}else if(d >= 10 && d <= 15){
return d - 10 + 'A';
}else{
printf("数据范围超出限制");
return 'N';
}
}
char * dec_to_hex(ULL a_in){
ULL a,b,c,d;
char * e = (char *)malloc(sizeof(char) * 4);
int h1,h2,h3;
h1 = 1 << 12;
h2 = 1 << 8;
h3 = 1 << 4;
a = a_in / h1;
a_in = a_in - a * h1;
b = a_in / h2;
a_in = a_in - b * h2;
c = a_in / h3;
a_in = a_in - c * h3;
d = a_in;
*(e) = get_c(a);
*(e+1) = get_c(b);
*(e+2) = get_c(c);
*(e+3) = get_c(d);
return e;
}
int main(int argc, const char * argv[]) {
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/initializer.txt","r",stdin);
int page_size;
printf("请输入每页的大小(Byte):");
scanf("%d",&page_size);
int page;
printf("请输入这是第几页:(编号从1开始)");
scanf("%d",&page);
ULL base_addr = page_size * (page - 1);
printf("请输入地址占据几个字符:");
int alpha_num;
scanf("%d",&alpha_num);
alpha_num*=2;
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/array_point.txt","r",stdin);
freopen("/Users/adog/Desktop/文档/a_cool/a_cool/output_all.txt","w",stdout);
scanf("%[^\n]",temp);
ULL len = strlen(temp);
//printf("temp.length=%llu\n",len);
ULL i = 0;
while(i < len){
ULL ans = get_dec(temp, i, alpha_num);
ULL addr = base_addr + ans;
printf("对应的十进制数是:%llu, 加基址之后:%llu\n",ans,addr);
addrs[addrs_index++] = addr;
}
printf("\n因此,指针数组内容为:\n");
for(int i=0;i<addrs_index;i++) cout<<addrs[i]<<" ";
cout<<endl<<"\nbase_addr:"<<base_addr<<endl;
for(int i=0;i<addrs_index;i++){
flag[addrs[i] - base_addr] = 1;//设定标志
}
ifstream fin("/Users/adog/Desktop/NBA.db");
char ch;
unsigned long long ti,j,k;
j=0;
ti=0;
k=0;
ULL low_addr = addrs[addrs_index - 1];
while(fin.get(ch)){
flag2 = 1;
//memset(store_two,0,sizeof(store_two));
if(k >= base_addr + page_size){
fin.close();
return 0;
}
if(k < low_addr){
k++;
continue;
}
ULL compara = k - base_addr;
if(flag[compara]==1) {
char * cc = dec_to_hex(compara);
printf("\n");
printf("%c%c%c%cH ",*(cc),*(cc+1),*(cc+2),*(cc+3));
printf("地址:%llu\n",k);
}
char * p_c = cal(ch);
if(k%4 !=0 && flag2 && p_c[0] >= '8'){
char * cha = (char *)malloc(2*sizeof(char));
*(cha) = p_c[0];
*(cha+1) = p_c[1];
strcat(store_two, cha);
}else{
char * cha = (char *)malloc(2*sizeof(char));
*(cha) = p_c[0];
*(cha+1) = p_c[1];
strcat(store_two, cha);
flag2 = 0;
//printf(" (%d) ",dd(store_two));
memset(store_two,0,sizeof(store_two));
}
printf("%c%c",p_c[0],p_c[1]);
if(k%4==3){
printf(" ");
}
k++;
}
fin.close();
return 0;
}