C言語のプログラムは現代の方法の第2版を設計して、第3章の授業の後でプログラミングの練習問題のすべての解答

3431 ワード

シロが1匹、間違いがあるかもしれません
3.1
#include 

int main (void)
{
	int day, month, year;
	
	printf ("Enter a date (mm/dd/yyyy):");
	scanf ("%d/%d/%d", &month, &day, &year);
	
	printf ("You entered the date %d%.2d%.2d", year, month, day);
//	%.2d              2 
	
	return 0;
}

3.2
#include 

int main (void)
{
	int number;
	float price;
	int day, month, year;
	
	printf ("Enter item number:");
	scanf ("%d", &number);
	
	printf ("Enter unit price:");
	scanf ("%f", &price);
	
	printf ("Enter purchase date (mm/dd/yyyy):");
	scanf ("%d/%d/%d", &month, &day, &year);
	
	printf ("Item\t\tUnit\t\tPurchase
"); printf ("\t\tPrice\t\tDate
"); printf ("%d\t\t$%7.2f\t%.2d/%.2d/%d", number, price, month, day, year); return 0; }

3.3
#include 

int main (void)
{
	int gp, gi, pc, in, cd;
	
	printf ("Enter ISBN:");
	scanf ("%d-%d-%d-%d-%d", &gp, &gi, &pc, &in, &cd);
	
	printf ("Gs1 prefix: %d
", gp); printf ("Group identifier: %d
", gi); printf ("Publisher code: %d
", pc); printf ("Item number: %d
", in); printf ("Check digit: %d
", cd); return 0; }

3.4
#include 

int main (void)
{
	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;
	
}

3.5    これは配列を使わないのが面倒なようで、まだ少し知っていて、ガチョウはどこがうるさいと感じて、長すぎます
#include 

int main (void)
{
	int a[4][4];
	int i, j;
	
	printf ("Enter the numbers from 1 to 16 in any order:
"); for (i=0; i<4; i++){ for (j=0; j<4; j++){ scanf ("%d", &a[i][j]); } } for (i=0; i<4; i++){ for (j=0; j<4; j++){ printf ("%2d\t", a[i][j]); } printf ("
"); } int row[4], col[4], dia[4]; int sum = 0; // ; printf ("Row sums: "); for (i=0; i<4; i++){ sum = 0; for (j=0; j<4; j++){ sum += a[i][j]; } row[i] = sum; } for (i=0; i<4; i++) { printf ("%d ", row[i]); } printf ("
"); // ; printf ("Column sums: "); for (j=0; j<4; j++){ sum = 0; for (i=0; i<4; i++){ sum += a[i][j]; } col[j] = sum; } for (i=0; i<4; i++) { printf ("%d ", col[i]); } printf ("
"); // ; printf ("Diagonal sums: "); sum = 0; for (i=0; i<4; i++){ sum += a[i][i]; } dia[0] = sum; sum = 0; for (i=0; i<4; i++){ sum += a[i][3-i]; } dia[1] = sum; for (i=0; i<2; i++) printf ("%d ", dia[i]); return 0; }

3.6   蛇を描いて最后の分式の约分に足を添えたいですね.
#include 

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", result_num, result_denom);
	
	return 0;
}