c-test02

1. 有以下c05_1.c程序
  #include <stdio.h>
  void  main()
{ char c1,c2;
  int n1,n2;
  c1=getchar();
c2=getchar();
n1=c1-'0';
n2= n1*10+(c2-'0');
  printf("%d\n",n2);
}
程序运行时输入:12<回车>,执行后输出结果是什么?
【提示】:
①getchar()函数用来输入两个字符分别赋给变量c1和c2;
②n1=c1-'0'相当于n1='1' -'0',字符1的ASCII码值比0大一,所以n1中的值为1,c2-'0'相当于'2' -'0';
③把字符串转换为多位数的算法。
2.从键盘输入1个3位数(100-999),计算输出这个3位数的个位、十位和百位之和。
#include
<stdio.h> #include <stdlib.h> int main() { int num,b0,b1,b2,sum; scanf("%d",&num); b2=num/100; //计算百位 b0=num%10; //计算个位 b1=(num/10)%10;//计算十位 sum=b0+b1+b2; printf("%d\n",sum); // printf("Hello world!\n"); return 0; } 测试样例:输入123 <回车> 输出6
3.从键盘输入一个大写英文字母,将其转化为小写英文字母后,将转换后的小写英文字母及其字母对应的ASCii码值显示到屏幕上。 #include <stdio.h> #include <stdlib.h> int main() { char a; scanf("%c",&a); a=a+32; printf("%c %d",a,a);//这句必须好好理解 return 0; } 测试样例 A a 97 Process returned 0 (0x0) execution time : 3.800 s Press any key to continue. 思考:可以将上面的scanf("%c",&a); 替换为a=getchar();吗? 4.从键盘输入圆的半径r,计算并输出圆的周长和面积。 #include <stdio.h> int main() { const double pi = 3.14159; double r, circum, area; printf("Input r:"); scanf("%lf", &r); circum = 2 * pi * r; area = pi * r * r; printf("printf WITHOUT width or precision specifications:\n"); printf("circumference = %f, area = %f\n", circum, area); printf("printf WITH width and precision specifications:\n"); printf("circumference = %7.2f, area = %7.2f\n", circum, area); return 0; } 运行结果: Input r: 5.3↙ printf WITHOUT width or precision specifications: circumference = 33.300854, area = 88.247263 printf WITH width and precision specifications: circumference = 33.30, area = 88.25 5.从键盘输入一个整数加法算式:操作数1+操作数2,然后计算并输出该表达式的计算结果,输出格式如下:操作数1+操作数2=计算结果 #include <stdio.h> int main() { int data1, data2, sum; char op; printf("Please input data1 + data2\n"); scanf("%d%c%d",&data1, &op, &data2); printf("%d%c%d = %d\n", data1, op, data2, data1+data2); return 0; } 测试样例: --------第1组---------- Please input data1 + data2 1+2 1+2 = 3 --------第2组------- Please input data1 + data2 12 + 3 12 4200192 = 4200204 思考:第2组数据中,为什么输入12、空格、+、空格和3后,运行结果会错误呢?

 

转载于:https://www.cnblogs.com/aimerrhythm/p/6695355.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值