can anyone here show me some examples of converter like Currency converter or
universal measurement converter...i tried to search but no working example..
15 2478
Well, there are rules against doing things like posting full code, and this sounds like a pretty common homework problem.
However, we can help you with your program, have you started this? I'd start this by looking at how this is done by hand - gathering the conversion ratios. Then you can write an algorithm for an "any case" conversion, and from there create your code.
I'd recommend Google for the current currency conversion rates, and probably Google or Wikipedia for measurement conversion ratios.
the problem is i don't understand my question ..
well my question is like this
PROBLEM STATEMENT
Write a program that takes a measurement in one unit (e.g., 4.5 miles, etc) and converts it to another unit (i.e. kilometers). For example this conversion request
450 km miles
Would result in this program output
Attempting conversion of 450.000 km to miles …
450.000 km = 279.6247 miles
The program should produce an error message if a conversion between two unit of different classes (e.g., distance to volume) is requested. The program should take a data base of conversion information from an input file before accepting conversion programs entered interactively by the user. The user should be able to specify units either by name (e.g. kilograms) or by abbreviation (e.g. kg).
the the ALGORITHM)
The algorithm for the program is fairly straightforward.
1
. Load units of measurement database.
2.
Get value to convert and old and new unit names.
3.
Repeat until data format error encountered.
4.
Search for old unit in database.
5.
Search for new units in database.
6.
If conversion is impossible, issue appropriate message,
7.
Else, compute and display conversion
8.
Get value to convert an old and new unit names.
about loading units of measurement database do we need to fscan database from a unit.dat or just do
define a structure type that groups all relevant attributes about one unit..
examples of unit is
miles mi distance 1609.3
kilometers km distance 1000
yards yd distance 0.9144
Ok, so, it seems pretty clear to me. Let's go over the information we have:
The program should take a data base of conversion information from an input file
So you are reading a file here. Now, clearly, you want to store this information somehow in your program. Some data structure needs to hold the information after you read the file, right?
If you want to define a structure type, that's up to you.
Ok, so, it seems pretty clear to me. Let's go over the information we have:
So you are reading a file here. Now, clearly, you want to store this information somehow in your program. Some data structure needs to hold the information after you read the file, right?
If you want to define a structure type, that's up to you.
Agreed, from this file you can determine what specific conversions you are going to need to make - distance, volume, time, etc..., and you can look up those conversion formulas. From there, you can store those in a "database" or data structure, and once you read the amount and type from the file, based on the type determine which formula to use.
Please have a look at our Posting and Homework Guidelines, take a stab at it, and come back with your questions.
miles mi distance "value"
may i know what use is miles and distance?
both of them have the same value...
i don't get the idea..is it for one way conversion??
Banfa 9,065
Expert Mod 8TB
distance (or length) is what is being measured, there are many units you could measure distance (or length) in, miles is one of them, kilometres is another.
miles mi distance "value"
may i know what use is miles and distance?
both of them have the same value...
i don't get the idea..is it for one way conversion??
Are you quoting one line from the database file? If so, then let's try to parse the line:
... miles ......... This is the name of the unit.
... mi ............. This is the abbreviation of the unit.
... distance .... This is the class the unit belongs to.
... "value" ....... Is this perhaps a numeric value in the file? If so, it is the scale factor used to convert a distance to or from miles. To understand how to use this scale factor you need to compare two lines from your database with identical class.
can i know the ways of acessing the arrays in my structure??
i want to use it in my calculations..........
like
struct unit
{
float value[10];
char unit[10];
char measurement[10];
}
how to access it from the main??
is it using pointers but how to call individually?
well here is my incomplete coding..i dunno how to get the file until error is found...in my first post..
stuck hard at point 3 and 4
where we have to encounter error..
the unit.dat
is -
kilometers km distance 1000
-
yards yd distance 0.9144
-
meters m distance 1
-
quarts qt volume 0.94635
-
liters l volume 1
-
gallons gal volume 3.7854
-
milliliters ml volume 0.001
-
kilograms kg mass 1
-
grams g mass 0.001
-
slugs slugs mass 0.14594
-
my code is this
-
i
-
#include <stdio.h>
-
void calculate conversion(double input)
-
char input_oldunit;
-
double input_value;
-
char input_newunit;
-
struct unit
-
4{
-
char oldunit[12];
-
char SI [5];
-
char newunit[10];
-
double value;
-
};
-
-
int main()
-
{
-
struct unit h[20];
-
int k = 0.0;
-
char choice
-
-
-
FILE *measurement;
-
-
printf("\THIS IS A UNIVERSAL MEASUREMENT PROGRAMn");
-
-
measurement = fopen("unit.dat","r");
-
do{
-
printf("\nPlease select q to quit or other to enter conversion program:");
-
scanf("%s",&choice);
-
printf("You have selected option %d\n\n",choice);
-
switch(choice)
-
{
-
-
-
case q:
-
-
printf("thank s and have a nice day\n);
-
-
return 0;
-
break;
-
}
-
default:{
-
-
-
if (measurement == NULL)
-
printf("Error opening data file.\n");
-
else
-
{
-
while(fscanf(measurement,"%s %s %s %lf ",&h1[k].oldunit,&h1[k].SI,&h1[k].newunit,&h1[k].value)==4)
-
{
-
printf(" input your values followed with it's measurement name or SI unit and the target measurement's name or SI unit.\n");
-
scanf(" %lf %s %s",&input_value,&input_oldunit,&input_newunit);
-
calculate conversion(input);
-
}
-
-
-
}
-
-
-
-
-
}
-
-
fclose(measurement);
-
}
-
printf("If you want to quit, press q or else press any key to continue\n");
-
scanf("%s",&choice);
-
}while(optn!=q);
-
printf("To the infinite..and BEYOND....!!!!!\n\n");
-
break;
-
return;
-
}
-
-
-
well here is my incomplete coding..i dunno how to get the file until error is found...in my first post..
stuck hard at point 3 and 4 where we have to encounter error..
What do you mean by "dunno how to get the file until error is found ... in my first post"? The first post for this thread doesn't describe a specific error. Can you describe the problem you're referring to?
When you say "stuck hard at point 3 and 4", do you mean steps 3 and 4 of the algorithm as outlined in message #3:
3. Repeat until data format error encountered.
4. Search for old unit in database.
It would be easier to analyze your code if I knew all the ways in which you believe it to be incomplete.
well now the search file until not found or error..
is a if statement right?
so do i have to use a binary search like this??
is it possible??
can i use it??/ -
int binary search(int arr[],int size,int target)
-
-
{
-
-
int middleposition;
-
int middlevalue;
-
int result = -1;
-
int low = 0;
-
int high = SIZE - 1;
-
while(result == -1&& low <= high)
-
{
-
-
middleposition = (low -high)/2;
-
middlevalue = arr[[middleposition];
-
if(target ==middlevalue)
-
{
-
result = middleposition;
-
}
-
else if (target<middlevalue)
-
high = middleposition -1;
-
else
-
low = middleposition +1;
-
}
-
return result;
-
}
-
The binary search algorithm only works if the input list is sorted.
Can you count on the database file to be in strictly sorted order?
Is it worthwhile for you to add a step to create a sorted database file?
An easy alternative to a binary search is a linear search (start at the beginning and look at each entry til you find the one you want). Does the database file have so many entries that the speed difference between linear search and binary search is significant?
well here goes my coding..any way to improve it?
i done with the unit.dat file which contains
measurement ,abbreviation, classes and it's value.
i use linear search...
any methods to make it more neat,tidy..?
anyway there are some errors that i tried but couldn't correct it..
is it possible to make the fscanf simple without the function prototype??? -
#include<stdio.h>
-
#include<string.h>
-
#define NOT_FOUND -1
-
typedef struct
-
{
-
char name[30];
-
char abbrev[15];
-
char class[20];
-
double standard;
-
} unit_t
-
-
int fscan_unit(FILE *filep,unit_t *unitp){
-
int status;
-
status = fscanf(filep, "%s%s%s%lf",(*unitp).name,(*unitp).abbrev,(*unitp).class,&(*unitp).standard);
-
if(status == 4)
-
status = 1;
-
else if (status != EOF)
-
status = 0;
-
-
return (status);
-
}
-
-
void
-
load_units(int unit_max,
-
unit_t units[],
-
int *unit_sizep)
-
-
{
-
FILE *inp;
-
unit_t data;
-
int i, status;
-
-
inp = fopen("units.dat","r");
-
i = 0;
-
-
for (status = fscan_unit(inp, &data);
-
status == 1 && i < unit_max;
-
status = fscan_unit(inp, &data)) {
-
units[i++] = data;
-
}
-
fclose(inp);
-
if (status == 0) {
-
printf("\n*** Error in data format ***\n");
-
printf("*** Using first &d data format ***\n");
-
} else if (status != EOF) {
-
printf("\n*** Error: too much data in file ***\n");
-
printf("*** Using first %d data values ***\n", i);
-
}
-
-
*unit_sizep = i;
-
}
-
-
-
-
int
-
search(const unit_t units[],
-
const char *target,
-
int n)
-
-
{
-
int i,
-
found = 0,
-
where;
-
-
i = 0;
-
while (!found && i < n) {
-
if (strcmp(units[i].name, target) == 0 ||
-
strcmp(units[i].abbrev, target) == 0)
-
found = 1;
-
else
-
++i;
-
}
-
if(found)
-
where = i;
-
else
-
where = NOT_FOUND;
-
return(where);
-
-
}
-
double convert(double quantity,double old_stand, double new_stand)
-
{
-
-
return(quantity*old_stand/new_stand);
-
-
}
-
-
-
-
-
int main(void)
-
-
{
-
-
-
unit_t units[20];
-
-
int num_units;
-
char old_units[30],new_units[30];
-
int status;
-
double quantity;
-
-
int old_index,new_index;
-
-
load_units(20,units,&num_units);
-
-
printf("Enter a conversion problem or q to quit.\n");
-
printf("To convert 25 kilometers to miles,you would enter\n");
-
printf(">25 kilometers miles\n");
-
printf(" or,alternatively,\n");
-
printf(">25 km mi\n>");
-
-
for(status = scanf("%lf %s%s",&quantity,&old_units,&new_units);
-
status == 3;
-
status = scanf("%lf %s%s",&quantity,&old_units,&new_units)){
-
printf("attempting conversion of %.4e %s to %s ...\n",quantity,&old_units,&new_units);
-
old_index = search(units,old_units,num_units);
-
new_index = search(units,old_units,num_units);
-
if (old_index == NOT_FOUND){
-
printf("Unit %s not in database\n",old_units);
-
}
-
else if (new_index == NOT_FOUND){
-
printf("UNit %s not in database\n",new_units);
-
}
-
else if (strcmp(units[old_index].class,units[new_index].class)!=0)
-
else
-
printf(" Cannot convert %s(%s) to %s (%s)\n",old_units,units[old_index].class,new_units,units[new_index].class);
-
else
-
printf("%.4e %s = %.4e %s\n",quantity,old_units,convert(quantity,units[old_index].standard,units[new_index].standard),new_index);
-
-
printf("\nEnter a conversion problem or q to quit\n");
-
}
-
return(0);
-
}
-
-
-
-
-
-
-
-
-
-
-
-
anyone here to help me errors??
Banfa 9,065
Expert Mod 8TB
You should list what the errors are.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: ttn |
last post by:
Hi,
I am using a separately downloaded Eclipse 3M5 on a Debian woody
(stable) system. To make Eclipse start at all I had to use the libgtk2
libraries from some backported gnome 2.2 to get a gtk...
|
by: jorfei |
last post by:
I have written a component with a property IPAdrress of type
System.Net.IPAddress. To ease the configuration of the component at design
time, I have written a type converter for the type...
|
by: |
last post by:
Hi there
In my app I host the webbrowser. If I return my own implementation in
GetDropTarget my methods are never called ( if i return the original
dropTarget or NOTIMPLEMENTED everything works...
|
by: Max Power |
last post by:
I'm having some trouble with an RTF converter called Logictran R2Net
when two users access my site simultaneously. My application simply
runs an SQL query (based on parameters typed by user) that...
|
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c...
|
by: ThunderMusic |
last post by:
Hi,
I have a value that contains flags that I must get using a bitmask. I tryied
with the && operator, but the compiler outputs this error :
Operator '&&' cannot be applied to operands of type...
|
by: ShihChengYu |
last post by:
Dear all:
I want to use a software API functions on my project,but it was written
VB.NET. I am using C#, So how to write these code by C#, espacially
Delegate. I tried several times, but I...
|
by: coosa |
last post by:
Dear all,
I have the following code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
|
by: esha |
last post by:
I tried several online converters. In many case they do the job, but
sometimes give some mess. I think that all converters I know are old, were
created for VS 2003 and do not understand new stuff...
|
by: Tony Johansson |
last post by:
Hello!
We have a C#.ASP.NET application that runs on a IIS 6. The application
contains actually several asp pages but there is no GUI.
The application receive an xml file that is processed....
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |