本文实例为大家分享了c语言实现简单计算器的具体代码,供大家参考,具体内容如下
实现效果如图:


实现代码如下:
#include<stdio.h>
#include<windows.h>//gotoxy
#include<conio.h>
#define width 80
#define height 30
void gotoxy(int x, int y);
void greateframe()
{
int i = 0;
for (i = 0 ; i < width; i += 2)
{
gotoxy(i, 0); printf("■");
gotoxy(i, height); printf("■");
}
for (i = 0; i < height + 1; i++)
{
gotoxy(0 , i); printf("■");
gotoxy(width, i); printf("■");
}
}
void add()
{
float i, j;
printf("*加法运算*\n");
scanf_s("%f %f", &i, &j);
printf("%5.2f + %5.2f=%5.2f\n", i, j, i+j);
_getch();
}
void sub()
{
float i, j;
printf("*减法运算*\n");
scanf_s("%f %f", &i, &j);
printf("%5.2f - %5.2f=%5.2f\n", i, j, i -j);
_getch();
}
void mul()
{
float i, j;
printf("*乘法运算*\n");
scanf_s("%f %f", &i, &j);
printf("%5.2f * %5.2f=%5.2f\n", i, j, i * j);
_getch();
}
void div()
{
float i, j;
printf("*除法运算*\n");
scanf_s("%f %f", &i, &j);
printf("%5.2f / %5.2f=%5.2f\n", i, j, i / j);
_getch();
}
int main(int argc, char* argv[])
{
int choose;
while (1)
{
greateframe();
gotoxy(width / 3, height / 3 + 2);
printf("1.加法");
gotoxy(width / 3, height / 3 + 4);
printf("2.减法");
gotoxy(width / 3, height / 3 + 6);
printf("3.乘法");
gotoxy(width / 3, height / 3 + 8);
printf("4.除法");
gotoxy(width / 3, height / 3 + 10);
printf("5.退出程序");
gotoxy(width / 3, height / 3);
printf("请选择你要计算的方式:( )\b\b");
scanf_s("%d", &choose);
switch (choose)
{
case 1:system("cls");
add(); break;
case 2:system("cls");
sub(); break;
case 3:system("cls");
mul(); break;
case 4:system("cls");
div(); break;
case 5:
exit(0);
}
system("cls");
}
}
void gotoxy(int x, int y)
{
coord pos;
pos.x = x;
pos.y = y;
setconsolecursorposition(getstdhandle(std_output_handle),pos);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
想当小明的人真多