65,211
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
#include <string.h>
using namespace std;
#define BUF_LEN 10
int add(int a, int b)
{
return a+b;
}
struct fraction
{
int son;
int mum;
};
void add(struct fraction *a,struct fraction *b)
{
a->son = (b->son * a->mum) + (a->son * b->mum);
a->mum *= b->mum;
}
void getLine(char* &p_data) //读入字符串函数
{
char char_buf[BUF_LEN];
cout <<"Type a line,press enter to end:\n";
(cin>>char_buf).get();
p_data = new char[strlen(char_buf)+1];
if(p_data == NULL)
{
cout <<"Not enough memory from the heap.\n";
exit(0);
}
for(int i = 0; i < strlen(p_data); i++)
p_data[i] = char_buf[i];
}
void add(char* &a,char *b) //连接字符串
{
char *temp = new char[strlen(a)+strlen(b)+1];
strcpy(temp,a);
strcat(temp,b);
a = temp;
}
int main()
{
int a,b;
char *p_string1,*p_string2;
fraction *p_struct1 = new fraction ,*p_struct2 = new fraction;
cout <<"input your integer numbers!\n";
cin>>a>>b;
cout <<add(a,b) <<endl;
cout <<"input two fractions!\n";
cin>>p_struct1->son>>p_struct1->mum>>p_struct2->son>>p_struct2->mum;
add(p_struct1,p_struct2);
cout <<"the sum of the fractions is" <<(float)p_struct1->son/p_struct1->mum <<endl;
getLine(p_string1);
cout <<p_string1 <<endl;
getLine(p_string2);
cout <<p_string2 <<endl;
add(p_string1,p_string2);
cout <<p_string1 <<endl;
delete[] p_string1;
delete[] p_string2;
return 0;
}#include <iostream>
#include <string.h>
using namespace std;
//10不够大,用100比较好
#define BUF_LEN 100
struct fraction
{
int son;
int mum;
};
void add(struct fraction *a,struct fraction *b)
{
a->son = (b->son * a->mum) + (a->son * b->mum);
a->mum *= b->mum;
}
int add(int a, int b)
{
return a+b;
}
void add(char *a,char *b) //连接字符串
{
strcat(a,b);
}
void getLine(char* &p_data) //读入字符串函数
{
char char_buf[BUF_LEN],*p_temp=NULL;
int len=1;
cout <<"Type a line,press enter to end:\n";
do
{
cin.get(char_buf,BUF_LEN);
/////strlen 是0,因为这个时候没有空间是0,只要你写就会越界,修改之
len += sizeof(char_buf);
p_data = new char[len];
if(p_data == NULL)
{
cout <<"Not enough memory from the heap.\n";
return;
}
if(p_temp == NULL)
strcpy(p_data,char_buf);
else
{
strcpy(p_data,p_temp);
strcat(p_data,char_buf);
}
delete[]p_temp;
p_temp = p_data;
if(cin.peek() == '\n')
{
cin.get();
break;
}
}while(true);
}
void main()
{
int a,b;
char *p_string1,*p_string2;
getLine(p_string1);
cout <<p_string1 <<endl;
getLine(p_string2);
cout <<p_string2 <<endl;
add(p_string1,p_string2);
struct fraction struct1,struct2,*p_struct1,*p_struct2;
cout <<"input your integer numbers!\n";
cin>>a;
cin>>b;
cout <<add(a,b) <<endl;
p_struct1 = &struct1;
p_struct2 = &struct2;
cout <<"input two fractions!\n";
cin>>p_struct1->son;
cin>>p_struct1->mum;
cin>>p_struct2->son;
cin>>p_struct2->mum;
add(p_struct1,p_struct2);
cout <<"the sum of the fractions is"
<<(float)p_struct1->son/p_struct1->mum <<endl;
cout <<p_string1 <<endl;
delete[]p_string1;
delete[]p_string2;
}