C++编程题 大一课后习题,做法越简单越好,谢谢 2到5题,求大神会做几题就帮忙做几题,帮帮忙呀
2.
//#include "stdafx.h"//vc++6.0加上这一行.#include <iostream>
using namespace std;
void mymax(int a,int b,int *pmax){
*pmax = a>b ? a : b;
}
int main(void){
int x,y,max;
cout << "Input a & b(int)...\n";
cin >> x >> y;
mymax(x,y,&max);
cout << "The max is " << max << endl;
return 0;
}
3.
//#include "stdafx.h"//vc++6.0加上这一行.#include <iostream>
using namespace std;
void myfun(int a,int b,int *padd,int *psub,int *pmul,int *pdiv){
*padd = a+b;
*psub = a-b;
*pmul = a*b;
*pdiv = b ? a/b : (1<<(sizeof(int)*8-1))-1;
}
int main(void){
int x,y,add,sub,mul,div;
cout << "Input a & b(int)...\n";
cin >> x >> y;
myfun(x,y,&add,&sub,&mul,&div);
cout << x << " + " << y << " = " << add << endl;
cout << x << " - " << y << " = " << sub << endl;
cout << x << " x " << y << " = " << mul << endl;
cout << x << " / " << y;
if(div!=(1<<(sizeof(int)*8-1))-1)
cout << " = " << div << endl;
else cout << ": By zero...\n";
return 0;
}
4.
//#include "stdafx.h"//vc++6.0加上这一行.#include <iostream>
using namespace std;
int myslen(char *str){
for(int ln=0;str[ln];ln++);
return ln;
}
int main(void){
char str[1000];
cout << "Input a string...\nstr=";
cin >> str;
cout << "The length of the string is " << myslen(str) << endl;
return 0;
}
5.
//#include "stdafx.h"//vc++6.0加上这一行.#include <iostream>
using namespace std;
void mysort(int *p,int n){
for(int i=0;i<n-1;i++){
for(int k=i,j=k+1;j<n;j++)
if(*(p+k)>*(p+j)) k=j;
if(k!=i){
j=*(p+i);
*(p+i)=*(p+k);
*(p+k)=j;
}
}
}
int main(void){
int a[1000],i=0,j;
cout << "Input some data(letter end)...\n";
while(cin >> a[i]) i++;
mysort(a,i);
for(j=0;j<i;cout << a[j++] << ' ');
cout << endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2015-06-29
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void Max(int left, int right, int* result)
{
*result = (left > right ? left : right);
}
void Calc(float left, float right, float result[4])
{
result[0] = left + right;
result[1] = left - right;
result[2] = left * right;
if (abs(right) > 0.0001)
result[3] = left / right;
}
int length(char* text)
{
return string(text).length();
}
void sort(int data[], int length)
{
std::sort(data, data + length);
}
int main()
{
//第二题
int result = 0;
Max(5, 3, &result);
cout << "Max(5, 3) = " << result << endl;
//第三题
float resultCalc[4] = {0.0f};
Calc(5.0, 3.0, resultCalc);
cout << "5 + 3 = " << resultCalc[0] << endl;
cout << "5 - 3 = " << resultCalc[1] << endl;
cout << "5 * 3 = " << resultCalc[2] << endl;
cout << "5 / 3 = " << resultCalc[3] << endl;
//第四题
cout << "Length of \"HelloWorld\" is " << length("HelloWorld") << endl;
//第五题
int data[] = { 7, 6, 1, 3, 6, 8, 2 };
sort(data, 7);
for (int i = 0; i < 7; ++i)
{
cout << data[i] << " ";
}
cout << endl;
return 0;
}
#include <string>
#include <algorithm>
using namespace std;
void Max(int left, int right, int* result)
{
*result = (left > right ? left : right);
}
void Calc(float left, float right, float result[4])
{
result[0] = left + right;
result[1] = left - right;
result[2] = left * right;
if (abs(right) > 0.0001)
result[3] = left / right;
}
int length(char* text)
{
return string(text).length();
}
void sort(int data[], int length)
{
std::sort(data, data + length);
}
int main()
{
//第二题
int result = 0;
Max(5, 3, &result);
cout << "Max(5, 3) = " << result << endl;
//第三题
float resultCalc[4] = {0.0f};
Calc(5.0, 3.0, resultCalc);
cout << "5 + 3 = " << resultCalc[0] << endl;
cout << "5 - 3 = " << resultCalc[1] << endl;
cout << "5 * 3 = " << resultCalc[2] << endl;
cout << "5 / 3 = " << resultCalc[3] << endl;
//第四题
cout << "Length of \"HelloWorld\" is " << length("HelloWorld") << endl;
//第五题
int data[] = { 7, 6, 1, 3, 6, 8, 2 };
sort(data, 7);
for (int i = 0; i < 7; ++i)
{
cout << data[i] << " ";
}
cout << endl;
return 0;
}
第2个回答 2015-06-29
for (int i = 0 ; i < strlen(s) ; i ++)
{
if(i %2 == 0)
{
t[i%2] = s[i];
}
}
{
if(i %2 == 0)
{
t[i%2] = s[i];
}
}