Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,179 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,002 people online right now. Registration is fast and FREE... Join Now!




a cool C program

 
Reply to this topicStart new topic

a cool C program, please help me out....

saggy29
1 Aug, 2007 - 08:03 AM
Post #1

New D.I.C Head
*

Joined: 1 Aug, 2007
Posts: 1


My Contributions
hi,
this is a program to convert a 4 digit integer into roman numeral... check if it is correct... i've taken a very raw method to get the o/p... plz suggest me a better and simpler code...





#include<stdio.h>
#include<conio.h>
void main()
{
int num,year;
clrscr();
printf("enter a year");
scanf("%d",&year);
if(year>=1 && year<=1999)
rom(year);
else
printf("sorry... cant write bigger code");
getch();
}
rom(int year)
{
int y;
y=year/1000;
{
if(y==1)
printf("m");
}
y=(year%1000)/100;
{
if(y==1)
printf("c");
else if(y==2)
printf("cc");
else if(y==3)
printf("ccc");
else if(y==4)
printf("cccc");
else if(y==5)
printf("d");
else if(y==6)
printf("dc");
else if(y==7)
printf("dcc");
else if(y==8)
printf("dccc");
else if(y==9)
printf("dcccc");
}
y=(year%100)/10;
{
if(y==1)
printf("x");
else if(y==2)
printf("xx");
else if(y==3)
printf("xxx");
else if(y==4)
printf("xl");
else if(y==5)
printf("l");
else if(y==6)
printf("lx");
else if(y==7)
printf("lxx");
else if(y==8)
printf("lxxx");
else
printf("xc");
}
y=year%10;
{
if(y==1)
printf("i");
else if(y==2)
printf("ii");
else if(y==3)
printf("iii");
else if(y==4)
printf("iv");
else if(y==5)
printf("v");
else if(y==6)
printf("vi");
else if(y==7)
printf("vii");
else if(y==8)
printf("viii");
else if(y==9)
printf("ix");
}
}
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: A Cool C Program
1 Aug, 2007 - 08:13 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Just for readability sake I would switch

CODE

if(y==1)
printf("c");
else if(y==2)
printf("cc");
else if(y==3)
printf("ccc");
else if(y==4)
printf("cccc");
else if(y==5)
printf("d");
else if(y==6)
printf("dc");
else if(y==7)
printf("dcc");
else if(y==8)
printf("dccc");
else if(y==9)
printf("dcccc");


To something like

CODE

switch (y)
{
    case 1:
        printf("c");
        break;
    case 2:
         printf("cc");
         break;
    case 3:
         printf("ccc")'
         break;
    case 4:
         printf("cccc");
         break;
    // and so on
}


In my opinion it just makes it easier to read
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: A Cool C Program
1 Aug, 2007 - 09:01 AM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,451



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
If you are going to hard code the values, here is a unique approach.

CODE

#include <stdio.h>
#include <ctype.h>

#define MAX 128

int main(int argc, char *argv[]) {
    char romans[13][MAX];
    int arg_1;

    if(argc < 2) {
        printf("You must provide at least one argument\n");
        exit(0);
    }

    arg_1=atoi(argv[1]);
    if(arg_1>12) {
        printf("We can only go as high as 12\n");
        exit(0);
    }    
    sprintf(romans[0],"Roman Strings");    
    sprintf(romans[1],"I");
    sprintf(romans[2],"II");
    sprintf(romans[3],"III");
    sprintf(romans[4],"IV");
    sprintf(romans[5],"V");
    sprintf(romans[6],"VI");
    sprintf(romans[7],"VII");
    sprintf(romans[8],"VIII");
    sprintf(romans[9],"IX");
    sprintf(romans[10],"X");
    sprintf(romans[11],"XI");
    sprintf(romans[12],"XII");

    printf("%i equals %s\n",arg_1,romans[arg_1]);
    
    return 0;
}

User is online!Profile CardPM
+Quote Post

ajaymatrix
RE: A Cool C Program
1 Aug, 2007 - 11:56 AM
Post #4

D.I.C Regular
Group Icon

Joined: 15 May, 2007
Posts: 389



Thanked: 1 times
Dream Kudos: 100
My Contributions
conversion into roman numerals is not that easy as it may seem...

for eg : 950 is coded as CML..
think!!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:03AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month