导航:首页 > 软件知识 > 如何编程算加减乘除的程序

如何编程算加减乘除的程序

发布时间:2024-10-22 16:29:11

① 加减乘除的c语言编程怎么写

c语言的加减乘除可以这样写,假设有int变量a,b,c

加法:c=a+b

减法:c=a-b

乘法:c=a*b

除法:c=a/b

② 用c语言编程简单的加减乘除以及混合运算的计算器

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <math.h>
#include <conio.h>

typedef unsigned char bool_t;
enum bool_value
{
PAL_FALSE = 0,
PAL_TRUE
};

enum operate_value
{
PLUS = 0,
MINUS,
MULTIP,
DIVIDE
};

char* opr_str[] = {"+", "-", "*", "/" };

#define FLOAT 0x1
#define INT 0x2
#define OPR 0x3
#define NONE 0x0

/*去掉输错的字符,重新输出*/
#define BACK_GRACEFUL(s) \
{(s)--; \
*(s) = '\0'; \
system("cls"); \
display_menu(); \
printf("%s", in_str);}

/*判断是否是数字*/
#define IS_DIGIT(x) \
((x) <= '9' && (x) >= '0')

/*判断是否是运算符*/
#define IS_OPR(x) \
(((x) == '+') \
|| ((x) == '-') \
|| ((x) == '*') \
|| ((x) == '/'))

/*判断是否是小数点*/
#define IS_POINT(x) \
((x) == '.')

#define IS_CLS(x) \
((x) == 'c' || (x) == 'C')

#define IS_RTN(x) \
((x) == 'r' || (x) == 'R')

#define IS_EQUAL(x) \
((x) == '=' || (x) == 10 || (x) == 13)

#define CPY_RST \
{ \
memcpy(&pa1, &result, sizeof(result));\
strcpy(str_pa1, in_str); \
result.type = NONE; \
result.u.i = 0; \
}

#define CLS_MEM \
{ \
memset(in_str, 0 , strlen(in_str)); \
memset(str_pa1, 0, strlen(str_pa1));\
memset(str_pa2, 0, strlen(str_pa2));\
ppa1 = str_pa1; \
ppa2 = str_pa2; \
pstr = in_str; \
pa1.type = NONE; \
pa2.type = NONE; \
opr.type = NONE; \
system("cls"); \
display_menu(); \
result.type = NONE; \
result.u.i = 0; \
}

unsigned char2int(char *str)
{
unsigned int len = 0;
int i = 0;
unsigned int rtn = 0;
char max[] = "4294967295";

if(!str)
{
return 0;
}

len = strlen(str);
if(len > 10 || ( len == 10 && strncmp(max, str, 10) < 0))
{
return 0;
}

for(i = 0; i < (int)len; i++)
{
if(str[i] > '9' || str[i] < '0')
{
return 0;
}
}

for(i = 0; i < (int)len; i++)
{
rtn += (str[i] - '0') * (unsigned)pow(10, len - 1 - i);
}
return rtn;
}

int str2int(char *str, unsigned *ret)
{
char constant[11] = "4294967295";
int digit_num = 0;
unsigned res = 0;

if(str == (char*)0)
{
return -1;
}

if(strlen(str) > 10)
{
return -1;
}
else if(strlen(str) == 10)
{
if(strcmp(constant, str) < 0)
{
return -1;
}
}

digit_num = strlen(str);

for(int i = 0; i < digit_num; i++)
{
res += (str[i] - '0') * pow((double)10, (double)(digit_num - i -1));
}

*ret = res;
return 0;
}

int str2float(char *str, float *ret)
{
char *p = NULL;
char ints[10] = {0};
char fs[10] = {0};
unsigned int i = 0;
unsigned int flo = 0;
float f = 0.0;
unsigned len_f = 0;

if(!str)
{
return -1;
}

p = str;
while(*p != 0)
{
if(*p == '.')
{
p++;
continue;
}

if(*p < '0' || *p > '9')
{
return -1;
}
p++;
}

p = strchr(str, '.');
if(!p)
{
strcpy(ints, str);
}
else
{
strncpy(ints, str, p - str);
strcpy(fs, ++p);
len_f = strlen(fs);
}

i = char2int(ints);
flo = char2int(fs);
f = (float)flo / (float)pow(10, len_f);
f += i;
*ret = f;
return 0;
}

void display_menu()
{
printf("\n**********************************************\n");
printf("* *\n");
printf("* 计算器 1.0版 *\n");
printf("* *\n");
printf("* c(清屏) r(返回菜单) *\n");
printf("* *\n");
printf("**********************************************\n");
printf("\n\nin: ");
}

int get_input(char *in)
{
char ch;
ch = getch();
if(IS_DIGIT(ch) ||
IS_OPR(ch) ||
IS_POINT(ch))
{
printf("%c", ch);
}
else if(IS_CLS(ch) ||
IS_RTN(ch) ||
IS_EQUAL(ch))
{
;
}
else
{
return -1;
}

*in = ch;
return 0;
}

struct member
{
unsigned type;
union
{
unsigned i;
unsigned o;
float f;
}u;
};

int cal(struct member *pa1, struct member *opr,
struct member *pa2, struct member *ret)
{
int is_float;

if(!pa1 || !pa2 || !opr || !ret)
{
return -1;
}

if(pa1->type == FLOAT || pa2->type == FLOAT)
is_float = 1;
else
is_float = 0;

switch(opr->u.o)
{
case PLUS:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f += pa1->u.f;
else
ret->u.f += pa1->u.i;

if(pa2->type == FLOAT)
ret->u.f += pa2->u.f;
else
ret->u.f += pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i + pa2->u.i;
}
break;
case MINUS:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f += pa1->u.f;
else
ret->u.f += pa1->u.i;

if(pa2->type == FLOAT)
ret->u.f -= pa2->u.f;
else
ret->u.f -= pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i - pa2->u.i;
}
break;
case MULTIP:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f = pa1->u.f;
else
ret->u.f = pa1->u.i;

if(pa2->type == FLOAT)
ret->u.f *= pa2->u.f;
else
ret->u.f *= pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i * pa2->u.i;
}
break;
case DIVIDE:
if(is_float)
{
ret->type = FLOAT;
if(pa1->type == FLOAT)
ret->u.f = pa1->u.f;
else
ret->u.f = pa1->u.i;

if(pa2->type == FLOAT)
ret->u.f /= pa2->u.f;
else
ret->u.f /= pa2->u.i;
}
else
{
ret->type = INT;
ret->u.i = pa1->u.i / pa2->u.i;
}
break;
}
return 0;
}

void caculator()
{
char in_ch;
struct member result;
struct member pa1, pa2;
struct member opr;
char in_str[50] = {0}, *pstr;
char str_pa1[20] = {0}, *ppa1;
char str_pa2[20] = {0}, *ppa2;

CLS_MEM;

while(1)
{
if(0 == get_input(&in_ch))
{
*pstr++ = in_ch;
if(IS_DIGIT(in_ch))
{
if(result.type != NONE)
{
CLS_MEM;
}

if(opr.type != NONE)
{
if(pa2.type != NONE)
{
*ppa2++ = in_ch;
if(pa2.type == INT)
{
if(str2int(str_pa2, &pa2.u.i))
{
BACK_GRACEFUL(pstr);
ppa2--;
*ppa2 = 0;
str2int(str_pa2, &pa2.u.i);
continue;
}
}
else if(pa2.type == FLOAT)
{
if(str2float(str_pa2, &pa2.u.f))
{
BACK_GRACEFUL(pstr);
ppa2--;
*ppa2 = 0;
str2float(str_pa2, &pa2.u.f);
continue;
}
}
}
else
{
*ppa2++ = in_ch;

if(str2int(str_pa2, &pa2.u.i))
{
BACK_GRACEFUL(pstr);
ppa2--;
*ppa2 = 0;
str2int(str_pa2, &pa2.u.i);
continue;
}
pa2.type = INT;
}
}
else if(pa1.type == NONE)
{
pa1.type = INT;
*ppa1++ = in_ch;
if(str2int(str_pa1, &pa1.u.i))
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
str2int(str_pa1, &pa1.u.i);
continue;
}
}
else if(pa1.type == INT)
{
*ppa1++ = in_ch;
if(str2int(str_pa1, &pa1.u.i))
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
str2int(str_pa1, &pa1.u.i);
continue;
}
}
else if(pa1.type == FLOAT)
{
*ppa1++ = in_ch;
if(str2float(str_pa1, &pa1.u.f))
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
str2float(str_pa1, &pa1.u.f);
continue;
}
}
else
{
BACK_GRACEFUL(pstr);
ppa1--;
*ppa1 = 0;
continue;
}
}

if(IS_OPR(in_ch))
{
if(pa1.type == NONE && result.type != NONE)
CPY_RST;

if(pa1.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(opr.type == NONE)
{
opr.type = OPR;
switch(in_ch)
{
case '+':
opr.u.o = PLUS;
break;
case '-':
opr.u.o = MINUS;
break;
case '*':
opr.u.o = MULTIP;
break;
case '/':
opr.u.o = DIVIDE;
break;
}
continue;
}
else if(pa2.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}
else
{
if(pa2.u.i == 0 && opr.u.o == DIVIDE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(0 == cal(&pa1, &opr, &pa2, &result))
{
opr.type = OPR;
switch(in_ch)
{
case '+':
opr.u.o = PLUS;
break;
case '-':
opr.u.o = MINUS;
break;
case '*':
opr.u.o = MULTIP;
break;
case '/':
opr.u.o = DIVIDE;
break;
}
memset(in_str, 0, strlen(in_str));
memset(str_pa1, 0, strlen(str_pa1));
memset(str_pa2, 0, strlen(str_pa2));
ppa1 = str_pa1;
ppa2 = str_pa2;
pstr = in_str;
pa1.type = NONE;
pa2.type = NONE;

if(result.type == INT)
sprintf(in_str, "%d", result.u.i);
else
sprintf(in_str, "%f", result.u.f);

system("cls");
display_menu();
strcat(in_str, opr_str[opr.u.o]);
pstr = in_str + strlen(in_str);
printf("%s", in_str);

if(pa1.type == NONE && result.type != NONE)
{
CPY_RST;
}
}
}
}

if(IS_POINT(in_ch))
{
if(pa1.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(opr.type != NONE && pa2.type == NONE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(opr.type == NONE)
{
*ppa1++ = in_ch;
pa1.type = FLOAT;
}
else
{
*ppa2++ = in_ch;
pa2.type = FLOAT;
}
}

if(IS_CLS(in_ch))
{
CLS_MEM;
continue;
}

if(IS_RTN(in_ch))
{
return;
}

if(IS_EQUAL(in_ch))
{
if(pa1.type != NONE && opr.type != NONE
&& pa2.type != NONE)
{
if(pa2.u.i == 0 && opr.u.o == DIVIDE)
{
BACK_GRACEFUL(pstr);
continue;
}

if(0 == cal(&pa1, &opr, &pa2, &result))
{
memset(in_str, 0, strlen(in_str));
memset(str_pa1, 0, strlen(str_pa1));
memset(str_pa2, 0, strlen(str_pa2));
ppa1 = str_pa1;
ppa2 = str_pa2;
pstr = in_str;
pa1.type = NONE;
pa2.type = NONE;
opr.type = NONE;
system("cls");
display_menu();

if(result.type == INT)
sprintf(in_str, "%d", result.u.i);
else
sprintf(in_str, "%f", result.u.f);

pstr = in_str + strlen(in_str);
printf("%s", in_str);
}
}
}
}
}
}

void main()
{
while(1)
{
caculator();
}
}

③ 输入两个整数,进行加减乘除四则运算的c语言程序怎么写啊,拜托了~

代码

#include<stdio.h>

int main()

{

int a,b;

scanf("%d %d",&a,&b);

printf("%d ",a + b);

printf("%d ",a - b);

printf("%d ",a * b);

printf("%d ",a / b);

return 0;

}

运行截图


分析

C语言中的加减乘除和数学中的加减乘除一样,不同在于符号的表示问题,乘号需要用“*”表示。除号需要用“/”表示。新手学习C语言,很容易把除号和取余好混淆,强调一下,取余号是“%”,百分号就是取余的意思。因此在输入两个整数以后,按照数学方法就可以直接输出结果,整数的输入用scanf()函数。

阅读全文

与如何编程算加减乘除的程序相关的资料

热点内容
花呗怎么删掉交易 浏览:357
如何在网上查船只信息 浏览:816
做交易有什么忌讳 浏览:620
dj市场有什么 浏览:506
医院信息部是做什么工作的 浏览:103
南方电网代理购电是什么意思 浏览:928
鸡什么时候才能交易 浏览:395
抖音评论数据哪里看 浏览:581
学信网手机版如何修改个人信息 浏览:424
菜市场卖卤水怎么样 浏览:68
什么数据分析方案好 浏览:975
拍卖外国人房子要什么程序 浏览:527
手机信息通道是什么 浏览:170
如何跑设备清洗市场 浏览:223
哪里有钣金无损修复技术培训 浏览:302
什么机油是纳米技术 浏览:85
服务器数据库如何备份 浏览:264
湖里有哪些水产品 浏览:254
数据库光盘不见了怎么办 浏览:257
程序员微服务什么意思 浏览:603