2016-09-11 15 views
-6
#include <stdio.h> 
#include<stdlib.h> 
char push(); 
char pop(); 
char display(); 
char peek(); 
char reverse(); 
# define MAX 50 
struct stack 
    { 
     char string[MAX]; 
     int top= -1; 
    }; 
void main() 
    { 
     int a; 
     printf("Choose an option\n"); 
     printf("1.Insert\n"); 
     printf("2.Delete\n"); 
     printf("3.Display\n"); 
     printf("4.Peek\n"); 
     printf("5.Reverse\n"); 
     scanf("%d", &a); 
      switch(a) 
       { 
        { 
         case 1: push(); 
         break; 
        } 
        { 
         case 2: pop(); 
        break; 
        } 
        { 
        case 3: display(); 
        break; 
        } 
        { 
        case 4: peek(); 
        break; 
        } 
        { 
         case 5: reverse(); 
        break; 
        } 
        default : printf("Invalid Input"); 
       } 


    } 
char push(char a) 
    (
     char a; 
     printf("Enter the string"); 
     scanf("%c",&a); 
     if(top=MAX-1) 
     { 
      printf("Stack Overflow"); 
     } 

     else 
     (
     top=top+1; 
     string[top]=a; 
     ) 

    ) 

char pop() 
    (
     char c; 
     (
      if(top=-1) 
      { 
       printf("Stack Underflow"); 
      } 
      else(
       c=string[top]; 
       top=top-1; 
       printf("The character removed is %c", c); 
       ) 
      ) 
    ) 


char display() 
    { 
     int i; 
     for(i=0,i<=MAX-1,i++) 
      { 
       printf("%c", string[i]); 
      } 
    } 


char peek() 
    (
    printf("The top element is %c", string[top]); 
    ) 


char reverse() 
    (
    int i; 
    printf("The reverse of string is"); 
    for(i=MAX-1,i>=0,i--) 

     (
     printf("%c",string[i]) 
     ) 

    ) 

Это моя программа, только что начавшаяся с программированием около месяца назад. Ниже приведены мои ошибки. Пожалуйста помоги. Я не могу понять из других ответов, если кто-то может исправить мою ошибку, я буду учиться лучше. БлагодаряОшибки компилятора в моей программе

prog.c:12:16: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token 
     int top= -1; 
       ^

prog.c:14:6: warning: return type of 'main' is not 'int' [-Wmain] 
void main() 
    ^

prog.c:54:9: error: expected declaration specifiers or '...' before 'printf' 
     printf("Enter the string"); 
     ^

prog.c:55:9: error: expected declaration specifiers or '...' before 'scanf' 
     scanf("%c",&a); 
     ^

prog.c:56:9: error: expected declaration specifiers or '...' before 'if' 
     if(top=MAX-1) 
     ^

prog.c:73:13: error: expected identifier or '(' before 'if' 
      if(top=-1) 
      ^

prog.c:98:6: error: expected declaration specifiers or '...' before 'printf' 
     printf("The top element is %c", string[top]); 
    ^

prog.c:96:6: error: 'peek' declared as function returning a function 
char peek() 
    ^

prog.c:96:6: error: conflicting types for 'peek' 

prog.c:6:6: note: previous declaration of 'peek' was here 
char peek(); 
    ^

prog.c: In function 'peek': 

prog.c:105:6: error: expected declaration specifiers or '...' before 'printf' 
     printf("The reverse of string is"); 
    ^

prog.c:106:6: error: expected declaration specifiers or '...' before 'for' 
     for(i=MAX-1,i>=0,i--) 
    ^

prog.c:112:6: error: expected '{' at end of input 
    ) 
    ^

prog.c:112:6: warning: control reaches end of non-void function [-Wreturn-type] 
    ) 
    ^
+2

Вы не можете инициализировать структур, как, что в с –

+0

Также 'если (top = -1) ', вероятно, является ошибкой. – detly

+0

Измените 'void main' на' int main'. Если вы используете ресурсы, предлагающие 'void main', то выбросите их далеко и получите что-то, написанное за последние 20 лет. Если вы находитесь в индийской образовательной системе и все еще работаете с TurboC и такими устаревшими идеями, то у вас есть мое сочувствие, но мы не можем сделать вашу домашнюю работу для вас. Вы должны знать через месяц, что вам нужно иметь дело с вашими ошибками, фиксируя их один за другим и перекомпилируя, чтобы посмотреть, какие из них остаются после каждого изменения. Если вам это пока не ясно, начните делать это сейчас. – jwpfox

ответ

2

struct stack {...}; определяет тип, а именно struct stack.

Вы не можете «инициализировать» тип, и вы не можете определить значения по умолчанию для членов типа struct, или что вы делаете, когда делаете ... top = -1.

Что вы можете сделать, это определить переменную этого типа и инициализирует его:

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


#define STRING_MAX (42) 

struct Stack { 
    char string[STRING_MAX]; 
    int top; 
} 


int main(void) { 
    struct Stack stack = { 
    "top entry", 
    1 
    }; 

    printf("string = '%s', top = %d\n", stack.string, stack.top); 

    return EXIT_SUCCESS; 
} 

В приведенном выше примере принтами:

string = 'top entry', top = 1 
Смежные вопросы