2016-09-11 6 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]) 
     ) 

    ) 

これは約1ヶ月前にCプログラミングを始めた私のプログラムです。 以下は私の間違いです。助けてください。私は他の答えから分かりません。誰かが私のミスを訂正できれば、私はよりよく学びます。おかげ私のプログラムのコンパイラエラー

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

あなたはまた 'もしC –

+0

でそのようなあなたの構造体を初期化することはできません(top = -1) 'はおそらく間違いです。 – detly

+0

'void main'を' int main'に変更します。 'void main'を提案するリソースを使用している場合は、それらを遠くに投げて、過去20年間に何かを書いてください。あなたがインドの教育システムにいて、まだTurboCなどの古いアイデアを使っているのであれば、あなたは私の同情を持っていますが、あなたの宿題はできません。 1ヶ月後にエラーを処理する必要があることを、1つずつ修正し、再コンパイルして各変更後に残っているものを確認する必要があることを知っておく必要があります。それがまだあなたにはっきりしていない場合は、今からやってみてください。 – 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