c++的一道题,刚学C++的小菜鸟。求解答
1.Askstheuserwhichtriangularnumbertheywanttocalculate询问用户,希望输入哪一个三角数。2.Calculatesthet...
1.Asks the user which triangular number they want to calculate询问用户,希望输入哪一个三角数。2.Calculates the triangular number计算用户输入的三角数。3.Displays the calculated triangular number on the screen在屏幕上显示出计算的三角数。4.After the calculation, ask the user if they would like to do another calculation计算完成后,询问用户是否想再做一个计算。a.If the user answers ‘y’, then repeat all the steps如果用户回答‘y’,那么重复所有步骤b.If the user answers ‘n’, then finish the program如果用户回答“n”,则该程序完成5.Uses the most appropriate loops in the program请用最适合这个程序的循环。运行结果如下;
展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
展开全部
// Number.h
//
// Number.h
// PerfectNumber
//
// Created by 晨辉明 on 2018/12/16.
// Copyright © 2018 晨辉明. All rights reserved.
//
#ifndef Number_h
#define Number_h
class Number {
public:
Number(int n) {this->n = n;}
int getTriangle() {
int i = 1, j = this->n, sum = 0;
while (i < j) {
sum += i + j;
i++;
j--;
}
if (i==j) {
sum += i;
}
return sum;
}
int getNumber() {return this->n;}
private:
int n;
};
#endif /* Number_h */
// main.cpp
//
// main.cpp
// PerfectNumber
//
// Created by 晨辉明 on 2018/12/16.
// Copyright © 2018 晨辉明. All rights reserved.
//
#include <iostream>
#include "Number.h"
using std::cout;
using std::cin;
int main(int argc, const char * argv[]) {
int n = 0;
char ch = 'y';
do {
cout << "Which triangular number do you want to calculate? ";
cin >> n;
Number *num = new Number(n);
cout << "The answer is " << num->getTriangle() << "\n";
cout << "Would you like to do another? (y/n) ";
cin >> ch;
delete(num);
} while ( ch != 'n' );
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询