我需要帮助设置打印到我的名为 Print_DB 的函数中的输出文件的精度。我实现了一个名为 Menu 的函数,它设置了一个主菜单,当用户选择打印到输出文件时,一个名为 Process 的函数进行必要的计算,从保存手机信息的数组中获取值。我希望将这些计算打印在输出文件上,如下所示:
9546321555 0 0 0.00 0.01 0.00 0.00 yes
5612971340 5 50 2.00 0.01 0.02 2.02 no
3051234567 8 25 1.60 0.03 0.05 1.65 no
7542346622 24 17 3.26 0.08 0.26 3.53 no
3054432762 15 30 3.60 0.05 0.18 3.78 yes
9544321011 50 100 40.00 0.08 3.20 43.20 yes
8776219988 87 82 57.07 0.12 6.85 63.92 yes
9042224556 4 5 0.16 0.01 0.00 0.16 yes
7877176590 11 1 0.09 0.03 0.00 0.09 no
5617278899 20 45 7.20 0.05 0.36 7.56 no
9546321555 4 3 0.10 0.01 0.00 0.10 yes
5612971340 79 86 54.35 0.12 6.52 60.87 no
3051234567 8 25 1.60 0.03 0.05 1.65 no
7542346622 24 118 22.66 0.08 1.81 24.47 no
3054432762 115 25 23.00 0.12 2.76 25.76 yes
9544321011 43 10 3.44 0.08 0.28 3.72 yes
8776219988 265 22 46.64 0.12 5.60 52.24 yes
9042224556 2 5 0.08 0.01 0.00 0.08 yes
7877176590 89 67 47.70 0.12 5.72 53.43 no
5617278899 40 56 17.92 0.08 1.43 19.35 no
但相反,我得到这样的东西:
9546321555 0 0 -9.25596e+061 -9.25596e+061 8.56729e+121 8.56729e+121 yes
5612971340 5 50 2 0.01 0.0002 2.0002 no
3051234567 8 25 1.6 0.03 0.00048 1.60048 no
7542346622 24 17 3.264 0.08 0.0026112 3.26661 no
3054432762 15 30 1.8 0.05 0.0009 1.8009 yes
9544321011 50 100 40 0.08 0.032 40.032 yes
8776219988 87 82 57.072 0.12 0.0684864 57.1405 yes
9042224556 4 5 0.16 0.01 1.6e-005 0.160016 yes
7877176590 11 1 0.088 0.03 2.64e-005 0.0880264 no
5617278899 20 45 3.2 0.05 0.0016 3.2016 no
9546321555 4 3 0.096 0.01 9.6e-006 0.0960096 yes
5612971340 79 86 54.352 0.12 0.0652224 54.4172 no
3051234567 8 25 1.6 0.03 0.00048 1.60048 no
7542346622 24 118 22.656 0.08 0.0181248 22.6741 no
3054432762 115 25 23 0.12 0.0276 23.0276 yes
9544321011 43 10 3.44 0.08 0.002752 3.44275 yes
8776219988 265 22 46.64 0.12 0.055968 46.696 yes
9042224556 2 5 0.08 0.01 8e-006 0.080008 yes
7877176590 89 67 47.704 0.12 0.0572448 47.7612 no
5617278899 40 56 17.92 0.08 0.014336 17.9343 no
我还希望功能“折扣”打印完全相同。我需要添加或修复什么?帮助?提示?谢谢你!这是我的代码:
#include <iostream>
#include <string>
#include <fstream>
//************************************************************************
//Name: Kevin Due Date: 022113
//Instructor: Dr. Bullard Total Points: 100 pts
//Assignment2: client_call.cpp UsIDFAU:
//:
using namespace std;
const int CAPACITY = 20;
class client_db
{
public:
string cellnum;
int numofrelay;
int call_length;
double net_cost;
double tax_rate;
double call_tax;
double total_cost;
string discount_aval;
};
bool IsFull(int); //returns true if the array is full; otherwise false.
bool IsEmpty(int count);// returns ture if the array is empty; otherwise false.
void Add(client_db A[], int & count, client_db & db);
void Remove(client_db A[], int *count, string name);// removes an item from the array if it is there
void Print_DB(client_db A[], int count);//prints to output file
void Call_stats(client_db A[], int count);// prints all the items in the array
int Search_DB(client_db A[], int count, string name); //if the name is in the array, its location is returned
// //otherwise return -1;
//
bool IsFull(int count)
////Description: Determines if the array is full
{
return (count == CAPACITY);
}
bool IsEmpty(int count)
////Description: Determines if the array is empty
{
return (count == 0);
}
void Process (client_db A[], int count)
{
for(int i=0; i<count; i++)
{
if (A[i].numofrelay >=1 && A[i].numofrelay<=5)
{
A[i].tax_rate=0.01;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);
}
else if (A[i].numofrelay >=6 && A[i].numofrelay<=11)
{
A[i].tax_rate=0.03;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);
}
else if (A[i].numofrelay>=12 && A[i].numofrelay<=20)
{
A[i].tax_rate=0.05;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].numofrelay);
}
else if (A[i].numofrelay >=21 && A[i].numofrelay<=50)
{
A[i].tax_rate =0.08;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);
}
else if (A[i].numofrelay >50)
{
A[i].tax_rate =0.12;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);
}
A[i].call_tax = ((A[i].tax_rate)/(100))*(A[i].net_cost);
A[i].total_cost = A[i].net_cost + A[i].call_tax;
}
}
void Print_DB(client_db A[], int count)
//Description: Prints the items stored in A to the standard i/o device
{
string filename;
cout<<"Enter output filename: "; //geting filename
cin>>filename;
ofstream output; //declaring an output file stream
output.open(filename.c_str()); // c_str() converts a C++ string into a
// c-style string (char array) &
//open binds an ofstream to a file
for(int i=0; i<count; i++)
{
output<<A[i].cellnum<<"\t"
<<A[i].numofrelay<<"\t"
<<A[i].call_length<<"\t"
<<A[i].net_cost<<"\t"
<<A[i].tax_rate<<"\t"
<<A[i].call_tax<<"\t"
<<A[i].total_cost<<"\t"
<<A[i].discount_aval<<endl;
}
output.close();
}
int Search(client_db A[], int count, string cellnum)
////Description: Locates cellnumbers in A's fields
{ int index = -1;
for(int i=0; i<count; i++)
{
if (!(A[i].cellnum.compare(cellnum)))
{
cout<<i<<endl;
index = i;
break;
}
}
return index;
}
void Add(client_db A[], int &count)
////Description: Adds key to the array
{
if (!IsFull(count))
{
cout<<"Enter a cellphone number, number of relay stations and the call lenght and if a discount is available: ";
cin>>A[count].cellnum>>A[count].numofrelay>>A[count].call_length>>A[count].discount_aval;
count++;
}
else
{
cout<<"The list is full\n";
}
}
void Add(client_db A[], int &count, client_db &db)
////Description: Adds key to the array
{
if (!IsFull(count))
{
A[count] = db;
count++;
}
else
{
cout<<"The list is FULL! \n";
}
}
void Remove(client_db A[], int *count, string cellnum )
////Description: Removes the number from the array is it is there
{
int loc;
while((loc=Search(A,*count,cellnum)) != -1)
{
if (IsEmpty(*count)){
cout<<"There is nothing to remove\n";
return;
}
else if (loc == -1){
cout<<"Number is not in data\n";
}
else
{
for(int j=loc; j<(*count)-1; j++)
{
A[j] = A[j+1];
}
(*count)--;
}
}
}
void Call_stats(client_db A[],int count) // prints to screen
{
for(int i=0; i<count; i++)
{
cout<<A[i].cellnum<<"\t"
<<A[i].numofrelay<<"\t"
<<A[i].call_length<<"\t"
<<A[i].discount_aval<<endl;
}
}
void Menu ()
{
cout<<"The values of the filename you entered have been recognized"<<endl;
cout<<"Please enter the letter of your application of choice"<<endl;
cout<<" "<<endl;
cout<<"************ WELCOME TO THE MAIN MENU ************"<<endl;
cout<<" Add an item...........................A"<<endl;
cout<<" Remove an item........................R"<<endl;
cout<<" Search for an item....................S"<<endl;
cout<<" Print current data....................P"<<endl;
cout<<" Print to output file..................O"<<endl;
cout<<" List Discount info....................D"<<endl;
cout<<"****************************************************"<<endl;
}
void Discount ( client_db A[], int count, string discount)
{
cout<<"Enter 'yes' for discounts and 'no' for no discounts: "<<endl;
cin>>discount;
for(int i=0; i<count; i++)
{
if (!(A[i].discount_aval.compare(discount)))
{
cout <<A[i].cellnum<<"\t"
<<A[i].numofrelay<<"\t"
<<A[i].call_length<<"\t"
<<A[i].net_cost<<"\t"
<<A[i].tax_rate<<"\t"
<<A[i].call_tax<<"\t"
<<A[i].total_cost<<"\t"
<<A[i].discount_aval<<endl;
}
}
}
int main()
{
char answer;
char answer2;
string discount;
client_db CLIENT[CAPACITY]; //declaring database
int count = 0; //initializing count
string cellnum;
string filename;
cout<<"Hello!, this program holds clients call data records."<<endl;
cout<<"Enter input filename: "; //geting filename
cin>>filename;
ifstream input; //declaring an input file stream
input.open(filename.c_str()); // c_str() converts a C++ string into
while(count<CAPACITY && !input.eof()) //reading until the end of the file (eof=end-of-file)
{
input>>CLIENT[count].cellnum
>>CLIENT[count].numofrelay
>>CLIENT[count].call_length
>>CLIENT[count].discount_aval;
count++;
}
do
{
Menu();
cout<<"Please enter a command letter: "<<endl;
cin>>answer;
client_db db;
switch (answer)
{
case 'A' :
cout<<"Enter a cellphone number, number of relay stations and the call lenght and if a discount is available: "<<endl;
cin>>db.cellnum>>db.numofrelay>>db.call_length>>db.discount_aval;
Add(CLIENT, count, db);
break;
case 'R' : cout<<"Please enter a phone number: "<<endl;
cin>>cellnum;
Remove(CLIENT,&count,cellnum);
break;
case 'S' :
cout<<"Please enter a phone number: "<<endl;
cin>>cellnum;
Search(CLIENT,count,cellnum);
break;
case 'P' : Call_stats(CLIENT,count);
break;
case 'O' :
Process(CLIENT,count); //how do i set the precision for this?
Print_DB(CLIENT,count);
break;
case 'D':
Process(CLIENT,count);
Discount(CLIENT,count,discount);
}
cout<<"Would you like to make another command?(y/n): "<<endl;
cin>>answer2;
} while (answer2 == 'Y' || answer2 == 'y');
cout<<"Goodbye"<<endl;
return 0;
}