2013-02-20 85 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