1.main()
{
float num1,num2;
ifloat he,cha,ji,shang;
printf("请输入两个数:");
scanf("%f,%f",&num1,&num2);
he=num1+num2;
cha=num1-num2;
ji=num1*num2;
shang=num1/num2;
printf("和为%f\n差为%f\n积为%f\n商为%f\n",he,cha,ji,shang);
}
这个不需要这么复杂吧
#include <stdio.h>
#include <conio.h>
int main(void)
{
float x,y; char o;
scanf("%f%c%f",&x,&o,&y);
switch(o)
{
case'+': printf("%f+%f=%f\n",x,y,x+y); break;
case'-': printf("%f+%f=%f\n",x,y,x-y); break;
case'*': printf("%f*%f=%f\n",x,y,x*y); break;
case'/': if(0==y) printf("Error\n");
printf("%f/%f=%f\n",x,y,x/y); break;
default: printf("It is unknow");
}
getch();
return 0;
}
这个我前几天才搞过,可以处理+-*/()和整数,不支持小数
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char* next;
double opd[1000];
int opt;
double *popd = opd;
#define PUSH(n) { *popd++ = n; }
#define POP() (*--popd)
#define TOP() (*(popd-1))
#define EMPTY() (popd == opd)
#define SIZE() (popd - opd)
void fail(char* s)
{
printf("match '%s' failed near '%c'.\n", s, *--next);
exit(0);
}
#define lbr() { if(*next++ == '(') ; else fail("("); }
#define rbr() { if(*next++ == ')') ; else fail(")"); }
void term();
void lv1();
void lv2();
void cal(char);
void expr()
{
term(); lv1();
}
void term()
{
double num = 0;
if(isdigit(*next)) {
do {
num = num * 10 + (*next - '0');
} while(isdigit(*++next));
PUSH(num);
} else if(*next == '(') {
lbr(); expr(); rbr();
} else {
fail("term");
}
}
void lv1()
{
while(1) {
char c = *next;
if(c == '+' || c == '-') {
++next;
lv2(); ++opt; cal(c);
} else if(*next == '*' || *next == '/') {
lv2();
} else {
break;
}
}
}
void lv2()
{
while(1) {
char c = *next;
if(c == '*' || c == '/') {
++next;
term(); ++opt; cal(c);
} else if(isdigit(*next)) {
term();
} else {
break;
}
}
}
double eval(char* s)
{
next = s;
expr();
if(*next || opt != 0) {
puts("invalid expression.");
exit(0);
}
return TOP();
}
void cal(char c)
{
double lhs, rhs, r;
if(SIZE() > 1) {
rhs = POP();
lhs = POP();
switch(c)
{
case '+': r = lhs + rhs; break;
case '-': r = lhs - rhs; break;
case '*': r = lhs * rhs; break;
case '/': r = lhs / rhs; break;
}
PUSH(r);
--opt;
}
}
int main()
{
printf("%g\n", eval("(20+2)*(6/2)"));
getchar();
return 0;
}
第二个太简单了
#include <stdio.h>
#include <ctype.h>
int main()
{
char a[] = "Hello, World!";
char* p = a;
for(; *p; ++p)
if(isalpha(*p) && islower(*p))
*p = toupper(*p);
puts(a);
getchar();
}
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息