English number models a calculator?

This is a discussion on English number models a calculator? within the C Programming forums, part of the General Programming Boards category; the c programme's like: one two + eight = two zero. input a number below 100 /*such as 'one two'*/ ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Unhappy English number models a calculator?

    the c programme's like:

    one two + eight =
    two zero.

    input a number below 100 /*such as 'one two'*/
    then it will convert to 12.
    input "+"/*plus*/
    while scan another number below 100/* eight */
    it converts to 8

    finally pc calculate 12+8=20
    ouput the result "two zero".

    I have tried to use switch and if-else control flow,but it does not convert well

    appreciate anyone to help me.

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Code:
    #include <stdio.h>
    void main()
    {   
    	int a;
    	char s1[10]="one",s2[10]="two",s3[10]="three";
    	char s[10];
    	
    	scanf("%s",&s[10]);
    	
    	if(s[10]==s1[10])
    	{a=1;
         printf("%d\n",a);	
    	}
        else if(s[10]==s2[10])
    	{a=2;
         printf("%d\n",a);	
    	}
        else if(s[10]==s3[10])
    	{a=3;
         printf("%d\n",a);	
    	}
    
    }
    i try on this way to convert, yet it when i input two a remains 1.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Do you know what this line of code does?
    Code:
    scanf("%s",&s[10]);
    If you don't, don't feel bad because neither do I. I think what you want is:
    Code:
    scanf("%s", s);
    Also, "void main()" should be "int main(void)"
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Looks like you need some pointers.

    First, here's an easier way to create an array of the word numbers:
    Code:
    char *nums[10] = { "zero", "one", "two", "three", "four", 
         "five", "six", "seven", "eight", "nine" };
    nums is an array of string literals. That means it has to be defined (assigned values) when it is declared (as above). Generally you should not try and modify a string literal after the declaration.

    Next, you cannot compare strings this way:
    Code:
    if(s[10]==s1[10])
    The easiest way is to use strcmp() from string.h. Strcmp() returns 0 when it's two parameters are identical. I suggest you look up this and the other new commands from this post in whatever reference you are using in order to make sure you understand them. Now lets put a few things together:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    	char *nums[10] = { "zero", "one", "two", "three", "four", 
    		"five", "six", "seven", "eight", "nine" }, input[16];
    	int i;
    
    	while (1) {
    		printf("Enter a number: ");
    		fgets(input,16,stdin);
    		if (input[0] == 'q') return 0;  // q to quit
    		if (sscanf(input, "%[a-z]", input) != 1) {
    			puts("Invalid! Try again...");
    			continue;
    		}                   
    		for (i=0;i<10;i++) {
    			if (strcmp(nums[i],input) == 0) {
    				printf("digit = %d\n",i);
    				break;
    			}
    			if (i==9) printf("\"%s\" is not valid input.\n",input);
    		}
    	}
    
    	return 0;
    }
    "while(1)" is an infinite loop, because the condition is always true. You must kill the program, or enter "q" to get out of it.

    I use fgets for the input because that's my personal preference (also: some methods are better than others ). However, this will include a "\n" (eg, "one\n", so to avoid that I rip a string of lower case characters from the raw input data with sscanf. That's the line in red. Notice you can read from, and read into, the same string variable with sscanf (or they could be two different variables). Sscanf, like all the "scan" type functions, returns the number of items successfully read. So if we do not find a lowercase word in "input", we throw an error to the user.

    If everything works out, we do the strcmp() in a loop through the array of word numbers.

    Try this out and ask questions if you want.
    Last edited by MK27; 04-21-2010 at 08:38 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Wink

    Be thankful for Mk27's detailing explaination.
    learnt much from your personal approach.:-)

    Yet,I still got a question about the code.

    Code:
    fgets(input,16,stdin);
    when i use the routine fgets,why needn't declare the type of the "input"?
    Last edited by procs; 04-22-2010 at 09:20 AM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Quote Originally Posted by NeonBlack View Post
    Do you know what this line of code does?
    Code:
    scanf("%s",&s[10]);
    If you don't, don't feel bad because neither do I. I think what you want is:
    Code:
    scanf("%s", s);
    Also, "void main()" should be "int main(void)"
    Code:
    scanf("%s",&s[10]);
    thanks for your reply,this is a lower mistake I made.
    it's a routine to store the resulting values thourgh the argument.:-)

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do need to declare the variable. MK just forgot, and will soon edit the code to put it in the proper place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21