【问题标题】:Creating a function and calling it in main program创建一个函数并在主程序中调用它
【发布时间】:2013-08-29 12:19:07
【问题描述】:

我有一个AVR编程案例

case (0xe7): keyPressed=".";

在此期间我想调用一个预定义的函数

switch (keyCode)               //generating key characetr to display on LCD
{

case (0xee): keyPressed="1";
            b=1;
            a=a*10+b; 
            i=i++;
            break;
case (0xed): keyPressed="4";
            b=4;
            a=a*10+b; 
            i=i++;
            break;
case (0xeb): keyPressed="7";
            b=7;
            a=a*10+b; 
            i=i++;
            break;
case (0xde): keyPressed="2";
            b=2;
            a=a*10+b; 
            i=i++;
            break;
case (0xdd): keyPressed="5";
            b=5;
            a=a*10+b; 
            i=i++;
            break;
case (0xdb): keyPressed="8";
            b=8;
            a=a*10+b; 
            i=i++;
            break;
case (0xd7): keyPressed="0";
            b=0;
            a=a*10+b; 
            i=i++;
            break;
case (0xbe): keyPressed="3";
            b=3;
            a=a*10+b; 
            i=i++;
            break;
case (0xbd): keyPressed="6";
            b=6;
            a=a*10+b; 
            i=i++;
            break;
case (0xbb): keyPressed="9";
            b=9;
            a=a*10+b; 
            i=i++;
            break;
}

我如何制作这个功能?并在我的情况下在主程序中调用它? 请指导我,我对这一切都很陌生... 请帮帮我..

【问题讨论】:

  • 不要做i = i++,它的行为是不确定的。
  • 我实际上希望将整数“a”存储为整数,然后将其除以 10^i 以获得十进制形式.. i 表示按键的总次数。 . 请指导我正确的方法?
  • i++; 相当于i = i+1; Joachim 是对的
  • 另外,在case 标签表达式周围加上括号是没有意义的。只是case 0xe7:等等更干净一些。
  • @ndj, i++ 在返回值上与 i=i+1 不同。 i=i++ 是 UB,而 i=i=i+1 是(我认为)有效的。

标签: c function calculator avr atmega


【解决方案1】:

假设,在你的情况下,当你从 avr 按下键时,你会得到 Keycode 值,例如,当你按下 1 时,你会得到地址 1,当你按下 2 时,你会得到地址 2,当你按下 3 时,你会得到地址 3 和依此类推,当您按 9 时,您将获得地址 9。当您按 时。您将获得地址 10。 但是在c中你没有得到这样的输入。使用scanf从stdin读取。并重复循环直到按下'。'

每次按键您都按住按键并将该键添加到按键序列和按键次数中。

b=whichkeypressed;
a=a*10+b;
i++;

如果您按 3,2,1 和 .然后 a=321 ,b=1 ,i=3

按“。”后 你正在调用你正在计算的预定义函数

c=pow(10,i); 
i=3 => c=1000
and d=a/c;
a=321 ,c=1000
d=0.321 

执行上述操作的代码是:

#include<stdio.h>
#include<math.h>
int a,b,i;
double  c,d;


void function()
{
c= pow(10,i);
d=a/c;
}


main()
{

int keyCode;
char keyPressed;

while(1)
{
printf("Enter Keycode: ");
scanf("%d",&keyCode);

switch (keyCode)               //generating key characetr to display on LCD
{

case 1: keyPressed='1';
            b=1;
            a=a*10+b;
            i++;
            break;
case 4: keyPressed='4';
            b=4;
            a=a*10+b;
            i++;
            break;
case 7: keyPressed='7';
            b=7;
            a=a*10+b;
            i++;
            break;
case 2: keyPressed='2';
            b=2;
            a=a*10+b;
            i++;
            break;
case 5: keyPressed='5';
            b=5;
            a=a*10+b;
            i++;
            break;
case 8: keyPressed='8';
            b=8;
            a=a*10+b;
            i++;
            break;
case 0: keyPressed='0';
            b=0;
            a=a*10+b;
            i++;
            break;
case 3: keyPressed='3';
            b=3;
            a=a*10+b;
            i++;
            break;
case 6: keyPressed='6';
            b=6;
            a=a*10+b;
            i++;
            break;
case 9: keyPressed='9';
            b=9;
            a=a*10+b;
            i++;
            break;

case 10: keyPressed='.';
             printf("No of Times Keys Pressed are = %d\n",i);

             function();
                printf("sequence of keys pressed=%d \nlast key pressed=%d\npower value of Keys=%lf\n The decimal valueof given sequence of keys=%6.10lf\n",a,b,c,d);   //use c and d with the values what you required.

                                break;

        }

        if (keyPressed=='.')
        break;

        }
getchar();
}

输出:

Enter Keycode: 1
Enter Keycode: 2
Enter Keycode: 3
Enter Keycode: 4
Enter Keycode: 5
Enter Keycode: 6
Enter Keycode: 7
Enter Keycode: 8
Enter Keycode: 9
Enter Keycode: 10
No of Times Keys Pressed are = 9
sequence of keys pressed=123456789
last key pressed=9
power value of Keys=1000000000.000000
The decimal valueof given sequence of keys=0.1234567890

对于 avr,您需要进行一些必要的更改,包括案例地址和下一个按键语句以及一些其他内容。

【讨论】:

  • 对不起,我无法得到想要的结果:/
  • @MohitGoyal 编辑了答案。如果我的假设是正确的,那么你会得到一些需要的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 2022-11-10
  • 2022-01-19
  • 1970-01-01
相关资源
最近更新 更多