T
T
Telyaha2020-05-13 20:00:35
C++ / C#
Telyaha, 2020-05-13 20:00:35

How to rewrite code from C++ to C(C)?

Greetings to all forum members. I’m programming recently and don’t know much(
Please help (tell me how) to rewrite this code from C ++ to C? (I don’t really understand C, but I don’t have much time left)
Task: count the number of unique words in the text.
What the code does: Reads all words from the test.txt file and puts them into the startArr array.After that, transfers all unique words to the res array (that is, there are words without repetitions and duplicates, in a single number).At the end, the number of unique words in the text is displayed (the size of the res array )
Commented, I hope everything will be clear

#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    setlocale(LC_ALL, "ru");//подключаем русский

    list <string> startArr;//исходные данные
    list <string> res;//конечный лист

    //чтение файла
    string str;
    ifstream In("D:\\test.txt"); //открываем файл
    if (!In) cout << "Error"; //если не закрыли, то даем ошибку
    if (In.is_open()) {
        while (In>>str) { // пока есть слова заносим их в массив
            startArr.emplace_back(str);
        }
    }
    In.close();//закрываем

    startArr.unique();//убираем дубликаты
    for (auto it = startArr.begin(); it != startArr.end(); it++) {
        auto fr = find(res.begin(), res.end(), *it);//проверяем, есть ли уже этот элемент в новом листе
        if (fr==res.end()) 
            res.emplace_back(*it);//если нет, то добавляем
    }
    int size = res.size();//записываем размер массива res
    cout << "Уникальных слов: " << size << endl;
    for (auto it = res.begin(); it != res.end(); it++) {
        cout << (*it) << " ";//вывод для отладки
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pinkman, 2020-05-14
@famousman204

I'm not sure I understood correctly, but still. I think you need to make a list of structures something like this:

// если я правильно понял, что делает list <string>
struct s_list
{
    char *str;
    struct s_list *next;
    struct s_list *back;
};

if you just need to make 2 arrays (although the essence will not change much), in startArr the initial data, and in res sorted, then you can do something like this: (sorting did not begin to write)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int		main(void)
{
  long lsize;
  char *startArr;
  size_t result;

  FILE *in_file = fopen("test.txt", "r");
  if (in_file == NULL)
  {
    printf("Error\n");
    return (1);
  }
  fseek(in_file, 0, SEEK_END);
  lsize = ftell(in_file);
  rewind(in_file);
  startArr = (char*)malloc(sizeof(char) *lsize);
  if (startArr == NULL)
  {
    printf("Error\n");
    return (2);
  }
  result = fread(startArr, 1, lsize, in_file);
  if (result != lsize)
  {
    printf("Error\n");
    return (3);
  }
  if (res == NULL)
  {
    printf("Error\n");
    return (2);
  }
  fclose(in_file);
  printf("%s\n", startArr);
  find_words(startArr);
        free(startArr);
  return (0);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question