我希望这段代码尽量减少 - 但是,我希望它显示完全相同的东西。
具体来说,关于如何删除代码中的一些重复(例如“if,else if”)等等使用其他语句或让编译器更快地完成它,因为我觉得我缺乏经验和知识这样做而不会弄乱一切(例如使用开关和案例)。
例如,当我测试“tf”时,我不想被迫重复“平均”,这取决于我们是否添加“TEZA”等级。
非常感谢,当然,如果有需要或者我不够清楚,我愿意分享更多细节。
我提前道歉,以防我不够清楚。
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="navtop"></div>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">CrystalDev</a>
</div>
<ul class="nav navbar-nav">
<li><a href="http://www.crystaldev.net/">Home</a></li>
<li><a href="/products.php">Products</a></li>
<li><a href="/about-us.php">About Us</a></li>
<li><a href="/contact.php">Contact</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<?php
if($_SESSION["active"]==false){
?>
<li class="active"><a href="/signup.php"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="/login.php"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
<?php
}else{
switch($_SESSION["rank"]){
case 10:
?>
<li><a href="/admin.php"><span class="glyphicon glyphicon-log-in"></span> Admin Panel</a>
<?php
break;
case 1:
?>
<li><a href="/signout.php"><span class="glyphicon glyphicon-log-out"></span> Sign Out</a>
<?php
break;
default:
break;
}
}
?>
</ul>
</div>
</nav>
</body>
答案 0 :(得分:1)
如果我希望它简短,我可能会写这样的东西:
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <numeric>
template <class T> T get(std::string const &prompt) {
std::cout << prompt;
T ret;
std::cin >> ret;
return ret;
}
int main() {
auto sub = get<std::string>("SIMPLE AVERAGE CALCULATOR\n\nSubject at hand?: ");
std::cout << "\nPlease enter the FOUR marks you'd like verified:\n";
std::vector<int> m;
for (int i = 0; i < 4; i++)
m.push_back(get<int>("M" + std::to_string(i + 1) + ": "));
if (std::toupper(get<char>("\nWould you like to include the TEZA grade?(Y/N): ")) == 'Y')
m.push_back(get<int>("What is the TEZA grade?: "));
auto tzm = std::accumulate(m.begin(), m.end(), 0.0) / m.size();
char const *yn[] = { "No", "Yes" };
std::cout << "\nYour average grade at " << sub << " is " << tzm
<< "\n\nYou got the following mark: " << "FFFFFEDCBAA"[(int)tzm]
<< "\nDo you pass?\n" << yn[tzm >= 5] << "\n";
}
我绝对不会把它变成家庭作业 - 我不仅会得到糟糕的成绩,而且可能会被投入疯人院。
虽然前面的代码过度使用了一些好东西,但是在这个混乱中隐藏着一些好东西。首先,使用向量而不是数组。几乎没有任何情况下在C ++中使用原始数组是真的有意义的。您可以使用std::array
,也可以使用std::vector
(或任意数量的其他集合),但原始内容很少有意义。在这种情况下,我们可能会处理两种不同大小的集合中的任何一种(即,包括或不包括TEZA等级的成绩),因此vector
更有意义。
由于矢量跟踪自己的大小,我们可以通过收集所有等级来简化代码来计算平均值,然后根据收集的等级数计算平均值。
我们可以通过从字母等级的某些类似数组的对象开始,并使用计算的数字等级来索引这些字母等级,从而简化计算。使用"FFFFFEDCBAA"[(int)tzm]
过于简洁。在现实生活中,这样的事情更合适:
std::string letter_grades = ""FFFFFEDCBAA";
std::cout << letter_grades[static_cast<int>(tzm)];
在更高级别,代码执行相同的基本操作序列:向用户打印提示,然后读取某种值。将该序列移动到一个函数中,这样我们就可以避免为同一个序列重复几乎相同的代码(再次),这是非常好的事情。
问题中的代码也会破坏可以被定义为单个字符串文字的内容,这些内容没有明显的原因。我猜某人可能会发现这种方式更容易理解,但我不确定谁会这样做,或者为什么 - 这对我来说并不是一种改善。
在逐行的基础上解决问题是有意义的,例如:
auto sub = get<std::string>(
"SIMPLE AVERAGE CALCULATOR\n"
"\n"
"Subject at hand?: ");
编译器将这样的字符串文字合并成一个文字,因此最终完全像上面显示的代码 - 但有些人发现它更具可读性。另一种可能性是使用原始字符串:
auto sub = get<std::string>(
R"("SIMPLE AVERAGE CALCULATOR
Subject at hand?: )");
原始字符串文字完全按原样包含所有内容,包括换行符,因此我们可以将新行表示为实际换行符,而不是将其编码为\n
。在这种特殊情况下,我发现它并没有什么好处,但我可以看到一些人可能更喜欢它。
在最后的分析中,代码的长度很少非常相关 - 但在代码中重复(例如)重要 - 很重要。摆脱重复绝对是件好事。
同样,在我们可以用数据而不是控制流来表示任务的程度上,这几乎总是一个胜利。
答案 1 :(得分:0)
您可以使用数组和循环缩短M的各个部分
#include <iostream>
using namespace std;
int main()
{
string sub, tf;
int m[4], TEZA, sum;
double avg, tzm;
cout << "SIMPLE AVERAGE CALCULATOR";
cout << "\n" << "\n" << "Subject at hand?: "; cin >> sub;
cout << "\n" << "Input the FOUR marks you'd like verified: " << "\n";
for(int i = 1; i < 5; i++)
{
cout<<"\n"<<"M"<<i<<":"; cin>>m[i-1];
}
cout << "\n" << "Would you like to include the TEZA grade?(Y/N): "; cin >> tf;
sum = m[0] + m[1] + m[2] + m[3];
avg = (double) sum / 4;
if (tf == "Y" | tf == "y")
{
cout << "What is the TEZA grade?: "; cin >> TEZA;
int tzm = ((double) avg * 3 + TEZA) / 4;
cout << "\n" << "Your average grade at " << sub << " is " << tzm << "\n" << "\n";
cout << "You got the following mark: ";
if (tzm >= 9 && tzm <= 10) cout << "A" << "\n";
else if (tzm >= 8 && tzm <= 9) cout << "B" << "\n";
else if (tzm >= 7 && tzm <= 8) cout << "C" << "\n";
else if (tzm >= 6 && tzm <= 7) cout << "D" << "\n";
else if (tzm >= 5 && tzm <= 6) cout << "E" << "\n";
else if (tzm < 5) cout << "F" << "\n";
cout << "DO YOU PASS: " << "\n";
if (tzm >= 5) cout << "Yes." << "\n";
else cout << "No." << "\n";
}
else
{
cout << "\n" << "Average at " << sub << " is " << avg << "\n" << "\n";
cout << "You got the following mark: ";
if (avg >= 9 && avg <= 10) cout << "A" << "\n";
else if (avg >= 8 && avg <= 9) cout << "B" << "\n";
else if (avg >= 7 && avg <= 8) cout << "C" << "\n";
else if (avg >= 6 && avg <= 7) cout << "D" << "\n";
else if (avg >= 5 && avg <= 6) cout << "E" << "\n";
else if (avg < 5) cout << "F" << "\n";
cout << "\n" << "DO YOU PASS?: " << "\n";
if (avg >= 5) cout << "Yes." << "\n";
else cout << "No." << "\n";
}
}
答案 2 :(得分:0)
PrintGrade()
和VerifyPassFail()
之类的方法,并在每次要使用该代码块时调用它们。int TEZA
,在if condition
通过时声明它,因为只有这样才能真正使用它。m1,m2,m3 and m4
。我建议std::vector
。如果你想扩展到更多的主题,你所要做的就是增加循环的范围。其他评论不会使用using namespace std
,而是使用std
标记作为前缀,例如std::vector
,std::cout
等。
#include <iostream>
#include <string>
#include <vector>
void PrintGrade( int inInput )
{
std::cout << "You got the following mark: ";
if (inInput >=9 && inInput <=10)
std::cout<<"A"<<"\n";
else if (inInput >=8 && inInput <=9)
std::cout<<"B"<<"\n";
else if (inInput >=7 && inInput <=8)
std::cout<<"C"<<"\n";
else if (inInput >=6 && inInput <=7)
std::cout<<"D"<<"\n";
else if (inInput >=5 && inInput <=6)
std::cout<<"E"<<"\n";
else if(inInput <5)
std::cout<<"F"<<"\n";
}
void VerifyPassFail( int inInput )
{
std::cout<<"\n"<<"DO YOU PASS?: "<<"\n";
if (inInput >=5)
std::cout<<"Yes."<<"\n";
else
std::cout<<"No."<<"\n";
}
int main()
{
std::string sub, tf;
int sum = 0;
int TEZA;
std::vector<int> marksList;
double avg, tzm;
std::cout << "SIMPLE AVERAGE CALCULATOR";
std::cout << "\n" << "\n" << "Subject at hand?: ";
std::cin >> sub;
std::cout << "\n" << "Input the FOUR marks you'd like verified: " << "\n";
for(int i = 1; i < 5; i++)
{
int marks;
std::cout<<"\n"<<"M"<<i<<":";
std::cin>>marks;
marksList.push_back(marks);
}
std::cout << "\n" << "Would you like to include the TEZA grade?(Y/N): ";
std::cin >> tf;
for( int i : marksList )
{
sum += i;
}
avg = (double) sum / 4;
if (tf == "Y" | tf == "y")
{
std::cout << "What is the TEZA grade?: ";
std::cin >> TEZA;
int tzm = ((double) avg * 3 + TEZA) / 4;
std::cout << "\n" << "Your average grade at " << sub << " is " << tzm << "\n"
<< "\n";
std::cout << "You got the following mark: ";
PrintGrade(tzm);
VerifyPassFail(tzm);
}
else
{
std::cout << "\n" << "Average at " << sub << " is " << avg << "\n" << "\n";
PrintGrade(avg);
VerifyPassFail(avg);
}
}