| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 804 人关注过本帖
标题:4进制计算器
只看楼主 加入收藏
tedwugood
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2010-10-7
结帖率:60%
收藏
 问题点数:0 回复次数:1 
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);
}
搜索更多相关主题的帖子: 进制 计算器 
2010-10-10 02:02
tedwugood
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2010-10-7
收藏
得分:0 
自己做的 能运行 就是不出结果
/* simplecalc.c - A Simple Base-10 Calculator
/* simplecalc.c - A Simple Base-10 Calculator
* Adapted from page 281 of Rojiani
*/
#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 {ans=ten_four(ans);
// 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 num)
{
   float 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;

    return (result);
}


[ 本帖最后由 tedwugood 于 2010-10-11 05:50 编辑 ]
2010-10-11 04:14
快速回复:4进制计算器
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016963 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved