C语言程序设计现代方法第二版,第三章课后编程习题全部答案

小白一只,可能会有错误

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\n");
	printf ("\t\tPrice\t\tDate\n");
	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\n", gp);
	printf ("Group identifier: %d\n", gi);
	printf ("Publisher code: %d\n", pc);
	printf ("Item number: %d\n", in);
	printf ("Check digit: %d\n", 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:\n");
	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 ("\n");
	}
	
	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 ("\n");
	
//	求列;
	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 ("\n");	

//  对角;
	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;
}

 

你可能感兴趣的:(C语言程序设计现代方法第二版,第三章课后编程习题全部答案)