c - 我怎样才能在我的代码中加入#define 语句?
问题描述
首先,如何在这段代码中实现#define?在定义和调用#define 时,我相当迷茫。此外,如果有任何方法可以使此代码看起来更好,我们将不胜感激!我是编码新手,所以任何帮助都会很棒。这在技术上是家庭作业,但我满足了作业的所有要求,所以现在这只是探索性的。
#include <stdio.h>
#include <math.h>
int main() {
int studentID;
int count= 0;
char course1[10]={};//issue with storing user input, used an array
char course2[10]={};
float coursecost1;
float coursecost2;
float credithour = 120.25;
float healthID=35.00;
float totalcost;
// Asks for student ID
printf("Enter the Students Id: \n");
scanf("%d",&studentID);
// Enter CRN/Credit hrs for first course
printf("Enter crn/credit hours for the first course: \n");
scanf("%s", &course1);
// Enter CRN/Credit hrs for Second Course
printf("Enter crn/credit hours for the second course: \n");
scanf("%s", &course2);
// Closing statement
printf("\nThank you!\nPRESS ANY KEY TO CONTINUE...\n\n");
// Calculates Cost of Class
int i = course1[5]-'0'; // Bad version of type casting from char to int.
int k = course2[5]-'0'; // Bad version of type casting from char to int.
coursecost1 = i*credithour;
coursecost2 = k*credithour;
totalcost= healthID+coursecost1+coursecost2;
// Printout
printf("\t\tVALENCE COMMUNITY COLLEGE\n\t\tORLANDO FL 10101\n\t\t");
do{
count++;
printf("*");
}while(count<26);
printf("\n\t\tFee Invoice Prepared for Student V%d\n",studentID);
printf("\t\t1 Credit Hour = %.2f\n",credithour);
printf("\t\tCRN\t CREDIT HOURS\n");
printf("\t\t%c%c%c%c\t %c\t\t $
%.2f\n",course1[0],course1[1],course1[2],course1[3],course1[5],coursecost1);
printf("\t\t%c%c%c%c\t %c\t\t $
%.2f\n",course2[0],course2[1],course2[2],course2[3],course2[5],coursecost2);
printf("\t\t\t Health & id fees $ %.2f\n",healthID);
printf("\t\t");
count=0;
do{ //could define a function to clean up code
count++;
printf("-");
}while (count < 39);
printf("\n\t\t\t Total Payments $ %.2f",totalcost);
return 0;
解决方案
“定义”基本上是为您经常在代码中编写的内容或您想要经常更改以测试代码的内容创建快捷方式。
一个例子:
#define fail printf("You did something wrong")
int main (void)
{
int number;
printf("Insert a number between 1 and 3");
scanf("%d", &number);
if (number>3||number<1)
{
fail;
return -1
}
else
{
printf("Insert a number between 4 and 6");
scanf("%d", &number);
if (number<4||number>6)
{
fail;
return -1;
}
else return 0;
}
}
当编译器读取这个程序时,每个“失败”都会被“printf(“you did something wrong”)替换。
这是define的主要用途,如果你现在开始,那是我认为重要的。
关于您的代码,当您对字符串执行 scanf 时,不要使用“&”或使用“gets”函数。我可以尝试向你解释原因,但我不是这份工作的最佳人选。我的建议是,继续学习,当你开始看到指针时,你会更好地理解为什么会发生这种情况。
此外,当您使用没有起始值的数组时,您不需要执行“={ }”的操作,只需在没有“=”的情况下调用它们即可。
现在,正如您所问的,这就是我编写代码的方式,并进行了更正:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int studentID, intcount = 0; // Count is a function, better not mix things up, that's why I called it "intcount"
char course1[10], course2[10];
float coursecost1, coursecost2, totalcost;
float credithour = 120.25;
float healthID=35.00;
// Asks for student ID
printf("Enter the Students Id: \n");
scanf("%d",&studentID);
// Enter CRN/Credit hrs for first course
printf("Enter crn/credit hours for the first course: \n");
scanf("%s", course1);
// Enter CRN/Credit hrs for Second Course
printf("Enter crn/credit hours for the second course: \n");
scanf("%s", course2);
// Closing statement
// printf("\nThank you!\nPRESS ANY KEY TO CONTINUE...\n\n");
/* Decided not to delete this one, so you can
compare this printf with the new one and the system pause */
printf("\nThank you!\n");
system("pause");
// Calculates Cost of Class
// I'm not sure what you were trying to do in these lines, so I won't touch them
int i = course1[5]-'0';
int k = course2[5]-'0';
coursecost1 = i*credithour;
coursecost2 = k*credithour;
totalcost= healthID+coursecost1+coursecost2;
// Printout
printf("\t\tVALENCE COMMUNITY COLLEGE\n\t\tORLANDO FL 10101\n\t\t");
do
{
intcount++;
printf("*");
}
while(intcount<26);
printf("\n\t\tFee Invoice Prepared for Student V%d\n",studentID);
printf("\t\t1 Credit Hour = %.2f\n",credithour);
printf("\t\tCRN\t CREDIT HOURS\n");
printf("\t\t%c%c%c%c\t %c\t\t $%.2f\n",course1[0],course1[1],course1[2],course1[3],course1[5],coursecost1);
printf("\t\t%c%c%c%c\t %c\t\t $%.2f\n",course2[0],course2[1],course2[2],course2[3],course2[5],coursecost2);
printf("\t\t\t Health & id fees $ %.2f\n",healthID);
printf("\t\t");
intcount=0;
do
{ //could define a function to clean up code
intcount++;
printf("-");
}
while (intcount < 39);
printf("\n\t\t\t Total Payments $ %.2f",totalcost);
}
基本上只是尽量避免在同一行放置太多信息,如果需要,甚至将单个 printfs 拆分为多个。
希望我能有点用!
推荐阅读
- python - 在 Pandas 中查看 Stata 变量标签
- java - Java中的数组列表和文本文件读取
- google-maps - 如何点击 googlemap pin
- r - 有什么方法可以在使用 R 传单包创建的交互式地图的表面上显示邮政编码?
- php - Magento 2:如何在尝试序列化结帐配置时找到导致错误“格式错误的 UTF-8 字符,可能编码不正确”的配置
- r - 为什么 ggpairs 函数中的散点图上没有黄土层?
- javascript - 有没有更好的方法来写“a == b?c:a”?
- php - 如何在 laravel 中使用 Laravel 子查询与 groupby 和 where
- javascript - 我正在寻找一种通过网站、应用程序或程序使用编程来批量发送 WhatsApp 网络消息的方法
- django - Django 代码不适用于以下脚本