Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 10-24-2004, 09:38 PM   #1 (permalink)
if13121
Registered User
 
if13121's Avatar
 
Join Date: Oct 2004
Location: Bandung,Indonesia
Posts: 16
if13121 is on a distinguished road
Unhappy c simple question problem with switch case

c simple question problem with switch case
I make program to get recognize command from user.
I dont know whats wrong with getCmdId() in file Simulation.c . When i run FileSys the program recognize the command that user input buat it doesnt work for the next command.
why i cannot assign : TCmd[0] = '"copy";
The program consist of several file.Sorry if the program too long i have try to cut them Here it is:
Code:
/*File : FileSys.c*/ #include "Simulasi.h" int main () { int exit = 0; int idx; //indeks line int cmdId; //command identifier int quit=0; kata cmd; Queue Q; CreateEmpty(&Q,10);//menyiapkan buffer perintah InfoType X; while(exit == 0) { printf("input command. End it with #\n"); printf("FileSys/mydir>"); insertCommand(X); Add(&Q,X); //memasukkan perintah ke queue printf("Head = %d\nTail = %d\n", Head(Q), Tail(Q)); idx = getKataLine(X,&cmd,0); cmdId = getCmdId(cmd); switch(cmdId) { case 1 : printf("copy\n"); break; case 2 : printf("rename \n"); break; case 3 : printf("delete\n"); break; case 4 : printf("view\n"); while (quit ==0) { printf("\nFileSys/mydir/"); printf("FileName>>"); insertCommand(X); Add(&Q,X); idx = getKataLine(X,&cmd,0); cmdId = getCmdId(cmd); switch(cmdId) { case 5 : printf("save\n"); break; case 6 : quit = 1; break; } } break; case 7 : printf("list\n"); break; case 8 : printf("concat\n"); break; case 9 : PrintQueue(Q); break; case 10 : exit = 1; printf("exit"); break; case -99: printf("Unknown command\n"); break; } } return 0; }
Code:
/*File : Simulation.h*/ /* prototype*/ #ifndef Simulation_h #define Simulation_h #include "queue.h" int getCmdId(kata Cmd); int IsEQ( kata T1, kata T2); #endif
Code:
/*File : Simulation.c*/ /*body prototype*/ #ifndef Simulation_c #define Simulation_c #include "Simulation.h" int getCmdId(kata Cmd) { int id ; int i=0; int found =0; kata TCmd[9]; TCmd[0].TKata[0] = 'c'; TCmd[0].TKata[1] = 'o'; TCmd[0].TKata[2] = 'p'; TCmd[0].TKata[3] = 'y'; TCmd[0].len = 4; TCmd[1].TKata[0] = 'r'; TCmd[1].TKata[1] = 'e'; TCmd[1].TKata[2] = 'n'; TCmd[1].TKata[3] = 'a'; TCmd[1].TKata[4] = 'm'; TCmd[1].TKata[5] = 'e'; TCmd[1].len = 6; TCmd[2].TKata[0] = 'd'; TCmd[2].TKata[1] = 'e'; TCmd[2].TKata[2] = 'l'; TCmd[2].TKata[3] = 'e'; TCmd[2].TKata[4] = 't'; TCmd[2].TKata[5] = 'e'; TCmd[2].len = 6; TCmd[3].TKata[0] = 'v'; TCmd[3].TKata[1] = 'i'; TCmd[3].TKata[2] = 'e'; TCmd[3].TKata[3] = 'w'; TCmd[3].len = 4; TCmd[4].TKata[0] = 's'; TCmd[4].TKata[1] = 'a'; TCmd[4].TKata[2] = 'v'; TCmd[4].TKata[3] = 'e'; TCmd[4].len = 4; TCmd[5].TKata[0] = 'q'; TCmd[5].TKata[1] = 'u'; TCmd[5].TKata[2] = 'i'; TCmd[5].TKata[3] = 't'; TCmd[5].len = 4; TCmd[6].TKata[0] = 'l'; TCmd[6].TKata[1] = 'i'; TCmd[6].TKata[2] = 's'; TCmd[6].TKata[3] = 't'; TCmd[6].len = 4; TCmd[7].TKata[0] = 'c'; TCmd[7].TKata[1] = 'o'; TCmd[7].TKata[2] = 'n'; TCmd[7].TKata[3] = 'c'; TCmd[7].TKata[4] = 'a'; TCmd[7].TKata[5] = 't'; TCmd[7].len = 6; TCmd[8].TKata[0] = 'd'; TCmd[8].TKata[1] = 'i'; TCmd[8].TKata[2] = 's'; TCmd[8].TKata[3] = 'p'; TCmd[8].TKata[4] = 'l'; TCmd[8].TKata[5] = 'a'; TCmd[8].TKata[6] = 'y'; TCmd[8].len = 7; TCmd[9].TKata[0] = 'e'; TCmd[9].TKata[1] = 'x'; TCmd[9].TKata[2] = 'i'; TCmd[9].TKata[3] = 't'; TCmd[9].len = 4; id =-99; while (found ==0 && i < 10) { if (IsEQ(Cmd,TCmd[i])==1) { id = i+1; found =1; } else { i++; } } return id; } int IsEQ( kata T1, kata T2) { int i = 0; int found = 1;//boolean if (T1.len != T2.len) { found = 0; } else { while (i < (T1.len) && found == 1) { if (T1.TKata[i] != T2.TKata[i]) { found = 0; } else { i++; } } } return found; } #endif
Code:
/*file queue.c*/ #ifndef queue_c #define queue_c #include "queue.h" #include "line.h" int IsEmpty (Queue Q) /*Mengirim true jika Q kosong*/ { return((Head(Q) == Nil) && (Tail(Q) == Nil)); } int IsFull(Queue Q) /*Mengirim true jika tabel penampung Q sudah penuh*/ /*Mengandung elemen MaxEl*/ { return(NbElmt1(Q)==MaxEl(Q)); } void PrintQueue(Queue Q) { int i; if (Head(Q)== Nil && Head(Q)== Tail(Q)) printf("Buffer perintah kosomg\n"); else { if (Head(Q) == Tail(Q)) { PrintLine((Q).T[Head(Q)]); } else { if (Head(Q) < Tail(Q)) { for (i = Head(Q);i <= (Tail(Q));i++ ) { PrintLine((Q).T[i]); } } else { for(i = Head(Q); i <=(MaxEl(Q));i++) { PrintLine((Q).T[i]); } for (i = 1; i<=(Tail(Q));i++) { PrintLine((Q).T[i]); } } } } printf("\n"); } int NbElmt1(Queue Q) /*Mengirimkan banyaknya Elemen Queue */ /*Mengirimkan 0 jika Q kosong*/ { if (IsEmpty(Q)) { return 0; } else { if (Head(Q) < Tail(Q)) { return(Tail(Q)-Head(Q)+1); } else { return(MaxEl(Q)- Head(Q) + Tail(Q)+1); } } } void CreateEmpty(Queue *Q, int size) /*is: sembarang */ /*fs: Sebuah Q kosong terbentuk*/ { (*Q).T = (InfoType * )malloc ((size+1) * sizeof (InfoType)); Head(*Q) = Nil; Tail(*Q) = Nil; MaxEl(*Q)= size; } void DeAlokasi(Queue * Q) /*is : Q pernah dialokasi*/ /*fs : Q menjadi tidak terdefinisi lagi MaxEl(Q) diset 0*/ { free((*Q).T); MaxEl(*Q)=0; } void Add (Queue * Q,InfoType X) /*is : Q mungkin kosong, tabel penampung Q tidak penuh*/ /*fs : X menjadi Tail yang baru*/ { if (IsEmpty(*Q)) { Head(*Q)= 1; Tail(*Q)= 1; } else { if (IsFull(*Q)) { printf("penuh\n"); if (Head(*Q)== MaxEl(*Q)) { Head(*Q)= 1; } else { Head(*Q)= Head(*Q)+ 1; } printf("masuk\n"); if (Tail(*Q)== MaxEl(*Q)) { Tail(*Q)= 1; } else { Tail(*Q)= Tail(*Q)+ 1; } } else { Tail(*Q)=Tail(*Q)+1; } } CopyLine(X,InfoTail(*Q)); } void Del (Queue * Q, InfoType * X) /*is : Q tidak mungkin kosong*/ /*fs : X dihapus dengan aturan FIFO*/ { CopyLine(InfoHead(*Q),*X); if (Head(*Q)== Tail(*Q))/*queue 1 elemen*/ { Head(*Q)= Nil; Tail(*Q)= Nil; } else { if (Head(*Q)== MaxEl(*Q)) { Head(*Q)= 1; } else { Head(*Q)= Head(*Q)+1; } } } #endif
Code:
/*File : ADTkata.h */ /*Realisasi fungsi dan prosedur mesin kata*/ /* Membuat type 'kata' yang merupakan structure */ #ifndef ADTkata_h #define ADTkata_h #define Maxkar 17 typedef struct { char TKata[Maxkar]; int len; }kata; #endif
Code:
/*File : lines.h*/ /* prototype*/ #ifndef line_h #define line_h #include "ADTkata.h" typedef char line[79]; void insertCommand(line in); void CopyLine (line in ,line out); int getKataLine(line in, kata * KK,int idx); void PrintLine(line in); #endif
Code:
/*File : line.c*/ /*body prototype*/ #ifndef line_c #define line_c #include "line.h" static int retval; void insertCommand(line in) { int i=0; char CC; while (CC !='#&#039 <-- ERROR { retval = scanf("%c", &CC); (in[i])= CC; i++; } in[i]='#'; } int getKataLine(line in, kata * KK, int idx) { int i = 0; int stop = 0; while (in[idx] ==' &#039//ignore blank <-- ERROR { idx++; } //get kata while(stop == 0) { KK->TKata[i] = in[idx]; i++; idx++; if (in[idx] =='#' || in[idx]==' &#039 <-- ERROR { stop = 1; } } KK->len = i; return idx; } void CopyLine (line in , line out) { int i=0; while(in[i]!='#&#039 <-- ERROR { (out)[i]=in[i]; i++; } (out)[i]='#'; } void PrintLine(line in) { int i=0; while (in[i] != '#&#039 <-- ERROR { printf("%c",in[i]); i++; } } #endif
Code:
/* file : queue.h*/ #ifndef queque_H #define queque_H #include <stdlib.h> #define Nil 0 #include "line.h" typedef int address; typedef line InfoType; typedef struct { InfoType *T ; address HEAD;/*delete address*/ address TAIL;/*add address*/ int MaxEl; }Queue; /*selector*/ #define Head(Q) (Q).HEAD #define Tail(Q) (Q).TAIL #define InfoHead(Q) (Q).T[(Q).HEAD] #define InfoTail(Q) (Q).T[(Q).TAIL] #define MaxEl(Q) (Q).MaxEl /*prototype*/ void PrintQueue(Queue Q); int IsEmpty (Queue Q); int IsFull(Queue Q); int NBElmt1(Queue Q); void CreateEmpty(Queue *Q, int size); void DeAlokasi(Queue * Q); void Add (Queue * Q,InfoType X); void Del (Queue * Q, InfoType * X); #endif
EDIT: Dude, you need to start using the [ code] [/ code] tags, or in some other way format the code so it's readable.
Also I've added some <-- ERROR marks, where my mind wont work with your code, I dont know if your compiler does or not, or if it's the cut/paste that's done it to your code..
What about your default lable in the switch statements ??
__________________

Last edited by redhead : 10-25-2004 at 12:34 AM.
if13121 is offline   Reply With Quote
Old 10-24-2004, 09:43 PM   #2 (permalink)
if13121
Registered User
 
if13121's Avatar
 
Join Date: Oct 2004
Location: Bandung,Indonesia
Posts: 16
if13121 is on a distinguished road
i dont want to use string.h
__________________
if13121 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question if13121 Standard C, C++ 10 11-01-2004 06:29 AM
Simple C program Spooky Standard C, C++ 1 10-22-2004 07:26 AM
simple c question if13121 Standard C, C++ 3 10-18-2004 10:20 PM
Help for another program Androto Standard C, C++ 54 10-15-2004 07:21 AM
Starting out with C anon C 0 02-24-2003 01:06 PM


All times are GMT -8. The time now is 11:57 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle