4进制计算器
老师给了我们一个10进制计算器的模版 让我们编4进制计算器 我认为用3步即可:1.将输入的两个4进制数转成十进制数 2.用模版做十进制运算 3. 将结果转回成4进制数 这3个分开的程序我都已经写出 而且运行没有错误 但是我不知道怎么把他们合到一起 好心人帮帮忙把 谢谢:)
下面是3段程序
这是4进制转十进制的程序输入只要求正数
#include<stdio.h>
#include<math.h>
int main(void)
{
int A,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--;
};
printf("%d",sum);
return (0);}
这是10进制转4进制的程序 输出要求保留两位小数的浮点数
int main(void)
{
double num,xs,b=0,result; int sum=0,i=0,j=1,zs,num_int,xs_int;
scanf("%lf",&num);
zs=num;
while(zs)
{
sum+=zs%4*pow(10,i);
zs/=4;
i++;
};zs=sum;
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;
};result=sum+b;
printf("%.2f",result);
return (0);
}
这个是老师给的10进制计算器的模版 因为是贴过来的 有课能单引号出现问题改一下即可
/* simplecalc.c - A Simple Base-10 Calculator
* Adapted from page 281 of Rojiani
*/
#include<stdio.h>
#define TRUE 1
#define FALSE !TRUE
int main(void){
// declarations with descriptive variable names
double operand1,operand2,ans;
char oprtr,ask;
int done = FALSE, problemflag = FALSE;
// Title prompt
printf("A Simple Base-10 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("%lf %c %lf", &operand1, &oprtr, &operand2);
// Based on operation, perform calculation
switch(oprtr) {
case '+’:
ans = operand1 + operand2;
break;
case ’-’:
ans = operand1 - operand2;
break;
case ’*’:
ans = operand1 * operand2;
break;
case ’/’:
if (operand2 == 0.0) {
printf("Error. Cannot divide by 0.\n");
ans = 0;
problemflag = TRUE;
} else {
ans = operand1 / operand2;
}
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 = %f", 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);
}