如何在其他功能中调用主要功能部分? [英] How do I call main function part in other function?
本文介绍了如何在其他功能中调用主要功能部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是m代码,我想在tabletfind()中重新调用main函数。我该怎么办?
我尝试过:
#include < iostream >
#include < string >
使用 命名空间标准
int tabletfind( int x){
if (x%2 == 0 )
{
cout<< ; 在21次移动中搜索解决方案......<< endl;
cout<< 在21次移动中找不到解决方案。抱歉。<< ; ENDL;
string ans;
cout<< 您想再试一次[是/否]? ;
cin>> ans;
if (ans == y || ans == Y){
// 这里我需要再次调用main函数部分我该怎么办?我无法理解
}
其他 如果(ans == n || ans == N){
退出;}
}
}
}
int main(){
int number;
tryagain:
cout<< 你开始使用多少平板电脑? ?;
cin>> number;
if (数字> 0&& number< = 10000 ) {
tabletfind(number);
}
其他 {cout<< Tablets的数量必须大于0且不超过10000<< endl;
goto tryagain;}
return 0 跨度>;
}
解决方案
Quote:这里是m代码,我想在tabletfind()中重新调用main函数。我该怎么办?
AC程序从不调用主函数,无论如何。
你必须组织你的另一种方式。
你应该考虑使用一个循环,并且避免goto
。
组织你的程序,以便main()调用另一个函数,然后你可以从其他地方再次调用,例如
#include < iostream >
#include < string >
使用 命名空间标准;
int aFunction()
{
int 数;
tryagain:
cout<< 你开始使用多少平板电脑? ?;
cin>> number;
if (数字> 0&& number< = 10000 )
{
tabletfind(number);
}
else {cout<< Tablets的数量必须大于0且不超过10000<< endl;
goto tryagain;
}
return 0 ;
}
int tabletfind( int x)
{
if (x%2 == 0 )
{
cout<< 在21次移动中搜索解决方案......<< ENDL;
cout<< 在21次移动中找不到解决方案。抱歉。<< ; ENDL;
string ans;
cout<< 您想再试一次[是/否]? ;
cin>> ans;
if (ans == y || ans == Y)
{
x = AFunction()}
else if (ans == n || ans == N)
{
退出;
}
}
}
int main()
{
int 数字;
number = aFunction();
}
请注意我没有测试过任何上述内容,因为我复制了goto,我可能会让眼泪掉下来。
here is m code where i want to re-call main function in tabletfind(). what can i do for this?
What I have tried:
#include <iostream>
#include <string>
using namespace std;
int tabletfind(int x){
if(x%2==0)
{
cout<<"Searching for a solution within 21 moves..."<<endl;
cout<<"No solution found within 21 moves. Sorry."<<endl;
string ans;
cout<<"Would you like to try again [Y/N]?";
cin>>ans;
if(ans=="y" || ans=="Y"){
//here i need to call main function part again what should i do? i can't understand
}
else if (ans=="n" || ans=="N"){
exit;}
}
}
}
int main() {
int number;
tryagain:
cout<<"How many tablets are you starting with ? ";
cin>>number;
if(number>0 && number<=10000){
tabletfind(number);
}
else {cout<<"Number of Tablets must be greater than 0 and no more than 10000"<<endl;
goto tryagain;}
return 0;
}
解决方案
Quote:here is m code where i want to re-call main function in tabletfind(). what can i do for this?
A C program never call the main function, no matter what.
You have to organize your another way.
You should think about using a loop, anf avoid thegoto
.
Organise your program so that main() calls another function which you can then call again from anywhere else e.g.
#include <iostream> #include <string> using namespace std; int aFunction() { int number; tryagain: cout<<"How many tablets are you starting with ? "; cin>>number; if(number>0 && number<=10000) { tabletfind(number); } else {cout<<"Number of Tablets must be greater than 0 and no more than 10000"<<endl; goto tryagain; } return 0; } int tabletfind(int x) { if(x%2==0) { cout<<"Searching for a solution within 21 moves..."<<endl; cout<<"No solution found within 21 moves. Sorry."<<endl; string ans; cout<<"Would you like to try again [Y/N]?"; cin>>ans; if(ans=="y" || ans=="Y") { x = AFunction() } else if (ans=="n" || ans=="N") { exit; } } } int main() { int number; number = aFunction(); }
Please note I have not tested any of the above and I may have let a tear fall from my eye as I copied the goto.
这篇关于如何在其他功能中调用主要功能部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文