基本转换〜帮助我纠正它,因为它无法运行 [英] A base conversion~ help me to correct it since it can't run
问题描述
//基本转换
//目标:此程序将输入的数字
//从基数M转换为基数N.显示已转换的
//基数为N的数字。
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
#define LENGTH 20
int temp,m,n,i,r,base10,true;
char num [LENGTH],结果[LENGTH];
//此函数用于转换基数为M的数字
//进入基数10的数字。
void baseM_to_base10(无效)
{
base10 = 0;
for(i = 0; i< 20&& true = 1; i ++)//从基数N得到数字
{num [i] = getchar(); // sub。数字为字符串
if(num ==''\ n'')
{num [i] =''\'''; //如果上面的陈述是真的,num [i]将
等于字符串的结尾
true = 0; }} //结束
fflush(stdin); //洗掉多余的字母
for(i = strlen(num) - 1; i> = 0; i--)
{if(num [i ] ==''1'')
base10 = base10 + 1 * pow(2,strlen(num) - 1 - i);
}
}
//此函数用于将基数10
//中的数字转换为基数N中的数字。
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp [30],i;
for(i = 0; i< 30& number< base; i ++)
{
temp [i] = number%base;
number = number / base;
}
temp [i] = number;
printf("%in%u base是:,base;
for(i = 29; i< 30; i--)
{
if( temp [i]< 10)
printf("%u",temp);
if(temp [i]> 10 || temp [i] < 36)
printf("%uc",temp [i] +51);
}
}
main()
{
// m - 基础M(inp ut base)
// n - 基数N(输出基数)
// num - 基数为M的输入数字
//结果 - 转换后的数字N
//变量声明
//提示用户输入所需数据。
printf(" ; Base Conversion\\\
");
printf(" --------------- \\\
");
printf(请输入输入的数字:);
scanf("%s",num);
printf(" Base M(2 to to 36):);
scanf("%d"& m);
printf(" Base N(2 to 36):" );
scanf("%d"& n);
printf(" Result is%d \ n",result);
//执行基本转换
// - 调用函数baseM_to_base10以相应地转换数字
// - 调用函数base10_to_baseN相应地转换数字
//显示转换后的数字
系统(" PAUSE&q uot;);
}
//主程序结束
jy****@gmail.com 写道:
//基本转换
//目标:此程序将输入的数字从基数M转换为基数N
// 。在基础N中显示转换后的
//数字。
/ ** /样式在C中更好。
< blockquote class =post_quotes>
#include< stdio.h>
#include< stdlib.h>
#include< string.h> ;
#define LENGTH 20
int temp,m,n,i,r,base10,true;
char num [LENGTH],结果[LENGTH];
//此函数用于将基数M
//中的数字转换为基数为10的数字。
void baseM_to_base10(无效)
{
base10 = 0;
for(i = 0;我< 20&& true = 1; i ++)//从基数N
获取数字
我没有阅读所有代码。你确定要''true = 1''吗?应该
是''true == 1''?
{num [i] = getchar(); // sub。数字到字符串
if(num ==''\ n'')
''num''是一个数组,但''\ n''是一个字母。你不能比较它们。
{num [i] =''\'''; //如果上面的陈述是真的,num [i]将
等于字符串的结尾
true = 0; }} //结束
fflush(stdin); //洗掉多余的字母
for(i = strlen(num) - 1; i> = 0; i--)
{if(num [i ] ==''1'')
base10 = base10 + 1 * pow(2,strlen(num) - 1 - i);
您确定应该使用pow而不是''<<''?如果是这样,为什么不给你?b $ b包括math.h?
}
}
//此函数将基数为10的数字转换为基数为N的数字。
void base10_to_baseN( unsigned long long number,unsigned short base)
{
unsigned short temp [30],i;
for(i = 0; i< ; 30&& number< base; i ++)
{
temp [i] = number%base;
number = number /基础;
}
temp [i] =数字;
printf("%u base中的数字是:",base );
for(i = 29; i< 30; i--)
{
if(temp [i]< 10 )
printf("%u",temp);
可疑。
if(temp [i]> 10 || temp [i]< ; 36)
printf("%uc",temp [i] +51);
}
}
main()
''int main(void)''更好。
{
// m - 基数M(输入基数)
// n - 基数N(输出基数)
/ / num - 基数M中输入的数字
//结果 - 基数N转换后的数字
//变量声明
/ /提示用户输入所需数据。
printf(Base Conversion\\\
);
printf(" --------- ------ \\\
");
printf(请输入输入的数字:);
scanf("%s", num);
printf(" Base M(2 to 36):");
scanf("%d"& m);
打印f(Base N(2 to 36):);
scanf("%d"& n);
printf(" Result是%d \ n",结果);
//执行基本转换
// - 调用函数baseM_to_base10转换数字
相应的
// - 调用函数base10_to_baseN来转换数字
相应
//显示转换后的数字
system(PAUSE);
''暂停''在Linux中不可用,所以'不可移植。
}
//主程序结束
< jy **** @ gmail。 comwrote:
//基本转换
//目标:此程序用于转换输入的数字
//从基数M到基数N.在基数N中显示转换后的
//数字。
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
#define LENGTH 20
< snip>
以下代码在MingW上编译并运行,但产生了错误的答案。
我修复了因换行而导致的一些问题新闻组并输入了什么
是我想在几个地方你想要什么。我没有攻击 - 或者
甚至看 - 逻辑。据我了解你的问题,你想要能够运行。
能够运行。对我来说,它现在运行。我没有注意到我做出的更改,你应该能够看到我从你得到的错误消息中做了什么。
没有错误消息被删除来自错误消息。
我发现你的用户提示混淆。怎么样:输入基数,输出基数和
nbr?
int temp,m,n,i,r,base10,true;
char num [LENGTH],结果[LENGTH];
//此函数用于将基数M
//中的数字转换为基数10.
void baseM_to_base10(无效)
{
base10 = 0;
for(i = 0; i< 20&& true = 1; i ++)//从基数N得到数字
{num [i] = getchar(); // sub。数字为字符串
if(num ==''\ n'')
{num [i] =''\'''; //如果上面的陈述是真的,num [i]将
等于字符串的结尾
true = 0; }} //结束
fflush(stdin); //洗掉多余的字母
for(i = strlen(num) - 1; i> = 0; i--)
{if(num [i ] ==''1'')
base10 = base10 + 1 * pow(2,strlen(num) - 1 - i);
}
}
//此函数用于将基数10
//中的数字转换为基数N中的数字。
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp [30],i;
for(i = 0; i< 30& number< base; i ++)
{
temp [i] = number%base;
number = number / base;
}
temp [i] = number;
printf("%in%u base是:,base;
for(i = 29; i< 30; i--)
{
if( temp [i]< 10)
printf("%u",temp);
if(temp [i]> 10 || temp [i] < 36)
printf("%uc",temp [i] +51);
}
}
main()
{
// m - 基础M(inp ut base)
// n - 基数N(输出基数)
// num - 基数为M的输入数字
//结果 - 转换后的数字N
//变量声明
//提示用户输入所需数据。
printf(" ; Base Conversion\\\
");
printf(" --------------- \\\
");
printf(请输入输入的数字:);
scanf("%s",num);
printf(" Base M(2 to to 36):);
scanf("%d"& m);
printf(" Base N(2 to 36):" );
scanf("%d"& n);
printf(" Result is%d \ n",result);
//执行基本转换
// - 调用函数baseM_to_base10以相应地转换数字
// - 调用函数base10_to_baseN相应地转换数字
//显示转换后的数字
系统(" PAUSE&q uot;);
}
//主程序结束
>
int temp,m,n,i,r,base10,true;
char num [LENGTH],结果[LENGTH];
//此函数用于将基数M
//中的数字转换为基数为10的数字。
void baseM_to_base10(void)
{
base10 = 0;
for(i = 0;我< 20&& true = 1; i ++)//从基数N得到数字
{num [i] = getchar(); // sub。数字为字符串
if(num ==''\ n'')
{num [i] =''\'''; //如果上面的陈述是真的,num [i]将
等于字符串的结尾
true = 0; }} //结束
fflush(stdin); //洗掉多余的字母
for(i = strlen(num) - 1; i> = 0; i--)
{if(num [i ] ==''1'')
base10 = base10 + 1 * pow(2,strlen(num) - 1 - i);
}
}
//此函数用于将基数10
//中的数字转换为基数N中的数字。
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp [30],i;
for(i = 0; i< 30& number< base; i ++)
{
temp [i] = number%base;
number = number / base;
}
temp [i] = number;
printf("%in%u base是:,base;
for(i = 29; i< 30; i--)
{
if( temp [i]< 10)
printf("%u",temp);
if(temp [i]> 10 || temp [i] < 36)
printf("%uc",temp [i] +51);
}
}
main()
{
// m - 基数M(输入基数)
// n - 基数N(输出基数)
// num - 基数M中输入的数字
//结果 - 基数N转换后的数字
//变量声明
//提示用户输入所需的数据。
printf(Base Conversion\\\
);
printf(" ----------- ---- \\\
");
printf(请输入输入的数字:);
scanf("%s",num) ;
printf(" Base M(2 to 36):");
scanf("%d"& m);
printf(" Base N(2 to 36):");
scanf("%d"& n);
printf (结果是%d \ n,结果);
//执行基本转换
// - 调用函数baseM_to_base10转换数字
相应
// - 调用函数base10_to_baseN转换数字
一致
//显示转换后的数字
系统(PAUSE);
}
//主程序结束
" osmium" < r1 ******** @ comcast.netwrote:
以下代码在MingW上编译并运行,但产生了错误的
回答。我修复了新闻组中的换行引起的一些问题,并将
放在我想到的几个地方你想要的内容中。我没有攻击 - 甚至看 - 逻辑。据我了解你的问题,你想要能够运行
。对我来说,它现在运行。我没有引起注意
我所做的更改,你应该能够看到我从错误中得到的消息
你得到的消息。没有错误消息远离错误消息。
没有错误消息远离错误。
// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20
int temp, m, n, i, r, base10, true;
char num[LENGTH], result[LENGTH];
// This function is to convert a number in base M
// into a number in base 10.
void baseM_to_base10(void)
{
base10 = 0;
for( i=0; i<20 && true = 1; i++) // get the number from base N
{ num[i] = getchar(); // sub. the number into string
if (num == ''\n'')
{num[i] = ''\0''; // if the above statement is true , num[i] will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == ''1'')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
}
}
// This function is to convert a number in base 10
// into a number in base N.
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp[30],i;
for (i=0;i<30 && number<base;i++)
{
temp[i] = number % base;
number = number / base;
}
temp[i] = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);
if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);
}
}
main()
{
// m - base M (input base)
// n - base N (output base)
// num - inputted number in base M
// result - converted number in base N
// Variable Declaration
// Prompt the user to enter data required.
printf("Base Conversion\n");
printf("---------------\n");
printf("Please enter an inputted number: ");
scanf("%s",num);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// Perform Base Conversion
// - Call function baseM_to_base10 to convert the number
accordingly
// - Call function base10_to_baseN to convert the number
accordingly
// Display the converted number
system("PAUSE");
}
// The End Of Main Program
jy****@gmail.com wrote:// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N./**/ style is better in C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20
int temp, m, n, i, r, base10, true;
char num[LENGTH], result[LENGTH];
// This function is to convert a number in base M
// into a number in base 10.
void baseM_to_base10(void)
{
base10 = 0;
for( i=0; i<20 && true = 1; i++) // get the number from base NI haven''t read all of the code. Are you sure you want ''true=1''? Should
it be ''true == 1''?
{ num[i] = getchar(); // sub. the number into string
if (num == ''\n'')''num'' is an array, but ''\n'' is a char. You can NOT compare them.
{num[i] = ''\0''; // if the above statement is true , num[i] will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == ''1'')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);Are you sure you should use pow instead of ''<<''? If so, why don''t you
include math.h?
}
}
// This function is to convert a number in base 10
// into a number in base N.
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp[30],i;
for (i=0;i<30 && number<base;i++)
{
temp[i] = number % base;
number = number / base;
}
temp[i] = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);Suspicious.
if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);
}
}
main()''int main(void)'' is better.
{
// m - base M (input base)
// n - base N (output base)
// num - inputted number in base M
// result - converted number in base N
// Variable Declaration
// Prompt the user to enter data required.
printf("Base Conversion\n");
printf("---------------\n");
printf("Please enter an inputted number: ");
scanf("%s",num);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// Perform Base Conversion
// - Call function baseM_to_base10 to convert the number
accordingly
// - Call function base10_to_baseN to convert the number
accordingly
// Display the converted number
system("PAUSE");''Pause'' is NOT available in Linux, so that''s NOT portable.
}
// The End Of Main Program
<jy****@gmail.comwrote:
// Base Conversion
// Aim: This program is to convert an inputted number
// from base M into base N. Display the converted
// number in base N.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20<snip>
The following code compiles and runs on MingW but produces the wrong answer.
I fixed some problems caused by line breaks on newsgroups and put in what
was my guess as to what you wanted in a few places. I didn''t attack - or
even look at - the logic. As I understand your question you wanted to be
able to run. it runs now, for me. I didn''t draw attention to changes I
made, you should be able to see what I did from the error messages you get.
No error message was far removed from the error message.
I find your user prompts confusing. How about :input base, output base and
nbr?
int temp, m, n, i, r, base10, true;
char num[LENGTH], result[LENGTH];
// This function is to convert a number in base M
// into a number in base 10.
void baseM_to_base10(void)
{
base10 = 0;
for( i=0; i<20 && true = 1; i++) // get the number from base N
{ num[i] = getchar(); // sub. the number into string
if (num == ''\n'')
{num[i] = ''\0''; // if the above statement is true , num[i] will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == ''1'')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
}
}
// This function is to convert a number in base 10
// into a number in base N.
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp[30],i;
for (i=0;i<30 && number<base;i++)
{
temp[i] = number % base;
number = number / base;
}
temp[i] = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);
if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);
}
}
main()
{
// m - base M (input base)
// n - base N (output base)
// num - inputted number in base M
// result - converted number in base N
// Variable Declaration
// Prompt the user to enter data required.
printf("Base Conversion\n");
printf("---------------\n");
printf("Please enter an inputted number: ");
scanf("%s",num);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// Perform Base Conversion
// - Call function baseM_to_base10 to convert the number
accordingly
// - Call function base10_to_baseN to convert the number
accordingly
// Display the converted number
system("PAUSE");
}
// The End Of Main Program
>
int temp, m, n, i, r, base10, true;
char num[LENGTH], result[LENGTH];
// This function is to convert a number in base M
// into a number in base 10.
void baseM_to_base10(void)
{
base10 = 0;
for( i=0; i<20 && true = 1; i++) // get the number from base N
{ num[i] = getchar(); // sub. the number into string
if (num == ''\n'')
{num[i] = ''\0''; // if the above statement is true , num[i] will
equal end of string
true = 0 ; }} // end
fflush(stdin); // wash away the excess char
for(i = strlen(num) - 1; i >=0; i--)
{if(num[i] == ''1'')
base10 = base10 + 1 * pow(2 , strlen(num)- 1 - i);
}
}
// This function is to convert a number in base 10
// into a number in base N.
void base10_to_baseN(unsigned long long number,unsigned short base)
{
unsigned short temp[30],i;
for (i=0;i<30 && number<base;i++)
{
temp[i] = number % base;
number = number / base;
}
temp[i] = number;
printf("The number in %u base is : ",base);
for (i=29;i<30;i--)
{
if (temp[i]<10)
printf("%u",temp);
if (temp[i]>10 || temp[i]<36)
printf("%uc",temp[i]+51);
}
}
main()
{
// m - base M (input base)
// n - base N (output base)
// num - inputted number in base M
// result - converted number in base N
// Variable Declaration
// Prompt the user to enter data required.
printf("Base Conversion\n");
printf("---------------\n");
printf("Please enter an inputted number: ");
scanf("%s",num);
printf("Base M (2 to 36): ");
scanf("%d",&m);
printf("Base N (2 to 36): ");
scanf("%d",&n);
printf("Result is %d \n", result);
// Perform Base Conversion
// - Call function baseM_to_base10 to convert the number
accordingly
// - Call function base10_to_baseN to convert the number
accordingly
// Display the converted number
system("PAUSE");
}
// The End Of Main Program
"osmium" <r1********@comcast.netwrote:
The following code compiles and runs on MingW but produces the wrong
answer. I fixed some problems caused by line breaks on newsgroups and put
in what was my guess as to what you wanted in a few places. I didn''t
attack - or even look at - the logic. As I understand your question you
wanted to be able to run. it runs now, for me. I didn''t draw attention
to changes I made, you should be able to see what I did from the error
messages you get. No error message was far removed from the error message.No error message was far removed from the error.
这篇关于基本转换〜帮助我纠正它,因为它无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!