C++ 根据用户输入的每行术语显示新行
该程序根据用户输入的第一个术语、要计算的术语数量和每行术语,输出一系列术语C++ 根据用户输入的每行术语显示新行,c++,C++,该程序根据用户输入的第一个术语、要计算的术语数量和每行术语,输出一系列术语 #include <iostream> #include <cmath> #include <iomanip> using namespace std; //prototype int ValidateInput (string Prompt); int main() { //local variables long long int firstTerm;
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
//prototype
int ValidateInput (string Prompt);
int main()
{
//local variables
long long int firstTerm;
int termsToCalc;
int termsPerLine;
int count;
//tells user what program does
cout << "Program will determine the terms in a Juggler Series" << endl << endl;
//calls user function to read in the frist term
firstTerm = ValidateInput ("Enter the first term: ");
cout << endl;
//calls user function to read in the number of terms to calculate (after the first)
termsToCalc = ValidateInput ("Enter the number of terms to calculate (after the first): ");
cout << endl;
//calls user function to read in the number of terms to display per line
termsPerLine = ValidateInput ("Enter the terms to display per line: ");
cout << endl;
cout << "First " << termsToCalc << " terms of Juggler series starting with " << firstTerm << endl << endl;
count = 1;
do
{
if ((count % termsPerLine) == 0)
{
cout << "\n";
}
//the term is even take it to the power of 1/2 and increase the count 1
if (firstTerm % 2 == 0 )
{
firstTerm = pow(firstTerm , 0.5);
cout << setw(16) << firstTerm << endl;
count++;
}
//the term is odd take it to the power of 3/2, and increase the count 1
else
{
firstTerm = pow(firstTerm, 1.5);
cout << setw(16) << firstTerm << endl;
count++;
}
}
//continue looping until the terms to calculate is no longer less than or
// equal to the count
while (count <= termsToCalc);
return 0;
}
int ValidateInput (string Prompt)
{
//local variable
int number;
//prompts user for first term, and reads in number
cout << Prompt;
cin >> number;
//user input must be positive, a while loop will check user input and
//continue to check until the term is positive.
while (number <=0)
{
cout << "Error - Enter a positive number" << endl;
cin >> number;
}
//returns number to main function
return number;
}
#包括
#包括
#包括
使用名称空间std;
//原型
int ValidateInput(字符串提示);
int main()
{
//局部变量
长期的;
国际术语计算;
国际术语汇编;
整数计数;
//告诉用户程序做什么
cout首先,从以下各项中删除endl
s:
cout << setw(16) << firstTerm << endl; // -> cout << setw(16) << firstTerm;
不必使用do while循环。现在我可以在帖子中添加更多内容,因为我已经删除了尾端,现在格式正确,但计数正在扭曲线条。配置了额外的术语!while(计数
if ((count % termsPerLine) == 0)
{
cout << "\n";
}
count = 0;
do {
if (firstTerm % 2 == 0 ) {
firstTerm = pow(firstTerm , 0.5);
cout << setw(16) << firstTerm;
} else {
firstTerm = pow(firstTerm, 1.5);
cout << setw(16) << firstTerm;
}
if ( (count + 1) % termsPerLine == 0) {
cout << "\n";
}
count++;
} while (count < termsToCalc);