⽤C语⾔实现⼀个简单的计算器代码
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//预处理指令
int main(void)
{
double bNumber, Number, Result; //给加减乘除定义的变量
int No; //选项的定义变量
double a, b, c, x1, x2, Rad; //给⼀元⼀次⽅程定义的变量
int Ary_10; //定义进制的变量
char string[32]; //⼆进制变量定义
system ("title 简单计算器 —— By:HaGit"); //程序窗⼝上的标题
while(1)2进制转十进制在线计算器
{
//界⾯
printf ("┏┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┓\n");
printf ("┇请选择你要计算的⽅法: ┇\n");
printf ("┣┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┫\n");
printf ("┇加法请按_1 进制转换_5 ┇\n");
printf ("┇减法请按_2 求⼀元⼆次⽅程_6 ┇\n");
printf ("┇乘法请按_3 ┇\n");
printf ("┇除法请按_4 退出_0 ┇\n");
printf ("┗┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┛\n");
printf ("Please write down the number:");
scanf ("%d",&No);
if (No == 1)
{
// 加法
printf (" 请输⼊被加数:");
scanf ("%lf",&bNumber);
printf (" 请输⼊加数:");
scanf ("%lf",&Number);
Result = bNumber + Number;
printf (" 结果是: %lf\n\n",Result);
}
else if (No == 2)
{
// 减法
printf (" 请输⼊被减数:");
scanf ("%lf",&bNumber);
printf (" 请输⼊减数:");
scanf ("%lf",&Number);
Result = bNumber - Number;
printf (" 结果是: %lf\n\n",Result);
}
else if (No == 3)
{
// 乘法
printf (" 请输⼊被乘数:");
scanf ("%lf",&bNumber);
printf (" 请输⼊乘数:");
scanf ("%lf",&Number);
Result = bNumber * Number;
printf (" 结果是: %lf\n\n",Result);
}
else if (No == 4)
{
// 除法
printf (" 请输⼊被除数:");
scanf ("%lf",&bNumber);
printf (" 请输⼊除数:");
scanf ("%lf",&Number);
Result = bNumber / Number;
printf (" 结果是: %lf\n\n",Result);
}
else if (No == 5)
{
//进制转换的代码
printf("请输⼊需要转换的⼗进制数:");
scanf("%d", &Ary_10);
itoa (Ary_10, string ,2);
printf("⼆进制: %s\n", &string);
printf("⼋进制: %o\n", &Ary_10);
printf("⼗六进制: %x\n", &Ary_10);
}
else if (No == 6)
{
//求⼀元⼆次⽅程的解的代码
printf("请输⼊⼀元⼀次⽅程的a,b,c三个数:");
scanf("%lf%lf%lf",&a,&b,&c);
Rad = b*b - 4*a*c;
if (Rad > 0)
{
x1 = -b + sqrt(Rad) / (2*a);
x2 = -b - sqrt(Rad) / (2*a);
printf("有两个解 x1 = %lf, x2 = %lf\n", &x1, &x2); }
else if (Rad == 0)
{
x1 = -b / (2*a);
printf("只有⼀个解 x1 = %lf\n", &x1);
}
else
{
printf("⽆解\n");
}
}
else if (No == 0)
{
/
/ 退出程序
break;
}
else
{
// 输⼊的选项不对
printf(" 请输⼊正确的数字。\n\n");
}
system ("pause"); //按任意键继续
system ("cls"); //清屏
}
return 0;
getchar();
}
⽤C语⾔实现⼀个简单的 ,希望⼤家指点.......
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论