第一题
代码示例:
#include<stdio.h>
int main()
{
printf("Enter a date(mm/dd/yyyy): ");
int day, month, year;
scanf_s("%d/%d/%d", &month, &day, &year);
printf("%04d%02d%02d", year, month, day);
return 0;
}
输出:
如果输出格式这样设置:
printf("%4d%2d%2d", year, month, day);
输出将为:
可见"d"前面的数字表示所占的位数,如果小于指定的位数,在前面加数字0,则会在高位补0,如果不加数字0,则为空,保留占位符。
第二题
示例代码:
#include<stdio.h>
int main()
{
printf("Enter item number: ");
int item;
scanf_s("%d", &item);
printf("Enter uint price: ");
float price;
scanf_s("%f", &price);
printf("Enter purchase date: ");
int month, day, year;
scanf_s("%d/%d/%d", &month, &day, &year);
printf("Item\t\tUint\t\tPurchase\n");
printf("\t\tPrice\t\tDate\n");
printf("%d\t\t$ %.2f\t%02d/%02d/%04d", item, price, month, day, year);
return 0;
}
输出:
第三题
示例代码:
#include<stdio.h>
int main()
{
printf("Enter ISBN: ");
int prefix, identifier, code, digit;
long number;
scanf_s("%d-%d-%d-%ld-%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: %ld\n", number);
printf("Check digit: %d\n", digit);
return 0;
}
输出:
第四题
示例代码:
#include<stdio.h>
int main()
{
printf("Enter phone number [(xxx) xxx-xxxx]: ");
int a, b, c;
scanf_s("(%d) %d-%d", &a, &b, &c);
printf("You entered %d.%d.%d", a, b, c);
return 0;
}
输出:
第五题
示例代码:
#include<stdio.h>
int main()
{
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
printf("Enter the numbers from 1 to 16 in any order: \n");
scanf_s("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n, &o, &p);
printf("%d\t%d\t%d\t%d\n", a, b, c, d);
printf("%d\t%d\t%d\t%d\n", e, f, g, h);
printf("%d\t%d\t%d\t%d\n", i, j, k, l);
printf("%d\t%d\t%d\t%d\n", m, n, o, p);
//row sum
int rowSum1, rowSum2, rowSum3, rowSum4;
int columnSum1, columnSum2, columnSum3, columnSum4;
int diagSum1, diagSum2;
rowSum1 = a + b + c + d;
rowSum2 = h + e + f + g;
rowSum3 = k + i + j + l;
rowSum4 = m + n + o + p;
//column sum
columnSum1 = a + e + i + m;
columnSum2 = b + f + j + n;
columnSum3 = c + g + k + o;
columnSum4 = d + h + l + p;
//diag sum
diagSum1 = a + f + k + p;
diagSum2 = d + j + g + m;
printf("Row sums: %d %d %d %d\n", rowSum1, rowSum2, rowSum3, rowSum4);
printf("Column sums: %d %d %d %d\n", columnSum1, columnSum2, columnSum3, columnSum4);
printf("Diagonal sums: %d %d\n", diagSum1, diagSum2);
return 0;
}
输出:
上面代码输出是左对齐的,而书中给出的示例是右对齐,虽然没有明确要求,但是这里给出实现代码:
#include<stdio.h>
int main()
{
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
printf("Enter the numbers from 1 to 16 in any order: \n");
scanf_s("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n, &o, &p);
printf("%2d\t%2d\t%2d\t%2d\n", a, b, c, d);
printf("%2d\t%2d\t%2d\t%2d\n", e, f, g, h);
printf("%2d\t%2d\t%2d\t%2d\n", i, j, k, l);
printf("%2d\t%2d\t%2d\t%2d\n", m, n, o, p);
//row sum
int rowSum1, rowSum2, rowSum3, rowSum4;
int columnSum1, columnSum2, columnSum3, columnSum4;
int diagSum1, diagSum2;
rowSum1 = a + b + c + d;
rowSum2 = h + e + f + g;
rowSum3 = k + i + j + l;
rowSum4 = m + n + o + p;
//column sum
columnSum1 = a + e + i + m;
columnSum2 = b + f + j + n;
columnSum3 = c + g + k + o;
columnSum4 = d + h + l + p;
//diag sum
diagSum1 = a + f + k + p;
diagSum2 = d + j + g + m;
printf("Row sums: %d %d %d %d\n", rowSum1, rowSum2, rowSum3, rowSum4);
printf("Column sums: %d %d %d %d\n", columnSum1, columnSum2, columnSum3, columnSum4);
printf("Diagonal sums: %d %d\n", diagSum1, diagSum2);
return 0;
}
输出:
可以发现已经和书中一致。
第六题
示例代码:
#include<stdio.h>
int main()
{
int num1, denom1, num2, denom2, result_num, result_denom;
printf("Enter two fractions separated by a plus sign: ");
scanf_s("%d/%d+%d/%d", &num1, &denom1, &num2, &denom2);
result_denom = denom1 * denom2;
result_num = num1 * denom2 + num2 * denom1;
printf("\nThe sum is: %d/%d", result_num, result_denom);
return 0;
}
输出:
这些题都是体力活,主要是慢慢练手感,一起加油。