| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 983 人关注过本帖, 1 人收藏
标题:关于用 C 程序设计进位制数换算
只看楼主 加入收藏
e1350511553
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-12-8
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:3 
关于用 C 程序设计进位制数换算
最近在做进位制数换算
 功能要求:
(1)编写选择式菜单程序,实现从十进制数到二进制、八进制、十六进制数的转换。
(2)二进制到十进制数、八进制、十六进制数的转换。
(3)八进制到十进制数、二进制、十六进制数的转换。
(4)十六进制数到十进制数、二进制、八进制数的转换。

但是我只会很零散的做各个部分。所以,可有人会这个程序?帮我设计一份完整的参考。不胜感激!
以下是我的编程
#include<iostream.h>
 #include<malloc.h>
 #define MaxSize 16
 typedef int ElemType;
 typedef struct
 {
     ElemType data[MaxSize];
     int top;
 }SqStack;
 //初始化栈
void InitStack(SqStack *&s)
 {
     s=(SqStack *)malloc(sizeof(SqStack));
     s->top=-1;
 }
 //销毁栈
void ClearStack(SqStack *&s)
 {
     free(s);
 }
 //进栈
int Push(SqStack *&s,ElemType e)
 {
     if(s->top==MaxSize-1)
         return 0;
     s->top++;
     s->data[s->top]=e;
     return 1;
 }
 //出栈
int Pop(SqStack *&s,ElemType &e)
 {
     if(s->top==-1)
         return 0;
     e=s->data[s->top];
     s->top--;
     return 1;
 }
 //取栈顶元素
int GetTop(SqStack *s,ElemType &e)
 {
     if(s->top==-1)
         return 0;
     e=s->data[s->top];
     return 1;
 }
 //显示栈中元素
void DispStack(SqStack *s)
 {
     int i;
     for(i=s->top;i>=0;i--)
         cout<<s->data[i];
 }
 //十六进制判断
void Judge(SqStack *s)
 {
     int i;
     for(i=s->top;i>=0;i--)
     {
         if(s->data[i]==10)   cout<<"A";
             else if(s->data[i]==11)   cout<<"B";
             else if(s->data[i]==12)   cout<<"C";
             else if(s->data[i]==13)   cout<<"D";
             else if(s->data[i]==14)   cout<<"E";
             else if(s->data[i]==15)   cout<<"F";
         else cout<<s->data[i];
     }
 }
 void main()
 {
     SqStack *s;
     InitStack(s);
     int i,t,j,m;//要转换的进制,j要被转换的数
    cout<<"请输入要转换的数(注意:j的范围):";
     cin>>j;
     cout<<"请输入要转换成的进制(二进制,八进制,十六进制):";
     cin>>i;
     if(i==2||i==8)
     {
         if(j<0)
         {   m=-j;
             while(m!=0)
             {
                 t=m%i;
                 Push(s,t);
                 m=m/i;
             }
             Push(s,1);
         }
         else if(j>0)
         {   
             while(j!=0)
             {
                 t=j%i;
                 Push(s,t);
                 j=j/i;
             }
             Push(s,0);
         }
     cout<<"转换成"<<i<<"进制后的代码(首位为符号位):";
     DispStack(s);
     ClearStack(s);
     }
     if(i==16)
     {
         if(j<0)
         {   m=-j;
             while(m!=0)
             {
                 t=m%i;
                 Push(s,t);
                 m=m/i;
             }
             Push(s,1);
         }
         else if(j>0)
         {   
             while(j!=0)
             {
                 t=j%i;
                 Push(s,t);
                 j=j/i;
             }
             Push(s,0);
         }
     cout<<"转换成"<<i<<"进制后的代码(首位为符号位):";
     Judge(s);
     ClearStack(s);
     }
     cout<<endl;
 }

#include "stdio.h"
 #include "math.h"
 void main(){
 int q,m,k,p,s[50];
 double n;
 m=0;
 printf("\n 请输入八进制数n:");
scanf("%lf",&n);
 printf("\n 请输入转换进制数p(2-16):");
scanf("%d",&p);
 printf(" (%f)10=(",n);
 while(n!=0){
 q=fmod(n,p);
 n=floor(n/p);
 m++;
 s[m]=q;
 }
 for(k=m;k>=1;k--){
 if(s[k]>9){
 printf("%c",s[k]+55);
 }
 else{
 printf("%d",s[k]);
 }


 }
 printf(")%d\n",p);
}

#include<iostream.h>
 #include<malloc.h>
 #define MaxSize 16
 typedef int ElemType;
 typedef struct
 {
     ElemType data[MaxSize];
     int top;
 }SqStack;
 //初始化栈
void InitStack(SqStack *&s)
 {
     s=(SqStack *)malloc(sizeof(SqStack));
     s->top=-1;
 }
 //销毁栈
void ClearStack(SqStack *&s)
 {
     free(s);
 }
 //进栈
int Push(SqStack *&s,ElemType e)
 {
     if(s->top==MaxSize-1)
         return 0;
     s->top++;
     s->data[s->top]=e;
     return 1;
 }
 //出栈
int Pop(SqStack *&s,ElemType &e)
 {
     if(s->top==-1)
         return 0;
     e=s->data[s->top];
     s->top--;
     return 1;
 }
 //取栈顶元素
int GetTop(SqStack *s,ElemType &e)
 {
     if(s->top==-1)
         return 0;
     e=s->data[s->top];
     return 1;
 }
 //显示栈中元素
void DispStack(SqStack *s)
 {
     int i;
     for(i=s->top;i>=0;i--)
         cout<<s->data[i];
 }
 //十六进制判断
void Judge(SqStack *s)
 {
     int i;
     for(i=s->top;i>=0;i--)
     {
         if(s->data[i]==10)   cout<<"A";
             else if(s->data[i]==11)   cout<<"B";
             else if(s->data[i]==12)   cout<<"C";
             else if(s->data[i]==13)   cout<<"D";
             else if(s->data[i]==14)   cout<<"E";
             else if(s->data[i]==15)   cout<<"F";
         else cout<<s->data[i];
     }
 }
 void main()
 {
     SqStack *s;
     InitStack(s);
     int i,t,j,m;//要转换的进制,j要被转换的数
    cout<<"请输入要转换的数(注意:j的范围):";
     cin>>j;
     cout<<"请输入要转换成的进制(二进制,八进制,十六进制):";
     cin>>i;
     if(i==2||i==8)
     {
         if(j<0)
         {   m=-j;
             while(m!=0)
             {
                 t=m%i;
                 Push(s,t);
                 m=m/i;
             }
             Push(s,1);
         }
         else if(j>0)
         {   
             while(j!=0)
             {
                 t=j%i;
                 Push(s,t);
                 j=j/i;
             }
             Push(s,0);
         }
     cout<<"转换成"<<i<<"进制后的代码(首位为符号位):";
     DispStack(s);
     ClearStack(s);
     }
     if(i==16)
     {
         if(j<0)
         {   m=-j;
             while(m!=0)
             {
                 t=m%i;
                 Push(s,t);
                 m=m/i;
             }
             Push(s,1);
         }
         else if(j>0)
         {   
             while(j!=0)
             {
                 t=j%i;
                 Push(s,t);
                 j=j/i;
             }
             Push(s,0);
         }
     cout<<"转换成"<<i<<"进制后的代码(首位为符号位):";
     Judge(s);
     ClearStack(s);
     }
     cout<<endl;
 }
搜索更多相关主题的帖子: 不胜感激 十六进制 程序设计 include 二进制 
2014-12-08 17:14
doudou74321
Rank: 2
等 级:论坛游民
帖 子:70
专家分:72
注 册:2014-11-5
收藏
得分:10 
#include "stdio.h"
#include "math.h"
main()
{
  int error,i,j,k,number_b,number_d,temp;
char type;
char number_copy[60],number[60];
error=1;
number_d=0;
printf("please choose type,1--Binery,2--Decimal,3--Hex\n");
scanf("%c",&type);
while((type!='1')&&(type!='2')&&(type!='3'))
   {
       printf("type error,please input the type again\n");
       scanf("%c",&type);
   }





switch (type)
   {
       case '1':
           {
               printf("\nplease input the binery number:");
               scanf("%s",number);
        /*判断二进制输入是否合法,不合法重新输入*/      
               while (error==1)
                 {
                   for(i=0;number[i]!='\0';i++)
                     {
                         if((number[i]!='0')&&(number[i]!='1'))
                            {
                                error=1;
                                break;
                            }
                         else
                         error=0;
                     }
                  if(error==0)
                  break;
                  else
                     {
                           printf("\nnumber error,pls input again:\n");
                           scanf("%s",number);
                     }
                 }
         
  /*输入的二进制转换为十进制和十六进制输出*/        
          for(i=i-1,j=0;i>=0;i--,j++)
            {
                number_d=(number[j]-48)*pow(2,i)+number_d;
               }
          printf("\nthe decimal number is:%d\nthe hex number is:%X",number_d,number_d);     
           }
       break;
      
      
       case '2':
           {
               printf("\nplease input the decimal number:");
               scanf("%s",number);
        /*判断二进制输入是否合法,不合法重新输入*/  
            
             while(error==1)
               {
                   for(i=0;number[i]!='\0';i++)
                     {
                       if(number[i]<'0'||number[i]>'9')
                         {
                             error=1;
                        break;
                         }
                       else
                       error=0;
                     }
                   if(error==0)
                   break;
                   else
                   {
                           printf("\nnumber error,pls input again:\n");
                           scanf("%s",number);
                     }
               }
           
/*输入的十进制转换为二进制和十六进制输出*/        
          for(i=i-1,j=0;i>=0;i--,j++)
            {
                number_d=(number[j]-48)*pow(10,i)+number_d;
            }
          printf("\nthe hex number is:%X",number_d);  
         
          for(i=0;number_d!=0;i++)
            {
                number[i]=number_d%2+48;
                number_d=number_d/2;
               }         

          for(j=i-1,k=0;k<(i-1)/2+1;j--,k++)
            {
                temp=number[j];
                number[j]=number[k];
                number[k]=temp;
               }
           
          printf("\nthe binary number is:%s",number);   
           }
       break;
      
      
       case '3':
           {
               printf("\nplease input the hex number:");
               scanf("%s",number);
               /*判断十六进制输入是否合法,不合法重新输入*/
               while(error==1)
               {
                   for(i=0;number[i]!='\0';i++)
                     {
                       if((number[i]>='0'&&number[i]<='9')||(number[i]>='a'&&number[i]<='f')||(number[i]>='A'&&number[i]<='F'))
                         {
                             error=0;
                         }
                       else
                        {
                            error=1;
                            break;
                        }
                     }
                   if(error==0)
                   break;
                   else
                   {
                           printf("\nnumber error,pls input again:\n");
                           scanf("%s",number);
                     }
               }
          /*输入的十六进制转换为二进制和十进制输出*/
         
                  for(i=i-1,j=0;i>=0;i--,j++)
                {
                    if(number[j]>='0'&&number[j]<='9')
                    number_d=(number[j]-48)*pow(16,i)+number_d;
                    else if(number[j]>='a'&&number[j]<='f')
                    number_d=(number[j]-87)*pow(16,i)+number_d;
                    else if(number[j]>='A'&&number[j]<='F')
                    number_d=(number[j]-55)*pow(16,i)+number_d;
                }
            
              printf("\nthe decimal number is:%d",number_d);  
              
              for(i=0;number_d!=0;i++)
                {
                    number[i]=number_d%2+48;
                    number_d=number_d/2;
                }      
         
            
          for(j=i-1,k=0;k<(i-1)/2+1;j--,k++)
            {
                temp=number[j];
                number[j]=number[k];
                number[k]=temp;
               }
         
           
          printf("\nthe binary number is:%s",number);     
           }
       break;
      
       }

}
2014-12-08 18:52
e1350511553
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-12-8
收藏
得分:0 
回复 2楼 doudou74321
少了个八进制哦。能否添加?
2014-12-08 19:32
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:10 
你这是彻底求人了,本来应该封帖的,不过你已经消失,就算了。

授人以渔,不授人以鱼。
2014-12-10 13:22
快速回复:关于用 C 程序设计进位制数换算
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.241944 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved