标题:帮忙把程序改成任意进制都能转换、还有程序的流程图!求解!
只看楼主
fan1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-5-23
结帖率:50%
已结贴  问题点数:20 回复次数:2 
帮忙把程序改成任意进制都能转换、还有程序的流程图!求解!
#include <stdio.h>
 #include <stdlib.h>
 #include <malloc.h>
 #define MAX 100

typedef struct
 {
      int data[MAX];
      int top;
 }seqstack, *pseqstack;
 pseqstack s;
 //s = (pseqstack)malloc(sizeof(seqstack));

 pseqstack init_seqstack()
 {
      pseqstack s;
      s = (pseqstack)malloc(sizeof(seqstack));
      if(s)
          s ->top = -1;
      return s;
 }

 int empty_seqstack(pseqstack s)
 {
      if(s ->top == -1)
          return 1;
      else
          return 0;
 }

 int input_seqstack(pseqstack s,int x)
 {
      if(s ->top ==MAX - 1)
          return 0;//栈满不能入栈
      else
      {
          s ->top ++;
          s ->data[s ->top] = x;
          return 1;
      }
 }

 int out_seqstack(pseqstack s)
 {
      int x;
      if (empty_seqstack(s)) //栈空不能出栈
          return 0;
      else
      {
          x = s ->data[s ->top];
          s ->top--;
          printf("%d",x);
          return 1;
      }
 }

 int gettop_seqstack(pseqstack s)
 {
      int x;
      if(empty_seqstack(s))
          return 0;
      else
      {
          x = s->data[s ->top];
          return (1);
      }
 }

 int change(int x)
 {
      int n;
      pseqstack s;
      s = (pseqstack)malloc(sizeof(seqstack));
      s->top=-1;         
     
     while(x != 0)
      {
          n = x % n;
          input_seqstack(s,n);
          x = x / n;
      }
      while(s->top>-1)
      {
          out_seqstack(s);//
      }
      putchar('\n');
      return 1;
 }

 int main()
 {
      int n;
      pseqstack s;
      s = (pseqstack)malloc(sizeof(seqstack));
      printf("输入要转化的进制整数:\n");
      scanf("%d",&n);
      //printf("选择你要转换成的进制数:\n");
      change(n);
      return 0;
 }
搜索更多相关主题的帖子: top include return 流程图 
2012-05-26 09:44
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
得分:20 
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 100
typedef struct
{
      int data[MAX];
      int top;
}seqstack, *pseqstack;
pseqstack s;
init_seqstack()
{
      s = (pseqstack)malloc(sizeof(seqstack));
      if(s)
          s ->top = -1;
      return;
}
int empty_seqstack()
{
      if(s ->top == -1)
          return 1;
      else
          return 0;
}
input_seqstack(int x)
{
      if(s ->top ==MAX - 1)
          return;//栈满不能入栈
      else
      {
          s ->top ++;
          s ->data[s ->top] = x;
          return;
      }
}
int out_seqstack()
{
      int x;
      if (empty_seqstack(s)) //栈空不能出栈
          return 0;
      else
      {
          x = s ->data[s ->top];
          s ->top--;
          return x;
      }
}
int gettop_seqstack(pseqstack s)
{
      int x;
      if(empty_seqstack(s))
          return 0;
      else
      {
          x = s->data[s ->top];
          return 1;
      }
}
void change(int x,int n)
{
      int temp;
         
     while(x != 0)
      {
          temp = x % n;
          input_seqstack(temp);
          x = x / n;
      }
      while(s->top>-1)
      {
          temp=out_seqstack();
          printf("%d",temp);     
      }
      return;
}
int main(void)
{
      int i,j;
      init_seqstack();
      printf("输入要转化的进制整数:\n");
      scanf("%d",&i);
      printf("选择你要转换成的进制数:\n");
      scanf("%d",&j);
      change(i,j);
      return 0;
}


效果


输入要转化的进制整数:
12
选择你要转换成的进制数:
2
1100
paul@ubuntu:~/prog$ ./a.out
输入要转化的进制整数:
15
选择你要转换成的进制数:
8
17
paul@ubuntu:~/prog$


[ 本帖最后由 pauljames 于 2012-5-27 16:00 编辑 ]

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2012-05-27 15:58
fan1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-5-23
得分:0 
回复 2楼 pauljames
能帮画下流程图么?谢谢!
2012-05-28 14:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-368056-1-1.html




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

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