更改数组中变量的值(C编程)
基本上,我想做的是获取我声明的三个温度传感器读数,并将它们组合成一个数组。从这一点开始,我希望能够增加数组中每个相应变量的值,这取决于用户选择的当前通道,即称为selectChannel的变量 因此,如果用户在通道0上,他们按R,我希望温度传感器1读数增加@icktoofay提到使用指针,我做了他们建议的更改,但当我按下r或f时,温度并没有升高更改数组中变量的值(C编程),c,arrays,variables,pointers,C,Arrays,Variables,Pointers,基本上,我想做的是获取我声明的三个温度传感器读数,并将它们组合成一个数组。从这一点开始,我希望能够增加数组中每个相应变量的值,这取决于用户选择的当前通道,即称为selectChannel的变量 因此,如果用户在通道0上,他们按R,我希望温度传感器1读数增加@icktoofay提到使用指针,我做了他们建议的更改,但当我按下r或f时,温度并没有升高 enter code here #include "stdafx.h" #include <Windows.h> //additional
enter code here
#include "stdafx.h"
#include <Windows.h> //additional header added to allow for coloured text
#include "console.h"
#include <conio.h>
#include "lab4temperatureController.h"
#include <time.h>
//#define hConsole
#define CH1 0
#define CH2 1
#define CH3 2
temperature_t selectChannel = 0;
temperature_t temperatureSensor1Reading = 75;
temperature_t temperatureSensor2Reading = 75;
temperature_t temperatureSensor3Reading = 75;
temperature_t *temperatureSensorReadings[3] = {&temperatureSensor1Reading, &temperatureSensor2Reading, &temperatureSensor3Reading};
//Update Display:---------------------
//Description: prints out readings and updates with increment, decrement and average
//
//Parameters: lowlimit, highlimit, temperatures, Channels
//
//
//Returns:
//
//------------------------------------------------------------------
void updateDisplay()
{
clrscr();
HANDLE hConsole; //standard c library call
hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //used for output screen buffer to allow for coloured text
SetConsoleTextAttribute(hConsole, 2); //sets output screen colour for following text
printf("\nCurrent Temperature for channel 1 is %d\n\n", temperatureSensor1Reading);
printf("Upper Limit for channel 1 is %d\n\n", getHighLimit(CH1));
printf("Lower Limit for channel 1 is %d\n\n", getLowLimit(CH1));
setCurrentTemperature(CH1, temperatureSensor1Reading);
SetConsoleTextAttribute(hConsole, 3); //sets output screen colour for following body of text
printf("Current Temperature for channel 2 is %d\n\n", temperatureSensor2Reading);
printf("Upper Limit for channel 2 is %d\n\n", getHighLimit(CH2));
printf("Lower Limit for channel 2 is %d\n\n", getLowLimit(CH2));
setCurrentTemperature(CH2, temperatureSensor2Reading);
SetConsoleTextAttribute(hConsole, 4); //sets output screen colour for following body of text
printf("Current Temperature for channel 3 is %d\n\n", temperatureSensor3Reading);
printf("Upper Limit for channel 3 is %d\n\n", getHighLimit(CH3));
printf("Lower Limit for channel 3 is %d\n\n", getLowLimit(CH3));
setCurrentTemperature(CH3, temperatureSensor3Reading);
compareMinLimit(CH1);
compareMinLimit(CH2);
compareMinLimit(CH3);
compareHighLimit(CH1);
compareHighLimit(CH2);
compareHighLimit(CH3);
if (isAverageValid() == TRUE)
{
//if average is valid call function to calculate average and print returned value
//SetConsoleTextAttribute(hConsole, 5); //sets output screen colour for following text
printf("average for channel 1 is %d\n\n", calculateAverageTemperature(CH1));
printf("average for channel 2 is %d\n\n", calculateAverageTemperature(CH2));
printf("average for channel 3 is %d\n\n", calculateAverageTemperature(CH3));
//--if average is valid call functions to determine min and max values and print returned values for each channel
temperature_t max1 = calculateMax(CH1);
temperature_t min1 = calculateMin(CH1);
temperature_t max2 = calculateMax(CH2);
temperature_t min2 = calculateMin(CH2);
temperature_t max3 = calculateMax(CH3);
temperature_t min3 = calculateMin(CH3);
printf("the maximum temperature is: %d \n \nthe minimum temperature is: %d \n \n", max1,min1); //display each channels max/min
printf("the maximum temperature is: %d \n \nthe minimum temperature is: %d \n \n ", max2,min2);
printf("the maximum temperature is: %d \n \nthe minimum temperature is: %d \n \n", max3,min3);
// if average is valid call function to print histogram
SetConsoleTextAttribute(hConsole, 15);
printf("the following is the pressure histogram for channel %d \n \n", selectChannel+1);
printPressureHistogram(selectChannel);
//print out the current channels histograms
//initializeTemperatureSubsystem();
}
}
//main:---------------------
//Description: initializes the temperature subsystem and provides user with various input commands
// which can be used to increment, decrement and get average value of temperatures
//Parameters: lowlimit, highlimit, temperatures, MAXSAMPLES
//
//Returns:
//------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
// -- insert variable declaration for time ------
time_t timeOfLastStoredSample, currentTime;
//==============================================
initializeTemperatureSubsystem();
//--insert code to store the initial sensor temperature------
recordCurrentTemperature(CH1, temperatureSensor1Reading); //records each channels temperature reading
recordCurrentTemperature(CH2, temperatureSensor2Reading);
recordCurrentTemperature(CH3, temperatureSensor3Reading);
//--record the time as timeOfLastSample
timeOfLastStoredSample = time(NULL);
//==========================================
printf("\n Q,A: Adjust the current temperature select, W,S adjust our limit, E,D adjust lower limit \n");
unsigned char quit = 'n'; //binds quit to literal value of 'n'
while(quit == 'n') //executes until user quits
{
//-- insert code to get currentTime----
time(¤tTime); //ASK ABOUT THIS (we haev to explicitly direct time to the address for current time
//--insert code to compare the two time values and see if 20 seconds have elapse
if (currentTime - timeOfLastStoredSample >= 1)
{
//if 20 seconds have elapsed, record the time and update value held by timeLastStoredSample
recordCurrentTemperature(CH1, temperatureSensor1Reading);
recordCurrentTemperature(CH2, temperatureSensor2Reading);
recordCurrentTemperature(CH3, temperatureSensor3Reading);
time(&timeOfLastStoredSample);
}
updateDisplay();
Sleep(200);
unsigned char selectedCommand = 0;
if( _kbhit() )
{
selectedCommand = _getch();
switch(selectedCommand)
{
case 'Q': //if user input is Q
case 'q'://if user input is q
selectChannel ++;
if (selectChannel > 2) //determine which channel the user is on, if greater than 2, then cycle to channel 0
selectChannel = CH1;
printf ("\n your current channel is %d \n \n ", selectChannel + 1);
break; //exits loop
case 'A': //if user input is A
case 'a'://if user input is a
selectChannel --;
if (selectChannel < 0) //determine which channel the user is on, if less than 0, then cycle to channel 2
selectChannel = CH3;
printf (" \n your current channel is %d \n \n", selectChannel + 1);
break; //exits loop
case 'R': //if user input is R
case 'r'://if user input is r
*temperatureSensorReadings[selectChannel]++;
break; //exits loop
case 'F': //if user input is 'F'
case 'f': //if user input is 'f'
*temperatureSensorReadings[selectChannel]--;
break; //exits loop
case 'W': //if user input is 'W'
case 'w': //if user input is 'w'
increaseHighLimit(selectChannel); //increase CH1 high limit
break; //exits loop
case 'S':
case 's'://if user input is 'S
decreaseHighLimit(selectChannel); //decrement CH1 high limit
break; //exits loop
case 'E':
case 'e': //if user input is E
increaseLowLimit(selectChannel); //decrement CH1 low limit
break; //exits loop
case'D':
case'd': //if user input is D
decreaseLowLimit(selectChannel); //decrement CH1 lowlimit
break;//exits loop
case'X': //conditions apply for preceding case as well (for upper and lowercase "q")
clrscr(); //executes function
gotoxy(2,2); //returns command line to 2,2 location
printf("Exiting...Bye!\n");
quit = 'y'; //cause exit of while loop
break;
default:
clrscr(); //executes function
gotoxy(1,1); //takes command line to location 1,1
printf("Q,A: Adjust the current temperature select, W,S adjust our limit, E,D adjust lower limit \n");
break;
}//eo of switch
}//eo kbhit()
}// eo while loop
return 0;
在此处输入代码
#包括“stdafx.h”
#include//添加额外的标题以允许彩色文本
#包括“console.h”
#包括
#包括“lab4temperatureController.h”
#包括
//#定义hConsole
#定义ch10
#定义CH2 1
#定义CH3 2
温度\u t选择通道=0;
温度传感器读数=75;
温度传感器2读数=75;
温度传感器3的温度读数=75;
温度传感器读数[3]={&TemperatureSensor1读数,&TemperatureSensor2读数,&TemperatureSensor3读数};
//更新显示:---------------------
//描述:打印读数并更新增量、减量和平均值
//
//参数:下限、上限、温度、通道
//
//
//返回:
//
//------------------------------------------------------------------
void updateDisplay()
{
clrsc();
HANDLE hConsole;//标准c库调用
hConsole=GetStdHandle(STD_OUTPUT_HANDLE);//用于输出屏幕缓冲区以允许彩色文本
SetConsoleTextAttribute(hConsole,2);//设置以下文本的输出屏幕颜色
printf(“\n通道1的当前温度为%d\n\n”,温度传感器1读取);
printf(“通道1的上限为%d\n\n”,getHighLimit(CH1));
printf(“通道1的下限为%d\n\n”,getLowLimit(CH1));
设置当前温度(CH1,温度传感器1读数);
SetConsoleTextAttribute(hConsole,3);//为以下文本体设置输出屏幕颜色
printf(“通道2的当前温度为%d\n\n”,温度传感器2读取);
printf(“通道2的上限为%d\n\n”,getHighLimit(CH2));
printf(“通道2的下限为%d\n\n”,getLowLimit(CH2));
设置当前温度(CH2,温度传感器2读数);
SetConsoleTextAttribute(hConsole,4);//为以下文本体设置输出屏幕颜色
printf(“通道3的当前温度为%d\n\n”,温度传感器3读数);
printf(“通道3的上限为%d\n\n”,getHighLimit(CH3));
printf(“通道3的下限为%d\n\n”,getLowLimit(CH3));
设置当前温度(CH3,温度传感器3读数);
比较极限(CH1);
比色极限(CH2);
比色极限(CH3);
比较高限值(CH1);
比较高限值(CH2);
比较高限值(CH3);
如果(isAverageValid()==TRUE)
{
//如果average有效,则调用函数计算平均值并打印返回值
//SetConsoleTextAttribute(hConsole,5);//设置以下文本的输出屏幕颜色
printf(“通道1的平均值为%d\n\n”,calculateAverageTemperature(CH1));
printf(“通道2的平均值为%d\n\n”,calculateAverageTemperature(CH2));
printf(“通道3的平均值为%d\n\n”,calculateAverageTemperature(CH3));
//--如果average有效,则调用函数确定最小值和最大值,并打印每个通道的返回值
温度=calculateMax(CH1);
温度_tmin1=calculateMin(CH1);
温度=计算最大值(CH2);
温度_tmin2=计算的最小值(CH2);
温度=计算最大值(CH3);
温度_tmin3=计算的最小值(CH3);
printf(“最高温度为:%d\n\n最低温度为:%d\n\n”,max1,min1);//显示每个通道的最大/最小值
printf(“最高温度为:%d\n\n最低温度为:%d\n\n”,max2,min2);
printf(“最高温度为:%d\n\n最低温度为:%d\n\n”,max3,min3);
//如果average有效,则调用函数打印直方图
SetConsoleTextAttribute(hConsole,15);
printf(“以下是通道%d的压力直方图\n\n”,选择通道+1);
打印压力直方图(选择通道);
//打印出当前通道直方图
//初始化温度子系统();
}
}
//主要内容:---------------------
//描述:初始化温度子系统并向用户提供各种输入命令
//它可以用来增加、减少和获得温度的平均值
//参数:下限、上限、温度、最大采样
//
//返回:
//------------------------------------------------------------------
int _tmain(int argc,_TCHAR*argv[]
{
//--插入时间的变量声明------
LastStoredSample的时间,currentTime;
//==============================================
初始化温度子系统();
//--插入代码以存储初始传感器温度------
recordCurrentTemperature(CH1,温度传感器1读数);//记录每个通道的温度读数
记录当前温度(CH2,温度传感器2读数);
记录当前温度(CH3,温度传感器3读数);
//--将时间记录为timeOfLastSample
timeOfLastStoredSample=时间(空);
//==========================================
printf(“\n Q,A:调整当前温度选择,W,S调整我们的限制,E,D调整下限\n”);
unsigned char quit='n';//将quit绑定到文本值'n'
while(quit=='n')//执行,直到用户退出
{
//--插入代码以获取currentTime----
time(¤tTime);//询问这个问题(我们可以显式地将时间指向当前时间的地址
//--插入代码以比较两个时间值,并查看是否已过20秒
如果(当前时间)
( *temperatureSensorReadings[selectChannel] )++;
temperature_t temperatureSensor1Reading = 75;
temperature_t temperatureSensor2Reading = 75;
temperature_t temperatureSensor3Reading = 75;
temperature_t temperatureSensorReadings[3] = { temperatureSensor1Reading, temperatureSensor2Reading, temperatureSensor3Reading};
case 'R': //if user input is R
case 'r'://if user input is r
temperatureSensorReadings[selectChannel]++;