用C++语言,编程2个题目

来源:百度知道 编辑:UC知道 时间:2024/09/28 10:19:52
第一题:

#include <iostream> // precompiler directive
using namespace std; // compiler directiv
//--------------------------------------------
// definition of function named “main”
//--------------------------------------------
//char *msg="Hello world!";
char msg[]="Hello world!";
sizeof(msg)=? 《-----这里要自己调试,考察对指针的理解
int f(char * msg)
{
return sizeof(msg);

}
int main (int argc,char *argv [])
{
cout << "hello, world!" << endl;
int i;
float f=1.0f;

cout<<sizeof(msg)<<endl;

cout<<f(msg)<<endl;

for(i=0;i<argc;i++)
cout << i<<" is "<<argv[i]<<endl;

return 0;

}

PS:最后调试要能运行出结果,说明结果是什么。(把调试的地方写出来下,谢谢了)

第二题:

输入的数目不确定,可以3个甚至更多,相加能得出结果(如2+3+4=9)

#include <iostream> // precompiler directive
using namespace std; // compiler directiv
//--------------------------------------------
// definition of function named “main”
//--------------------------------------------
//char *msg="Hello world!";
char msg[]="Hello world!";
//sizeof(msg)=? 《-----?里要自己??,考察?指?的理解
int f(char * msg)
{
return sizeof(msg);
}
int main (int argc,char *argv [])
{
cout << "hello, world!" << endl;
int i;
float f1=1.0f;

cout<<sizeof(msg)<<endl;

cout<<f(msg)<<endl; ////////////////////////这个和变量名冲突了,我把变量改成f1了

for(i=0;i<argc;i++)
cout << i<<" is "<<argv[i]<<endl;

return 0;

}

第二题:
#include<iostream>
using namespace std;
struct Node
{
int data;