|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Programming Challenge
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#41 (permalink) | |||||||||||||
|
With great difficulty
![]() |
What are the parameter bounds? Should it be able to handle amounts in the hundreds? Thousands? Billions?
__________________
|
|||||||||||||
|
|
|
|
#42 (permalink) | ||||||||||||||
|
Photography nut
![]() |
I set it to be under a thousand, so your program should be able to handle any dollar amount smaller then that, but you can go higher.
__________________
"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
|
||||||||||||||
|
|
|
|
#43 (permalink) | |||||||||||||
|
With great difficulty
![]() |
I just threw something together using a bash shell and ran into a problem. In bash $WHATEVER assumes WHATEVER is a variable (such as HOME or PATH) and expands it. Passing $12.11 expands $1 (to nothing) and passes only the string 2.11. In order to pass "$12.11" it must be in quotes or passed as \$12.11
__________________
|
|||||||||||||
|
|
|
|
#44 (permalink) | |||||||||||||
|
.
![]() |
So if we write this is something like java or c#, would it also be acceptable just to ask for the dollar amount? Maybe to allow someone to type as many as they want in a single line rather than having it stuctured like the run command you posted earlier. I'm going to assume yes while I start on this
.
__________________
Imaging with Windows PE Please keep the OCN Terms of Service in mind when posting.
|
|||||||||||||
|
|
|
|
#45 (permalink) | ||||||||||||||
|
Photography nut
![]() |
Ok, it doesn't matter how someone can enter the amount to the program, just as long as it has a dollar sign($) and a decimal point.
__________________
"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
|
||||||||||||||
|
|
|
|
#46 (permalink) | |||||||||||||
|
.
![]() |
sounds good. Should be fun.
__________________
Imaging with Windows PE Please keep the OCN Terms of Service in mind when posting.
|
|||||||||||||
|
|
|
|
#47 (permalink) | |||||||||||||
|
With great difficulty
![]() |
Here it is in C (not ideal for the task, but my default language) with the user input method. It does not check for erroneous input
__________________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;
printf("Enter a string begining with $ to convert: ");
while(fgets(read_buffer, BUFF_SIZE, stdin)){
string = read_buffer;
curr_pos = string;
if(*curr_pos != '$'){
printf("Done converting, exitingn");
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);
printf("Enter a string begining with $ to convert: ");
}
}
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);
}
Last edited by rabidgnome229 : 07-24-08 at 09:53 PM |
|||||||||||||
|
|
|
|
#48 (permalink) | |||||||||||||
|
.
![]() |
Good job man. I haven't even started yet haha.
__________________
Imaging with Windows PE Please keep the OCN Terms of Service in mind when posting.
|
|||||||||||||
|
|
|
|
#49 (permalink) | ||||||||||||||
|
Photography nut
![]() |
I did want solutions posted so early but I guess it doesn't really hurt.
![]() Edit: looks a little messy if you don't know ruby, but works. Code:
$ time ruby challenge_1.rb $56.08 "fifty six dollars and eight cents" real 0m0.008s user 0m0.004s sys 0m0.003s Code:
#!/usr/bin/ruby
d1 = ARGV.shift
ones = { 0 => "", 1 => "one", 2 => "two", 3 => "three", 4 => "four",
5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine",
10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen", 14 => "fourteen",
15 => "fifteen", 16 => "sixteen", 17 => "seventeen", 18 => "eighteen", 19 => "nineteen" }
tens = { 0 => "", 1 => "ten", 2 => "twenty", 3 => "thirty", 4 => "fourty",
5 => "fifty", 6 => "sixty", 7 => "seventy", 8 => "eighty", 9 => "ninety" }
d1 = d1.split('').map {|x| x.to_i}
d1.shift
d1[-3] = '.'
f = String.new
f = "#{ones[d1.shift]} hundred and " if d1.join.to_f > 100
if d1.join.to_f > 20.00
f.concat "#{tens[d1.shift]} #{ones[d1.shift]} dollars "
else
f.concat "#{ones[d1.shift]} #{ones[d1.shift]} dollars "
end
d1.shift
if d1.join.to_i < 20
f.concat "and #{ones[d1.join.to_i]} cents"
else
f.concat "and #{tens[d1.shift]} #{ones[d1.shift]} cents"
end
p f
__________________
"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
Last edited by dangerousHobo : 07-24-08 at 10:43 PM |
||||||||||||||
|
|
|
|
#50 (permalink) | |||||||||||||
|
Programmer
![]() |
Here's my code in C++
Code:
#include<iostream>
using namespace std;
//Printing Function
void print(int a)
{
int temp;
temp=a;
int i=0;
int tens=0;
int temp2;
temp2=temp;
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";
if(i>0 && i<10)
cout<<" Hundred ";
temp2=temp;
i=temp2%100;
i=i/10;
if(i>0)
{
if(i==2) cout<<"Twenty";
else if(i==3) cout<<"Thirty";
else if(i==4) cout<<"Fourty";
else if(i==5) cout<<"Fifty";
else if(i==6) cout<<"Sixty";
else if(i==7) cout<<"Seventy";
else if(i==8) cout<<"Eightty";
else if(i==9) cout<<"Ninety";
tens=i;
cout<<" ";
}
temp2=temp;
i=temp2%10;
if(tens==1)
{
if(i==1) cout<<"Eleven";
else if(i==2) cout<<"Twelve";
else if(i==3) cout<<"Thirteen";
else if(i==4) cout<<"Fourteen";
else if(i==5) cout<<"Fifteen";
else if(i==6) cout<<"Sixteen";
else if(i==7) cout<<"Seventeen";
else if(i==8) cout<<"Eighteen";
else if(i==9) cout<<"Nineteen";
}
else if(i>0)
{
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";
}
cout<<endl;
}
//end of Function
//Main
int main()
{
int amount=0;
char dollar;
cout<<"Menu"<<endl;
cout<<"Please Enter the amount with the dollar sign first"<<endl;
cin>>dollar>>amount;
while(amount<0 || amount>1000)
{
cout<<"Please Enter the amount with the dollar sign first"<<endl;
cin>>dollar>>amount;
}
print(amount);
int b;
cout<<"Enter anything to Exit"<<endl;
cin>>b;
return 0;
}
Also it only accepts entering the data manually as I wanted to code it fast I will try to fix them later
__________________
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
|
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|