一个困扰了我很久的问题!

shine_paul 2008-06-01 03:57:09
先声明一下,本人是个大一学生,接触C语言4个月了,菜鸟级别!一些很幼稚的话请见谅,不要笑话我哦!
我做funtion的时候要返回多个值,但是总是达不到效果,我也知道用指针和结构体的方法去做,但是我刚接触到结构体这些东西,所以用不来,希望大家能帮个比较麻烦的忙,举个例子给我,并且最好写上一些说明,这样帮助我好理解嘛!,先谢了啊!呵呵!
...全文
180 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2008-06-04
  • 打赏
  • 举报
回复
对C来说,只能返回一个值,要返回多个值,只能用struct或者传入指针,在上面的代码都有演示,level1_operation是返回一个struct的,然后里面有多个值,当然这种方法当struct很大的时候很没效率(浪费很多空间),level2_operation是传入一个指针,然后在函数中改变指针所指对象的值起到返回多个值的效果。

对返回多个值来说,2个和20个方法没什么区别,不过20 > 2所以代码相应多点也是应该的。
或者说不如你说说你的具体的问题/你最终想干嘛,也许能从其他方面提供思路。
shine_paul 2008-06-04
  • 打赏
  • 举报
回复
真是非常感谢,我已经解决这个问题了,呵呵,万分感谢!
我结帖了,再一次谢谢了!
sukyin 2008-06-04
  • 打赏
  • 举报
回复
用数组啊。。
typedef struct
{
int ans[20];
}Correct_Answer;
typedef struct
{
int answer[20];
}Your_Answer;

这样赋值和比较可以用循环做啊。
for(int i = 0;i<20;++i){
Your_Answer.answer[i] = Your_ans[i];
}

比较:
for(int i = 0;i<20;++i){
if(Correct_Ans.ans[i]==Your_Answer.answer[i])
score+=3;
}

shine_paul 2008-06-04
  • 打赏
  • 举报
回复
对了,那个Display()和Password()俩个funtion不用看的,没有问题,也跟我的问题没啥关系,方便一下大家哦!我真的是很希望大家帮个忙,非常感谢!呵呵!
shine_paul 2008-06-04
  • 打赏
  • 举报
回复
这是我们老师要每个人做一个project,我选了考试系统,毕竟才大一嘛,所以很多不懂!
我就是希望把Funtion------DisplayTest()和Answer()中的20个值都用到Scores()中,然后进行对比得到分数!
我做的就是这个,全部都是我自己写的,不懂的就问大家,程序写到这里是没有问题的,在C++中可以运行,结果都正确,我就是想把
代码简化,因为我知道我的方法太笨,呵呵!请大家认真看一下,很简单的程序,不要烦躁哦,谢谢啊!呵呵!


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>


typedef struct
{
int ans1;
int ans2;
int ans3;
int ans4;
int ans5;
int ans6;
int ans7;
int ans8;
int ans9;
int ans10;
int ans11;
int ans12;
int ans13;
int ans14;
int ans15;
int ans16;
int ans17;
int ans18;
int ans19;
int ans20;
}Correct_Answer;

typedef struct
{
int answer1;
int answer2;
int answer3;
int answer4;
int answer5;
int answer6;
int answer7;
int answer8;
int answer9;
int answer10;
int answer11;
int answer12;
int answer13;
int answer14;
int answer15;
int answer16;
int answer17;
int answer18;
int answer19;
int answer20;
}Your_Answer;



void Display();
int PasswordInterface();
Correct_Answer DisplayTest(int result);
Your_Answer Answer();
int Scores(Correct_Answer Correct_Ans,Your_Answer Your_Ans);
void DemandScores(int score);

void Display()
{
printf(" ----------------------------------------------------\n");
printf(" | Examination System |\n");
printf(" | |\n");
printf(" | This system is designed for primary students |\n");
printf(" | to test their ability do the arithmetic question!|\n");
printf(" | System will give expressions automatically , and |\n");
printf(" | Studends are required to give the answer. After |\n");
printf(" | it, the marks and right answer will be given by |\n");
printf(" | the system. Good luck and enjoy it! |\n");
printf(" | |\n");
printf(" | Administrators:Rooney and Roy |\n");
printf(" | Summer 2008 |\n");
printf(" ----------------------------------------------------\n");
return;
}


int PasswordInterface()
{
long int num;
int i,j,ans,answer;
char pw[4];
int Real_pw[4]={1,2,3,4};
start:
printf(" ----------------------------------\n");
printf(" Student Number:");
scanf("%d",&num);
for(i=1;i<=36;i++)
{
if(num==710704300+i)
{
answer=1;
break;
}
else
answer=0;
}
if(answer==1)
{
printf(" ----------------------------------\n");
for(i=0;i<3;i++)
{
printf(" Your Password:");
for(j=0;j<4;j++)
{
pw[j]=getch();
putch('*');
}
getch();
if(pw[0]-48==Real_pw[0]&&pw[1]-48==Real_pw[1]&&pw[2]-48==Real_pw[2]&&pw[3]-48==Real_pw[3])
{
ans=1;
break;
}
else
{
printf("\nYour password is wrong!\n");
ans=0;
}
}
}
else
{
printf("\n\nYour student_number isn't found!Enter again!\n");
goto start;
}
return ans;
}


Correct_Answer DisplayTest(int result)
{
int i,num1,num2,sign;
int Correct_ans[20];
Correct_Answer Correct_Ans;
srand(time(NULL));
if(result==1)
{
printf("\n ----------------------------------------------\n");
printf(" | Our text includes 2 parts |\n");
printf(" | NO.1: Subject of filling |\n");
printf(" | NO.2: Subject of choosing |\n");
printf(" ----------------------------------------------\n");
printf("\n\n\n");
printf("The subject of filling includes 20 subjects.Each subject is 3 scores.\n");
for(i=0;i<20;i++)
{
num1=rand()%200;
num2=rand()%200;
sign=rand()%2;
if(sign==1)
{
printf("NO.%d: %d + %d\n",i+1,num1,num2);
Correct_ans[i]=num1+num2;
}
if(sign==0)
{
printf("NO.%d: %d - %d\n",i+1,num1,num2);
Correct_ans[i]=num1-num2;
}
}
Correct_Ans.ans1=Correct_ans[0];
Correct_Ans.ans2=Correct_ans[1];
Correct_Ans.ans3=Correct_ans[2];
Correct_Ans.ans4=Correct_ans[3];
Correct_Ans.ans5=Correct_ans[4];
Correct_Ans.ans6=Correct_ans[5];
Correct_Ans.ans7=Correct_ans[6];
Correct_Ans.ans8=Correct_ans[7];
Correct_Ans.ans9=Correct_ans[8];
Correct_Ans.ans10=Correct_ans[9];
Correct_Ans.ans11=Correct_ans[10];
Correct_Ans.ans12=Correct_ans[11];
Correct_Ans.ans13=Correct_ans[12];
Correct_Ans.ans14=Correct_ans[13];
Correct_Ans.ans15=Correct_ans[14];
Correct_Ans.ans16=Correct_ans[15];
Correct_Ans.ans17=Correct_ans[16];
Correct_Ans.ans18=Correct_ans[17];
Correct_Ans.ans19=Correct_ans[18];
Correct_Ans.ans20=Correct_ans[19];
}
return Correct_Ans;
}


Your_Answer Answer()
{
int i=0,Your_ans[20];
Your_Answer Your_Ans;
printf("\n\nPlease answer the text,good luck!\n\n");
while(i<20)
{
printf("\nThe answer of NO.%d is : ",i+1);
scanf("%d",&Your_ans[i]);
i++;
}
Your_Ans.answer1=Your_ans[0];
Your_Ans.answer2=Your_ans[1];
Your_Ans.answer3=Your_ans[2];
Your_Ans.answer4=Your_ans[3];
Your_Ans.answer5=Your_ans[4];
Your_Ans.answer6=Your_ans[5];
Your_Ans.answer7=Your_ans[6];
Your_Ans.answer8=Your_ans[7];
Your_Ans.answer9=Your_ans[8];
Your_Ans.answer10=Your_ans[9];
Your_Ans.answer11=Your_ans[10];
Your_Ans.answer12=Your_ans[11];
Your_Ans.answer13=Your_ans[12];
Your_Ans.answer14=Your_ans[13];
Your_Ans.answer15=Your_ans[14];
Your_Ans.answer16=Your_ans[15];
Your_Ans.answer17=Your_ans[16];
Your_Ans.answer18=Your_ans[17];
Your_Ans.answer19=Your_ans[18];
Your_Ans.answer20=Your_ans[19];
return Your_Ans;
}


int Scores(Correct_Answer Correct_Ans,Your_Answer Your_Ans)
{
int i,score=0;
if(Correct_Ans.ans1==Your_Ans.answer1)
score+=3;
if(Correct_Ans.ans2==Your_Ans.answer2)
score+=3;
if(Correct_Ans.ans3==Your_Ans.answer3)
score+=3;
if(Correct_Ans.ans4==Your_Ans.answer4)
score+=3;
if(Correct_Ans.ans5==Your_Ans.answer5)
score+=3;
if(Correct_Ans.ans6==Your_Ans.answer6)
score+=3;
if(Correct_Ans.ans7==Your_Ans.answer7)
score+=3;
if(Correct_Ans.ans8==Your_Ans.answer8)
score+=3;
if(Correct_Ans.ans9==Your_Ans.answer9)
score+=3;
if(Correct_Ans.ans10==Your_Ans.answer10)
score+=3;
if(Correct_Ans.ans11==Your_Ans.answer11)
score+=3;
if(Correct_Ans.ans12==Your_Ans.answer12)
score+=3;
if(Correct_Ans.ans13==Your_Ans.answer13)
score+=3;
if(Correct_Ans.ans14==Your_Ans.answer14)
score+=3;
if(Correct_Ans.ans15==Your_Ans.answer15)
score+=3;
if(Correct_Ans.ans16==Your_Ans.answer16)
score+=3;
if(Correct_Ans.ans17==Your_Ans.answer17)
score+=3;
if(Correct_Ans.ans18==Your_Ans.answer18)
score+=3;
if(Correct_Ans.ans19==Your_Ans.answer19)
score+=3;
if(Correct_Ans.ans20==Your_Ans.answer20)
score+=3;
return score;
}


void DemandScores(int score)
{
printf("\n\nYour scores is %d",score);

return;
}


void main()
{
Correct_Answer Ans1;
Your_Answer Ans2;
int result,Your_Scores;
Display();
result=PasswordInterface();
Ans1=DisplayTest(result);
Ans2=Answer();
Your_Scores=Scores(Ans1,Ans2);
DemandScores(Your_Scores);
getch();
return;
}



shine_paul 2008-06-03
  • 打赏
  • 举报
回复
9楼的这位,先当然是谢谢帮忙咯!
但是你这个方法适用于个数比较少的返回值,但当个数多起来的时候这个好象就要写很多类似重复的代码,我也试过了,的确是可以返回20个值,但是这样好象很麻烦,不知道有没有方便的方法,就是说用指针来返回,但我就是做不出来,真是烦恼啊!呵呵!希望接着帮下去咯,送佛送到西嘛,呵呵!
ForestDB 2008-06-02
  • 打赏
  • 举报
回复

#include <stdio.h>

#define IN
#define OUT

struct result1 {
int addition;
int subtraction;
};

struct result2 {
int multiplication;
int division;
};

OUT struct result1 level1_operation(IN int a, IN int b)
{
struct result1 return_value;
return_value.addition = a + b;
return_value.subtraction = a - b;
return return_value;
}

void level2_operation(IN int a, IN int b, OUT struct result2 *return_value)
{
return_value->multiplication = a * b;
return_value->division = a / b;
}

int main(void)
{
struct result1 r1;
struct result2 r2;
int a = 4, b = 2;

r1 = level1_operation(a, b);
level2_operation(a, b, &r2);

printf("%d + %d = %d\n", a, b, r1.addition);
printf("%d - %d = %d\n", a, b, r1.subtraction);
printf("%d * %d = %d\n", a, b, r2.multiplication);
printf("%d / %d = %d\n", a, b, r2.division);

return 0;
}
roooot 2008-06-01
  • 打赏
  • 举报
回复
我当初学的时候是这样理解的,要传递多个参数的话只能把这些参数的地址作为实参传递过去,然后函数说明中要用指针接这些地址,函数体中用到这个变量的时候都是用的地址,要是用到这个参数的值得话,可以在前面加个星号,这样就是数值了,呵呵,肤浅啊,不过听别人说多多发言才会有进步的,楼主,共同进步
sc_valentine21 2008-06-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 gaofanfan 的回复:]
建议把谭浩强的书看上至少5遍,你就能理解了。
当初我也是菜鸟啊!就是这样一步步走过来的啊!
[/Quote]
up
gaofanfan 2008-06-01
  • 打赏
  • 举报
回复
建议把谭浩强的书看上至少5遍,你就能理解了。
当初我也是菜鸟啊!就是这样一步步走过来的啊!
gezichong 2008-06-01
  • 打赏
  • 举报
回复
要返回多个值,如你所说的20个,可以定义一个结构体,然后把要返回的值放到相应的结构体成员中,再返回..


或者放到一个数组中,返回这个数组也可以.

就像2楼所说的:
typedef struct _res {

float sum;
float dif;
float product;
float quotient;

} res;

res calc(float f1, float f2) {
res r;
r.sum = f1 + f2;
r.dif = f1 - f2;
r.product = f1 * f2;
r.quotient = f1 / f2;
return r; //返回res变量,然后在程序中使用这个返回值就可以了
}

shine_paul 2008-06-01
  • 打赏
  • 举报
回复
上面的代码写的是很详细,我看是看的懂,但就是很难用到我自己的funtion中,看样子是要好好理解下!
我现在做的funtion要返回20个值,我用数组将其记录下来了,然后怎么返回呢!
gccli 2008-06-01
  • 打赏
  • 举报
回复
>我做funtion的时候要返回多个值
你可以把返回值放在形参里,用一个引用&可指针调用
星羽 2008-06-01
  • 打赏
  • 举报
回复

#include "stdio.h"

typedef struct _res {

float sum;
float dif;
float product;
float quotient;

} res;

res calc(float f1, float f2) {
res r;
r.sum = f1 + f2;
r.dif = f1 - f2;
r.product = f1 * f2;
r.quotient = f1 / f2;
return r;
}

int main() {

float f1 = 1.5f;
float f2 = 2.5f;
res r = calc(f1, f2);

printf("%f + %f = %f\n", f1, f2, r.sum);
printf("%f - %f = %f\n", f1, f2, r.dif);
printf("%f * %f = %f\n", f1, f2, r.product);
printf("%f / %f = %f\n", f1, f2, r.quotient);

return 0;
}

--------

1.500000 + 2.500000 = 4.000000
1.500000 - 2.500000 = -1.000000
1.500000 * 2.500000 = 3.750000
1.500000 / 2.500000 = 0.600000


xabcxyz 2008-06-01
  • 打赏
  • 举报
回复
结构是管理不同类型的批数据的手段,本质就是画个大圈起个名。 两个班都有张三,说起张三就用1班.张三这种形式。

struct Test { 画的一个圈名叫Test
short i1; 圈里有两个名为i1,i2的变量
short i2;
};

void Add(Test *t1,Test t2) 函数参数是结构变量t1,t2,其中t1要返回数据用指针
{
t1->i1 = t1->i1 + t2.i1; 结构指针访问结构成员,用->
t1->i2 = t1->i2 + t2.i2; 结构访问结构成员,用.
}

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧