生活随笔
收集整理的這篇文章主要介紹了
C语言程序设计50例(三)(经典收藏)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【程序31】 題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續 判斷第二個字母。 1.程序分析:用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母。 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 void main()
4 {
5 char letter;
6 printf(" please input the first letter of someday\n " );
7 while ((letter=getch())!=' Y ' )/* 當所按字母為Y時才結束 */
8 {
9 switch (letter)
10 {
11 case ' S ' :printf(" please input second letter\n " );
12 if ((letter=getch())==' a ' )
13 printf(" saturday\n " );
14 else if ((letter=getch())==' u ' )
15 printf(" sunday\n " );
16 else printf(" data error\n " );
17 break ;
18 case ' F ' :printf(" friday\n " );break ;
19 case ' M ' :printf(" monday\n " );break ;
20 case ' T ' :printf(" please input second letter\n " );
21 if ((letter=getch())==' u ' )
22 printf(" tuesday\n " );
23 else if ((letter=getch())==' h ' )
24 printf(" thursday\n " );
25 else printf(" data error\n " );
26 break ;
27 case ' W ' :printf(" wednesday\n " );break ;
28 default : printf(" data error\n " );
29 }
30 }
31 getch();
32 } 【程序32】 題目:Press any key to change color, do you want to try it. Please hurry up! 1.程序分析: 2.程序源代碼:
1 #include " conio.h "
2 #include " stdio.h "
3 void main(void )
4 {
5 int color;
6 for (color = 0 ; color < 8 ; color++)
7 {
8 textbackground(color);/* 設置文本的背景顏色 */
9 cprintf(" This is color %d\r\n " , color);
10 cprintf(" Press any key to continue\r\n " );
11 getch();/* 輸入字符看不見 */
12 }
13 } 【程序33】 題目:學習gotoxy()與clrscr()函數 1.程序分析: 2.程序源代碼:
1 #include " conio.h "
2 #include " stdio.h "
3 void main(void )
4 {
5 clrscr();/* 清屏函數 */
6 textbackground(2 );
7 gotoxy(1 , 5 );/* 定位函數 */
8 cprintf(" Output at row 5 column 1\n " );
9 textbackground(3 );
10 gotoxy(20 , 10 );
11 cprintf(" Output at row 10 column 20\n " );
12 getch();
13 } 【程序34】 題目:練習函數調用 1. 程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 void hello_world(void )
4 {
5 printf(" Hello, world!\n " );
6 }
7 void three_hellos(void )
8 {
9 int counter;
10 for (counter = 1 ; counter <= 3 ; counter++)
11 hello_world();/* 調用此函數 */
12 }
13 void main(void )
14 {
15 three_hellos();/* 調用此函數 */
16 getch();
17 } 【程序35】 題目:文本顏色設置 1.程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 void main(void )
4 {
5 int color;
6 for (color = 1 ; color < 16 ; color++)
7 {
8 textcolor(color);/* 設置文本顏色 */
9 cprintf(" This is color %d\r\n " , color);
10 }
11 textcolor(128 + 15 );
12 cprintf(" This is blinking\r\n " );
13 getch();
14 } 【程序36】 題目:求100之內的素數 1.程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " math.h "
3 #define N 101
4 main()
5 {
6 int i,j,line,a[N];
7 for (i=2 ;i<N;i++) a=i;
8 for (i=2 ;i<sqrt(N);i++)
9 for (j=i+1 ;j<N;j++)
10 {
11 if (a!=0 &&a[j]!=0 )
12 if (a[j]%a==0 )
13 a[j]=0 ;
14 }
15 printf(" \n " );
16 for (i=2 ,line=0 ;i<N;i++)
17 {
18 if (a!=0 )
19 {
20 printf(" %5d " ,a);
21 line++;
22 }
23 if (line==10 )
24 {
25 printf(" \n " );
26 line=0 ;
27 }
28 }
29 getch();
30 } 【程序37】 題目:對10個數進行排序 1.程序分析:可以利用選擇法,即從后9個比較過程中,選擇一個最小的與第一個元素交換, 下次類推,即用第二個元素與后8個進行比較,并進行交換。 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 #define N 10
4 main()
5 {
6 int i,j,min,tem,a[N];
7 /* input data */
8 printf(" please input ten num:\n " );
9 for (i=0 ;i<N;i++)
10 {
11 printf(" a[%d]= " ,i);
12 scanf(" %d " ,&a);
13 }
14 printf(" \n " );
15 for (i=0 ;i<N;i++)
16 printf(" %5d " ,a);
17 printf(" \n " );
18 /* sort ten num */
19 for (i=0 ;i<N-1 ;i++)
20 {
21 min=i;
22 for (j=i+1 ;j<N;j++)
23 if (a[min]>a[j])
24 min=j;
25 tem=a;
26 a=a[min];
27 a[min]=tem;
28 }
29 /* output data */
30 printf(" After sorted \n " );
31 for (i=0 ;i<N;i++)
32 printf(" %5d " ,a);
33 getch();
34 } 【程序38】 題目:求一個3*3矩陣對角線元素之和 1.程序分析:利用雙重for循環控制輸入二維數組,再將a累加后輸出。 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 /* 如果使用的是TC系列編譯器則可能需要添加下句 */
4 static void dummyfloat(float *x){ float y; dummyfloat(&y);}
5 main()
6 {
7 float a[3 ][3 ],sum=0 ;
8 int i,j;
9 printf(" please input rectangle element:\n " );
10 for (i=0 ;i<3 ;i++)
11 for (j=0 ;j<3 ;j++)
12 scanf(" %f " ,&a[j]);
13 for (i=0 ;i<3 ;i++)
14 sum=sum+a;
15 printf(" duijiaoxian he is %6.2f " ,sum);
16 getch();
17 } 【程序39】 題目:有一個已經排好序的數組。現輸入一個數,要求按原來的規律將它插入數組中。 1. 程序分析:首先判斷此數是否大于最后一個數,然后再考慮插入中間的數的情況,插入后 此元素之后的數,依次后移一個位置。 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 main()
4 {
5 int a[11 ]={1 ,4 ,6 ,9 ,13 ,16 ,19 ,28 ,40 ,100 };
6 int temp1,temp2,number,end,i,j;
7 printf(" original array is:\n " );
8 for (i=0 ;i<10 ;i++)
9 printf(" %5d " ,a);
10 printf(" \n " );
11 printf(" insert a new number: " );
12 scanf(" %d " ,&number);
13 end=a[9 ];
14 if (number>end)
15 a[10 ]=number;
16 else
17 {
18 for (i=0 ;i<10 ;i++)
19 {
20 if (a>number)
21 {
22 temp1=a;
23 a=number;
24 for (j=i+1 ;j<11 ;j++)
25 {
26 temp2=a[j];
27 a[j]=temp1;
28 temp1=temp2;
29 }
30 break ;
31 }
32 }
33 }
34 for (i=0 ;i<11 ;i++)
35 printf(" %6d " ,a);
36 getch();
37 } 【程序40】 題目:將一個數組逆序輸出。 1.程序分析:用第一個與最后一個交換。 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 #define N 5
4 main()
5 {
6 int a[N]={9 ,6 ,5 ,4 ,1 },i,temp;
7 printf(" \n original array:\n " );
8 for (i=0 ;i<N;i++)
9 printf(" %4d " ,a);
10 for (i=0 ;i<N/2 ;i++)
11 {
12 temp=a;
13 a=a[N-i-1 ];
14 a[N-i-1 ]=temp;
15 }
16 printf(" \n sorted array:\n " );
17 for (i=0 ;i<N;i++)
18 printf(" %4d " ,a);
19 getch();
20 } 【程序41】 題目:學習static定義靜態變量的用法 1.程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 varfunc()
4 {
5 int var =0 ;
6 static int static_var=0 ;
7 printf(" \40:var equal %d \n " ,var );
8 printf(" \40:static var equal %d \n " ,static_var);
9 printf(" \n " );
10 var ++;
11 static_var++;
12 }
13 void main()
14 {
15 int i;
16 for (i=0 ;i<3 ;i++)
17 varfunc();
18 getch();
19 } 【程序42】 題目:學習使用auto定義變量的用法 1.程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 main()
4 {
5 int i,num;
6 num=2 ;
7 for (i=0 ;i<3 ;i++)
8 {
9 printf(" \40: The num equal %d \n " ,num);
10 num++;
11 {
12 auto int num=1 ;
13 printf(" \40: The internal block num equal %d \n " ,num);
14 num++;
15 }
16 }
17 getch();
18 } 【程序43】 題目:學習使用static的另一用法。 1.程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 main()
4 {
5 int i,num;
6 num=2 ;
7 for (i=0 ;i<3 ;i++)
8 {
9 printf(" \40: The num equal %d \n " ,num);
10 num++;
11 {
12 static int num=1 ;
13 printf(" \40:The internal block num equal %d\n " ,num);
14 num++;
15 }
16 }
17 getch();
18 } 【程序44】 題目:學習使用external的用法。 1.程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 int a,b,c;
4 void add()
5 {
6 int a;
7 a=3 ;
8 c=a+b;
9 }
10 void main()
11 {
12 a=b=4 ;
13 add();
14 printf(" The value of c is equal to %d\n " ,c);
15 getch();
16 } 【程序45】 題目:學習使用register定義變量的方法。 1.程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 void main()
4 {
5 register int i;
6 int tmp=0 ;
7 for (i=1 ;i<=100 ;i++)
8 tmp+=i;
9 printf(" The sum is %d\n " ,tmp);
10 getch();
11 } 【程序46】 題目:宏#define命令練習(1) 1.程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 #define TRUE 1
4 #define FALSE 0
5 #define SQ(x) (x)*(x)
6 void main()
7 {
8 int num;
9 int again=1 ;
10 printf(" \40: Program will stop if input value less than 50.\n " );
11 while (again)
12 {
13 printf(" \40:Please input number==> " );
14 scanf(" %d " ,&num);
15 printf(" \40:The square for this number is %d \n " ,SQ(num));
16 if (num>=50 )
17 again=TRUE;
18 else
19 again=FALSE;
20 }
21 getch();
22 } 【程序47】 題目:宏#define命令練習(2) 1.程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 /* 宏定義中允許包含兩道衣裳命令的情形,此時必須在最右邊加上"\" */
4 #define exchange(a,b) { \
5 int t;\
6 t=a;\
7 a=b;\
8 b=t;\
9 }
10 void main(void )
11 {
12 int x=10 ;
13 int y=20 ;
14 printf(" x=%d; y=%d\n " ,x,y);
15 exchange(x,y);
16 printf(" x=%d; y=%d\n " ,x,y);
17 getch();
18 } 【程序48】 題目:宏#define命令練習(3) 1.程序分析: 2.程序源代碼:
1 #define LAG >
2 #define SMA <
3 #define EQ ==
4 #include " stdio.h "
5 #include " conio.h "
6 void main()
7 {
8 int i=10 ;
9 int j=20 ;
10 if (i LAG j)
11 printf(" \40: %d larger than %d \n " ,i,j);
12 else if (i EQ j)
13 printf(" \40: %d equal to %d \n " ,i,j);
14 else if (i SMA j)
15 printf(" \40:%d smaller than %d \n " ,i,j);
16 else
17 printf(" \40: No such value.\n " );
18 getch();
19 } 【程序49】 題目:#if #ifdef和#ifndef的綜合應用。 1. 程序分析: 2.程序源代碼:
1 #include " stdio.h "
2 #include " conio.h "
3 #define MAX
4 #define MAXIMUM(x,y) (x>y)?x:y
5 #define MINIMUM(x,y) (x>y)?y:x
6 void main()
7 {
8 int a=10 ,b=20 ;
9 #ifdef MAX
10 printf(" \40: The larger one is %d\n " ,MAXIMUM(a,b));
11 #else
12 printf(" \40: The lower one is %d\n " ,MINIMUM(a,b));
13 #endif
14 #ifndef MIN
15 printf(" \40: The lower one is %d\n " ,MINIMUM(a,b));
16 #else
17 printf(" \40: The larger one is %d\n " ,MAXIMUM(a,b));
18 #endif
19 #undef MAX
20 #ifdef MAX
21 printf(" \40: The larger one is %d\n " ,MAXIMUM(a,b));
22 #else
23 printf(" \40: The lower one is %d\n " ,MINIMUM(a,b));
24 #endif
25 #define MIN
26 #ifndef MIN
27 printf(" \40: The lower one is %d\n " ,MINIMUM(a,b));
28 #else
29 printf(" \40: The larger one is %d\n " ,MAXIMUM(a,b));
30 #endif
31 getch();
32 } 【程序50】
題目:打印出所有的 "水仙花數 ",所謂 "水仙花數 "是指一個三位數,其各位數字立方和等于該數本身。
? ? ? ?1. 程序分析:例如:153是一個 "水仙花數 ",因為153=1的三次方+5的三次方+3的三次方。 ? ? ? ?2.程序源代碼:
1 #include <iostream>
2 #include <Cmath>
3 using namespace std;
4 /*
5 求100-999之間的水仙花數
6 */
7 int main()
8 {
9 int number,hun,ten,gw,sum;
10 for (number=100 ;number<1000 ;++number){
11 hun=number/100 ;
12 ten=number%100 /10 ;
13 gw=number%10 ;
14 sum=pow(hun,3 )+pow(ten,3 )+pow(gw,3 );
15 if (sum==number)
16 {
17 // 是水仙花數
18 cout<<number<<" 是水仙花數 " <<endl;
19
20 }
21 }
22 return 0 ;
23 } ?
轉載于:https://www.cnblogs.com/iloverain/p/5523721.html
總結
以上是生活随笔 為你收集整理的C语言程序设计50例(三)(经典收藏) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。