挑选几个喜欢的程序以备日后回顾
- 编写一个程序,以月/日/年的格式录入,并以年月日的格式输出显示
#include <stdio.h>
int main(void){
int mm=0,dd=0,yyyy=0;
printf("enter a data for example ,mm/dd/yyyy");
scanf("%d/%d/%d",&mm,&dd,&yyyy);
printf("you entered the data%4.4d%2.2d%2.2d",yyyy,mm,dd);
return 0;
}
- 编写程序,对用户录入的产品信息进行格式化,其中产品编号和日期 采用左对齐,单位价格采用右对齐,允许最大取值为9999.99美元(制表符)
#include <stdio.h>
int main(void){
int item,mm,dd,yyyy;
float price;
printf("enter your number");
scanf("%d",&item);
printf("enter your price");
scanf("%f",&price);
printf("enter your data like this mm/dd/yyyy");
scanf("%d/%d/%d",&mm,&dd,&yyyy);
printf("Item\tPrice\t\tData\n");
printf("%d\t$%7.2f\t%4.4d%2.2d%2.2d",item,price,yyyy,mm,dd);
return 0;
}
- 图书用国际标准书号(ISBN)进行标识
#include <stdio.h>
int main(void){
printf("enter ISBN");
int GS1,identifier,publish_code,item_number,check_digit;
scanf("%d-%d-%d-%d-%d",&GS1,&identifier,&publish_code,&item_number,&check_digit);
printf("GS1 prefix:\t%d\nGroup identifier:\t%d\nPublisher code:\t%d\n\
Item number:\t%d\nCheck digit:\t%d\n",GS1,identifier,publish_code,item_number,check_digit);
return 0;
}
- 使用户可以同时输入两个分数,中间用加号隔开
#include <stdio.h>
int main(void){
int num1,denom1,num2,denom2,result_num,result_denom;
printf("Enter two fractions separted by a plus sign:");
scanf("%d/%d+%d/%d",&num1,&denom1,&num2,&denom2);
result_num=num1*denom2+num2*denom1;
result_denom=denom1*denom2;
printf("The sum is %d/%d",result_num,result_denom);
return 0;
}