#代码如下:
#include<stdio.h>
#include<math.h>
void ball(int k){
double r;
printf("Please enter the radius:\n");
scanf("%lf",&r);
printf("%.2f\n",4.0/3*3.1415926*pow(r,3));
printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");
}
void squre(int k){
double r,h;
printf("Please enter the radius and the height:\n");
scanf("%lf%lf",&r,&h); //测试数据:
1
2
3
2.4 3
0
printf("%f %f",r,h); //这个是用来看r和h的数值,结果显示r=3 h=2.4 好困惑!!!
if(k==2) printf("%.2f\n",3.1415926*pow(r,2)*h);
else printf("%.2f\n",1.0/3*4*pow(r,2)*h);
printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");
}
int main(void)
{
int k;
printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");
scanf("%d",&k);
switch(k){
case 1: ball(k);
case 2:
case 3: squre(k);
default: break;
}
return 0;
}
主要是因为switch-case结构里面,第一个case后面没加break,执行完第一个case,又继续执行其他case里的函数而导致结果不对,给第一个case添加一个break,然后整理下流程应该就可以了。
修改如下:
参考链接:
#include<stdio.h>
#include<math.h>
// 每次计算前要显示的菜单
void menu(){
printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");
}
// 计算球体体积的函数
void ball(int k){
double r;
printf("Please enter the radius:\n");
scanf("%lf",&r);
printf("%.2f\n",4.0/3*3.1415926*pow(r,3));
}
// 计算圆柱体和圆锥体体积的函数
void squre(int k){
double r,h;
printf("Please enter the radius and the height:\n");
scanf("%lf%lf",&r,&h);
//测试数据:
/*
1
2
3
2.4 3
0
*/
// printf("%f %f",r,h); //这个是用来看r和h的数值,结果显示r=3 h=2.4 好困惑!!!
if(k==2) printf("%.2f\n",3.1415926*pow(r,2)*h);
// 根据题目信息,求圆锥体的体积,应该是1.0/3*PI*pow(r,2)*h
// 这里PI按照上面的 3.1415926来计算圆锥体的体积
// https://miniwebtool.com/zh-cn/volume-of-a-cone-calculator/
else printf("%.2f\n",1.0/3*3.1415926*pow(r,2)*h);
}
int main(void)
{
int k;
menu();
scanf("%d",&k); // 获取选项序号
switch(k){
// 输入1 ,计算完球体体积,则退出swith,不再继续往下执行
case 1: ball(k);break;
// 输入2和3,则计算对应圆柱体和圆锥体体积, 计算完成,则退出switch
case 2:
case 3: squre(k);break;
default: break;
}
return 0;
}
1、在屏幕上输出以下图案:菱形的打印
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
//C语言
//输出菱形
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
void Diamond(int line) {
if (line % 2 == 0) {
printf("菱形的行数为奇数,请输入奇数!\n");
}
else
{
int hang = line / 2 + 1;
int col = line / 2;
for (int i = 1; i <= hang; i++) {
for (int j = 1; j <= col - i + 1; j++) {
printf(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
printf("*");
}
printf("\n");
Sleep(500);
}
for (int i = 1; i < hang; i++) {
for (int j = 0; j < i; j++) {
printf(" ");
}
for (int k = 0; k < line - (2 * i - 1) - 1; k++) {
printf("*");
}
printf("\n");
Sleep(500);
}
}
system("pause");
}
void main() {
int line;
printf("请输入行数:");
scanf_s("%d",&line);
Diamond(line);
}
3、求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,例如:2+22+222+2222+22222
//C语言
//求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,例如:2+22+222+2222+22222
//不能用于位数多
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/*void main() {
int num;
printf("请输入0~9中的数字:");
scanf_s("%d",&num);
int a = 11111 * num;
int b = 1111 * num;
int c = 111 * num;
int d = 11 * num;
printf("%d+%d+%d+%d+%d=%d\n",a,b,c,d,num,a+b+c+d+num);
system("pause");
}*/
void main() {
int num=2;
int a=2;
int b = 0;
int c = 0;
int i = 0;
while(i<5){
b = b + a;
c= c + b;
a = a * 10;
++i;
}
printf("%d",c);
system("pause");
}
//又一钟方法。(摘)
/*void main()
{
int sum,a,n,i;
printf("a\n");
scanf("%d,%d",&n,&a);
for(sum=0,i=0;i<n;i++)
{
sum=sum+a;
a+=a*10;
}
printf("%d\n",sum);
}*/
4、编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现。
/编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现。
#include <stdio.h>
#include<stdlib.h>
void main()
{
int ch;
int count = 0;
while((ch = getchar()) != EOF)
{
if(ch == '{')
{
count ++;
}
if(ch == '}' && count == 0)
{
printf("不匹配\n");
}
if(ch == '}' && count !=0)
{
count --;
}
}
if(count == 0)
{
printf("匹配!\n");
}
else
{
printf("不匹配!\n");
}
system("pause");
}
5、5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果
A选手说:B第二,我第三;
B选手说:我第二,E第四;
C选手说:我第一,D第二;
D选手说:C最后,我第三;
E选手说:我第四,A第一;
比赛结束后,每位选手都说对了一半,请编程确定比赛的名次。
//C语言
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果
//A选手说:B第二,我第三;
//B选手说:我第二,E第四;
//C选手说:我第一,D第二;
//D选手说:C最后,我第三;
//E选手说:我第四,A第一;
//比赛结束后,每位选手都说对了一半,请编程确定比赛的名次
void player_rank(int p1,int p2,int p3,int p4,int p5) {
for (p1 = 1; p1 <= 5;p1++) {
for (p2 = 1; p2 <= 5; p2++) {
for (p3 = 1; p3 <= 5; p3++) {
for (p4 = 1; p4 <= 5; p4++) {
for (p5 = 1; p5 <= 5; p5++) {
if ((p1!=p2)&&(p1!=p3)&&(p1!=p4)&&(p1!=p5)&&(p2!=p3)&&(p2!=p4)&&(p2!=p5)&&(p3!=p4)&&(p3!=p5)&&(p4!=p5)) {
//if(p1*p2*p3*p4*p5==120){
if (((p2 == 2) + (p1 == 3) == 1) &&
((p2 == 2) + (p5 == 4) == 1) &&
((p3 == 1) + (p4 == 2) == 1) &&
((p3 == 5) + (p4 == 3) == 1) &&
((p5 == 4) + (p1 == 1) == 1) == 1) {
printf("A=%d B=%d C=%d D=%d E=%d \n", p1, p2, p3, p4, p5);
}
}
}
}
}
}
}
}
void main(){
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
player_rank(a,b,c,d,e);
system("pause");
}
6、
日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个
嫌疑犯的一个。以下为4个嫌疑犯的供词。
A说:不是我。
B说:是C。
C说:是D。
D说:C在胡说
已知3个人说了真话,1个人说的是假话。
现在请根据这些信息,写一个程序来确定到底谁是凶手。
//C语言
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个
//嫌疑犯的一个。以下为4个嫌疑犯的供词。
//A说:不是我。
//B说:是C。
//C说:是D。
//D说:C在胡说
//已知3个人说了真话,1个人说的是假话。
//现在请根据这些信息,写一个程序来确定到底谁是凶手。
//如果A假,则A是凶手。B和D矛盾
//如果B假,不是C凶手,C和D矛盾
//如果C假,成立
//如果D假,则B和C矛盾
int find_x(char man[]) {
for (int temp = 0; temp < sizeof(man) / sizeof(man[0]);temp++) {
if (((man[temp]!='A')+(man[temp]=='C')+(man[temp]=='D')+(man[temp]!='D'))==3) {
printf("凶手是%c\n",man[temp]);
}
}
return 0;
}
void main() {
char xiongshou[4] = {'A','B','C','D'};
find_x(xiongshou);
system("pause");
}
7、在屏幕上打印杨辉三角。
1
1 1
1 2 1
1 3 3 1
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define ROW 6
#define COL 6
void main() {
int YHsanjiao[ROW][COL] = {1};
for (int row = 0; row < ROW; row++) {
YHsanjiao[row][0] =YHsanjiao[row][row]= 1;
}
for (int row = 2; row < ROW;row++) {
for (int col = 1; col <= row;col++) {
YHsanjiao[row][col] = YHsanjiao[row - 1][col - 1] + YHsanjiao[row - 1][col];
}
}
for (int row = 0; row < ROW;row++) {
for (int temp = 0; temp <= 2*(ROW -row); temp++) {
printf(" ");
}
for (int col = 0; col <= row;col++) {
printf("%4d",YHsanjiao[row][col]);
}
printf("\n");
}
system("pause");
}
答案:
原因是因为不同类型的数据在内存中的存储方式不同,在进行赋值或运算时可能会发生隐式类型转换,导致数据类型的变化,从而影响最终结果。
解决方案是尽量避免不同类型的数据之间的运算和赋值,如果必须进行,则需要注意数据类型之间的差异,进行显式类型转换,并根据具体情况选择合适的取值范围和精度。
可以通过以下代码来演示不同类型的数据在内存中的存储方式和相互之间的转换:
int main() { int n = 9; float pFloat = (float )&n; printf("n存储方式: %d\n", n); printf("pFloat存储方式: %f\n", pFloat); printf("n的值和pFloat的值: %d, %f\n", n, pFloat); return 0; }
代码的运行结果如下所示:
n存储方式: 9 pFloat存储方式: -14737312896.000000 n的值和pFloat的值: 9, -14737312896.000000
可以看到,通过将整型变量的地址转换为浮点型指针的地址,可以访问内存中的数据,但是由于存储方式的不同,取出的值可能并不是预期的结果。