题目:使用命令行参数编写一个程序,其功能是将任意一个正整数m变换为指定的n 进制数串输出。命令行的格式为:

 
主  题: 请教一个问题?这道题目怎么编?
作  者: hlj3171 ()        Blog   加为好友
等  级: 
信 誉 值: 100
所属社区: C/C++ C语言
问题点数: 10
回复次数: 12
发表时间: 2007-4-16 11:58:47

题目:使用命令行参数编写一个程序,其功能是将任意一个正整数m变换为指定的n 进制数串输出。命令行的格式为:

在这里贴一个我过去写的数制转换的程序,CSDN上只能连发三贴,没贴完就到这来贴吧。是用面向对象写的!

/*用栈来做
这是一个栈的头文件*/
/*stack.h*/
#ifndef STACK_H
#define STACK_H
typedef struct N
{
int data;
char Ch;
struct N *next;
} NODE;

class STACK
{
private:
NODE *Top,*Bottom;
int M_Size;
public:
STACK();
STACK(int Max);
int Empty();
void Push(char c);
char Pop();
char const GetTop();
void Clear();
int const CurrentSize();
~STACK();
};
#endif


/*栈有实现*/
/*stack.cpp*/
#include "stack.h"
#include <iostream.h>
STACK::STACK()
{
Top=Bottom = new NODE;
Bottom->next=NULL;
Bottom->data=0;
Bottom->Ch=0;
M_Size=-1;
}

STACK::STACK(int Max)
{
Top=Bottom = new NODE;
Bottom->next=NULL;
Bottom->data=0;
Bottom->Ch=0;
M_Size=Max;
}

void STACK::Push(char c)
{
if (Bottom->data<M_Size||M_Size==-1)
{
NODE *S = new NODE;
S->next = Top;
//cout<<"Input your want push number:"<<endl;
//cin>>S->Ch;
S->Ch = c;
Top = S;
Bottom->data++;
Top->data=Bottom->data;
//cout<<"The number push stack successed !"<<endl;
}
//else
//cout<<"The stack are top overflow ! "<<endl;
}

char STACK::Pop()
{
NODE *temp;
char t;
if (Top->next!=NULL || Top!=Bottom)
{
temp = Top;
Top = Top->next;
t=temp->Ch;
delete temp;
Bottom->data--;
//cout<<"The number pop stack successed ! "<<endl;
return t;
}
else
{
cout<<"The stack are bottom overflow ! "<<endl;
return 0;
}
}

char const STACK::GetTop()
{
if (Top->next!=NULL||Top!=Bottom)
{
//cout<<"The stack top element is "<<Top->data<<" ! "<<endl;
return Top->Ch;
}
else
{
//cout<<"The stack is employ ! "<<endl;
return 0;
}
}

int const STACK::CurrentSize()
{
if (M_Size!=-1)
cout<<"The stack total length is "<<M_Size<<" ! "<<endl;
cout<<"The stack length is "<<Bottom->data<<" ! "<<endl;
return Bottom->data;
}

int STACK::Empty()
{
if (Top==Bottom)
{
//cout<<"The stack is employ ! "<<endl;
return 1;
}
else
{
//cout<<"The stack have element ! "<<endl;
return 0;
}

}

void STACK::Clear()
{
NODE *temp;
while(Top!=Bottom)
{
temp=Top;
Top=Top->next;
Bottom->data--;
delete temp;

}
if (Bottom->data==0)
cout<<"Stack clear successed ! "<<endl;
}

STACK::~STACK()
{
NODE *t;
while (Top!=Bottom)
{
t=Top;
Top=Top->next;
delete t;
}
delete Bottom;
}


/*数制转换的头声明!*/
/*fun.h*/
#ifndef FUN_H
#define FUN_H

void itoa(const long int number, char* &str,int bit);

void sleep(int n);

#endif

/*转换函数的实现*/

/*fun.h*/

#include <iostream.h>
#include "stack.h"
char H[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void itoa(const long int number, char *&str,int bit)
{
 STACK s;
 long int n = number;
 int strlen = 1;
 if (n<0)
 {
  n=-1*n;
  strlen = strlen + 1;
 }
 while(n!=0)
 {
  s.Push(H[n%bit]);
  n=n/bit;
  strlen++;  
 }
 str = new char[strlen];
 if(number<0) *str++ = '-';
 while(!s.Empty())
 {
  *str++ = s.Pop();
  
 }
 *str = '/0';
 str = str - strlen +1;
}

void sleep(int n)
{
 for (int j = 0 ; j<n ; j++)
  for(int i=0 ; i<10000 ;i++);
}

/*主函数*/

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "fun.h"

 

void main(int argc, char **argv)
{
 long int a = atol(argv[1]);
 int b = atoi(argv[2]);
 char *S;
 if(argc !=3)
       fprintf(stderr, "format input error usage:<chang m n>/n");
 itoa(a,S,b);
 int len=strlen(S)+1;
 cout<<len<<endl;
 for(int i=0;i<len;i++)
 {
  printf("%c",S[i]);
  sleep(5000);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值