/*
从键盘读入一个字符cBegin和一个数iCount,
要求输出 ≤ cBegin的iCount个字符。
*/
#include <stdio.h>
int main(void)
{
char cBegin;
int iCount;
printf("Please Input a char and a number:");
scanf("%c%d",&cBegin,&iCount);
printf("Result:");
for(int i=0;i<iCount;i++){
printf("%c",cBegin-i);
}
putchar('\n');
return 0;
}
总结:
①输出ASCII码依次小于cBegin的iCount个字符。
/*
先从键盘读入5个整数,然后倒序输出这5个数。
*/
#include <stdio.h>
int main(void)
{
int a,b,c,d,e;
printf("请输入5个数:");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("这五个数的倒序为:%d %d %d %d %d\n",e,d,c,b,a);
return 0;
}
总结:
①简单的输入输出语句的应用。
/*
输入月份,输出2003年该月有几天。
当输入的月份超范围时,应输出“Invalid month input!”。
*/
#include <stdio.h>
int main(void)
{
int month;
printf("please input the month number:");
scanf("%d",&month);
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("2003.%d has 31 days\n",month);
break;
case 2:
printf("2003.%d has 28 days\n",month);
break;
case 4:
case 6:
case 9:
case 11:
printf("2003.%d has 30 days\n",month);
break;
default:
printf("Invalid month input!\n");
}
return 0;
}
总结:
①简单的switch多分枝语句实现指定输出2003年每个月有多少天。
/*
已知某公司员工的保底薪水为500,
某月所接工程的利润profit(整数)与利润提成的关系如下
(计量单位:元):
profit ≤ 1000 没有提成;
1000 < profit ≤ 2000 提成10%;
2000 < profit ≤ 5000 提成15%;
5000 < profit ≤ 10000 提成20%;
10000 < profit 提成25%。
请根据输入的利润计算员工的薪水。
*/
#include <stdio.h>
int main(void)
{
int bas=500,profit;
double salary;
printf("Input profit:");
scanf("%d",&profit);
if(profit<=1000){
salary = bas;
printf("salary = %.2lf\n",salary);
}else if(profit<=2000){
salary = bas+profit*0.1;
printf("salary = %.2lf\n",salary);
}else if(profit<=5000){
salary = bas+profit*0.15;
printf("salary = %.2lf\n",salary);
}else if(profit<=10000){
salary = bas+profit*0.2;
printf("salary = %.2lf\n",salary);
}else{
salary = bas+profit*0.25;
printf("salary = %.2lf\n",salary);
}
return 0;
}
总结:
①这里else语句又嵌套了多层if语句。
②优化:可直接将printf语句写在最后。(意思就是只用写一个printf语句)
/*
用scanf() 输入某年某月某日,
判断这一天是这一年的第几天。
以3月5日为例,应该先把前两个月的加起来,
然后再加上5天即本年的第几天,
特殊情况,闰年且输入月份 ≥ 3时需考虑多加一天。
注:判断年份是否为闰年的方法:
为400的倍数为闰年,如2000年;
若非100的倍数,而是4的倍数,为闰年,如1996年。
*/
#include <stdio.h>
int main(void)
{
int year,month,day,count;
printf("Please input year-month-day:");
scanf("%d-%d-%d",&year,&month,&day);
if(year%400==0 || (year%100!=0 && year%4==0)){
switch(month){
case 1:
count = day;
printf("It is the %dth day\n",count);
break;
case 2:
count = 31+day;
printf("It is the %dth day\n",count);
break;
case 3:
count = 31+29+day;
printf("It is the %dth day\n",count);
break;
case 4:
count = 31+29+31+day;
printf("It is the %dth day\n",count);
break;
case 5:
count = 31+29+31+30+day;
printf("It is the %dth day\n",count);
break;
case 6:
count = 31+29+31+30+31+day;
printf("It is the %dth day\n",count);
break;
case 7:
count = 31+29+31+30+31+30+day;
printf("It is the %dth day\n",count);
break;
case 8:
count = 31+29+31+30+31+30+31+day;
printf("It is the %dth day\n",count);
break;
case 9:
count = 31+29+31+30+31+30+31+31+day;
printf("It is the %dth day\n",count);
break;
case 10:
count = 31+29+31+30+31+30+31+31+30+day;
printf("It is the %dth day\n",count);
break;
case 11:
count = 31+29+31+30+31+30+31+31+30+31+day;
printf("It is the %dth day\n",count);
break;
case 12:
count = 31+29+31+30+31+30+31+31+30+31+30+day;
printf("It is the %dth day\n",count);
break;
}
}else{
switch(month){
case 1:
count = day;
printf("It is the %dth day\n",count);
break;
case 2:
count = 31+day;
printf("It is the %dth day\n",count);
break;
case 3:
count = 31+28+day;
printf("It is the %dth day\n",count);
break;
case 4:
count = 31+28+31+day;
printf("It is the %dth day\n",count);
break;
case 5:
count = 31+28+31+30+day;
printf("It is the %dth day\n",count);
break;
case 6:
count = 31+28+31+30+31+day;
printf("It is the %dth day\n",count);
break;
case 7:
count = 31+28+31+30+31+30+day;
printf("It is the %dth day\n",count);
break;
case 8:
count = 31+28+31+30+31+30+31+day;
printf("It is the %dth day\n",count);
break;
case 9:
count = 31+28+31+30+31+30+31+31+day;
printf("It is the %dth day\n",count);
break;
case 10:
count = 31+28+31+30+31+30+31+31+30+day;
printf("It is the %dth day\n",count);
break;
case 11:
count = 31+28+31+30+31+30+31+31+30+31+day;
printf("It is the %dth day\n",count);
break;
case 12:
count = 31+28+31+30+31+30+31+31+30+31+30+day;
printf("It is the %dth day\n",count);
break;
}
}
return 0;
}
总结:
①此段代码写得太没技术含量,因此对它进行优化。
#include <stdio.h>
int main(void)
{
int year,month,day,sum=0;
printf("Please input year-month-day:");
scanf("%d-%d-%d",&year,&month,&day);
//此switch语句用于统计平年此月前一共有多少天。
switch(month-1){
case 11:
sum += 30;
case 10:
sum += 31;
case 9:
sum += 30;
case 8:
sum += 31;
case 7:
sum += 31;
case 6:
sum += 30;
case 5:
sum += 31;
case 4:
sum += 30;
case 3:
sum += 31;
case 2:
sum += 28;
case 1:
sum += 31;
default:
;
}
if(year%400==0 || (year%100!=0 && year%4==0))
{
if(month>=3){
sum += day+1;
}
else{
sum += day;
}
}
else{
sum += day;
}
printf("\nIt is the %dth day.\n", sum);
return 0;
}
总结:
①千万别忘记给sum赋初值0。
/*
输入三角形的三边长a、b、c(边长可以是小数),
求三角形面积area,并输出。
如果输入的三边构不成三角形,
应给出“data error”的信息提示。
注:根据“海伦-秦九韶”公式,
area =sqrt(p*(p - a)*(p - b)*(p - c)),其中p =( a + b + c)/2。
*/
#include <stdio.h>
#include <math.h>
int main(void)
{
float a,b,c,p,area;
printf("please input triange sides:");
scanf("%f,%f,%f",&a,&b,&c);
if((a+b>c) && (a+c>b) && (b+c>a)){
p = (a+b+c)/2;
area = sqrt(p*(p - a)*(p - b)*(p - c));
printf("area=%.2f\n",area);
}
else{
printf("data error\n");
}
return 0;
}
总结:
①判断三角形成不成立为:两边之和大于第三边,否定为两边之和小于或等于第三边。
②使用sqrt()函数需要引入数学函数#include<math.h>。
/*
输出n行星号,每行5个星号“*”。
*/
#include <stdio.h>
int main(void)
{
int i,j,n;
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++){
for(j=0;j<5;j++){
printf("* ");
}
printf("\n");
}
return 0;
}
总结:
①这里使用了for循环的嵌套,第一个for循环控制输出行数,第二个for循环控制输出"*"个数。
②使用变量n,指定输出几行。
/*
从键盘输入3个整数,输出绝对值最大的数。
*/
#include <stdio.h>
#include <math.h>
int main(void)
{
int a,b,c,max;
printf("Input 3 numbers:");
scanf("%d,%d,%d",&a,&b,&c);
max=a;
if(abs(b)>abs(a)){
if(abs(b)>abs(c)){
max=b;
}else{
max=c;
}
}else{
if(abs(a)>abs(c)){
max=a;
}else{
max=c;
}
}
printf("The number with maximum absolute value is %d\n",max);
return 0;
}
总结:
①这里需要使用绝对值函数abs(),因此需要在源文件中写入#include<stdio.h>。
②利用max变量获取绝对值最大那个数。
/*
从键盘上输入两个实数,计算这两个实数的商(前面的数除以后面的数)。
*/
#include <stdio.h>
int main(void)
{
float a,b,result;
printf("Input 2 numbers:");
scanf("%f%f",&a,&b);
if(b!=0){
result = a/b;
printf("The result is:%.2f\n",result);
}else{
printf("Divid by zero\n");
}
return 0;
}
总结:
①简单的除法运算,注意除数不能为0的情况。
/*
用键盘输入的整数产生5 × 5矩阵N,
并按行输出该矩阵,每个元素占4个数位、右对齐。
*/
#include <stdio.h>
int main(void)
{
int num,i,j,a[5][5];
printf("please input an integer:");
scanf("%d",&num);
for(i=0;i<5;i++){
for(j=0;j<5;j++){
a[i][j] = num+i+j;
}
}
for(i=0;i<5;i++){
for(j=0;j<5;j++){
printf("%4d",a[i][j]);
}
printf("\n");
}
return 0;
}
总结:
①这里用到了二维数组,通过对二维数组的赋值,输出,解决该问题。