如何使a代码在更改C中的月份日期时重复自身?
我已经完成了这段代码,我做了一个循环,我的代码将从头开始重新启动,但是我希望程序在更改月份日期的同时重新启动,并询问用户他们想要多少个月,这样程序将运行一定的时间,因为我添加了第一个月如何使a代码在更改C中的月份日期时重复自身?,c,C,我已经完成了这段代码,我做了一个循环,我的代码将从头开始重新启动,但是我希望程序在更改月份日期的同时重新启动,并询问用户他们想要多少个月,这样程序将运行一定的时间,因为我添加了第一个月 #include<stdio.h> #include<stdlib.h> float twag(float amount,float rate) { float si; si = (amount * rate) / 100; return si; } float
#include<stdio.h>
#include<stdlib.h>
float twag(float amount,float rate)
{
float si;
si = (amount * rate) / 100;
return si;
}
float twag2(float amount,float rate)
{
float er;
er = twag(amount, rate) + amount;
return er;
}
int main()
{
char answer;
do {
float amount;
float rate;
float si;
float er;
printf("Month1\n");
printf("\nEnter intial deposit : ");
scanf("%f", &amount);
printf("\nEnter Rate of Interest : ");
scanf("%f", &rate);
printf("\nSimple Interest : %.2f \n", twag(amount,rate));
printf("\nEnd Payment: %.2f \n",twag2(amount,rate));
if (amount <= 100)
printf("interest rate should be 10%%\n");
else if (amount <= 200)
printf("interest rate should be 50%%\n");
else if (amount <= 500)
printf("interest rate should be 80%%\n");
else if (amount >= 500)
printf("interest rate should be 90%%\n");
system("PAUSE");
printf("\nPress Y to continue to the second month or Press any Key To
Exit");
scanf(" %c", &answer); //
}
while (answer == 'y' || answer == 'Y');
return 0;
}
#包括
#包括
浮动twag(浮动量、浮动率)
{
浮硅;
si=(金额*费率)/100;
返回si;
}
浮动twag2(浮动量、浮动率)
{
浮子;
er=twag(金额、费率)+金额;
回程电流互感器;
}
int main()
{
答案;
做{
浮动金额;
浮动汇率;
浮硅;
浮子;
printf(“Month1\n”);
printf(“\n初始存款:”);
scanf(“%f”和金额);
printf(“\n输入利率:”);
scanf(“%f”、&rate);
printf(“\n简单利息:%.2f\n”,twag(金额、利率));
printf(“\n付款:%.2f\n”,twag2(金额、费率));
如果(金额假设您正在申请每月利息,您至少需要从今天开始记录当前月份和年份,因此方法是使用localtime
我建议您解释为什么系统(“暂停”)
不好,除此之外,您在循环中定义了循环变量,这意味着它们将在每次循环时重置,并且您的兴趣应该在循环期间保持不变,因此您的代码应该如下所示
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float twag(float amount,float rate);
float twag2(float amount,float rate);
void get_date(int *month, int *day, int *year);
int main()
{
char answer;
float amount, rate, si, er;
int day, month, year;
printf("\nEnter intial deposit : ");
scanf("%f", &amount);
printf("\nEnter Rate of Interest : ");
scanf("%f", &rate);
get_date(&month, &day, &year);
do
{
// increment date by 1 month
if (++month > 12) month=1, year++;
printf("\nDate %d/%d/%d \n", day,month,year);
printf("\nSimple Interest : %.2f \n", twag(amount,rate));
printf("\nEnd Payment: %.2f \n",twag2(amount,rate));
amount = twag2(amount,rate);
printf("\nPress Y to continue to the next month or Press any Key To Exit");
scanf("%c", &answer);
}
while (answer == 'y' || answer == 'Y');
return 0;
}
#包括
#包括
#包括
浮动twag(浮动量、浮动率);
浮动twag2(浮动量、浮动率);
作废日期(整数*月,整数*日,整数*年);
int main()
{
答案;
浮动金额、比率、si、er;
整数日、月、年;
printf(“\n初始存款:”);
scanf(“%f”和金额);
printf(“\n输入利率:”);
scanf(“%f”、&rate);
获取日期(月、日、年);
做
{
//增量日期为1个月
如果(++月>12)月=1,年++;
printf(“\n日期%d/%d/%d\n”,日、月、年);
printf(“\n简单利息:%.2f\n”,twag(金额、利率));
printf(“\n付款:%.2f\n”,twag2(金额、费率));
金额=twag2(金额、费率);
printf(“\n按Y键继续到下个月,或按任意键退出”);
scanf(“%c”和“应答”);
}
而(答案='y'|答案='y');
返回0;
}
Formatting/indentation:(我是初学者,很抱歉:/请尝试清理一下。错误的F/I会导致控制错误的范围/流,加上其他错误(比如那些希望提供帮助的人),必须付出可以避免的努力来理解:(问题是什么?顺便说一句,我添加了一个解决方案,您可能想查看一下。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float twag(float amount,float rate);
float twag2(float amount,float rate);
void get_date(int *month, int *day, int *year);
int main()
{
char answer;
float amount, rate, si, er;
int day, month, year;
printf("\nEnter intial deposit : ");
scanf("%f", &amount);
printf("\nEnter Rate of Interest : ");
scanf("%f", &rate);
get_date(&month, &day, &year);
do
{
// increment date by 1 month
if (++month > 12) month=1, year++;
printf("\nDate %d/%d/%d \n", day,month,year);
printf("\nSimple Interest : %.2f \n", twag(amount,rate));
printf("\nEnd Payment: %.2f \n",twag2(amount,rate));
amount = twag2(amount,rate);
printf("\nPress Y to continue to the next month or Press any Key To Exit");
scanf("%c", &answer);
}
while (answer == 'y' || answer == 'Y');
return 0;
}