C语言使用DOS参数传入实现简易计算器

一、程序如下:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void calc(char *s)
{
	char* pRes = strpbrk(s, "+-*/");
	if (!pRes)
	{
		printf("本软件支持+-*/运算,请按格式输入,例如:输入 xx 1+2 回车\n");
		return;
	}
	char c = *pRes;
	double left = atof(s);
	double right = atof(pRes + 1);
	double result = 0;
	switch (c)
	{
	case '+':
		result = left + right;
		break;
	case '-':
		result = left - right;
		break;
	case '*':
		result = left * right;
		break;
	case '/':
		result = left / right;
		break;
	}
	printf("result=%0.2lf\n", result);
}
int main(int argv, char* argc[])
{
	if (argv < 2)
	{
		printf("本软件支持+-*/运算,请按格式输入,例如:输入 xx 1+2 回车\n");
		return -1;
	}
	int i = 1;
	char s[200] = {0};
	while (i<argv)
	{
		strcat(s, argc[i++]);
	}
	calc(s);

	return 0;
}

发布了21 篇原创文章 · 获赞 20 · 访问量 2982

猜你喜欢

转载自blog.csdn.net/weixin_42844163/article/details/104105930