
首先,你需要定义一个变量来存储这个总和的值,可以命名为sum,然后将它初始化为0。
接下来,你可以使用一个for循环来遍历1到10之间的所有数字,并将它们累加到sum变量中。for循环的语法如下:
```
for (int i = 1i <= 10i++) {
sum += i
}
```
在这个循环中,我们定义了一个计数器变量i,它从1开始,每次增加1,直到它的值达到10为止。在每次循环中,我们将i的值加到sum变量中。
最后,你可以输出sum变量的值,以显示1到10的总和。你可以使用printf函数来打印输出,如下所示:
```
printf("1+2+3+4+5+6+7+8+9+10 = %d\n", sum)
```
这将在控制台上显示以下内容:
```
1+2+3+4+5+6+7+8+9+10 = 55
```
至于流程图,我会尝试画一个简单的:
```
开始 -->初始化sum为0 -->进入循环 -->判断i是否小于等于10 -->是 -->将i加到sum中 -->i自增 -->返回循环开始 -->否 -->输出sum的值 -->结束
```
希望这些解释能帮助你理解如何编写这个程序并画出流程图。
以下环境均在GNU/Linux下。
并且由于本人常年使用C语言,所以写出来的代码类似C
原理:
通过遍历该目录下所有文件,并取得后缀名,再次遍历是否有后缀名相同的文件,并且复制如以该文件后缀名为名的文件。
代码:
#include <unistd.h>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
void cpFile(char *src, char *target) /* src为文件,target为文件夹 */
{
FILE *srcH = NULL
FILE *outH = NULL
char buffer[1024] = ""
char path[1024] = ""
snprintf(path, sizeof(path), "%s/%s", target, src)
srcH = fopen(src, "rb") /* rb表示打开二进制 */
if (srcH == NULL)
{
perror("fopen")
return
}
outH = fopen(path, "wb+")
if (outH == NULL)
{
perror("fopen")
return
}
while (fread(buffer, 1, 1024, srcH) != 0)
{
fwrite(buffer, 1, 1024, outH)
memset(buffer, 0, sizeof(buffer)) /* 清空buffer
中的内容 */
}
fclose(outH)
fclose(srcH)
}
int main()
{
DIR *dp
struct dirent *dirInfo
char lastName[10] = ""
DIR *kindDir
struct dirent *kindDirInfo
if (NULL == (dp = opendir("./")))
{
perror("opendir")
exit(EXIT_FAILURE)
}
while ((dirInfo = readdir(dp)) != NULL) /* 遍历 */
{
if (dirInfo->d_type & DT_DIR)
{
continue
}
memcpy(lastName, strstr(dirInfo->d_name, "."), sizeof(lastName))
if (0 != access(lastName, F_OK)) /* 检测文件夹是否存在,
如果不存在那么创建该文件夹 */
{
if (-1 == mkdir(lastName, 0755))
{
perror("mkdir")
continue
}
}
if (NULL == (kindDir = opendir("./")))
{
perror("opendir")
continue
}
/* 再次遍历查找是否有相同后缀名的文件,
并将其复制到指定文件夹 */
while ((kindDirInfo = readdir(kindDir)) != NULL)
{
if (dirInfo->d_type & DT_DIR)
{
continue
}
if (0 == strcmp(lastName, strstr(dirInfo->d_name, ".")))
{
cpFile(dirInfo->d_name, lastName)
}
}
closedir(kindDir)
}
closedir(dp)
return 0
}
注:由于是以.开头,所以在GNU/Linux为隐藏文件。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)