加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C代码有效,但似乎非常低效

发布时间:2020-12-16 10:36:49 所属栏目:百科 来源:网络整理
导读:我正在学习C并且让我的代码完成我想要的所有操作,但似乎代码效率不高,因为我基本上将输出控制台中的代码加倍,因此它显示在文本文件中.如果可以的话,你能解释一下我做错了什么,你建议我做些什么来提高代码的效率. (第一列需要左对齐,第二列必须在控制台和文本
我正在学习C并且让我的代码完成我想要的所有操作,但似乎代码效率不高,因为我基本上将输出控制台中的代码加倍,因此它显示在文本文件中.如果可以的话,你能解释一下我做错了什么,你建议我做些什么来提高代码的效率. (第一列需要左对齐,第二列必须在控制台和文本文件中都是正确的,我相信我做的正确.)

/*  Description: This program calculates and prints the monthly paycheck for an employee (Output in command prompt and .txt file).*/

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;        
char name[256];
double gross;
double fiTax;
double sTax;
double ssTax;
double mediTax;
double pPlan;
double hInsurance;
double tax;
double total;

int main() {  
    std::cout << "Please enter your name: ";
    std::cin.getline(name,256);        
    cout << "Please enter your gross amount: ";
    cin >> gross;            
    std::cout << std::fixed;    
    std::cout << std::setprecision(2);    
    fiTax = gross * .15;
    sTax = gross * .035;
    ssTax = gross * .0575;
    mediTax = gross * .0275;
    pPlan = gross * .05;
    hInsurance = 75;

    tax = fiTax + sTax + ssTax + mediTax + pPlan + hInsurance;    
    total = gross - tax;    
    system("cls");

    ofstream file;
    file << std::fixed << std::setprecision(2);
    file.open("report.txt");    

    cout << left<< setw(28) << name << endl;
    file << left << setw(28) << name << endl;

    cout << left << setw(28) << "Gross Amount: ............ $";
    cout << right << setw(7) << gross << endl;

    file << left << setw(28) << "Gross Amount: ............ $";
    file << right << setw(7) << gross << endl;

    cout << left << setw(28) << "Federal Tax: ............. $";
    cout << right << setw(7) << fiTax << endl;

    file << left << setw(28) << "Federal Tax: ............. $";
    file << right << setw(7) << fiTax << endl;

    cout << left << setw(28) << "State Tax: ............... $";
    cout << right << setw(7) << sTax << endl;

    file << left << setw(28) << "State Tax: ............... $";
    file << right << setw(7) << sTax << endl;

    cout << left << setw(28) << "Social Security Tax: ..... $";
    cout << right << setw(7) << ssTax << endl;

    file << left << setw(28) << "Social Security Tax: ..... $";
    file << right << setw(7) << ssTax << endl;

    cout << left << setw(28) << "Medicare/medicaid Tax: ... $";
    cout << right << setw(7) << mediTax << endl;

    file << left << setw(28) << "Medicare/medicaid Tax: ... $";
    file << right << setw(7) << mediTax << endl;

    cout << left << setw(28) << "Pension Plan: ............ $";
    cout << right << setw(7) << pPlan << endl;

    file << left << setw(28) << "Pension Plan: ............ $";
    file << right << setw(7) << pPlan << endl;

    cout << left << setw(28) << "Health Insurance: ........ $";
    cout << right << setw(7) << hInsurance << endl;

    file << left << setw(28) << "Health Insurance: ........ $";
    file << right << setw(7) << hInsurance << endl;

    cout << left << setw(28) << "Net Pay: ................. $";
    cout << right << setw(7) << total << endl;

    file << left << setw(28) << "Net Pay: ................. $";
    file << right << setw(7) << total << endl;

    file.close();        
    return 0;
}

解决方法

>您可以编写一个函数来打印文件和输出中的值.

void WriteToFileAndOutput(const double &val,const string &s,ofstream &fname) {
cout << left << setw(28) << s;
cout << right << setw(7) << val << endl;

fname << left << setw(28) << s;
fname << right << setw(7) << val << endl;
}
//You can use it as
WriteToFileAndOutput(gross,"Gross Amount: ............ $",file);
WriteToFileAndOutput(fiTax,"Federal Tax: ............. $",file);

> C具有内置字符串数据类型,使用它而不是char数组.
>如果您已经包含使用命名空间std,那么您不必再次指定cout,cin等来自std命名空间.
>不要使用全局变量.

仅举例如:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

void WriteToFileAndOutput(const double &val,ofstream &fname) {
    cout << left << setw(28) << s;
    cout << right << setw(7) << val << endl;

    fname << left << setw(28) << s;
    fname << right << setw(7) << val << endl;
}

int main() {

    string name;
    double gross,fiTax,sTax,ssTax,mediTax,pPlan,hInsurance,tax,total;
    cout << "Please enter your name: ";
    getline(cin,name);
    cout << "Please enter your gross amount: ";
    cin >> gross;

    cout << fixed;
    cout << setprecision(2);

    fiTax = gross * .15;
    sTax = gross * .035;
    ssTax = gross * .0575;
    mediTax = gross * .0275;
    pPlan = gross * .05;
    hInsurance = 75;
    tax = fiTax + sTax + ssTax + mediTax + pPlan + hInsurance;
    total = gross - tax;

    ofstream file;

    file << fixed << setprecision(2);
    file.open("report.txt");

    cout << left << setw(28) << name << endl;
    file << left << setw(28) << name << endl; 

    WriteToFileAndOutput(gross,file);
    WriteToFileAndOutput(fiTax,file);
    WriteToFileAndOutput(sTax,"State Tax: ............... $",file);
    WriteToFileAndOutput(ssTax,"Social Security Tax: ..... $",file);
    WriteToFileAndOutput(mediTax,"Medicare/medicaid Tax: ... $",file);
    WriteToFileAndOutput(pPlan,"Pension Plan: ............ $",file);
    WriteToFileAndOutput(hInsurance,"Health Insurance: ........ $",file);
    WriteToFileAndOutput(total,"Net Pay: ................. $",file);

    file.close();
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读