main(int argc, char *argv[]){
	int i,j,k, height;
	char* string = argv[1];
	int negative = -1;
	//int height = 5;
	//sscanf(argv[1], "%i", &input);
	if(argc == 1){
		printf("No argument provided.\n");
	}
	if(argc > 2){
		printf("Too many arguments provided.\n");
	}
	if(argc == 2){
		//Check if the string contains invalid characters
		
		//if(invalid_char(argv[1])){
		
		//
		if(invalid_char(string) == 0){
			printf("The string contains invalid characters.\n");
			return;
		}
		if(invalid_char(string) == 2){
			printf("The number is too small.\n");
		}
		else{
			//Convert to int
			//sscanf(argv[1], "%i", &height);
			sscanf(string, "%i", &height);
			//check if larger or equal to 1
			if(height < 1){
				printf("The number is too small.\n");
			}
			//check if smaller or equal to 128
			if(height > 128){
				printf("The number is too large.\n");
			}
			else{			
				//Keeps track of the lines
				for(i=1; i <= height; i=i+1){
					//Prints the spaces
					for(j = height-i; j > 0; j=j-1){
						printf(" ");
					}
					//Prints the *
					//The number of * on each line is: 2*i-1, where i is the index of the line
					for(k = 1; k <= (i*2)-1; k=k+1){ 
						printf("*");
					}
					printf("\n");
				}
			}	
		}
	}
}
Beispiel #2
0
void
tokenize(Lexer& lex) {
  while (lex.head != lex.tail) {
    switch (*lex.head) {
    case ' ':
    case '\t':
      if (std::size_t n = space(lex))
        consume(lex, n);
      break;

    case '\n':
      if (std::size_t n = newline(lex))
        consume_newline(lex, n);
      break;

    case '(':
      save_unigraph(lex, Left_paren_tok);
      break;

    case ')':
      save_unigraph(lex, Right_paren_tok);
      break;

    case ':':
      save_unigraph(lex, Colon_tok);
      break;

    case '.':
      save_unigraph(lex, Dot_tok);
      break;

    case '=':
      if (symbol(lex, "=="))
        save_digraph(lex, Equal_equal_tok);
      else
        invalid_char(lex);
      break;

    case '!':
      if (symbol(lex, "!="))
        save_digraph(lex, Not_equal_tok);
      else
        invalid_char(lex);
      break;

    case '+':
      save_unigraph(lex, Plus_tok);
      break;

    case '*':
      save_unigraph(lex, Star_tok);
      break;

    case '/':
      save_unigraph(lex, Div_tok);
      break;

    case '-':
      if (symbol(lex, "->"))
        save_digraph(lex, Imp_tok);
      else
        save_unigraph(lex, Minus_tok);
      break;

    case '<':
      if (symbol(lex, "<->"))
        save_trigraph(lex, Iff_tok);
      else if (symbol(lex, "<="))
        save_digraph(lex, Less_equal_tok);
      else
        save_unigraph(lex, Less_tok);
      break;

    case '>':
      if (symbol(lex, ">="))
        save_digraph(lex, Greater_equal_tok);
      else
        save_unigraph(lex, Greater_tok);
      break;

    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9': {
      std::size_t n = int_literal(lex);
      save_token(lex, Int_literal_tok, n);
      break;
    }

    default:
      // Check for keywords and identifiers.
      if (std::isalpha(*lex.head)) {
        if (std::size_t n = identifier(lex)) {
          save_identifier(lex, n);
          break;
        }
      }
      else {
        invalid_char(lex);
      }

      break;
    }
  }
}