C Primer Plus Test 6(1)
試題來(lái)源:C Primer Plus (第6版)中文版
本章主要學(xué)習(xí)循環(huán)語(yǔ)句的使用。

6.1編寫(xiě)一個(gè)程序,創(chuàng)建一個(gè)包含26個(gè)元素的數(shù)組,并在其中存儲(chǔ)26個(gè)小寫(xiě)字母。然后打印數(shù)組的所有內(nèi)容。
#include<stdio.h>
#include<stdlib.h>
int main(){
????char ch[26];
????char c;
????int i;
????for (i = 0; c = 'a'; c <= 'z'; i++, c++){
????????ch[i] = c;
????}
????for (i = 0; i < 26; i ++){
????????printf("%c\t", ch[i]);
????}
????printf("\n");
????system("pause");
????return 0;
}

6.2使用嵌套循環(huán),按下面的格式打印字符:
$
$$
$$$
$$$$
$$$$$
#include<stdio.h>
#include<stdlib.h>
int main(){
????int i, j;
????for (i=1; i<6; i++){
????????for(j=0; j<i; j++){
????????????printf("$");
????????}
????????printf("$");
????}
????system("pause");
????return 0;
}

6.3 使用嵌套循環(huán),按下面格式打印字母:
F
FE
FED
FEDC
FEDCB
FEDCBA
#include<stdio.h>
#include<stdlib.h>
int main(){
????char ch;
????int i, j;
????for(i=1; i<7; i++){
????????ch = 'F";
????????for(j=i; j>0; j--){
????????????printf("%c", ch--);
?????????}
????????printf("\n");
????}
????system("pause");
????return 0;
}

6.4 使用嵌套循環(huán),按下面的格式打印字母:
A
BC
DEF
GHIJ
KLMNO
PQRSTU
#include<stdio.h>
#include<stdlib.h>
int main(){
????char ch = 'A';
????int i, j;
????for(i=1; i<7; i++){
????????for(j=0; j<i; j++){
????????????printf("%c", ch++);
????????}
????????printf("\n");
????}
????system(“pause");
????return 0;
}

6.5 編寫(xiě)一個(gè)程序,提示用戶輸入大寫(xiě)字母,使用嵌套循環(huán)以下面
金字塔型的格式打印字母:??
A
ABA
ABCBA?
ABCDCBA
ABCDEDCBA
打印這樣的圖形,要根據(jù)用戶輸入的字母來(lái)決定,例如上面的圖像是在用戶輸入
E后的打印結(jié)果。
提示:用外層循環(huán)處理行,每行使用3個(gè)內(nèi)層循環(huán),分別處理空格、以升序打印字母、
以降序打印字母。
#include<stdio.h>
#include<stdlib.h>
int main(){
????char ch, nul, f_ch;
????int i, j, k, d, m;
????printf("Enter a zimu:");
????scanf("%c", &ch);
????d = ch - 'A' + 1; //計(jì)算空格數(shù)
????nul = ' '; //空格
????for(i=0; i<d; i++){
????????f_ch = 'A';
????????for(j=1; j<d-i; j++){
????????????printf(" %c", nul);
????????}
????????for (k=0; k<i+1; k++){
????????????printf("%c", f_ch++);
????????}
????????f_ch --;
????????for(m=0; m<i; m++){
????????????printf("%c", --f_ch);
????????}
????}
????system("pause");
????return 0;
}

6.6 編寫(xiě)一個(gè)程序,打印一個(gè)表格,每行打印一個(gè)整數(shù)、該數(shù)的平方、該數(shù)的立方。要求用書(shū)輸入表格的上下限。使用一個(gè)for循環(huán)。
# include<stdio.h>
#include<stdlib.h>
int main(){
????int max, min;
????int i;
????print("Enter the max:");
????scanf("%d", &max);
????print("Enter the min:")
????scanf("%d", &min);
????print("number\tsquare\tcube\n");
????for (i=min; i<=max; i++){
????????printf("%6d\t%6d\t%4d\n", i, i*i, i*i*i);
????}
????system('pause");
????return 0;
}

6.7 編寫(xiě)一個(gè)程序把一個(gè)單詞讀入一個(gè)字符數(shù)組中,然后倒序打印這個(gè)單詞。提示:strlen()可用于計(jì)算數(shù)組最后有一個(gè)字符的下標(biāo)。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 20
int main(){
????char str, ch[MAX];
????int i, status;
????printf("Enter a word");
????scanf("%s", ch);
????for(i=strlen(ch)-1; i>=0; i--){
????????printf("%c", ch[i]);
????}
????printf("\n");
????system("pause");
????return 0;
}

6.8 編寫(xiě)一個(gè)程序,要求用戶輸入兩個(gè)浮點(diǎn)數(shù),并打印兩數(shù)之差除以兩數(shù)乘積的結(jié)果。在用戶輸入非數(shù)字之前,程序應(yīng)循環(huán)處理用戶輸入的每對(duì)值。
#include<stdio.h>
#include<stdlib.h>
int main(){
????float num1, num2, d, x,e;
????int status;
????printf("Enter two float number:");
????status = scanf("%f %f", &num1, &num2);
????while(status == 2){
????????if(num1 > num2){
????????????d = num1 - num2;
????????}else{
????????????d = num2 - num1;
????????}
????????x = num1 * num2;
????????e = d / x;
????????printf("%f\n", e);
????????printf("Enter two float(other to quit):");
????????status = scanf("%f %f", &num1, &num2);
????}
????printf("Bye\n");
????return 0;
}

6.9 修改6.8,用一個(gè)函數(shù)返回計(jì)算的結(jié)果。
#include<stdio.h>
#include<stdllib.h>
?float End(float num1, float num2){
????float d, x, e;
????if(num1 > num2){
????????d = num1 - num2;
????}else{
????????d = num2 - num1;
????}
????x = num1 * num2;
????e = d / x;
????return e;
}
int main(){
????float num1, num2;
????int stauts;
????printf("Enter tow float numbers:");
????status = scanf("%f %f", &num1, &num2);
????while(status == 2){
????????printf("%f\n", End(num1, num2));
????????printf("Enter two float (other to quit):");
????????status = scanf("%f %f", &num1, &num2);
????}
????printf("Bye\n");
????return 0;
}
