琰ER 发表于 2021-11-16 13:09:11

一个关于使用软件的问题,求大家回答一下QAQ

我用的软件是visual studio code来学c语言,课后作业没有推荐这个软件的。我做作业是做windows那个版本的吗?用VSC会在做作业上遇到什么问题吗?

琰ER 发表于 2021-11-16 20:07:17

问题解决了,用的是windows版本的代码。只是代码里面不能加中文,我把中文去掉了就好了。

人造人 发表于 2021-11-16 13:14:40

一般不会遇到什么问题
我怎么感觉是应该抄 linux 的那个版本
话又说回来,两个都抄一遍好像也没有什么坏处,对不对?
所以说,先随便选一个来抄,不对就抄下一个
建议先选 linux 的那个版本

jackz007 发表于 2021-11-16 13:15:36

      你这是杞人忧天啊,年轻人,要有闯劲!

琰ER 发表于 2021-11-16 13:37:55

人造人 发表于 2021-11-16 13:14
一般不会遇到什么问题
我怎么感觉是应该抄 linux 的那个版本
话又说回来,两个都抄一遍好像也没有什么坏 ...

我就是两个都抄了一次不行,然后又复制了一次,不知道为什么还是不行,程序没跑起来......

琰ER 发表于 2021-11-16 13:38:34

jackz007 发表于 2021-11-16 13:15
你这是杞人忧天啊,年轻人,要有闯劲!

着急啊,两个都没跑起来,不知道为什么

人造人 发表于 2021-11-16 13:43:25

琰ER 发表于 2021-11-16 13:37
我就是两个都抄了一次不行,然后又复制了一次,不知道为什么还是不行,程序没跑起来......

如果复制也不行,我感觉你的文件后缀是 .cpp
把后缀改成 .c

琰ER 发表于 2021-11-16 13:46:05

人造人 发表于 2021-11-16 13:43
如果复制也不行,我感觉你的文件后缀是 .cpp
把后缀改成 .c

文件的后缀我可以确定是.c,VSC用的也是c语言的环境,就是不晓得为什么跑不起来

人造人 发表于 2021-11-16 13:49:42

琰ER 发表于 2021-11-16 13:46
文件的后缀我可以确定是.c,VSC用的也是c语言的环境,就是不晓得为什么跑不起来

截图,贴代码

琰ER 发表于 2021-11-16 13:55:25

人造人 发表于 2021-11-16 13:49
截图,贴代码

这个是linux版本的代码,对应的是前面那个图片
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

#define MAX 256

long total;

int countLines(const char *filename);
int isCode(const char *filename);
void findAllDirs(const char *path);

int countLines(const char *filename)
{
      FILE *fp;
      int count = 0;
      int temp;

      if ((fp = fopen(filename, "r")) == NULL)
      {
                fprintf(stderr, "Can not open the file: %s\n", filename);
                return 0;
      }

      while ((temp = fgetc(fp)) != EOF)
      {
                if (temp == '\n')
                {
                        count++;
                }
      }

      fclose(fp);

      return count;
}

int isCode(const char *filename)
{
      int length;

      length = strlen(filename);
      
      if (!strcmp(filename + (length - 2), ".c"))
      {
                return 1;
      }
      else
      {
                return 0;
      }
}

void findAllDirs(const char *path)
{
      DIR *dp;
      struct dirent *entry;
      struct stat statbuf;

      if ((dp = opendir(path)) == NULL)
      {
                fprintf(stderr, "The path %s is wrong!\n", path);
                return;
      }

      chdir(path);
      while ((entry = readdir(dp)) != NULL)
      {
                lstat(entry->d_name, &statbuf);

                if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
                        continue;

                if (S_ISDIR(statbuf.st_mode))
                {
                        findAllDirs(entry->d_name);
                }
                else
                {
                        if (isCode(entry->d_name))
                        {
                              total += countLines(entry->d_name);
                        }
                }
      }

      chdir("..");
      closedir(dp);
}

int main()
{
      char path = ".";

      printf("计算中...\n");

      findAllDirs(path);

      printf("目前你总共写了 %ld 行代码!\n\n", total);

      return 0;
}







这个是windos版本的代码,对应后面那个图片。

#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX      256

long total;

int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(const char *path);

int countLines(const char *filename)
{
      FILE *fp;
      int count = 0;
      int temp;
      
      if ((fp = fopen(filename, "r")) == NULL)
      {
                fprintf(stderr, "Can not open the file:%s\n", filename);
                return 0;
      }
      
      while ((temp = fgetc(fp)) != EOF)
      {
                if (temp == '\n')
                {
                        count++;
                }
      }
      
      fclose(fp);
      
      return count;
}

void findAllCodes(const char *path)
{
      struct _finddata_t fa;
      long handle;
      char thePath, target;
      
      strcpy(thePath, path);
      if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
      {
                do
                {
                        sprintf(target, "%s/%s", path, fa.name);
                        total += countLines(target);
                }while (_findnext(handle, &fa) == 0);
      }
   
      _findclose(handle);
}

void findALLDirs(const char *path)
{
      struct _finddata_t fa;
      long handle;
      char thePath;
      
      strcpy(thePath, path);
      if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
      {
                fprintf(stderr, "The path %s is wrong!\n",path);
                return;
      }
   
      do
      {      
                if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
                        continue;
                  
                if( fa.attrib == _A_SUBDIR)
                {      
                        sprintf(thePath, "%s/%s", path, fa.name);
                        findAllCodes(thePath);
                        findALLDirs(thePath);
                }
      }while (_findnext(handle, &fa) == 0);
   
      _findclose(handle);   
}

int main()
{
      char path = ".";
      
      printf("计算中...\n");
      
      findAllCodes(path);
      findALLDirs(path);
      
      printf("目前你总共写了 %ld 行代码!\n\n", total);
      system("pause");
      
      return 0;
}

琰ER 发表于 2021-11-16 13:57:23

人造人 发表于 2021-11-16 13:49
截图,贴代码

我刚才试了一下,第二个作业可以正常运行。第一个不管是那个版本都运行不起来X﹏X

人造人 发表于 2021-11-16 14:01:35

琰ER 发表于 2021-11-16 13:57
我刚才试了一下,第二个作业可以正常运行。第一个不管是那个版本都运行不起来X﹏X

看起来应该用第2个代码
你那边乱码了,把文件编码改成 utf-8 试试

琰ER 发表于 2021-11-16 14:06:24

人造人 发表于 2021-11-16 14:01
看起来应该用第2个代码
你那边乱码了,把文件编码改成 utf-8 试试

请问“文件编码”是什么?怎么修改?(。・∀・)ノ

人造人 发表于 2021-11-16 14:18:33

琰ER 发表于 2021-11-16 14:06
请问“文件编码”是什么?怎么修改?(。・∀・)ノ

vscode么?不晓得,我不用vscode

琰ER 发表于 2021-11-16 14:27:49

人造人 发表于 2021-11-16 14:18
vscode么?不晓得,我不用vscode

文件编码改了,改成utf-8了,不知道为什么还是不行(。_。)

人造人 发表于 2021-11-16 14:29:22

琰ER 发表于 2021-11-16 14:27
文件编码改了,改成utf-8了,不知道为什么还是不行(。_。)

改gbk

最终的执念 发表于 2021-11-16 14:43:19

VSCode 用Code Runner + MS C++插件 就是闲的没事给自己添堵...

琰ER 发表于 2021-11-16 15:05:45

人造人 发表于 2021-11-16 14:29
改gbk

我找了半天,没有gbk这个文件编码......

人造人 发表于 2021-11-16 15:06:47

琰ER 发表于 2021-11-16 15:05
我找了半天,没有gbk这个文件编码......

我不用vscode,这个我帮不了你

琰ER 发表于 2021-11-16 15:19:02

人造人 发表于 2021-11-16 15:06
我不用vscode,这个我帮不了你

好的,还是谢谢你

琰ER 发表于 2021-11-16 20:06:24

人造人 发表于 2021-11-16 15:06
我不用vscode,这个我帮不了你

问题解决了,用的是windows版本的代码。只是代码里面不能加中文,我把中文去掉了就好了。
页: [1] 2
查看完整版本: 一个关于使用软件的问题,求大家回答一下QAQ