美文网首页C语言C语言&嵌入式
C语言程序设计学习--经典实例100题(第二部分)

C语言程序设计学习--经典实例100题(第二部分)

作者: 再让你三行代码 | 来源:发表于2019-12-13 15:31 被阅读0次

最近在看黑马程序员的教程自学C语言,整理了C语言课后练习实例一百题。

附上自学教程:
轻松掌握C语言视频教程(会打字就能学会)
资料网盘 提取码:ofh9


题目三十一:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
2.程序源代码:

#include 
void main()
{
char letter;
printf("please input the first letter of someday\n");
while ((letter=getch())!='y')/*当所按字母为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("data error\n");
     break;
case 'f':printf("friday\n");break;
case 'm':printf("monday\n");break;
case 't':printf("please input second letter\n");
     if((letter=getch())=='u')
      printf("tuesday\n");
     else if ((letter=getch())=='h')
         printf("thursday\n");
       else printf("data error\n");
     break;
case 'w':printf("wednesday\n");break;
default: printf("data error\n");
  }
 }
}

题目三十二:press any key to change color, do you want to try it. please hurry up!
          
程序源代码:

#include 
void main(void)
{
int color;
for (color = 0; color < 8; color++)
 { 
 textbackground(color);/*设置文本的背景颜色*/
 cprintf("this is color %d\r\n", color);
 cprintf("press any key to continue\r\n");
 getch();/*输入字符看不见*/
 }
}

题目三十三:学习gotoxy()与clrscr()函数

程序源代码:

#include 
void main(void)
{
clrscr();/*清屏函数*/
textbackground(2);
gotoxy(1, 5);/*定位函数*/
cprintf("output at row 5 column 1\n");
textbackground(3);
gotoxy(20, 10);
cprintf("output at row 10 column 20\n");
}

题目三十四:练习函数调用

程序源代码:

#include 
void hello_world(void)
{
printf("hello, world!\n");
}
void three_hellos(void)
{
int counter;
for (counter = 1; counter <= 3; counter++)
hello_world();/*调用此函数*/
}
void main(void)
{
three_hellos();/*调用此函数*/
}

题目三十五:文本颜色设置

程序源代码:

#include 
void main(void)
{
int color;
for (color = 1; color < 16; color++)
 {
 textcolor(color);/*设置文本颜色*/
 cprintf("this is color %d\r\n", color);
 }
textcolor(128 + 15);
cprintf("this is blinking\r\n");
}

题目三十六:求100之内的素数

程序源代码:

#include 
#include "math.h"
#define n 101
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("%5d",a[i]);
 line++;}
 if(line==10)
 {printf("\n");
line=0;}
}
}

题目三十七:对10个数进行排序

1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换。       
2.程序源代码:

#define n 10
main()
{int i,j,min,tem,a[n];
/*input data*/
printf("please input ten num:\n");
for(i=0;i{
printf("a[%d]=",i);
scanf("%d",&a[i]);}
printf("\n");
for(i=0;iprintf("%5d",a[i]);
printf("\n");
/*sort ten num*/
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;
}
/*output data*/
printf("after sorted \n");
for(i=0;iprintf("%5d",a[i]);
}

题目三十八:求一个3 * 3矩阵对角线元素之和

1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
2.程序源代码:

main()
{
float a[3][3],sum=0;
int i,j;
printf("please input rectangle element:\n");
for(i=0;i<3;i++)
 for(j=0;j<3;j++)
 scanf("%f",&a[i][j]);
for(i=0;i<3;i++)
 sum=sum+a[i][i];
printf("duijiaoxian he is %6.2f",sum);
}

题目三十九:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

1.程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。
2.程序源代码:

main()
{
int a[11]={1,4,6,9,13,16,19,28,40,100};
int temp1,temp2,number,end,i,j;
printf("original array is:\n");
for(i=0;i<10;i++)
 printf("%5d",a[i]);
printf("\n");
printf("insert a new number:");
scanf("%d",&number);
end=a[9];
if(number>end)
 a[10]=number;
else
 {for(i=0;i<10;i++)
  { if(a[i]>number)
   {temp1=a[i];
    a[i]=number;
   for(j=i+1;j<11;j++)
   {temp2=a[j];
    a[j]=temp1;
    temp1=temp2;
   }
   break;
   }
  }
}
for(i=0;i<11;i++)
 printf("%6d",a[i]);
}

题目四十:将一个数组逆序输出。

1.程序分析:用第一个与最后一个交换。
2.程序源代码:

#define n 5
main()
{ int a[n]={9,6,5,4,1},i,temp;
 printf("\n original array:\n");
 for(i=0;i printf("%4d",a[i]);
 for(i=0;i {temp=a[i];
  a[i]=a[n-i-1];
  a[n-i-1]=temp;
 }
printf("\n sorted array:\n");
for(i=0;i printf("%4d",a[i]);
}

题目四十一:学习static定义静态变量的用法

程序源代码:

#include "stdio.h"
varfunc()
{
int var=0;
static int static_var=0;
printf("\40:var equal %d \n",var);
printf("\40:static var equal %d \n",static_var);
printf("\n");
var++;
static_var++;
}
void main()
{int i;
 for(i=0;i<3;i++)
  varfunc();
}

题目四十二:学习使用auto定义变量的用法

程序源代码:

#include "stdio.h"
main()
{int i,num;
num=2;
 for (i=0;i<3;i++)
 { printf("\40: the num equal %d \n",num);
  num++;
  {
  auto int num=1;
  printf("\40: the internal block num equal %d \n",num);
  num++;
  }
 }
}

题目四十三:学习使用static的另一用法。

程序源代码:

#include "stdio.h"
main()
{
int i,num;
num=2;
for(i=0;i<3;i++)
{
printf("\40: the num equal %d \n",num);
num++;
{
static int num=1;
printf("\40:the internal block num equal %d\n",num);
num++;
}
}
}

题目四十四:学习使用external的用法。

程序源代码:

#include "stdio.h"
int a,b,c;
void add()
{ int a;
a=3;
c=a+b;
}
void main()
{ a=b=4;
add();
printf("the value of c is equal to %d\n",c);
}

题目四十五:学习使用register定义变量的方法。

程序源代码:

void main()
{
register int i;
int tmp=0;
for(i=1;i<=100;i++)
tmp+=i;
printf("the sum is %d\n",tmp);
}

题目四十六:宏#define命令练习(1)

程序源代码:

#include "stdio.h"
#define true 1
#define false 0
#define sq(x) (x)*(x)
void main()
{
int num;
int again=1;
printf("\40: program will stop if input value less than 50.\n");
while(again)
{
printf("\40:please input number==>");
scanf("%d",&num);
printf("\40:the square for this number is %d \n",sq(num));
if(num>=50)
 again=true;
else
 again=false;
}
}

题目四十七:宏#define命令练习(2)

程序源代码:

#include "stdio.h"
#define exchange(a,b) { \ /*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/
            int t;\
            t=a;\
            a=b;\
            b=t;\
           }
void main(void)
{
int x=10;
int y=20;
printf("x=%d; y=%d\n",x,y);
exchange(x,y);
printf("x=%d; y=%d\n",x,y);
}

题目四十八:宏#define命令练习(3)

程序源代码:

#define lag >
#define sma <
#define eq ==
#include "stdio.h"
void main()
{ int i=10;
int j=20;
if(i lag j)
printf("\40: %d larger than %d \n",i,j);
else if(i eq j)
printf("\40: %d equal to %d \n",i,j);
else if(i sma j)
printf("\40:%d smaller than %d \n",i,j);
else
printf("\40: no such value.\n");
}

题目四十九:#if #ifdef和#ifndef的综合应用。

程序源代码:

#include "stdio.h"
#define max
#define maximum(x,y) (x>y)?x:y
#define minimum(x,y) (x>y)?y:x
void main()
{ int a=10,b=20;
#ifdef max
printf("\40: the larger one is %d\n",maximum(a,b));
#else
printf("\40: the lower one is %d\n",minimum(a,b));
#endif
#ifndef min
printf("\40: the lower one is %d\n",minimum(a,b));
#else
printf("\40: the larger one is %d\n",maximum(a,b));
#endif
#undef max
#ifdef max
printf("\40: the larger one is %d\n",maximum(a,b));
#else
printf("\40: the lower one is %d\n",minimum(a,b));
#endif
#define min
#ifndef min
printf("\40: the lower one is %d\n",minimum(a,b));
#else
printf("\40: the larger one is %d\n",maximum(a,b));
#endif
}

题目五十:#include 的应用练习

程序源代码:

test.h 文件如下:
#define lag >
#define sma <
#define eq ==
#include "test.h" /*一个新文件50.c,包含test.h*/
#include "stdio.h"
void main()
{ int i=10;
int j=20;
if(i lag j)
printf("\40: %d larger than %d \n",i,j);
else if(i eq j)
printf("\40: %d equal to %d \n",i,j);
else if(i sma j)
printf("\40:%d smaller than %d \n",i,j);
else
printf("\40: no such value.\n");
}

题目五十一:学习使用按位与 & 。 
  
1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
2.程序源代码:

#include "stdio.h"
main()
{
int a,b;
a=077;
b=a&3;
printf("\40: the a & b(decimal) is %d \n",b);
b&=7;
printf("\40: the a & b(decimal) is %d \n",b);
}

题目五十二:学习使用按位或 | 。

1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1            
2.程序源代码:

#include "stdio.h"
main()
{
int a,b;
a=077;
b=a|3;
printf("\40: the a & b(decimal) is %d \n",b);
b|=7;
printf("\40: the a & b(decimal) is %d \n",b);
}

题目五十三:学习使用按位异或 ^ 。  
 
1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
2.程序源代码:

#include "stdio.h"
main()
{
int a,b;
a=077;
b=a^3;
printf("\40: the a & b(decimal) is %d \n",b);
b^=7;
printf("\40: the a & b(decimal) is %d \n",b);
}

题目五十四:取一个整数a从右端开始的4~7位。

1.程序分析:可以这样考虑:
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用(0<<4)
(3)将上面二者进行&运算。
2.程序源代码:

main()
{
unsigned a,b,c,d;
scanf("%o",&a);
b=a>>4;
c=~(~0<<4);
d=b&c;
printf("%o\n%o\n",a,d);
}

题目五十五:学习使用按位取反~。 
  
1.程序分析:~0=1; ~1=0;
2.程序源代码:

#include "stdio.h"
main()
{
int a,b;
a=234;
b=~a;
printf("\40: the a's 1 complement(decimal) is %d \n",b);
a=~a;
printf("\40: the a's 1 complement(hexidecimal) is %x \n",a);
} 

题目五十六:画图,学用circle画圆形。 
  
程序源代码:

/*circle*/
#include "graphics.h"
main()
{int driver,mode,i;
float j=1,k=1;
driver=vga;mode=vgahi;
initgraph(&driver,&mode,"");
setbkcolor(yellow);
for(i=0;i<=25;i++)
{
setcolor(8);
circle(310,250,k);
k=k+j;
j=j+0.3;
}
} 

题目五十七:画图,学用line画直线。

程序源代码:

#include "graphics.h"
main()
{int driver,mode,i;
float x0,y0,y1,x1;
float j=12,k;
driver=vga;mode=vgahi;
initgraph(&driver,&mode,"");
setbkcolor(green);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i<=18;i++)
{
setcolor(5);
line(x0,y0,x0,y1);
x0=x0-5;
y0=y0-5;
x1=x1+5;
y1=y1+5;
j=j+10;
}
x0=263;y1=275;y0=263;
for(i=0;i<=20;i++)
{
setcolor(5);
line(x0,y0,x0,y1);
x0=x0+5;
y0=y0+5;
y1=y1-5;
}
}

题目五十八:画图,学用rectangle画方形。 
  
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
2.程序源代码:

#include "graphics.h"
main()
{int x0,y0,y1,x1,driver,mode,i;
driver=vga;mode=vgahi;
initgraph(&driver,&mode,"");
setbkcolor(yellow);
x0=263;y0=263;y1=275;x1=275;
for(i=0;i<=18;i++)
{
setcolor(1);
rectangle(x0,y0,x1,y1);
x0=x0-5;
y0=y0-5;
x1=x1+5;
y1=y1+5;
}
settextstyle(default_font,horiz_dir,2);
outtextxy(150,40,"how beautiful it is!");
line(130,60,480,60);
setcolor(2);
circle(269,269,137);
}

题目五十九:画图,综合例子。

程序源代码:

# define pai 3.1415926
# define b 0.809
# include "graphics.h"
#include "math.h"
main()
{
int i,j,k,x0,y0,x,y,driver,mode;
float a;
driver=cga;mode=cgac0;
initgraph(&driver,&mode,"");
setcolor(3);
setbkcolor(green);
x0=150;y0=100;
circle(x0,y0,10);
circle(x0,y0,20);
circle(x0,y0,50);
for(i=0;i<16;i++)
{
 a=(2*pai/16)*i;
 x=ceil(x0+48*cos(a));
 y=ceil(y0+48*sin(a)*b);
 setcolor(2); line(x0,y0,x,y);}
setcolor(3);circle(x0,y0,60);
/* make 0 time normal size letters */
settextstyle(default_font,horiz_dir,0);
outtextxy(10,170,"press a key");
getch();
setfillstyle(hatch_fill,yellow);
floodfill(202,100,white);
getch();
for(k=0;k<=500;k++)
{
 setcolor(3);
 for(i=0;i<=16;i++)
 {
  a=(2*pai/16)*i+(2*pai/180)*k;
  x=ceil(x0+48*cos(a));
  y=ceil(y0+48+sin(a)*b);
  setcolor(2); line(x0,y0,x,y);
 }
 for(j=1;j<=50;j++)
 {
  a=(2*pai/16)*i+(2*pai/180)*k-1;
  x=ceil(x0+48*cos(a));
  y=ceil(y0+48*sin(a)*b);
  line(x0,y0,x,y);
 }
}
restorecrtmode();
}

题目六十:画图,综合例子。

程序源代码:

#include "graphics.h"
#define left 0
#define top 0
#define right 639
#define bottom 479
#define lines 400
#define maxcolor 15
main()
{
int driver,mode,error;
int x1,y1;
int x2,y2;
int dx1,dy1,dx2,dy2,i=1;
int count=0;
int color=0;
driver=vga;
mode=vgahi;
initgraph(&driver,&mode,"");
x1=x2=y1=y2=10;
dx1=dy1=2;
dx2=dy2=3;
while(!kbhit())
{
 line(x1,y1,x2,y2);
 x1+=dx1;y1+=dy1;
 x2+=dx2;y2+dy2;
 if(x1<=left||x1>=right)
 dx1=-dx1;
 if(y1<=top||y1>=bottom)
  dy1=-dy1;
 if(x2<=left||x2>=right)
  dx2=-dx2;
 if(y2<=top||y2>=bottom)
  dy2=-dy2;
 if(++count>lines)
 {
  setcolor(color);
  color=(color>=maxcolor)?0:++color;
 }
}
closegraph();
}

相关文章

网友评论

    本文标题:C语言程序设计学习--经典实例100题(第二部分)

    本文链接:https://www.haomeiwen.com/subject/jebfnctx.html