GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Oct-2004, 23:37
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road

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:


CPP / C++ / C 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;
}

CPP / C++ / C 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

CPP / C++ / C 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

CPP / C++ / C 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

CPP / C++ / C 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

CPP / C++ / C 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

CPP / C++ / C 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 !='#')
	{	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] ==' ')//ignore blank
	{idx++;}
	 //get kata
	while(stop == 0)
	{
		KK->TKata[i] = in[idx];
		i++; idx++;
		if (in[idx] =='#' || in[idx]==' ')
		{stop = 1;}
	}
	KK->len = i;
	return idx;
}

void CopyLine (line in ,  line  out)
{	
	int i=0;
	while(in[i]!='#')
		{	(out)[i]=in[i];
			i++;
		}
	(out)[i]='#';
}
void PrintLine(line in)
{
	int i=0;
	while (in[i] != '#')
	{printf("%c",in[i]);
	i++;}
}
#endif

CPP / C++ / C 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);
  /*Menampillkan queue command ke layar*/
  
  int IsEmpty (Queue Q);
  /*Mengirim true jika Q kosong*/
  int IsFull(Queue Q);
  /*Mengirim true jika tabel penampung Q sudah penuh*/ 
  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
Last edited by LuciWiz : 25-Oct-2004 at 01:01. Reason: Please insert your C code between [c] [/c] tags
  #2  
Old 24-Oct-2004, 23:49
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road
i dont want to use string.h
Please help
  #3  
Old 25-Oct-2004, 01:06
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by if13121
i dont want to use string.h
Please help
1) Where is your problem? There's 390 lines of uncommented, unformatted code which is extremely difficult to read, and no indication as to where the problem is nor really what the problem is? And you have been asked 4 times to use code tags, and still no code tags.
2) Why don't you want to use the string manipulation package to manipulate strings? It's standard with C. What would you like to use?
3) Strings must end in a '\0' to be strings. In the code where you load the strings you don't end them in '\0'

[edit]Thank you LuciWiz, but I think we have to stop hand holding them sometime... at least IMAO [/edit]
__________________

Age is unimportant -- except in cheese
Last edited by WaltP : 25-Oct-2004 at 01:57.
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Repetition structure problem and question brookeville C++ Forum 17 29-Oct-2004 17:48
there has to be a better way dabigmooish C++ Forum 8 17-May-2004 10:24
c++ help homz C++ Forum 16 14-May-2004 09:16
C++ file I/O CronoX C++ Forum 36 09-Mar-2004 17:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:44.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.