我将如何实现数组到这个代码
我一直是这个网站的潜伏者,现在我需要在这个代码中实现数组的帮助。我不知道如何将代码添加到这里,所以这里的我将如何实现数组到这个代码
#include <iostream> #include <fstream>
#include <iomanip>
using namespace std;
//Named constants – residential customers
const double RES_BILL_PROC_FEES = 4.50;
const double RES_BASIC_SERV_COST = 20.50;
const double RES_COST_PREM_CHANNEL = 7.50;
//Named constants – business customers
const double BUS_BILL_PROC_FEES = 15.00;
const double BUS_BASIC_SERV_COST = 75.00;
const double BUS_BASIC_CONN_COST = 5.00;
const double BUS_COST_PREM_CHANNEL = 50.00;
int main()
{
//Variable declaration
int accountNumber;
char customerType;
int numOfPremChannels;
int numOfBasicServConn;
double amountDue;
int rescount = 0, buscount = 0;
double ressum = 0.00, bussum = 0.00;
double resavg, busavg;
ifstream fin;
ofstream fout;
//open input file
fin.open("custin.txt", ios::in);
if (fin.fail()) {
cout << "Unable to open file " << endl;
exit(0);
}
//open ouput file
fout.open("custout.txt", ios::out);
fout << fixed << showpoint;
fout << setprecision(2);
fout << "This program computes a cable "
<< "bill." << endl;
//read from input file
while (!fin.eof()) {
//read customer type from file
fin >> customerType;
//read account number from file
fin >> accountNumber;
//If customer type is residential
if (customerType == 'r' || customerType == 'R') {
//read number of prem channels from file
fin >> numOfPremChannels;
amountDue = RES_BILL_PROC_FEES + RES_BASIC_SERV_COST + numOfPremChannels * RES_COST_PREM_CHANNEL;
//write into output file
fout << "Account number: " << accountNumber << endl;
fout << "Amount due: $" << amountDue << endl
<< endl;
//compute running average of residential cusomer's amount due
rescount += 1;
ressum += amountDue;
resavg = ressum/rescount;
}
//If customer type is business
else if (customerType == 'b' || customerType == 'B') {
//read number of service connection from file
fin >> numOfBasicSevConn;
//read number of prem channels from file
fin >> numOfPremChannels;
if (numOfBasicServConn <= 10)
amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL;
else
amountDue = BUS_BILL_PROC_FEES + BUS_BASIC_SERV_COST + (numOfBasicServConn - 10) * BUS_BASIC_CONN_COST + numOfPremChannels * BUS_COST_PREM_CHANNEL;
fout << "Account number: " << accountNumber << endl; //Step 7f
fout << "Amount due: $" << amountDue << endl
<< endl; //Step 7f
//compute running average of business cusomer's amount due
buscount += 1;
bussum += amountDue;
busavg = bussum/buscount;
}
else
fout << "Invalid customer type.in " << accountNumber << endl; //Step 8
}
fout << endl
<< "Average Due for residential Customer " << resavg << endl;
fout << endl
<< "Average Due for Business Customer " << busavg << endl;
fin.close();
fout.close();
return 0;
}
回答:
有两种方法可以在C++中添加数组。第一种方法是指定数组的类型(int,double,float,char,string),然后指定数组的长度并输入所有值直到达到长度。例如int arr[5] = {1, 2, 3, 4, 5};
对于另一种方式,您不需要指定长度,这意味着您可以根据需要将尽可能多的元素放入数组中,但仍需要数据类型和变量名称,如int arr[] = {1, 2, 3, 5, 8, 13, 21, 34};
。请记住,虽然在编程中我们从0开始计数。
以上是 我将如何实现数组到这个代码 的全部内容, 来源链接: utcz.com/qa/257142.html