C语言程序设计50例(三)(经典收藏)

时间:2022-05-06
本文章向大家介绍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 somedayn");
 7   while((letter=getch())!='Y')/*当所按字母为Y时才结束*/
 8   {
 9     switch (letter)
10     {
11       case 'S':printf("please input second lettern");
12       if((letter=getch())=='a')
13         printf("saturdayn");
14         else if ((letter=getch())=='u')
15           printf("sundayn");
16           else printf("data errorn");
17       break;
18       case 'F':printf("fridayn");break;
19       case 'M':printf("mondayn");break;
20       case 'T':printf("please input second lettern");
21       if((letter=getch())=='u')
22         printf("tuesdayn");
23         else if ((letter=getch())=='h')
24           printf("thursdayn");
25         else printf("data errorn");
26       break;
27       case 'W':printf("wednesdayn");break;
28       default: printf("data errorn");
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 %drn", color);
10     cprintf("Press any key to continuern");
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 1n");
 9   textbackground(3);
10   gotoxy(20, 10);
11   cprintf("Output at row 10 column 20n");
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 %drn", color);
10   }
11   textcolor(128 + 15);
12   cprintf("This is blinkingrn");
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 %dn",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 %dn",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 %dn",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=%dn",x,y);
15   exchange(x,y);
16   printf("x=%d; y=%dn",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 %dn",MAXIMUM(a,b));
11 #else
12   printf("40: The lower one is %dn",MINIMUM(a,b));
13 #endif
14 #ifndef MIN
15   printf("40: The lower one is %dn",MINIMUM(a,b));
16 #else
17   printf("40: The larger one is %dn",MAXIMUM(a,b));
18 #endif
19 #undef MAX
20 #ifdef MAX
21   printf("40: The larger one is %dn",MAXIMUM(a,b));
22 #else
23   printf("40: The lower one is %dn",MINIMUM(a,b));
24 #endif
25 #define MIN
26 #ifndef MIN
27   printf("40: The lower one is %dn",MINIMUM(a,b));
28 #else
29   printf("40: The larger one is %dn",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 }