小练习

传送文件数据

a文件中数据包括学生学号、姓名、以及体重,体重会随时上传,现在需要将a文件中的数据传送到b文件,要求b文件中只显示每个学生最新的一次数据

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
FILE* b = fopen("C:\\Users\\丁长春\\Desktop\\新建文本文档.txt", "r");
FILE* z = fopen("C:\\Users\\丁长春\\Desktop\\新建文本文档 - 副本.txt", "w");
FILE* x = fopen("C:\\Users\\丁长春\\Desktop\\新建 - 副本.txt", "w");
if (b== NULL||z == NULL || x == NULL)
{
printf("文件打开失败1!\n");
}
char buf[1024] = "";
char buff[1024] = "";
char duf[1024] = "";
char aaa[1024] = "";
int c = 0;
fputs(buff, x);
while (fgets(buff, 1024,b))
{
fputs(buff, z);
}
fclose(z);
fclose(x);
fclose(b);
FILE* f = fopen("C:\\Users\\丁长春\\Desktop\\新建文本文档.txt", "r");
if (f == NULL)
{
printf("文件打开失败2!\n");
}
while (fgets(buf, 1024, f))
{
c = 0;
FILE* z = fopen("C:\\Users\\丁长春\\Desktop\\新建文本文档 - 副本.txt", "r");
if (z == NULL )
{
printf("文件打开失败3!\n");
}
while(fgets(duf, 1024, z))
{
if ((buf[0] == duf[0])&& (buf[1] == duf[1]))
{
strcpy(buf,duf);
}
}
fclose(z);
FILE* x = fopen("C:\\Users\\丁长春\\Desktop\\新建 - 副本.txt", "r");
if (x == NULL)
{
printf("文件打开失败4!\n");
}
while (fgets(aaa, 1024, x))
{
if ((buf[0] == aaa[0])&& (buf[1] == aaa[1]))
{
c = 1;
}
}
fclose(x);
if (c == 0)
{
FILE* x = fopen("C:\\Users\\丁长春\\Desktop\\新建 - 副本.txt", "a");
if (x == NULL)
{
printf("文件打开失败5!\n");
}
fputs(buf, x);
fclose(x);
}
}
fclose(f);
}

方法可能有些麻烦,而且如果a文件中最上边学生的学号不是按书序排列,那b文件中的学号顺序也不是按顺序排列,这个怎么搞还没有想好。

找到1000以内所有素数

int main()
{
int arry[1001] = { 0 };
for (int i =2; i < 1001; i++)
{
if (arry[i] == 0)
{
printf("%d ", i);
for (int j = i + i; j < 1001; j += i)
{
arry[j] = 1;
}
}
}
return 0;
}

1 2 4 7 11 16 22.。。。。第五十个数

   int s = 1;
for (int i = 0; i <50; i++)
{
s += i;
}
printf("%d\n", s);

n*n数阵(递增)

   int n ;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 1; j <= n; j++)
{
printf("%d ", i * n + j);
}
printf("%c",'\n');
}

直角三角

    int n;
scanf("%d", &n);
for (int i = 1; i <=n; i++)
{
for (int j = 0; j < i; j++)
{
printf("%c ", '\*');
}
printf("%c", '\n');
}

等腰三角

    int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
for (int j = i; j < n; j++)
{
printf("%c", '\0');
}
for (int j = 0; j <i; j++)
{
printf("%c", '\*');
printf("%c", '\0');
}
printf("%c",'\n');

}

等腰三角(第一行一个,第二行三个,第三行5个*……)

   int n;
scanf("%d", &n);
for (int i = 1; i <= (n + 1) / 2; i++)
{
for (int j = i; j < (n + 1) / 2; j++)
{
printf(" ");
}
for (int j = 0; j < 2*i-1; j++)
{
printf("%c", '\*');
}
printf("%c", '\n');
}

待续……