计算器 (连续运算)
用switch编程设计一个计算器
要求用户从键盘输入的表达式
操作数1运算符操作数2(例如3+2)
计算表达式的值 + - * /
要求 连续多次算数运算 每次运算结果后程序都给出提示 是否继续(Y/N)
#include<stdio.h>
#include<string.h>
int main()
{
float x,y;
char p;
float jieguo=0;
scanf("%f%c%f", &x, &p, &y);//第一次运算
do
{
switch (p)
{
case '+':
{
jieguo = x + y;
break;
}
case '-':
{
jieguo = x - y;
break;
}
case '*':
{
jieguo = x * y;
break;
}
case '/':
{
jieguo = x / y;
break;
}
}
printf("%.2f\n", jieguo);
printf("是否继续(Y/N)\n");
getchar();// 抵消换行
char y_n;
scanf("%c", &y_n);
getchar();// 抵消换行
if (y_n == 'N')
{
break;
}
scanf("%c%f", &p, &y);//继续运算
x = jieguo; //继续运算
} while (1);
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/190d099185f9c8803b1ae47f9f43c3af.png)
换硬币
用1元5角人民币兑换5分2分和1分的硬币(每一种都要有)共100枚,问共有几种兑换方法?每一种方案各换多少枚?
#include<stdio.h>
int main()
{
int count = 0;
for (int x = 1; x < 150; x++)
{
for (int y = 1; y < 75; y++)
{
for (int z = 1; z < 30; z++)
{
if ((x + y * 2 + z * 5 == 150)&&(x+y+z==100))
{
printf("一分硬币:%d 二分硬币:%d 五分硬币:%d\n", x, y, z);
count++;
}
}
}
}
printf("一共有%d种方法\n", count);
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/ef9059291748ac69a3889f54b26a3fc8.png)
计算2^0 + 2^1 + 2^2 + ... + 2^15的值
#include<stdio.h>
#include<math.h>
int main()
{
int sum1 = 0;
int sum2 = 1;
int j = 0;
for (int i = 0; i <= 15; i++)
{
for (; j <= i; j++)
{
sum2=pow(2,j);
}
sum1 += sum2;
}
printf("%d\n", sum1);
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/d41a94b4a30c9258bacfc29624b9339c.png)
打印所有水仙花数
打印所有的“水仙花数”。所谓"水仙花数"是指一个三位数,其各位数字的立方和等于该数本身。
例如,153是"水仙花数”,因为111+5+5*5+3+3*3=153。
#include<stdio.h>
#include<math.h>
int main()
{
for (int i = 100; i <= 999; i++)
{
if (pow(i % 10, 3) + pow(i / 10 % 10, 3) + pow(i / 100, 3) == i)
{
printf("%d\n", i);
}
}
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/616aebe0d1ed0dd8d3e840a344c946a2.png)
骰子每面出现的概率
编写程序模拟骰子的6000次投掷,
用函数rand(产生1--6之间的随机数ace,然后统计16每一面出现的机会(概率)存放到数组frequency中。
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
srand((unsigned int)time(NULL));
float frequency[6] = { 0 };
float a, b, c, d, e, f;
a = b = c = d = e = f = 0;
for (int i = 0; i < 6000; i++)
{
int x = 0;
x = rand() % 6;
switch (x)
{
case 0:
{
a++;
break;
}
case 1:
{
b++;
break;
}
case 2:
{
c++;
break;
}
case 3:
{
d++;
break;
}
case 4:
{
e++;
break;
}
case 5:
{
f++;
break;
}
}
}
frequency[0]= a / 6000;
frequency[1] = b / 6000;
frequency[2] = c / 6000;
frequency[3] = d / 6000;
frequency[4] = e / 6000;
frequency[5] = f / 6000;
for (int i = 0; i < 6; i++)
{
printf("%d出现的概率:%.2f\n", i + 1, frequency[i]);
}
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/a1869f58db39d1ac2adac8c527791dfe.png)
九九乘法表
编程输出如下"下三角形"的九九乘法表。
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
#include<stdio.h>
int main()
{
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
int sum = i * j;
printf("%d ", sum);
}
printf("\n");
}
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/3cc96cc6ea1be661f736d276a362042b.png)