有没有人可以帮助将C ++转换为C? [英] Does anyone can help to convert C++ to C?

查看:106
本文介绍了有没有人可以帮助将C ++转换为C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <string>

using namespace std;

string modifiedSentence(string str, int len);
string sortedSentence(string sentence, int length);

int main(){
    string sentence = "";
    double length = 0;

    cout << "Prepared by ABC"<<endl;
    cout << "Class XYZ"<<endl;
    cout << "This program will take in a sentence from you and invert the letters in each individual words"<<endl;
    cout << "Type in a sentence with no more than 100 characters including space: "<<endl;
    getline (cin,sentence);
    length = sentence.length();
    cout << "String length is "<<length<<endl;
    cout << "The original sentence is "<<sentence<<endl;
    cout << "The modified sentence is "<<modifiedSentence(sentence, length)<<endl;
    cout << "The sorted sentence is "<<sortedSentence(sentence, length)<<endl;
    return 0;
}

string modifiedSentence(string str, int len) {
    int i, j, k;
    char tmp;

    for(i = 0, j = 0; j < len; j++){
        if(!isalpha(str[j]) || (j == len - 1)){
            if(j < len - 1){
                k = j - 1;
            } else {
                k = j;
            }

            while( i < k){
                tmp = str[i];
                str[i] = str[k];
                str[k] = tmp;
                i++;
                k--;
            }
            i = j + 1;
        }
    }
    return str;
}

string sortedSentence(string sentence, int length){
    string lower = "";
    string upper = "";
    string num = "";
    string symbols = "";
    int temp;
    for (int i = 0; i<sentence.length(); i++){
        if(islower(sentence[i])){
            lower += sentence[i];
        }
        if(isupper(sentence[i])){
            upper += sentence[i];
        }
        if(isdigit(sentence[i])){
            num += sentence[i];
        }
        if(!isalnum(sentence[i])){
            symbols += sentence[i];
        }
    }

    for (int i = 0; i<lower.length(); i++) {
        for (int j = i + 1; j < lower.length(); j++) {
            if (lower[i] > lower[j]) {
                temp = lower[i];
                lower[i] = lower[j];
                lower[j] = temp;
            }
        }
    }
    for (int i = 0; i<upper.length(); i++) {
        for (int j = i + 1; j < upper.length(); j++) {
            if (upper[i] > upper[j]) {
                temp = upper[i];
                upper[i] = upper[j];
                upper[j] = temp;
            }
        }
    }
    for (int i = 0; i<num.length(); i++) {
        for (int j = i + 1; j < num.length(); j++) {
            if (num[i] > num[j]) {
                temp = num[i];
                num[i] = num[j];
                num[j] = temp;
            }
        }
    }

    return lower+upper+num+symbols;
}





我的尝试:



有没有人可以帮助将C ++转换为C?因为我是编程的新手,我的任务请求使用C.谢谢



What I have tried:

Does anyone can help to convert C++ to C? because I was newbie on programming and my assignment request to use C. Thanks

推荐答案

以下代码显示了该技术。它应该足以使您能够使用 sort_sentence 函数完成它。

The following code shows the technique. It should be enough to make you able to complete it with the sort_sentence function.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 100

char * modify_sentence( const char * sentence, size_t length);

//TODO:
//char * sort_sentence(const char * sentence, size_t length);

int main()
{
  char sentence[SIZE];
  printf("Prepared by ABC\n");
  //...
  printf("Type in a sentence with no more than %d characters including space: \n", SIZE);
  fgets(sentence, SIZE, stdin);
  size_t len = strlen(sentence);
  if ( !len ) return -1;
  // handle the newline (if present)
  if ( sentence[len-1] == '\n')
  {
    sentence[len-1] = '\0';
    --len;
  }
  printf("Sentence length is %lu\n", len);
  printf("The original sentence is %s\n", sentence);

  char * modified_sentence = modify_sentence(sentence, len);
  if ( modified_sentence)
  {
    printf("The modified sentence is %s\n", modified_sentence);
    free(modified_sentence);
  }

  // TODO:
  //char * sorted_sentence = sort_sentence(sentence, len);
  //...
}

char * modify_sentence( const char * sentence, size_t length)
{
  char * s = (char *) malloc(length);
  if ( ! s ) return s;

  strncpy( s, sentence, length);
  // as before
  int i, j, k;
  char tmp;

  for(i = 0, j = 0; j < length; j++)
  {
    if(!isalpha(s[j]) || (j == length - 1))
    {
      if(j < length - 1)
        k = j - 1;
      else
        k = j;

      while( i < k)
      {
        tmp = s[i];
        s[i] = s[k];
        s[k] = tmp;
        i++;
        k--;
      }
      i = j + 1;
    }
  }
  return s;
}

//TODO:
//char * sort_sentence(const char * sentence, size_t length)
//{
//...
//}







[update]

此代码包含 sort_sentence 函数的部分实现。

lower,upper,digit和not-aluhanumeric 子字符串被分组在返回值中。缺少对子串的排序(左侧为练习)。




[update]
This code contains a partial implementation of the sort_sentence function.
The lower, upper, digit, and not-alphanumeric substrings are grouped in the return value. Sorting the substrings is missing (left as exercise).

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 100

char * modify_sentence( const char * sentence, size_t length);
char * sort_sentence(const char * sentence, size_t length);

int main()
{
  char sentence[SIZE];
  printf("Prepared by ABC\n");
  //...
  printf("Type in a sentence with no more than %d characters including space: \n", SIZE);
  fgets(sentence, SIZE, stdin);
  size_t len = strlen(sentence);
  if ( !len ) return -1;
  // handle the newline (if present)
  if ( sentence[len-1] == '\n')
  {
    sentence[len-1] = '\0';
    --len;
  }
  printf("Sentence length is %lu\n", len);
  printf("The original sentence is %s\n", sentence);

  char * modified_sentence = modify_sentence(sentence, len);
  if ( modified_sentence)
  {
    printf("The modified sentence is %s\n", modified_sentence);
    free(modified_sentence);
  }

  char * sorted_sentence = sort_sentence(sentence, len);

  if ( sorted_sentence )
  {
    printf("The sorted sentence is %s\n", sorted_sentence);
    free(sorted_sentence);
  }
}
char * modify_sentence( const char * sentence, size_t length)
{
  char * s = (char *) malloc(length);
  if ( ! s ) return s;

  strncpy( s, sentence, length);
  // as before
  int i, j, k;
  char tmp;

  for(i = 0, j = 0; j < length; j++)
  {
    if(!isalpha(s[j]) || (j == length - 1))
    {
      if(j < length - 1)
        k = j - 1;
      else
        k = j;

      while( i < k)
      {
        tmp = s[i];
        s[i] = s[k];
        s[k] = tmp;
        i++;
        k--;
      }
      i = j + 1;
    }
  }
  return s;
}

struct CharacterInfo
{
  int beg; // start index 
  int end; // pass-the-end index
  int (*fun)(); // discriminant function
};

int notisalnum(int c) { return (! isalnum(c)); }

char * sort_sentence(const char * sentence, size_t length)
{
  struct CharacterInfo ci[] = // this array is used to keep track of the substrings
  {
    { 0, 0, islower }, // start, pass-the-end, discriminant of lower characters
    { 0, 0, isupper }, //  "           "            "          upper    "
    { 0, 0, isdigit }, //  "           "            "          digit    "
    { 0, 0, notisalnum }//  "           "            "         not alphanumeric    "
  };

  const int LEN = sizeof(ci)/sizeof(ci[0]);

  char * s = (char *) malloc(length);
  if ( ! s ) return s;

  int n;
  int index = 0;
  for (n = 0; n < LEN; ++n)
  {
    int i;
    for (i = 0; i<length; ++i)
    {
      ci[n].beg = ci[n].end = index;
      if ( ci[n].fun(sentence[i]) )
      {
        s[index] = sentence[i];
        ++index;
        ++(ci[n].end);
      }
    }
  }
  // TODO: sort the substrings

  return s;
}



[/ update]


在互联网上查找代码似乎适合你的作业,然后意识到它不是正确的语言,不会编译将无法帮助你。事实上,从长远来看,从互联网上获取代码对你没有帮助。实际上只做这项工作会教会你真实的东西。



而且......我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!
Finding code on the internet that seems to fit your homework and then realizing it's not in the right language and won't compile will not help you. In fact, getting the code from the internet at all won't help you in the long run. Only actually doing the work will teach you anything real.

And ... we do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


引用:

有没有人可以提供帮助将C ++转换为C?

Does anyone can help to convert C++ to C?



您提出的帮助是做我的工作。这就像是要求某人停车,因为你是学习者,它无助于你学习。


The kind of help you ask is 'do my work'. It is like to ask someone to park your car because you are a learner, it don't help you to learn.

引用:

因为我是编程的新手和我使用C的作业请求。

because I was newbie on programming and my assignment request to use C.



任何作业都是让你练习因为你是新手,这就是作业的目的。

建议:试一试,换弦不复杂,拿一张纸,一支铅笔,手工做。以机械方式考虑您的程序,这是您的算法。



试用和错误是如何学习编程,但你必须尝试。

如果您遇到特定问题,请回过头来解释您的问题以获得解决问题的帮助。


Any assignment is to make you practice because you are a newbie, that is the purpose of assignments.
Advice: Give it a try, reversing a string is not complicated, take a sheet of paper, a pencil and do it by hand. Think of your procedure in a mechanical fashion, it is your algorithm.

Trial and error is how you will learn programming, but you have to try.
If you encounter a specific problem, come back and explain your problem to get some help on fixing it.


这篇关于有没有人可以帮助将C ++转换为C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆