第十六周学习报告

这周我没有往后学链表,但是我学习了文件的相关知识和一些其他函数,还有system()的变色知识,还做了一些算法题。

一·算法
例如:
1.查抄书籍

#include<stdio.h>
int main()
{
 typedef struct book
 {
  char name[31];
  double j;
 }B;
 B b[10];
 int i, z = 0, a = 0, n;
 scanf("%d", &n);
 getchar();//用于处理缓冲区中的回车。
 for (i = 0; i<n; i++)
 {
  //scanf("\n");//当不用getchar时,换行符就要加上,若是连用,则多一个回车,此时,无输出。
  gets(b[i].name);
  scanf("%lf", &b[i].j);
  getchar();
  if (b[i].j>b[z].j)
     {
   z = i;
     }
     if (b[i].j<b[a].j)
     {
          a = i;
     }
 }
 printf("%.2f, %s\n", b[z].j, b[z].name);
 printf("%.2f, %s\n", b[a].j, b[a].name);
 return 0;
}

2.低洼地

#include <stdio.h>
int main(void)
{
 int n,a[10000];
 int i,j,k=0;
 scanf("%d",&n);
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 for(i=0;i<n;i++)
 {
  if(i==0||i==n-1)
   continue;
  if(a[i-1]>a[i]&&a[i]<a[i+1])
   k++;
  for(j=i;j<n-1;j++)
  {
   if(a[j]!=a[j+1])
    break;
   else
    if(a[i-1]>a[i] && a[j+1]<a[j+2])
     k++;
  }
 }
 printf("%d\n",k);
}

3.IBSN代码

#include <stdio.h>
int main(void)
{
 int n,a[10000];
 int i,j,k=0;
 scanf("%d",&n);
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 for(i=0;i<n;i++)
 {
  if(i==0||i==n-1)
   continue;
  if(a[i-1]>a[i]&&a[i]<a[i+1])
   k++;
  for(j=i;j<n-1;j++)
  {
   if(a[j]!=a[j+1])
    break;
   else
    if(a[i-1]>a[i] && a[j+1]<a[j+2])
     k++;
  }
 }
 printf("%d\n",k);
}

二·新学函数

1.qsort()函数

#include<stdio.h>
#include<stdlib.h>
int comp(const void *a,const void *b)
{
 return *(int*)a-*(int*)b;
}
int main(void)
{
 int i;
 int *array;
 int n;
 scanf("%d",&n);
 array=(int*)malloc(n*sizeof(int));
 for(i=0;i<n;i++)
 {
  scanf("%d",(array+i));
 }
    qsort(array,n,sizeof(int),comp);
    for(i=0;i<n;i++)
 {
  printf("%d\t",array[i]);
 }
 return 0;
}
/*
    如果compar返回值小于0(< 0),那么p1所指向元素会被排在p2所指向元素的左面;
  如果compar返回值等于0(= 0),那么p1所指向元素与p2所指向元素的顺序不确定;
  如果compar返回值大于0(> 0),那么p1所指向元素会被排在p2所指向元素的右面。
*/

2.rand()函数

#include<stdlib.h>
#include<stdio.h>
int main(void)
{
 int i;
 for(i = 0; i <10;i++)
 {
  printf("%d\n",rand());
 }
 return 0;
}

3.getch()函数

#include <stdio.h>
#include <conio.h>  //为getch()函数提供原型
int main(void)
{
 char t;
 do
 {
  t = getch();
     if(t=='1')
  {
   printf("\nYou are right!\n");
   break;
  }
  else
   printf("You are a fool!\n");
 }while(1);
}
/*
getch()这个函数是一个不回显函数,当用户按下某个字符时,函数自动读取,无需按回车。
所在头文件:conio。
函数用途:从控制台读取一个字符,但不显示在屏幕上。
函数原型:int getch(void)。
返回值:读取的字符。
*/

三·system()变色

system(“color 0A”); 其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:
0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色

上面这个可以和下面这个搭配,这个是我学习的位置函数。

#include<windows.h>
void toxy(int x,int y)
{
COORD pos={x,y};
HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out,pos);
} 

四·文件
文件的灵活运用需要学会各种变量的运用,十五周已经学习,所以这个文件学习起来比较轻松,只用创建工程和引用就可以。

// parta.c ---不同的存储类别
// 与partb.c 一起编译
#include <stdio.h>
void report_count();
void accumulate(int k);
int count = 0; //文件作用域外部链接
int main(void)
{
 int value;  //自动变量
 register int i;  //寄存器变量
 printf("Enter a positive integer (0 to quit):");
 while(scanf("%d",&value)==1 && value > 0)
 {
  ++count;  //使用文件作用域变量
  for(i=value;i>=0;i--)
   accumulate(i);
  printf("Enter a positive integer (0 to quit):");
 }
 report_count();
 return 0;
}
void report_count()
{
 printf("Loop executed %d times\n",count);
}
//partb.c -- 程序的其余部分
//与parta.c 一起编译
#include <stdio.h>
extern int count;  //引用
static int total = 0; //静态定义,内部链接
void accumulate(int k);  //函数原型
void accumulate(int k) // k具有块作用域,无链接
{
 static int subtotal = 0;  //静态,无链接
 if(k<=0)
 {
  printf("loop cycle: %d\n",count);
  printf("subtotal: %d; total; %d\n",subtotal,total);
 }
 else
 {
  subtotal += k;
  total +=k;
 }
}

再打开工作空间运行就可以了。

发布了5 篇原创文章 · 获赞 0 · 访问量 286

猜你喜欢

转载自blog.csdn.net/firm_people/article/details/103538600