主题:[讨论]4进制计算器求助
老师给了我们一个10进制计算器的模版 让我们编4进制计算器 我认为用3步即可:1.将输入的两个4进制数转成十进制数 2.用模版做十进制运算 3. 将结果转回成4进制数
这3个分开的程序我都已经写出 而且运行没有错误 但是我编完后程序无法正确运行 求助
#include<stdio.h>
#define TRUE 1
#define FALSE !TRUE
int four_ten(int a);
float ten_four(float b);
int main(void){
// declarations with descriptive variable names
int operand1,operand2,operand1_t,operand2_t;float ans;
char oprtr,ask;
int done = FALSE, problemflag = FALSE;
// Title prompt
printf("A Simple Base-4 Calculator\n");
do {// Loop while variable done is FALSE
// Prompt user for arithmetic expression
printf("Enter calculator command in the following form:\n");
printf("<operand> <oprtr> <operand>\n");
printf("Valid operators are +, -, *, and /.\n");
scanf("%d %c %d", &operand1, &oprtr, &operand2);
operand1_t=four_ten(operand1);operand2_t=four_ten(operand2);
// Based on operation, perform calculation
switch(oprtr) {
case '+':
ans = operand1_t + operand2_t;
break;
case '-':
ans = operand1_t - operand2_t;
break;
case '*':
ans = operand1_t * operand2_t;
break;
case '/':
if (operand2_t == 0.0) {
printf("Error. Cannot divide by 0.\n");
ans = 0;
problemflag = TRUE;
} else {
ans = operand1_t / operand2_t;
}
break;
default:
printf("*** Invalid operator. *** \n");
problemflag = TRUE;
break;
}
if (problemflag) {
// if there was a problem encountered
printf("Arithmetic operation not performed.\n");
problemflag = FALSE; // Our error checking flag reset
} else {
// no problem encountered, calc present result
printf("%f %c %f = %.2f", operand1, oprtr, operand2, ans);
}
printf("\n\n Enter '1' to continue or '0' to quit: ");
scanf("%d", &ask);
if (ask == 0) // ask if done
done = TRUE;
} while (!done);
return(0);
}
int four_ten(int A)
{
int x,n,ans,i=0,sum=0,dif=0;
scanf("%d",&A);
for(n=0;A/pow(10,n)!=0;n++){i=n;};
while(i>=0)
{x=pow(10,i);
ans=A/x*pow(4,i);
sum=sum+ans;
dif=A%x;
A=dif;
i--;
};
return (sum);}
float ten_four (float B)
{
float num,xs,b=0; int j=1,num_int,xs_int;
scanf("%f",&num);num_int=num;
xs=num-num_int;
for(xs_int=xs;xs-xs_int!=0.00&&j<=3;j++){
xs=xs*4;xs_int=xs;
b+=xs_int/pow(10,j);
xs=xs-xs_int;
};B=b;
return (B);
}
这3个分开的程序我都已经写出 而且运行没有错误 但是我编完后程序无法正确运行 求助
#include<stdio.h>
#define TRUE 1
#define FALSE !TRUE
int four_ten(int a);
float ten_four(float b);
int main(void){
// declarations with descriptive variable names
int operand1,operand2,operand1_t,operand2_t;float ans;
char oprtr,ask;
int done = FALSE, problemflag = FALSE;
// Title prompt
printf("A Simple Base-4 Calculator\n");
do {// Loop while variable done is FALSE
// Prompt user for arithmetic expression
printf("Enter calculator command in the following form:\n");
printf("<operand> <oprtr> <operand>\n");
printf("Valid operators are +, -, *, and /.\n");
scanf("%d %c %d", &operand1, &oprtr, &operand2);
operand1_t=four_ten(operand1);operand2_t=four_ten(operand2);
// Based on operation, perform calculation
switch(oprtr) {
case '+':
ans = operand1_t + operand2_t;
break;
case '-':
ans = operand1_t - operand2_t;
break;
case '*':
ans = operand1_t * operand2_t;
break;
case '/':
if (operand2_t == 0.0) {
printf("Error. Cannot divide by 0.\n");
ans = 0;
problemflag = TRUE;
} else {
ans = operand1_t / operand2_t;
}
break;
default:
printf("*** Invalid operator. *** \n");
problemflag = TRUE;
break;
}
if (problemflag) {
// if there was a problem encountered
printf("Arithmetic operation not performed.\n");
problemflag = FALSE; // Our error checking flag reset
} else {
// no problem encountered, calc present result
printf("%f %c %f = %.2f", operand1, oprtr, operand2, ans);
}
printf("\n\n Enter '1' to continue or '0' to quit: ");
scanf("%d", &ask);
if (ask == 0) // ask if done
done = TRUE;
} while (!done);
return(0);
}
int four_ten(int A)
{
int x,n,ans,i=0,sum=0,dif=0;
scanf("%d",&A);
for(n=0;A/pow(10,n)!=0;n++){i=n;};
while(i>=0)
{x=pow(10,i);
ans=A/x*pow(4,i);
sum=sum+ans;
dif=A%x;
A=dif;
i--;
};
return (sum);}
float ten_four (float B)
{
float num,xs,b=0; int j=1,num_int,xs_int;
scanf("%f",&num);num_int=num;
xs=num-num_int;
for(xs_int=xs;xs-xs_int!=0.00&&j<=3;j++){
xs=xs*4;xs_int=xs;
b+=xs_int/pow(10,j);
xs=xs-xs_int;
};B=b;
return (B);
}