以前学习C语言资料4

150 篇文章 0 订阅
6 篇文章 0 订阅


TX31
请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。
LN:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母
SHN:仔细看一下这个吧,看看它有什么优点吧!优点是只有在输入Y时才能跳出,这点很好。不足呢?就是在输入第二个字母时要换大小写,那样用起来很不方便,能不能改进一下呢?可以,只要在case ’ 大写字母’: 后加上一行case ‘小写字母’: 这样就每个对应的都加上就可以了呀。这样输入时就不用来回按Caps Lock键来转换大小写了,哈哈。看出其它缺点了么?好像是没有了,但是我们来运行一下,会发现在输入第二个字母时,反应好慢呀!且有时需要输入两次才能正常,这个不足由怎么改呢?想想吧
#include<stdio.h>
#include<conio.h>
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");
  }
 }
 getch();
}

TX32
Press any key to change color, do you want to try it. Please hurry up!
#include<stdio.h>
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 continut\r\n");
  getch(); /*输入字符看不见*/
 }
}

TX33
学习gotoxy()与clrscr()函数
SHN:gotoxy()就是将光标定位在屏幕的一个位置,一般屏幕分为25行80列。且与我们平时用的数学坐标有点不同,它是以左上角为坐标原点。且有序对(x,y),x指的是纵坐标,y指的是横坐标。这点要注意了呀!
#include<stdio.h>
void main(void)
{
 clrscr(); /*清屏函数*/
 textbackground(2);
 gotoxy(1,5); /*定位函数*/
 cprintf("Output at row 5 column1\n");
 textbackground(3);
 gotoxy(20,10);
 cprintf("Output at row 20 column20\n");
}

TX34
练习函数调用
SHN:看一下TX6。
#include<stdio.h>
void hello_world(void)
{
 printf("Hello,world!\n");
}
void three_hellos(void)
{
 int counter;
 for(counter=1;counter<=3;counter++)
 hello_world(); /*调用此函数*/
}
void main()
{
 three_hellos();  /*调用此函数*/
}

TX35
文本颜色设置
#include<stdio.h>
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");
}

TX36没意思
TX37
对10个数进行排序。
LN:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,下次类推,即用第二个元素与后8个进行比较,并进行交换。
#include<stdio.h>
#include<conio.h>
#define N 10
main()
{
 int i;
 int j;
 int min;
 int tem;
 int a[N];
 printf("please input ten num:\n");
 for(i=0;i<N;i++)
 {
  printf("a[%d]=",i);
  scanf("%d",&a[i]);
 }
 printf("\n");
 for(i=0;i<N;i++)
  printf("%5d",a[i]);
 printf("\n");
 for(i=0;i<N-1;i++)
 {
  min=i;
  for(j=i+1;j<N;j++)
  if(a[min]>a[j])
   min=j;
  tem=a[i];
  a[i]=a[min];
  a[min]=tem;
 }
 printf("after sorted\n");
 for(i=0;i<N;i++)
 printf("%5d",a[i]);
 getch();
}

TX38
求一个3*3矩阵对角线元素之和
LN:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
#include<stdio.h>
#include<conio.h>
main()
{
 int a[3][3];
 int sum=0;
 int i;
 int j;
 printf("please input rectangle element:\n");
 for(i=0;i<3;i++)
 for(j=0;j<3;j++)
  scanf("%d",&a[i][j]);
 for(i=0;i<3;i++)
  sum=sum+a[i][i];
 printf("dui jiao xian he shi %d",sum);
}

TX39
有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
LN:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。
main()
{
 int a[11]={1,4,6,9,13,16,19,28,40,100};
 int temp1;
 int temp2;
 int number;
 int end;
 int i;
 int j;
 printf("original arry is:\n");
 for(i=0;i<10;i++)
 printf("]",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("%d ",a[i]);
}

TX40
将一个数组逆序输出。
LN:用第一个与最后一个交换。
#define N 5
main()
{
 int a[N]={9,6,5,4,1};
 int i;
 int temp;
 printf("\n original arry:\n");
 for(i=0;i<N;i++)
  printf("%4d",a[i]);
 for(i=0;i<N/2;i++)
  {
   temp=a[i];
   a[i]=a[N-i-1];
   a[N-i-1]=temp;
 }
 printf("\n sorted array:\n");
 for(i=0;i<N;i++)
  printf("%4d",a[i]);
 getch();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值