Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Fucking C

Name: Anonymous 2012-11-03 14:43

Why it doesn't show me strings like "  a"?
Why is the while loop in the trim function bugged (if I input 2 chars the next input length can't be less than 2)?

#include <stdio.h>

#define MAX 1001

int getLine(char s[], int length){ // returns 1 or 0

    int i, qttWord = 0; //qttWord = counter of letters for s[]
    int c; // c = getchar()

    /*Reads the input and puts it into s[], then, verifies if the input is just \n,
    * if so, returns 0(i), if not, puts '\0' at the end of the string.
    */
    for (i = 0; i < length-1 && (c = getchar()) != EOF && c != '\n'; ++i){
            s[i] = c;
            ++qttWord;

    }
    if (i == 0){
        return 0;
    } else if (c == '\n'){
        ++i;
        s[i] = '\0';

    }
    /*Verifies if the string is just ' ' or '\t'
    * if so, returns 0
    */
    char flag = '\0';
    for (i = 0; i < qttWord && flag != '1'; ++i){
        if (s[i] == ' ' || s[i] == '\t'){
            flag = '0';
        } else{
            flag = '1';
        }
    }
    if (flag == '0')
        return 0;

    return qttWord;
}
void trim(char s[], int length){

    char s2[MAX];
    int i, qttWord = 0;

    /*while (s[length] != '\0'){
        ++length;
    }
    printf("length:%d\n", length);*/
    for (i = 0; i < length; ++i){
        if (i < length-1){
            if (s[i] == ' ' && s[i+1] != ' '){
                s2[i] = s[i];
                ++qttWord;
                printf("1:%d\n", s2[i]);// if true prints "1" and the character(' ')
            }
        }
        if (s[i] != ' '){
            s2[i] = s[i];
            ++qttWord;
            printf("0:%d\n", s2[i]);//if true prints "0" and the character
        }
    }
    s[0] = '\0';
    s2[qttWord] = '\0';

    for (i = 0; i < qttWord; ++i){
        s[i] = s2[i];
    }
    s2[0] = '\0';
}

int main(){
    char line[MAX];
    int lgh = 0;

    while ((lgh = getLine(line, MAX)) != 0){
        trim(line, lgh);
        printf("%s\n", line);
        line[0] = '\0';
        lgh = 0;
    }
    return 0;
}

Name: Anonymous 2012-11-04 6:08

Symta:

yoba M = O:<F [V X@T]={F V X=[V@(r F [X@T])];[V]};F X=X>
       = hyp 0 0 M,0,len M,len
       | m:[X Y] (drop Y M |m (drop X ? | O `≤≤`) | O <A B = B-A | all ?≥≥0>)
       | sort by:?,len*?,0,len | rhd



C/C++:

#include <stdio.h>
 
typedef int bool;
#define true 1
#define false 0
 
struct solution {
  int top_left_x;
  int top_left_y;
  int width;
  int height;
};
 
int main(int argc,char** argv)
{
  const int x_size = 4;
  const int y_size = 4;
  int data[4][4] = {{9,4,5,5},{5,1,3,3},{8,1,4,5},{8,0,5,2}};
 
  int top_left_x;
  int top_left_y;
  int width;
  int height;
 
  struct solution sol;
  sol.top_left_x = -1;
  sol.width = 0;
  sol.height = 0;
 
  // width and height also zero based, so 1 means 2
 
  for (width=1;width<x_size;width++)
  {
    for (height=1;height<y_size;height++)
    {
      for (top_left_x=0;top_left_x<x_size-width;top_left_x++)
      {
        for (top_left_y=0;top_left_y<y_size-height;top_left_y++)
        {
          bool passed = true;
          int i,j;
          //x check
          for (i=0;(i<width) && passed;i++)
          {
            for (j=0;(j<height+1) && passed;j++)
            {
              int k = i + top_left_x;
              int l = j + top_left_y;
              int current = data[l][k];
              int next = data[l][k+1];
              if (current>next)
                passed = false;
            }
          }
 
          //y check
          for (i=0;(i<width+1) && passed;i++)
          {
            for (j=0;(j<height) && passed;j++)
            {
              int k = i + top_left_x;
              int l = j + top_left_y;
              int current = data[l][k];
              int next = data[l+1][k];
              if (current>next)
                passed = false;
            }
          }
 
          if (passed && ((width+1)*(height+1) >= (sol.width+1)*(sol.height+1)))
          {
            sol.top_left_x = top_left_x;
            sol.top_left_y = top_left_y;
            sol.width = width;
            sol.height = height;
          }
        }
      }
    }
  }
 
  int i,j;
  if (sol.top_left_x>-1)
  {
    for (j=0;j<sol.height+1;j++)
    {
      for (i=0;i<sol.width+1;i++)
      {
        printf("%d ",data[j+sol.top_left_y][i+sol.top_left_x]);
      }
      printf("\n");
    }
  }
 
 
  return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List