2013-02-20 94 views
-1

我有一个程序用于管理名为“client_DB”的记录数据库。数组“client_DB”由客户手机通话记录组成。每个客户呼叫记录包含八个字段,如下所示:1)十位手机号码(字符串,不带破折号),2)用于拨打电话的中继站数量(整数),3)长度(4)呼叫的净费用(双倍),5)税率(双倍),6)通话税(双倍),7)通话的总费用(双倍)和8 )字符串字段名为“discount_aval”,其值为“yes”或“no”。数组client_DB的容量(SIZE)为20条记录。搜索并删除数组中的函数

它从第一个名为“client_data.txt”的输入文件读取,由这些值组成:

9546321555 0 0 yes 
5612971340 5 50 no 
3051234567 8 25 no 
7542346622 24 17 no 
3054432762 15 30 yes 
11 50 100 yes 
8776219988 87 82 yes 
9042224556 4 5 yes 
7877176590 11 1 no 
5617278899 20 45 no 
9546321555 4 3 yes 
5612971340 79 86 no 
3051234567 8 25 no 
7542346622 24 118 no 
3054432762 115 25 yes 
11 43 10 yes 
8776219988 265 22 yes 
9042224556 2 5 yes 
7877176590 89 67 no 
5617278899 40 56 no 

我的删除功能只删除第一个值,如果我输入到r取消任何其他价值,它只是不会!我的搜索功能只是给我回2个数字。这是关闭的,而不是我想要的。帮帮我?我想要求用户输入一个单元号,然后让它搜索整个数组并找到并删除它。我想让我的搜索抓取一个输入,找到它并将我的位置还给我。我试过但我不知道我做错了什么。在我的主要中,当用户从功能菜单中选择它时,我调用了这两个函数。 继承人我的代码:

#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 
{ 
    cout<<"Please enter a phone number: "<<endl; 
    cin>>cellnum; 

    for(int i=0; i<count; i++) 
    { 
     if (cellnum == A[i].cellnum) 
     { 
      cout<<i<<endl; 
     } 

    } 
    return -1; 
} 

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 = Search(A,*count,cellnum); 

    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<<"****************************************************"<<endl; 
} 



int main() 
{ 

    char answer; 
    char answer2; 
    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' : Remove(CLIENT,&count,cellnum); 
break; 
case 'S' : 
    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; 
    } 
    cout<<"Would you like to make another command?(y/n): "<<endl; 
    cin>>answer2; 
} while (answer2 == 'Y' || answer2 == 'y'); 
cout<<"Goodbye"<<endl; 
    return 0; 






} 
+0

函数搜索给我回2个数字。为什么?例如,如果我搜索数组[2]中的3051234567 ..它给了我一个2,然后是12。为什么? – Kevers 2013-02-20 04:36:23

回答

1

这似乎正是你想要的函数返回什么。请注意,索引2和12处的电话号码相同。如果事实上,似乎只有10个唯一的电话号码在列表中。因此,当搜索这10个数字中的每一个时,您将得到2个数字作为输出,因为它们都有一个重复。

如果你只是想第一场比赛要打印,只需添加一个break;如下:在搜索功能

for(int i=0; i<count; i++) 
{ 
    if (!(A[i].cellnum.compare(cellnum))) 
    { 
     cout<<i<<endl; 
     break; 
    } 

} 

。如果不需要相同的电话号码,则可以考虑在允许用户搜索电话号码之前检查该号码。

编辑: 我看到你的删除功能无法正常工作。您尝试使用搜索功能获取电话号码的索引,但搜索功能始终返回-1。 我会像上面提到的那样添加break,然后返回i而不是-1。声明i for循环以外的工作。

当你想所有出现在选择删除被删除,我会做到以下几点:

在您的主要功能:

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; 

搜索:

int Search(client_db A[], int count, string cellnum){ 
    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 Remove(client_db A[], int *count, string cellnum){ 
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)--; 

     } 
} 
} 
+0

你是完全正确的,我希望这两个都被删除。在我的删除功能中,它只删除第一个单元格号码。我希望它找到在搜索中找到的位置,然后删除这两个位置。 – Kevers 2013-02-20 05:14:29

+0

添加“break”会导致只有第一次出现的电话号码被删除。如果没有找到数字,我会添加一个else语句并返回-1,否则返回索引。然后在while循环中调用remove函数,直到给定索引为-1。 – ThijsW 2013-02-20 05:17:57

+0

谢谢!修复!顺便说一句,因为你是唯一一个帮助我,我有一个称为进程计算的功能。这些计算结果与数组中的所有内容一起输出到输出文件。我在哪里填充代码来设置打印到文件(不是屏幕)上的精度? – Kevers 2013-02-20 05:24:53