C#include
#include
#include
Int zhuanru ();
Void zhuanchu (int num);
Int main (int arg c, char * argv [])
{
Char shuru, shuchu, s;
Int jiezhi;
//the while (1) {
Jiezhi=zhuanru ();
Zhuanchu (jiezhi);
//}
return 0;
}
Int zhuanru ()
{int b, I, a=0;
Char shuru, s [20];
Printf (" please enter the source data into the system (D - a decimal H - hexadecimal O - octal B - binary);" );
The scanf (" % s ", & amp; Shuru);
Printf (" please input the source data (please uppercase letters) : ");
The scanf (" % s ", & amp; S);
B=strlen (s);
Printf (" \ n b=% d ", b);
for(i=0; I{
If (shuru=='H')
{
If (s [I] & gt;='A' & amp; & S [I] <='Z')
S=int [I] [I] - 'A' (s) + 10 + '0';
A=a + ([I] - '0' (s) * (pow (16, b - 1 - I)));
}
If (shuru=='O')
{
A=a + ([I] - '0' (s) * (pow (8, b - 1 - I)));
}
If (shuru=='B')
{
A=a + ([I] - '0' (s) * (pow (2, 1 b - I)));
}
If (shuru=='D')
{
A=a + ([I] - '0' (s) * (pow (10, b - 1 - I)));
}
}
Printf (" \ n \ na=% d ", a);
return a;
}
Void zhuanchu (int num)
{int y, I=0, m=0, n=0, yushu;
Char z [20], a [20], [20] b;
Char shiliu [20]={' 0 ', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
Char shi [20]={' 0 ', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
Char ba [20]={' 0 ', '1', '2', '3', '4', '5', '6', '7'};
Char er [20]={' 0 ', '1'};
Char shuchu;
Printf (" please enter the target data base (D - a decimal H - hexadecimal O - octal B - binary);" );
The scanf (" % s ", & amp; Shuchu);
Printf (" \ n \ nnum=% d ", num);
If (shuchu=='H')
{y=16;
for(i=0; i<20; [I] i++) b=shiliu [I];
}
If (shuchu=='D')
{y=10;
for(i=0; i<20; [I] i++) b=shi [I];
}
If (shuchu=='O')
{y=8;
for(i=0; i<20; [I] i++) b=ba [I];
}
If (shuchu=='B')
{y=2;
for(i=0; i<20; [I] i++) b=er [I];
Printf (" \ ner=% s ", er);
Printf (" \ n b=% s ", b);
}
i=0;
While (num> 0)
{
Yushu=num % y;
A [I]=yushu;
//printf (" \ n \ na [I]=% d ", a [I]);
i++;
Num=num/y;
}
For (I=I - 1; I>=0; I -)
{m=a, [I].
//printf (" \ n \ nM=% d ", m);
Z=b [n] [m].
Printf (" \ n \ nb [m]=% d ", b) [m].
//printf (" \ n \ nZ [I]=% d ", z [I]);
N++;
}
Z [n]='\ 0';
Printf (" values for the converted: % s ", z);
}
Why this program function zhuanchu use inside loop will shiliu [20], ba [20] and other arrays cycle assigned to b [20] when make a mistake?
I separate the zhuanchu function as the main function of the assignment is right, but don't know how this is wrong, how to change? Thank you very much
(program is a hexadecimal conversion)
CodePudding user response:
# include & lt; Stdio. H>
#include
#include
Int zhuanru ();
Void zhuanchu (int num);
Int main (int arg c, char * argv []) {
Char shuru, shuchu, s;
Int jiezhi;
//the while (1) {
Jiezhi=zhuanru ();
Zhuanchu (jiezhi);
//}
return 0;
}
Int zhuanru () {
Int b, I, a=0;
Char shuru, s [20];
Printf (" please enter the source data into the system (D - a decimal H - hexadecimal O - octal B - binary);" );
The scanf (" % s ", & amp; Shuru);
Printf (" please input the source data (please uppercase letters) : ");
The scanf (" % s ", & amp; S);
B=strlen (s);
Printf (" \ n b=% d ", b);
for(i=0; iIf (shuru=='H') {
If (s [I] & gt;='A' & amp; & S [I] <='Z')
S=int [I] [I] - 'A' (s) + 10 + '0';
A=a + ([I] - '0' (s) * (pow (16, b - 1 - I)));
}
If (shuru=='O') {
A=a + ([I] - '0' (s) * (pow (8, b - 1 - I)));
}
If (shuru=='B') {
A=a + ([I] - '0' (s) * (pow (2, 1 b - I)));
}
If (shuru=='D') {
A=a + ([I] - '0' (s) * (pow (10, b - 1 - I)));
}
}
Printf (" \ n \ na=% d ", a);
return a;
}
Void zhuanchu (int num) {
Int y, I=0, m=0, n=0, yushu;
Char z [20], a [20], [20] b;
Char shiliu [20]={' 0 ', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
Char shi [20]={' 0 ', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
Char ba [20]={' 0 ', '1', '2', '3', '4', '5', '6', '7'};
Char er [20]={' 0 ', '1'};
Char shuchu;
Printf (" please enter the target data base (D - a decimal H - hexadecimal O - octal B - binary);" );
The scanf (" % s ", & amp; Shuchu);
Printf (" \ n \ nnum=% d ", num);
If (shuchu=='H') {
Y=16;
for(i=0; iB [y]='\ 0';
}
If (shuchu=='D') {
Y=10;
for(i=0; iB [y]='\ 0';
}
If (shuchu=='O') {
Y=8;
for(i=0; iB [y]='\ 0';
}
If (shuchu=='B') {
Y=2;
for(i=0; iB [y]='\ 0';
Printf (" \ ner=% 2 s ", er);
Printf (" \ n b=% s ", b);
}
i=0;
While (num> 0 {
Yushu=num % y;
A [I]=yushu;
//printf (" \ n \ na [I]=% d ", a [I]);
i++;
Num=num/y;
}
For (I=I - 1; I>=0; I -) {
M=a, [I].
//printf (" \ n \ nM=% d ", m);
Z=b [n] [m].
Printf (" \ n \ nb [m]=% d ", b) [m].
//printf (" \ n \ nZ [I]=% d ", z [I]);
N++;
}
Z [n]='\ 0';
Printf (" values for the converted: % s ", z);
}
//this program why zhuanchu inside using a loop () function converts a shiliu [20], ba [20] array cycle such as assigned to b made a mistake when [20] array?
//I alone the zhuanchu function as the main function of the assignment is right, but don't know how this is wrong, how to change? Thank you very much
//(program is a hexadecimal conversion)