|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Programming Challenge
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#51 (permalink) | |||||||||||||
|
With great difficulty
![]() |
From the version that accepts cmd line args
__________________Code:
time ./a.out $56.08 fifty six dollars and eight cents real 0m0.004s user 0m0.001s sys 0m0.003s
|
|||||||||||||
|
|
|
|
#52 (permalink) | ||||||||||||||
|
With great difficulty
![]() |
Quote:
Code:
i=temp2/100; if(i==1) cout<<"One"; else if(i==2) cout<<"Two"; else if(i==3) cout<<"Three"; else if(i==4) cout<<"Four"; else if(i==5) cout<<"Five"; else if(i==6) cout<<"Six"; else if(i==7) cout<<"Seven"; else if(i==8) cout<<"Eight"; else if(i==9) cout<<"Nine"; Code:
i=temp2/100;
switch(i){
case(1) :
cout << "One";
break;
case(2) :
cout << "Two";
break;
//etc, etc
}
|
||||||||||||||
|
|
|
|
#53 (permalink) | ||||||||||||||
|
Photography nut
![]() |
Yeah thats the downside of scripting languages. I was interested though in seeing how much faster it would be in C.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
|
|
#54 (permalink) | ||||||||||||||
|
Programmer
![]() |
Quote:
There's a lot of methods (class, another function, using an array to store values ....etc) which is smaller/consumes less memory but as I told you I did what I'm fast at ![]() Thanks anyway
__________________
GeForce 8800GTS 320MB/9600GT/8800GT 3DMark06 11518/10963/13622 3DMark05 17948/17446/23147 3DMark03 36902/36240/42768 3DMark01 49944/43675/53827 Total 116312/109324/133364
|
||||||||||||||
|
|
|
|
|
#55 (permalink) | |||||||||||||||
|
Photography nut
![]() |
Quote:
The last time I tested its execution speed was on my macbook pro. I just ran in on my linux box (Slackware 12.1) and it was this. Code:
$ time ruby challange_1.rb $56.04 "fifty six dollars and four cents" real 0m0.004s user 0m0.004s sys 0m0.000s
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
|||||||||||||||
|
|
|
|
#56 (permalink) | ||||||||||||||
|
With great difficulty
![]() |
Quote:
![]() Here is the code for the command line version if you want to test for consistency Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFF_SIZE 1024
char *toString(int num);
char *makeString(char *buffer);
int main(int argc, char **argv){
enum { file, arguments } mode; //describes input type
FILE *infile;
int i = -1; //counts argument position
char read_buffer[BUFF_SIZE], *string, *curr_pos, *decimal_ptr;
int dollars, cents;
if(argc < 2){
printf("Usage: ./a.out [filename][amount...]n");
exit(1);
}else if(argv[1][0] == '$'){
mode = arguments;
i = 1;
}else{
printf("Reading from filen");
mode = file;
infile = fopen(argv[1], NULL);
if(!infile){
printf("Unable to open %sn", argv[1]);
}
fgets(read_buffer, BUFF_SIZE, infile);
printf("%sn", read_buffer);
}
while(((i > 0) && (i < argc)) || ((i < 0) && fgets(read_buffer, BUFF_SIZE, infile))){
if(mode == file)
string = read_buffer;
else
string = argv[i++];
curr_pos = string;
if(*curr_pos != '$'){
printf("Formatting error on input: %sn", string);
exit(1);
}
/* separate decimal from whole number amount */
while(*curr_pos && (*curr_pos != '.')) curr_pos++;
*curr_pos = '';
decimal_ptr = ++curr_pos;
curr_pos = string + 1;
/* get integer values of dollars/cents */
dollars = atoi(curr_pos);
cents = atoi(decimal_ptr);
char *d = (dollars == 1) ? "dollar" : "dollars";
char *c = (cents == 1) ? "cent" : "cents";
printf("%s %s and %s %sn", toString(dollars), d, toString(cents), c);
}
}
char *toString(int num){
char *ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
char *teens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
char *tens[] = {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
char buffer[1024], *buff_ptr = buffer, *ret;
buffer[0] = '';
if(num == 0) return makeString(ones[0]);
int index = num/100;
if(index){
strcpy(buff_ptr, ones[index]);
buff_ptr += strlen(ones[index]);
*buff_ptr++ = ' ';
strcpy(buff_ptr, "hundred");
buff_ptr += strlen("hundred");
*buff_ptr = '';
}
num %= 100;
index = num/10;
if(index){
if(buffer[0])
*buff_ptr++ = ' ';
if(index == 1){
strcpy(buff_ptr, teens[num-10]);
return makeString(buffer);
}
strcpy(buff_ptr, tens[index-2]);
buff_ptr += strlen(tens[index-2]);
*buff_ptr = '';
}
num %= 10;
if(num){
if(buffer[0])
*buff_ptr++ = ' ';
strcpy(buff_ptr, ones[num]);
}
return makeString(buffer);
}
char *makeString(char *buffer){
char *ret;
size_t len = strlen(buffer);
if(len > 1024) return NULL;
ret = malloc(sizeof(char) * len);
strcpy(ret, buffer);
}
|
||||||||||||||
|
|
|
|
#57 (permalink) | |||||||||||||||
|
Photography nut
![]() |
Quote:
Haha ![]() Code:
$ time ./a.out $56.09 fifty six dollars and nine cents real 0m0.001s user 0m0.000s sys 0m0.000s
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
|||||||||||||||
|
|
|
|
#58 (permalink) | |||||||||||||
|
Programmer
|
Hmm, when's the next contest?
need to sharpen up my programming skills again =D
__________________
http://img165.imageshack.us/img165/8987/neoou0.png http://img518.imageshack.us/img518/7995/neoxt9.jpg _look! Butterfly. *chases*
|
|||||||||||||
|
|
|
|
|
#59 (permalink) | |||||||||||||
|
With great difficulty
![]() |
You can still submit a solution to this one. Next one gets posted thursday according to DH
__________________
|
|||||||||||||
|
|
|
|
#60 (permalink) | ||||||||||||||
|
Photography nut
![]() |
Hey guys. I'll be a little late on posting the next challenge. Worked late tonight! I'll try getting the next challenge though posted tonight.
As for the previous challenge, thanks rabidgnome229 and alawadhi3000 for participating. rabidgnome provided the most efficient solution. Congrates!
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|