C语言程序设计现代方法第二版课后习题 第三章
1
#include <stdio.h>
int main(){
int month,day,year;
printf("Enter a date (mm/dd/yyyy): ");
scanf("%d/%d/%d", &month, &day, &year);
printf("You entered the date %d%.2d%d", year, month, day);
return 0;
}
2
#include <stdio.h>
int main(){
int i_n,month,day,year;
double price;
printf("Enter item number: ");
scanf("%d", &i_n);
printf("Enter unit price: ");
scanf("%lf", &price);
printf("Enter purchase date (mm/dd/yyyy): ");
scanf("%d/%d/%d", &month, &day, &year);
printf("Item\tUnit\t\tPurchase\n");
printf("\t\tPrice\t\tDate\n");
printf("%-d\t\t$%7.2f\t%-.2d/%-.2d/%-.4d", i_n, price, month, day, year);
return 0;
}
3
#include <stdio.h>
int main(){
int GP, GI, PC, IN, CD;
printf("Enter ISBN: ");
scanf("%d-%d-%d-%d-%d", &GP, &GI, &PC, &IN, &CD);
printf("GS1 prefix: %d\n", GP);
printf("Group identifier: %d\n", GI);
printf("Publisher code: %d\n", PC);
printf("Item number: %d\n", IN);
printf("Check digit: %d", CD);
return 0;
}
4
#include <stdio.h>
int main(){
int a, b, c;
printf("Enter phone number [(xxx) xxx-xxxx]: ");
scanf("(%d) %d-%d", &a, &b, &c);
printf("You entered %d.%d.%d", a, b, c);
return 0;
}
5
#include <stdio.h>
int main(){
int a[16];
printf("Enter the numbers from 1 to 16 in any order: \n");
for(int i=0; i<16; i++){
scanf("%d", &a[i]);
}
for(int i=0, k=0; i<4; i++){
for(int j=0; j<4; j++){
printf("%2d\t", a[k++]);
}
printf("\n");
}
printf("Row sums: ");
for(int i=0, k=0; i<4; i++){
int b = 0;
for(int j=0; j<4; j++){
b += a[k++];
}
printf("%d ", b);
if (i==3) printf("\n");
}
printf("Column sums: ");
for(int i=0; i<4; i++){
int b = 0;
for(int j=0; j<13+i; j+=4){
b += a[j];
}
printf("%d ", b);
if(i==3) printf("\n");
}
printf("Diagonal sums: %d %d", a[0]+a[5]+a[10]+a[15], a[3]+a[6]+a[9]+a[12]);
return 0;
}
6
#include <stdio.h>
int main(){
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;
}
以上代码仅供参考,可能存在错误欢迎大家交流指正。