《C语言程序设计现代方法:第2版 修订版》第三章课后编程题,水平有限,仅供参考。 1.
#include <stdio.h>
int main (void)
{
int year,month,date;
printf("Enter a date (mm/dd/yyyy):");
scanf("%d/%d%/%d",&month,&date,&year);
if(month<10)
printf("You entered the date%d0%d%d",year,month,date);
else
printf("You entered the date%d%d%d",year,month,date);
return 0;
}
#include <stdio.h>
int main (void)
{
int number,month,day,year;
float price;
printf("Enter item number:");
scanf("%d",&number);
printf("Enter unit price:");
scanf("%f",&price);
printf("Enter a date (mm/dd/yyyy):");
scanf("%d/%d%/%d",&month,&day,&year);
printf("Item Unit Purchase\n");
printf(" Price Date\n");
if(month<10)
printf("%d $%6.2f 0%d/%d/%d",number,price,month,day,year);
else
printf("%d $%6.2f %d/%d/%d",number,price,month,day,year);
return 0;
}
#include <stdio.h>
int main (void)
{
int prefix,identifier,code,number,digit;
printf("Enter ISBN:");
scanf("%d-%d-%d-%d-%d",&prefix,&identifier,&code,&number,&digit);
printf("GS1 prefix:%d\n",prefix);
printf("Group identifier:%d\n",identifier);
printf("Publisher code:%d\n",code);
printf("Item number:%d\n",number);
printf("Check digit:%d",digit);
return 0;
}
4
#include <stdio.h>
int main (void)
{
int phone,number_head,number_end;
printf("Enter phone number [(xxx) xxx-xxxx]:");
scanf("(%d) %d-%d",&phone,&number_head,&number_end);
printf("You entered %d.%d.%d",phone,number_head,number_end);
return 0;
}
5
#include <stdio.h>
int main (void)
{
int num1,num2,num3,num4,num5,num6,num7,num8;
int num9,num10,num11,num12,num13,num14,num15,num16;
printf("Enter the number fron 1 to 16 in any order:");
scanf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",&num1,&num2,&num3,&num4,&num5,&num6,&num7,&num8,&num9,&num10,&num11,&num12,&num13,&num14,&num15,&num16);
printf("%2d%3d%3d%3d\n",num1,num2,num3,num4);
printf("%2d%3d%3d%3d\n",num5,num6,num7,num8);
printf("%2d%3d%3d%3d\n",num9,num10,num11,num12);
printf("%2d%3d%3d%3d\n",num13,num14,num15,num16);
printf("\nRow sums:%3d%3d%3d%3d",num1+num2+num3+num4,num5+num6+num7+num8,num9+num10+num11+num12,num13+num14+num15+num16);
printf("\nColumn sums:%3d%3d%3d%3d",num1+num5+num9+num13,num2+num6+num10+num14,num3+num7+num11+num15,num4+num8+num12+num16);
printf("\nDiagonal sums:%3d",num1+num6+num11+num16);
return 0;
}
6
#include <stdio.h>
int main (void)
{
int num1,denom1,num2,denom2,result_num,result_denom;
printf("Enter two fractions separated 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\n",result_num,result_denom);
return 0;
}
|