本文实例为大家分享了C语言实现简单计算器的具体代码,供大家参考,具体内容如下
实现效果如图:
实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#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);
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_53391957/article/details/115867323