計算機等級二級C語言程序設計100例三

計算機等級二級C語言程序設計100例三,第1張

計算機等級二級C語言程序設計100例三,第2張

[程序31]

標題:請輸入星期幾的第一個字母來判斷星期幾。如果第一個字母相同,繼續。

判斷第二個字母。

1.程序分析:用情景陳述比較好。如果第一個字母相同,用情景陳述或if陳述判斷第二個字母。

2.程序源代碼:

以下是引號片段:
# include
void main()
{
char letter;
printf("請輸入某一天的首字母/n");
while ((letter=getch())!='Y')
{ switch(letter)
{ case ' s ':printf(" please input second letter/n");
if((letter = getch())= = ' a ')
printf(" Saturday/n");
else if((letter = getch())= = ' u ')
printf(" Sunday/n");
else printf("數據錯誤/n");
break;
case ' F ':printf(" Friday/n");打破;
case ' M ':printf(" Monday/n");打破;
case 'T':printf("請輸入第二個字母/n");
if((letter = getch())= = ' u ')
printf(" Tuesday/n");
else if((letter = getch())= = ' h ')
printf(" Thursday/n");
else printf("數據錯誤/n");
break;
case 'W':printf("星期三/n");打破;
默認值:printf("數據錯誤/n");
}
}
}


[程序32]

題目:按任意鍵變色,要不要試試。請振作起來!

1.程序分析:

2.程序源代碼:

以下是一段引用片段:
# include
void main(void)
{
int color;
for(color = 0;顔色< 8;color )
{
text background(color);
c printf(" this is color % d/r/n",color);
cprintf("按任意鍵繼續/r/n");
getch();
}
}


[程序33]

主題:學習gotoxy()和clrscr()函數

1.程序分析:

2.程序源代碼:

以下是一個引號片段:
# include
void main(void)
{
clr SCR();
text background(2);
gotoxy(1,5);
cprintf("第5行第1列的輸出/n");
text background(3);
gotoxy(20,10);
cprintf("第10行第20列的輸出/n");
}


[程序34]

主題:練習函數調用

1.程序分析:

2.程序源代碼:

以下是引用片段:
# include
void Hello _ world(void)
{
printf(" Hello,world!/n");
}
void three _ hellos(void)
{
int counter;
for(counter = 1;計數器hello _ world();
}
void main(void)
{
three _ hellos();
}


[程序35]

標題:文本顔色設置

1.程序分析:

2.程序源代碼:

以下是一段引用片段:
# include
void main(void)
{
int color;
for(color = 1;顔色< 16;color )
{
text color(color);
c printf("這是顔色% d/r/n",color);
}
text color(128 15);
cprintf("這是閃爍的/r/n");
}
[程序36]

題目:求100以內的質數

1.程序分析:

2.程序源代碼:

下麪是一個引用片段:
# include
# include" math . h"
# define n101
main()
{
int I,j,line,a [n]。
for(I = 2;ifor(I = 2;I for(j = I 1;j {
if(a[i]!=0&&a[j]!= 0)
if(a[j]% a[I]= = 0)
a[j]= 0;}
printf("/n");
for(i=2,line = 0;i{
if(a[i]!=0)
{printf("]",a[I]);
line ;}
if(line = = 10)
{ printf("/n");
line = 0;}
}
}


[程序37]

標題:排序10個數字。

1.方案分析:可以使用選擇法,即從最後九個比較過程中,選擇最小的一個與第一個元素交換,

下一次,以此類推,將第二個元素與最後八個元素進行比較竝交換。

2.程序源代碼:

下麪是加引號的片段:
# define n10
main()
{ int I,j,min,tem,a[n];

printf("請輸入十個數:/n");
for(I = 0;i{
printf("a[%d]=",I);
scanf("%d",& a[I]);}
printf("/n");
for(I = 0;iprintf("]",a[I]);
printf("/n");

for(I = 0;i { min = i
for(j = I 1;jif(a[min]> a[j])min = j;
tem = a[I];
a[I]= a[min];
a[min]= tem;
}

printf("排序後/n");
for(I = 0;iprintf("]",a[I]);
}


[程序38]

題目:求一個3*3矩陣的對角元素之和。

1.程序分析:用double for循環控制輸入二維數組,再加一個[i][i]輸出。

2.程序源代碼:

以下是加引號的片段:
main()
{
float a[3][3],sum = 0;
int i,j;
printf("請輸入矩形元素:/n");
for(I = 0;I for(j = 0;j scanf("%f",& a[I][j]);
for(I = 0;I sum = sum a[I][I];
printf("對角仙他是%6.2f",sum);
}


[程序39]

標題:有一個已經排序的數組。現在輸入一個數字,按照原來的槼則插入數組。

1.程序分析:先判斷這個數是否大於最後一個數,再考慮插入中間數的情況。插入後

此元素後的數字依次曏後移動一個位置。

2.程序源代碼:

以下是引用:
main()
{
int a[11]= { 1,4,6,9,13,16,19,28,40,100 };
int temp1,temp2,number,end,I,j;
printf("原始數組是:/n");
for(I = 0;i printf("]",a[I]);
printf("/n");
printf("插入新號碼:");
scanf("%d",& number);
end = a[9];
if(number >end)
a[10]= number;
else
{ for(I = 0;I { if(a[I]>number)
{ temp 1 = a[I];
a[I]=數字;
for(j = I 1;j { temp 2 = a[j];
a[j]= temp 1;
temp 1 = temp 2;
}
break;
}
}
}
for(I = 0;i printf("m",a[I]);
}


[程序40]

標題:以逆序輸出數組。

1.程序分析:把第一個換成最後一個。

2.程序源代碼:

下麪是加引號的片段:
# define n5
main()
{ int a[n]= { 9,6,5,4,1},I,temp
printf("/n原始數組:/n");
for(I = 0;i printf("M",a[I]);
for(I = 0;I { temp = a[I];
a[I]= a[N-I-1];
a[N-I-1]= temp;
}
printf("/n排序數組:/n");
for(I = 0;i printf("M",a[I]);
}

位律師廻複

生活常識_百科知識_各類知識大全»計算機等級二級C語言程序設計100例三

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情