问题描述
- 帮忙看下代码,运行不了了,哪里有问题麻烦指出来下谢谢~!麻烦修改下 谢谢!
-
#include
#include
using namespace std;
int min(int n, int m);
int max(int n, int m);
const int N = 50;
void Read_Store(int Number[N], int &length);
void Add_Output();
int main()
{cout<<" This is a calculate number program"<<endl;
}
int min(int n,int m)
{
return (n<m) ? n : m;
}int max(int n,int m)
{
return (n>m) ? n : m;
}void Read_Store(int Number[N],int &length)
{
string num ;
int i;
cout<<"please input a number:";
cin>>num;
length = num.length();
for (i = 0; i < length; i++)
{
Number[i] = num[length - 1 - i] - '0';
}
}
void Add_Output()
{
int i,carry;
int strlen_num1,strlen_num2;
int Num1[N],Num2[N],Result[N];
int minnum,maxnum;Read_Store(Num1,strlen_num1); Read_Store(Num2,strlen_num2); minnum = min(strlen_num1,strlen_num2); maxnum = max(strlen_num1,strlen_num2); carry = 0; for(i=0;i<minnum;i++) { Result[i] = (Num1[i] + Num2[i] + carry) % 10; carry = (Num1[i] + Num2[i] + carry)/10; } if(strlen_num1>minnum) { for(i=minnum;i<strlen_num1;i++) { Result[i] = (Num1[i]+ carry) % 10; carry = (Num1[i]+ carry)/10; } } if(strlen_num2>minnum) { for(i=minnum;i<strlen_num2;i++) { Result[i] = (Num2[i]+ carry) % 10; carry = (Num2[i]+ carry)/10; } } if(carry>0) { Result[maxnum] = 1; maxnum++; } cout<<"Result:"; for(i=maxnum-1;i>=0;i--) { cout<<Result[i]; } cout<<endl; system("pause"); return ;
}
int main 的函数 要求放在第一位,可是我就是运行不了。。为什么。。
解决方案
#include<iostream>
#include<string>
解决方案二:
这原码
#include
#include
using namespace std;
const int N=50;
int min(int n,int m)//返回两个数中的最小一个数
{
return (n<m) ? n : m;
}
int max(int n,int m)//返回两个数中的最大一个数
{
return (n>m) ? n : m;
}
void Read_Store(int Number[N],int &length)
{
string num ;
int i;
cout<<"please input a number:";
cin>>num;
length = num.length();
for(i=0;i<length;i++)
{
Number[i] = num[length-1-i] - '0';//字符转化为整数
}
}
void Add_Output()//两个长整数相加
{
int i,carry;
int strlen_num1,strlen_num2;//数字1的长度,数字2的长度
int Num1[N],Num2[N],Result[N];
int minnum,maxnum;//两个数中长度最大的一个
Read_Store(Num1,strlen_num1);
Read_Store(Num2,strlen_num2);
minnum = min(strlen_num1,strlen_num2);
maxnum = max(strlen_num1,strlen_num2);
carry = 0;//进位初始为0
for(i=0;i<minnum;i++)
{
Result[i] = (Num1[i] + Num2[i] + carry) % 10;
carry = (Num1[i] + Num2[i] + carry)/10;
}
if(strlen_num1>minnum)
{
for(i=minnum;i<strlen_num1;i++)
{
Result[i] = (Num1[i]+ carry) % 10;
carry = (Num1[i]+ carry)/10;
}
}
if(strlen_num2>minnum)
{
for(i=minnum;i<strlen_num2;i++)
{
Result[i] = (Num2[i]+ carry) % 10;
carry = (Num2[i]+ carry)/10;
}
}
if(carry>0)//最后一位有进位
{
Result[maxnum] = 1;
maxnum++;
}
cout<<"Result:";
for(i=maxnum-1;i>=0;i--)
{
cout<<Result[i];
}
cout<<endl;
}
int main()
{
Add_Output();
return 0;
}
如何将int main() 里的程序 放在开头,并让程序运行
解决方案四:
直接放上来啊。。函数声明一下就可以了啊
#include<iostream>
#include<string>
using namespace std;
const int N=50;
int min(int n, int m);
int max(int n, int m);
void Read_Store(int Number[N], int &length);
void Add_Output();
int main()
{
Add_Output();
return 0;
}
int min(int n,int m)//返回两个数中的最小一个数
{
return (n<m) ? n : m;
}
int max(int n,int m)//返回两个数中的最大一个数
{
return (n>m) ? n : m;
}
void Read_Store(int Number[N],int &length)
{
string num ;
int i;
cout<<"please input a number:";
cin>>num;
length = num.length();
for(i=0;i<length;i++)
{
Number[i] = num[length-1-i] - '0';//字符转化为整数
}
}
void Add_Output()//两个长整数相加
{
int i,carry;
int strlen_num1,strlen_num2;//数字1的长度,数字2的长度
int Num1[N],Num2[N],Result[N];
int minnum,maxnum;//两个数中长度最大的一个
Read_Store(Num1,strlen_num1);
Read_Store(Num2,strlen_num2);
minnum = min(strlen_num1,strlen_num2);
maxnum = max(strlen_num1,strlen_num2);
carry = 0;//进位初始为0
for(i=0;i<minnum;i++)
{
Result[i] = (Num1[i] + Num2[i] + carry) % 10;
carry = (Num1[i] + Num2[i] + carry)/10;
}
if(strlen_num1>minnum)
{
for(i=minnum;i<strlen_num1;i++)
{
Result[i] = (Num1[i]+ carry) % 10;
carry = (Num1[i]+ carry)/10;
}
}
if(strlen_num2>minnum)
{
for(i=minnum;i<strlen_num2;i++)
{
Result[i] = (Num2[i]+ carry) % 10;
carry = (Num2[i]+ carry)/10;
}
}
if(carry>0)//最后一位有进位
{
Result[maxnum] = 1;
maxnum++;
}
cout<<"Result:";
for(i=maxnum-1;i>=0;i--)
{
cout<<Result[i];
}
cout<<endl;
}