C语言学习之路—简单计算器

简单计算器

在这里插入图片描述

#include <stdio.h>
#include <math.h>

int main(){
	int x, y;
	char ch;
	
	scanf("%d%c", &x, &ch);
	while(ch != '='){
		scanf("%d", &y);
		if(ch == '/' && y == 0){
			printf("ERROR");
			return 0;
		}
		switch(ch){
			case '+': x = x + y;break;
			case '-': x = x - y;break;
			case '*': x = x * y;break;
			case '/': x = x / y;break;
		}
		scanf("%c", &ch);
	}
	printf("%d", x);
	return 0;
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,可以使用图形库来实现可视化界面。常用的图形库有: 1. Graphics.h:这是一个简单的图形库,适用于学习和小型项目。但是它只能在 Windows 平台上使用。 2. SDL(Simple DirectMedia Layer):这是一个跨平台的多媒体库,可以用于创建游戏、图形界面等。它支持多种操作系统和编程语言。 3. OpenGL:这是一个跨平台的图形库,适用于创建高性能的三维图形界面。 以下是一个简易计算器的可视化界面的示例代码,使用 Graphics.h 库实现: ``` #include <graphics.h> #include <conio.h> #include <stdio.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, ""); int x, y, a, b; char c; setcolor(WHITE); settextstyle(DEFAULT_FONT, HORIZ_DIR, 2); outtextxy(100, 100, "Enter the first number: "); scanf("%d", &a); outtextxy(100, 150, "Enter the second number: "); scanf("%d", &b); setcolor(YELLOW); outtextxy(100, 200, "Choose an operation (+, -, *, /): "); c = getch(); switch(c) { case '+': x = a + b; break; case '-': x = a - b; break; case '*': x = a * b; break; case '/': x = a / b; break; default: printf("Invalid operation.\n"); return 0; } setcolor(GREEN); outtextxy(100, 250, "The result is: "); printf("%d\n", x); getch(); closegraph(); return 0; } ``` 这段代码使用 `initgraph()` 函数初始化图形界面,在屏幕上显示了几个提示信息和输入框,然后根据用户选择的操作计算结果并输出。最后使用 `closegraph()` 函数关闭图形界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值