user-0 提问: 大佬知道这个用c语言怎么写吗

回复:
user: user-1
content: //为什么printf（“”%c“”,a[i].data）输出不了 #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_SIZE 1024 typedef struct {     char data;     int weight;     int parent;     int lchild;     int rchild; } Huffnode,*Hufftree; typedef struct stack {     int top;     int elem[MAX_SIZE]; } Stack; void Initstack(Stack *s) {     s->top=-1; } void push(Stack *s,int e) {     s->top++;     s->elem[s->top]=e; } int pop(Stack *s) {     return s->elem[s->top--]; } bool isempty(Stack *s) {     if(s->top==-1)         return true;     else         return false; } void GetHuffmamCode(Hufftree a,int n) {     int i,j,s1,s2,max,c,p;     Stack s;     char ch;     Initstack(&s);     printf("输入字符集：");fflush(stdout);     rewind(stdin);     for(i=1;i<=n;i++) {         scanf("%c",&a[i].data);     }     for(i=1;i<=n;i++) {         printf("a[%d].data:%c\n",i,a[i].data);     }     printf("\n");     printf("输入权值：");fflush(stdout);     rewind(stdin);     for(i=1;i<=n;i++) {         scanf("%d",&a[i].weight);     }     for(i=1;i<=n;i++) {         printf("a[%d].weight:%d\n",i,a[i].weight);     }     printf("\n");     int m=2*n-1;     for(i=1;i<=n;i++) {         a[i].parent=0;         a[i].rchild=0;         a[i].lchild=0;     }     for(i=n+1;i<=m;i++) {         a[i].data=NULL;         a[i].weight=0;         a[i].parent=0;         a[i].lchild=0;         a[i].rchild=0;     }     for(i=n+1;i<=m;i++) {         max=65535;         for(j=1;j<=i-1;j++) {             if(a[j].parent==0&&a[j].weight<=max) {                 s1=j;                 max=a[j].weight;             }         }         a[s1].parent=i;         max=65535;         for(j=1;j<=i-1;j++) {             if(a[j].parent==0&&a[j].weight<=max) {                 s2=j;                 max=a[j].weight;             }         }         a[s2].parent=i;         a[i].weight=a[s1].weight+a[s2].weight;         a[i].lchild=s1;         a[i].rchild=s2;     }     for(i=1;i<=n;i++) {         c=i;         p=a[i].parent;         while(p!=0) {             if(a[p].lchild==c)                 push(&s,0);             else                 push(&s,1);             c=p;             p=a[p].parent;         }         printf("i:%d\n",i);         printf("%c\n",a[i].data);         while(!isempty(&s))             printf("%d",pop(&s));         printf("\n");     } } int main() {     Hufftree a;     int n;     int m;     printf("输入字符个数：");     scanf("%d",&n); //  m=2*n-1;     m=3*n;     a=(Hufftree)malloc(sizeof(Huffnode)*m);     GetHuffmamCode(a,n);     return 0; } //输入字符个数：3 //输入字符集：abc //a[1].data:a //a[2].data:b //a[3].data:c // //输入权值：10 20 30 //a[1].weight:10 //a[2].weight:20 //a[3].weight:30 // //i:1 //a //00 //i:2 //b //01 //i:3 //c //1 //

user: user-0
content: 引用 2 楼 赵4老师的回复:仅供参考：#include <stdio.h> #include <stdlib.h> #include <conio.h> unsigned int iv,i; unsigned char BCD[5];//定长10位BCD码 unsigned char sv[11]; void main() {     iv=123456789;//最大10位无符号正整数     //INT2BCD     sprintf(sv,"%010u",iv);     for (i=0;i<10;i+=2) {         BCD[i/2]=(sv[i]<<4)|(sv[i+1]&0x0F);     }     printf("BCD=%02x%02x%02x%02x%02x\n",BCD[0],BCD[1],BCD[2],BCD[3],BCD[4]);     //BCD2INT     for (i=0;i<10;i+=2) {         sv[i]='0'|(BCD[i/2]>>4);         sv[i+1]='0'|(BCD[i/2]&0x0F);     }     sscanf(sv,"%010u",&iv);     printf("iv=%010u\n",iv);     getch(); }  可以请你帮我看看为什么printf（“”%c“”,a[i].data）输出不了吗 #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_SIZE 1024 typedef struct{ char data; int weight; int parent; int lchild; int rchild; }Huffnode,*Hufftree; typedef struct stack{ int top; int elem[MAX_SIZE]; }Stack; void Initstack(Stack *s) { s->top=-1; } void push(Stack *s,int e) { s->top++; s->elem[s->top]=e; } int pop(Stack *s) { return s->elem[s->top--]; } bool isempty(Stack *s) { if(s->top==-1) return true; else return false; } void GetHuffmamCode(Hufftree a,int n) { int i,j,s1,s2,max,c,p; Stack s; char ch; Initstack(&s); printf("输入字符集："); for(i=1;i<=n;i++) { scanf("%c",&a[i].data); scanf("%c",&ch); } printf("输入权值："); for(i=1;i<=n;i++) { scanf("%d",&a[i].weight); scanf("%c",&ch); } int m=2*n-1; for(i=1;i<=n;i++) { a[i].parent=0; a[i].rchild=0; a[i].lchild=0; } for(i=n+1;i<=m;i++) { a[i].data=NULL; a[i].weight=0; a[i].parent=0; a[i].lchild=0; a[i].rchild=0; } for(i=n+1;i<=m;i++) { max=65535; for(j=1;j<=i-1;j++) { if(a[j].parent==0&&a[j].weight<=max) { s1=j; max=a[j].weight; } } a[s1].parent=i; max=65535; for(j=1;j<=i-1;j++) { if(a[j].parent==0&&a[j].weight<=max) { s2=j; max=a[j].weight; } } a[s2].parent=i; a[i].weight=a[s1].weight+a[s2].weight; a[i].lchild=s1; a[i].rchild=s2; } for(i=1;i<=n;i++) { c=i; p=a[i].parent; while(p!=0) { if(a[p].lchild==c) push(&s,0); else push(&s,1); c=p; p=a[p].parent; } printf("%c\n",a[i].data); while(!isempty(&s)) printf("%d",pop(&s)); printf("\n"); } } int main() { Hufftree a; int n; int m; printf("输入字符个数："); scanf("%d",&n); m=2*n-1; a=(Hufftree)malloc(sizeof(Huffnode)*m); GetHuffmamCode(a,n); return 0; }

user: user-1
content: 仅供参考：#include <stdio.h> #include <stdlib.h> #include <conio.h> unsigned int iv,i; unsigned char BCD[5];//定长10位BCD码 unsigned char sv[11]; void main() {     iv=123456789;//最大10位无符号正整数     //INT2BCD     sprintf(sv,"%010u",iv);     for (i=0;i<10;i+=2) {         BCD[i/2]=(sv[i]<<4)|(sv[i+1]&0x0F);     }     printf("BCD=%02x%02x%02x%02x%02x\n",BCD[0],BCD[1],BCD[2],BCD[3],BCD[4]);     //BCD2INT     for (i=0;i<10;i+=2) {         sv[i]='0'|(BCD[i/2]>>4);         sv[i+1]='0'|(BCD[i/2]&0x0F);     }     sscanf(sv,"%010u",&iv);     printf("iv=%010u\n",iv);     getch(); }

user: user-2
content: 先确定大小端。接下来就是指针操作。。